SWD in STM32 when SWD is configured as GPIO.

Jackob2001

Junior Member level 1
Joined
Nov 3, 2024
Messages
17
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
188
I do have a a couple of question regarding the SWD in STM32 :

- How do I program the STM32 when I configure SWDIO and SWCLK into GPIO ?
- Why when I have SWDIO and SWCLK and I send code from CubeIDE into STM32 the MCU is set into RESET mode ? It happens in my Nucleo and in my Custom board that uses only SWDIO, SWCLK and GND.
- How unprogrammed STM32 (no program in flash) in every MCU works when it does not have configured any pins because it is fresh unprogrammed STM32 out of the box so how it has SWD pins when it has no program in it ?
- Is it still possible to debug STM32 when I have SWDIO and SWCLK as GPIO during the RESET ? I've heard it resets the GPIO pins into SWD but only after I UNRESET the button but then the Flash is running.
- When I have unprogrammed STM32 and it was powered on, then I somehow could still program it using SWD or JTAG. How it works ? Why I could program the clear STM32 ?
- It is powered on I can program using SWD or JTAG even though I haven't picked it in STMCubeIDE, after RESET it says SWD is set no matter what but I could still program it using JTAG. I am confused in so many levels.

This isn't for specific MCU, I just noticed in my custom board with STM32 or with Nucleo that SWD works in a way I don't understand.

I couldn't find info about it in Reference Manual or in Datasheet so maybe somebody know the answer ?
If there is any information needed I will help to give info as much as I can, let me know please.
 
Last edited:

Read chapter Debug Support in reference manual.

In unprogrammed device all optional SWD pin pairs have SWD function, also in reset state before user configuration has been established. This way, you have always access to SWD. To actually use SWD, one SWD pin pair must be wired to a debug connector, in most board designs, it will be used exclusively for this purpose and not configured as GPIO.
 

Ah okey so I know the answer to part of my questions.

So after reset the SWD pins are set up during the booting, before executing the code.
So even unprogrammed STM32 will have these SWD pin set up during the booting and after it because it is unprogrammed MCU.

Although I don't know why SWD when connected to MCU does the soft reset. Because it restart the MCU and it must connect to MCU again after soft reset. Plus it holds the MCU in that reset and programs the MCU ? Normally during reset nothing can be done, so it somehow halts the booting code after reset which I don't know how xD.

And I don't know how this one thing works.
When I have SWD configured as GPIO and have NRST as button. Then when I press NRST and press in CubeIDE send code button and release NRST how on earth SWD is able to connect to MCU in this short time during the booting ? What If I release the button when cube sent the command to stop MCU but it missed this chance ??? I can't imagine how he gains controll so fast no matter when I release the button. When cube sends data through SWD when I still have pressed button or halfway when I release the button but SWD sent half the data to stop MCU before user code will be executed.

The window is so small so how is this possible ? How many times it sends the command to stop MCU ?



Sorry for many questions.
 
Last edited:


I've now learned that SWD also works during the RESET of NRST or the soft RESET from SWD. So SWD doesn't have to be so fast to send data when MCU is executing the boot code before user code. So infact SWD can do his job while MCU is on RESET mode doing nothing.

I still do have 2 more questions though.

- When using STLink does it do 2 RESETS ? NRST and the soft reset of SWD ? if so why ?

I've said 2 resets because if SWD is set as GPIO then NRST must be made because SWD can't do soft reset, and nucleo programmer STlink does NRST and when SWD is connected it does also soft resets. In my custom board without NRST the SWD does the soft reset to STLink does both.

Does it do 2 resets because for nucleo the SWD can be changed to different pins so it does NRST and the soft reset just happens without bigger purpose. While Soft reset is good for devices that doesn't connect NRST because SWD does it. My thoughts of how it could work. Because I have problem knowing how to find some information.

- Does JTAG do the same thing as SWD ? JTAG is also configured to work on RESET like SWD ? I had a board with no SWD but only JTAG and it was unprogrammed and it work as well and also restarted the MCU so I guess it also works from start like SWD ?
 

Hi,

it should be an easy task to do an internet search for "what´s the difference between SWD and JTAG" ... maybe also add "STM32."
***

About SWD and RESET:
When DEBUGGING: then performing a RESET initializes a lot of registers and will restart the microcontroller. .. like after power down
But for sure you may step through your code .. thus not for every SWD communication a RESET is used. Then you rather use HALT (without modifying registers) and RESUME the code processing (as if no debug happenend). In this case any alternative use of the SWD pins may cause problems.

When using the SWD for DOWNLOADING code ONLY. Then there always will be a RESET (no HALT and RESUME) to get the SWD pin access and start the new programmed code.

****
STM provides a lot of documents (applicatio notes) to explain SWD and JTAG.
If you do an internet search about this topic you also will find youtube videos for more "visual learning".

Please spend the time to go through some of them ... and if there are any questions left: please refer to these documents. (Provide links and tell chapter, page, time stamp...)

Klaus
 

it should be an easy task to do an internet search for "what´s the difference between SWD and JTAG" ... maybe also add "STM32."

I mean I know the difference, my question was not precise which I am sorry.
It was about JTAG getting into the MCU if it's similar to the SWD (similar I mean after reset the pins GPIO turns into JTAG similar to SWD etc)



I was searching for youtube videos or rather the visualization. It was basics how SWD communication works.
I was rather looking for information about how SWD is able to connect with MCU when it is under RESET.


