# Change SWD pin Back to SWD mode at run time in STM32F030F4

Im using PA_14 (SWCLK) as Output mode in stm32f030f4 microcontroller.

I change state of this pin to output mode for a buzzer that is not in service at all time. At the end of the buzzer function I always try to configure the pin back to its default state(SWCLK) to make the chip ready for programming.

Im using this code to configure the PA_14 back to its SWCLK mode:

GPIO_InitTypeDef GPIO_InitStruct;
GPIO_InitStruct.Pin = GPIO_PIN_14;
GPIO_InitStruct.Mode = GPIO_AF0_SWCLK;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH ;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);


But after any small change in the state of pin, SWD mode never works and I can not program the microcontroller. I test other mode like Input mode too.

How is it possible to re enable the SWD mode at run time as discussed in this link:

Can i connect something else to programming pins in STM32 (SWD)?

• Your best bet may be to get a debug adapter and programming software which can properly assert the reset signal, and to give your firmware a delay before it initially reconfigures the pin. If you want to know what to set it back to, try reading out the actual GPIO registers before your customization, and make sure that the relevant bits get back to the initial values afterwards. – Chris Stratton Apr 15 at 20:41
• Have you tried enabling the internal pull-up on the SWCLK pin? IIRC SWD requires pins to be pulled high. – Tony K Apr 17 at 4:01
• [SOLVED] Yes I did. but pull up had no effect. The problem solved when I called TM function TM_GPIO_SetPinAsAlternate after above codes. TM_GPIO_SetPinAsAlternate(GPIOA,GPIO_PIN_14); After running this line, programmer is able to program the chip. – Ghm y Apr 18 at 9:55