Skip to main content
Graduate II
November 9, 2024
Question

How SWD works when it is not assigned in CubeIDE ?

  • November 9, 2024
  • 2 replies
  • 2463 views

Hello I am using : STM32L031G6U6.

I am confused how programming the MCU works. I don't have let's say SWDIO and SWCLK pins declared in CubeIDE. Then how it works ? Why it works this way ? 

I read something that when I use NRST pin to reset manually then the pins GPIO are automatically assigned as SWD but I could read it anywhere in Reference Manual typing NRST. I only found how BOOT works which I also don't understand what do these modes mean like selecting boot area ... : 

Xenon02_0-1731162952360.png

 

In my other PCB I only used SWDIO and SWCLK and GND and it was resetting the MCU without me using NRST pin. So I am a bit confused how it works and why I sometime have to use NRST and sometime not. 

I wanted to use SWDIO and SWCLK for other purpose but I wanted to know how it works and can I do it ? What is a step by step method to program MCU ? Should I hold NRST and send program until it says 100% ? 

 

    This topic has been closed for replies.

    2 replies

    Graduate II
    November 9, 2024

    JTAG - Wikipedia

    And boot table is about what ARM do after go from RESET to UNRESET state.

    Xenon02Author
    Graduate II
    November 9, 2024

    Well it is not the question what is JTAG or what is SWD but rather if I change the SWD into normal GPIO then how after the reset it is again SWD ? 

    Okey maybe my bad I'll ask it this way : 

    - What happens after the RESET ? Which is UNRESET state, what happens ? Normally the Flash program is going on but it changes into SWD for a short while when my SWD in program changes it into normal GPIO ? 
    - If SWD is change into GPIO when is the moment I can send SWD code into the flash after the RESET ? Should I choose a specific Boot option ???

    - Why when let's say I have GPIO instead of SWD, when I upload code into Nucleo via USB it somehow programs it using SWD ? How ? 

    Graduate II
    November 10, 2024

    As wiki says SWD uses an ARM CPU standard bi-directional wire protocol, defined in the ARM Debug Interface v5.

    Then unused GPIO is always internaly connected to ARM DEBUG port inside MCU. FYI programming works... except:

    MCU in STOP or lower power mode = DEBUG port is off. (Overided with set debug in low power register)

    GPIO used in code = DEBUG port is disconnected = Reset is required .

    Technical Moderator
    November 10, 2024

    Dear @Xenon02 ,

    You can go to GPIO chapter inside the reference manual on how the Alternate functions are working and therefore how to configure them . Here 

    https://www.st.com/resource/en/reference_manual/rm0376-ultralowpower-stm32l0x2-advanced-armbased-32bit-mcus-stmicroelectronics.pdf#page235

    NRST pin if connected to your debugger (ST-Link for instance) is a way to force a hardware reset and pull the line low synchronized with the debug so it can enter debug mode and send the right commands there SWD protocol to take control of the CPU. This is very useful when customer applications is entering STOP mode as soon as the CPU is starting or theses two pins PA14/PA13 are configured to others peripherals . So I would recommend in your case to route NRST to your debugger to have 4 lines including ground . SWD protocol has also a SOFT reset and can send it to the CPU.

    IMG_0378.jpeg

    Hope it helps you .

    STOne-32

    Xenon02Author
    Graduate II
    November 10, 2024

    Hello :> 

    Most of what I asked has been answered, but I do have 5 more questions. 

    When the MCU is restarted it does its booting code (turning on clocks, PLL, RAM etc.), after booting the MCU starts the user code which is the code the user uploaded into the flash using SWD or anything else. 
    Before the user code changes the SWD into GPIO then SWD must connect with the MCU during the booting code or right before the booting code is finished. 

    -How PC know when to send data to the MCU with SWD ?

    -How PC know when SWD was configured in booting stage (SWD can be configured in any time in booting stage at the start of booting or at the end) ??? 

    -How fast SWD must send message and does it do many times during the booting stage, and if does send many times the command, how fast it is ? 

    - Let's say I have pressed NRST, and pressed Upload code, then I randomly release NRST the booting in MCU started to SWD must send many same commands to connect with MCU, so it must be super fast because booting is fast, but ST-Link is like any other STM32 MCU so how fast is it ? 

    - Lastly, so ST-Link first does NRST reset, then Connects using SWD and SWD does again soft restart. Why SWD does soft reset and keeps it restarted ? MCU can't be programmed during the reset and it keeps restarted I guess because nothing works. Or it stopped booting code I don't know. But after flashing the code SWD should again do soft restart I guess. 

    I got confused how the mechanism works because I was wondering how these stuff works or how fast it must be because coding it must be hard or to create so fast code it works. 
    Sorry for inconvenience. 

    Graduate II
    November 10, 2024

    You still mixup ... SWD is reset independent = work in reset always and out of reset in some conditions up writed.

    And too init = starting communication is relative speed independent 

    All this you can see in programming/debug config for STLink aka connection Normal vs Under reset and speeds from N kHz to Z MHz... 

    SWD simply wait fr change state on pins ... ARM DS-5 ARM DSTREAM System and Interface Design Reference Guide Version 5.24