Author Topic: Re-assigning SWD pins of STM32  (Read 3658 times)

0 Members and 1 Guest are viewing this topic.

Offline syntax333Topic starter

  • Regular Contributor
  • *
  • Posts: 158
  • Country: 00
Re-assigning SWD pins of STM32
« on: March 18, 2020, 03:21:10 pm »
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?
 

Offline thinkfat

  • Supporter
  • ****
  • Posts: 2155
  • Country: de
  • This is just a hobby I spend too much time on.
    • Matthias' Hackerstübchen
Re: Re-assigning SWD pins of STM32
« Reply #1 on: March 18, 2020, 04:21:38 pm »
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.
Everybody likes gadgets. Until they try to make them.
 

Offline dave j

  • Regular Contributor
  • *
  • Posts: 137
  • Country: gb
Re: Re-assigning SWD pins of STM32
« Reply #2 on: March 18, 2020, 04:36:25 pm »
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.

I'm not David L Jones. Apparently I actually do have to point this out.
 

Offline martinayotte

  • Regular Contributor
  • *
  • Posts: 64
Re: Re-assigning SWD pins of STM32
« Reply #3 on: March 18, 2020, 06:36:48 pm »
In stm32duino/libmaple, there is helpers for different modes :
Code: [Select]
/**
 * @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 free
                                   for 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;
 

Offline syntax333Topic starter

  • Regular Contributor
  • *
  • Posts: 158
  • Country: 00
Re: Re-assigning SWD pins of STM32
« Reply #4 on: March 18, 2020, 07:05:28 pm »
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?
« Last Edit: March 18, 2020, 08:23:17 pm by syntax333 »
 

Offline thinkfat

  • Supporter
  • ****
  • Posts: 2155
  • Country: de
  • This is just a hobby I spend too much time on.
    • Matthias' Hackerstübchen
Re: Re-assigning SWD pins of STM32
« Reply #5 on: March 18, 2020, 07:25:10 pm »
Yes.
Everybody likes gadgets. Until they try to make them.
 

Offline hans

  • Super Contributor
  • ***
  • Posts: 1682
  • Country: nl
Re: Re-assigning SWD pins of STM32
« Reply #6 on: March 18, 2020, 08:42:31 pm »
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.
« Last Edit: March 18, 2020, 08:45:01 pm by hans »
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf