Re-assigning SWD pins of STM32 - Page 1
Forum section: electronics / general

author: syntax333
author_rank: Regular Contributor
timestamp: March 18, 2020, 03:21:10 pm
content: Hi, I was wondering if I could use SWD pins as GPIOs? I am ok with not being able to Debug. Is assinging SWD pins as GPIOs same with other peripherals or do I need to do something special?Also how do I reset the STM32 so that it can be able to debug again after re-assinging SWD pins?

author: thinkfat
author_rank: Unknown
timestamp: March 18, 2020, 04:21:38 pm
content: Yes, you can do that, just assign an alternate function (GPIO) and you're set.Make sure you have the reset line available somewhere, if you hold it down while connecting the debugger, you can grab the core before it executes any instruction and update the internal flash.

author: dave j
author_rank: Regular Contributor
timestamp: March 18, 2020, 04:36:25 pm
content: The pins are in debug mode at reset and your code has to configure them to not be. There's some flexibility in the setup so you can trade level of debug support against number of pins you free up. If you can get away with not reassigning SWCLK and SWDIO you can debug whilst still reassigning the other pins.Look in the "Debug support" section of the MCU reference manuals.

author: martinayotte
author_rank: Regular Contributor
timestamp: March 18, 2020, 06:36:48 pm
content: In stm32duino/libmaple, there is helpers for different modes :Code:/*** @brief Debug port configuration** Used to configure the behavior of JTAG and Serial Wire (SW) debug* ports and their associated GPIO pins.** @see afio_cfg_debug_ports()*/typedef enum afio_debug_cfg {AFIO_DEBUG_FULL_SWJ = AFIO_MAPR_SWJ_CFG_FULL_SWJ, /**<Full Serial Wire and JTAG debug */AFIO_DEBUG_FULL_SWJ_NO_NJRST = AFIO_MAPR_SWJ_CFG_FULL_SWJ_NO_NJRST, /**<Full Serial Wire and JTAG, but no NJTRST. */AFIO_DEBUG_SW_ONLY = AFIO_MAPR_SWJ_CFG_NO_JTAG_SW, /**<Serial Wire debug only (JTAG-DP disabled,SW-DP enabled) */AFIO_DEBUG_NONE = AFIO_MAPR_SWJ_CFG_NO_JTAG_NO_SW /**<No debug; all JTAG and SW pins are freefor use as GPIOs. */} afio_debug_cfg;/*** @brief Enable or disable the JTAG and SW debug ports.* @param config Desired debug port configuration* @see afio_debug_cfg*/static inline void afio_cfg_debug_ports(afio_debug_cfg config) {__io uint32 *mapr = &AFIO_BASE->MAPR;*mapr = (*mapr & ~AFIO_MAPR_SWJ_CFG) | config;

author: syntax333
author_rank: Regular Contributor
timestamp: March 18, 2020, 07:05:28 pm
content: Thank you for your answers. Just to make it clear•I program the stm32 and re-assign SWD pins as GPIOs•In order to reset the stm32, I connect debugger (STlink) to "former" SWD pins(now GPIOs) and hold down RESET while flashing the new program to update firmware.right?

author: thinkfat
author_rank: Unknown
timestamp: March 18, 2020, 07:25:10 pm
content: Yes.

author: hans
author_rank: Super Contributor
timestamp: March 18, 2020, 08:42:31 pm
content: It may be useful to include a small reset delay in your program, before you reconfigure the SWD pins. I typically use values in the order of 100ms to 1s. This can be useful to prevent "bricking" the MCU, for example, if you reconfigure SWD pins, disable/reconfigure the system clock accidently, or enter shutdown mode.Some reprogramming/flashing routines will issue a system/hardware reset on the MCU before attempting to reprogram. Technically this is not necessary if the debugger can halt the MCU core and reprogram it straight away. However with a system reset, the MCU will start from a known state. When the start-up delay is sufficiently long, reconfiguration of the SWD pins is postponed such that the debug interface stays accessible long enough to halt the MCU and start rewriting the FLASH memory.Alternatively on most STM32 chips, you can pull the BOOT0 pin to enter the ROM bootloader (the datasheet lists if you need to pull it high or low). The ROM bootloader doesn't touch the SWD pins, and therefore the MCU remains accessible via SWD. However, this usually requires a manual intervention, especially if you don't got a button or jumper attached to this pin, so it gets tiresome real fast if you reprogram the MCU often.