cancel
Showing results for 
Search instead for 
Did you mean: 

How SWD works when it is not assigned in CubeIDE ?

Xenon02
Senior

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% ? 

 

8 REPLIES 8
MM..1
Chief II

JTAG - Wikipedia

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

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 ? 

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 .

The answer to your questions is simple :

just think, how would you build a cpu, that can be programmed/accessed after power-up or reset in any way ?

Right - the needed pins start with this function enabled after power-up; otherwise you could never access the cpu - right ?

see ds : (here from F030)

 

AScha3_0-1731225465328.png

Until you change the pin functions in your program, they will do the "function after reset" .

See detail for your chip in ds, different series have different settings here.

If you feel a post has answered your question, please click "Accept as Solution".
STOne-32
ST Employee

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 

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. 

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

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

Where it is written that SWD works also in RESET state ? What MCU does in reset state what he can do ? Because I thought that MCU does completelly nothing. So maybe after reset multiplexers just during reset sets the GPIO so even MCU doesn't have to do it. But MCU is not working so how SWD is communicating with him during reset. 

So as I understand ST-Link does Hard reset (because SWD was changed into GPIO) and keeps it restarted like that then SWD connects and does Soft reset while NRST (hard reset) is released from STLink, and SWD does his stuff under soft reset ? 
So like two resets and SWD works in both reset states when SWD takes control then NRST is released and SWD does his own soft reset. 

"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..."

Normally when I program using SWD without NRST then SWD PC automatically pick Normal or Under reset ? Because SWD is still reseting the MCU. So if GPIO has been changed then NRST must be also added to do double restarts ? But how it determines when it's normal or Under reset ? Because as you said SWD is working under reset.