I might know the answer for two of my questions :
- SWD does double reset from ST Link for 2 reasons, NRST to make sure the SWD pins are now configured instead of GPIO, the soft reset must be made because SWD always do resets. Soft reset exist if SWD pins are already there then SWD will program it under RESET during the program execution.

- JTAG I dunno if it does soft reset or after NRST the pins change from GPIO to JTAG as well. I've had a MCU which I programmed only with JTAG so unprogrammed MCU also works with JTAG, so perhaps JTAG is set after RESET.



When Debugging (clicking the debug button), it will once do the RESET to start the program from beginning. So every SWD a RESET is used to start again from the start of the code.
Like you said not for every SWD communication a RESET is used, but maybe you refered to different MCU and not STM.

HALT and RESUME is built in the IDE of STM so SWD sends HALT right before the first instruction.


Although I must somehow find info how SWD sends HALT right before the user code is being executed because MCU first does Booting code after that it does the user code (the one user uploaded to the STM32).


Please spend the time to go through some of them ... and if there are any questions left: please refer to these documents. (Provide links and tell chapter, page, time stamp...)

I do but there are many documentations and either I couldn't find it or there was nothing about it or I am just dumb.
So after checking Datasheet or Reference Manual or some special notes about specific peripherals (forgot the name sorry). I wanted to ask here maybe someone had link or information about it.


I thank you kindly for respond.
 

I was searching for youtube videos or rather the visualization. It was basics how SWD communication works.
I was rather looking for information about how SWD is able to connect with MCU when it is under RESET.
the most origine information is in the manufacturer´s documentation.
Usually all other informations (videos, tutorials, school...) is based on the manaufacturer´s information.

in post#2: FvM wrote:
Read chapter Debug Support in reference manual.
Did you do so?

We don´t even know which exact STM32 you are using ... but you should know. So go the manufacturer, to the exact microcontroller site, press the [documentation] link, press the [Reference manual] link. There you are. Open it, go to the [Debug Support] chapter and read ..

Klaus
 

Yes Indeed I've read the Debug Support for STM32L0x1 or in other form STM32L031G6U6 (sorry for not mentioning it you are totally right here).

For example this part of the Reference Manual of the Debug Support chapter :



SW-DP is an important part for the SWD connection to work and to communicate.


If it's not TURNED ON then SWD cannot communicate with it.
According to the above statement the SW-DP state machine is in RESET STATE when the line SWDIO I suppose is in high for more than 50 cycles or after powering up the MCU, after power up the SW-DP should be all the time in IDLE mode unless the line is high for to long (I don't know what can cause it).

So in fact if I understood it correctly when MCU is in RESET state (pressing NRST or in soft RESET) the SW-DP is in IDLE STATE so infact it can communicate with MCU during the RESET STATE of that MCU.

I may be correct or not dunno.

There was on the other side nothing said about JTAG. Although on the unprogrammed MCU I could program it using JTAG but here it is said that SWD works after reset and nothing about JTAG.

Thank you for this part : "Open it, go to the [Debug Support] chapter and read .."
I read part of it but there was mainly about registers, but there was small part how SW-DP state machine works and maybe I interpreted it correctly for how SWD works even under NRST or soft reset state.

Thank you and sorry for me being annoying here. I just get random curiosity about random small detail and want to understand it.
In this chapter of Debuging there is a part I don't understand in any bits even after reading it like SW-DP and SW-AP registers what is this thing.
 

Attachments

  • rm0377-ultralowpower-stm32l0x1-advanced-armbased-32bit-mcus-stmicroelectronics.pdf
    10 MB · Views: 8

Although on the unprogrammed MCU I could program it using JTAG
..so you could program it using JTAG?
The STM32L031G6U6?

I took the effort to search for the microcontroller´s datasheet (since you did not provide a link) ... then I did a simple search for "JTAG" ... but there is no JTAG.

.. and it takes you 8 posts and about 1 day to tell us which microcontroller you use .. for us to validate what you tell us...

If you don´t even read the datasheet ... I won´t be the one reading it for you.
It´s your job. And it´s nothing special .. since every designer needs to read it.

Klaus
 


I've provided the RM (reference manual) the PDF file, I thought this is what you've needed.
I'm sorry.

So what I said before about SWD in reset is correct from what I've read from datasheet ?
I'll finish after this.
 

not I ... YOU are the first to read the datasheet ... and if necessary the additional documents

Nwm.
I've read something about SWD and I also wanted a confirmation of it if I correctly interpreted it.
If nothing is said about JTAG then nevermind.

But since you've said nothing about what I've read about SWD I assume I have correctly assumed how it works.
 

But since you've said nothing about what I've read about SWD
You have read about SWD ...
you have read about JTAG..
and you did program your microcontroller using JTAG ..

What is true and what is not?

This is why we ask for links and references like chapters and pages ... to find out what "one thinks is true" and what is "really written".

do your job first ...

Klaus
 

and you did program your microcontroller using JTAG ..

I may have been mistaken.
Under again checking the cable my friend used he soldered the wires to SWD not jtag which usually this cable with connector is used for.

I was just also curioused in general if what I have read in post nr.8 works similar with JTAG as well in some STM. I don't remember the exact model but some unprogrammed STM I could program not with SWD but with JTAG. That maybe after reset the JTAG pins are active like with SWD.

Also what I read about SWD I have posted in post nr.8 and asked if I interpreted It correctly, there are mentioned chapters and what document it is. That's basically all.


Sorry, for wasting time
 

Cookies are required to use this site. You must accept them to continue using the site. Learn more…