logo elektroda
logo elektroda
X
logo elektroda

Title: STM32 Serial Wire Viewer & Output, ST-LINK: Beginner Questions on SWO, SWCLK & Reset

vp32 8022 17
Best answers

How do Serial Wire Viewer/Serial Wire Output and ST-Link's Connect Under Reset work on STM32, and when are SWD, SWO, and reset lines actually needed?

Serial Wire Viewer/Output (SWV/SWO) is a one-way trace output from the MCU: SWO is the dedicated pin, and the MCU sends data only if you enable trace/ITM; it is independent of SWCLK, and using SWD with async trace means three lines: SWDIO, SWCLK, and SWO [#18330651][#18331006] If SWO is not enabled, that pin can be used as a normal GPIO; the debugger or your code can initialize ITM/trace when you do want output [#18331006] On STM32F103, debug pin use is controlled by AFIO->MAPR SWJ_CFG: 000/001/010 keep JTAG/SWD active, while 100 disables SWJ so PA13/PA14 can be ordinary GPIOs [#18334959][#18335216] Connect Under Reset means the ST-Link holds the target in reset, initializes the CPU/debug interface during that reset, then releases reset so it can connect even if your program disabled SWD or broke the clocks/pins [#18330651][#18359367] If SWD is configured and wired correctly, you should not normally need to hold reset; it is mainly a recovery/debug feature [#18335216][#18359367]
Generated by the language model.
ADVERTISEMENT
Treść została przetłumaczona polish » english Zobacz oryginalną wersję tematu
  • #1 18330556
    vp32
    Level 11  
    Posts: 505
    Rate: 47
    Hi
    I turned around pretty hard. I am asking for your understanding because I am just starting with STM32, or at least I have been trying for some time.
    I'm not jumping into deep water, I'm just interested in the topic.
    I realize that these are obvious things to you, but I don't have an ARM mentor (it's a pity) and sometimes I connect contradictory things reading the internet.

    1. What exactly is Serial Wire Viewer and Serial Wire Output?
    I mean exactly SWO is the pin dedicated to this?
    does it activate somehow?
    In that case, how does the uC know what to send to this output?
    Is this data related to the clock somehow, SWCLK?

    2. I use ST-Link. I came across such Connect Under Reset setting. What does it give me? How to take advantage of it? Does it mean the reset button has to be pressed? Why in cubeMX you can programmatically set two pins to work with SWD?
  • ADVERTISEMENT
  • Helpful post
    #2 18330651
    Sareph
    Level 24  
    Posts: 638
    Help: 65
    Rate: 378
    vp32 wrote:
    1. What exactly is Serial Wire Viewer and Serial Wire Output?
    It's kind of a one-way serial port to put whatever information you see fit on it. http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.ddi0314h/Chdcicdb.html

    vp32 wrote:
    I mean exactly SWO is the pin dedicated to this?
    Of course.

    vp32 wrote:
    does it activate somehow?
    Yes.

    vp32 wrote:
    In that case, how does the uC know what to send to this output?
    Like all CPUs, it will do what you ask it to do, so if you enable trace and give it data, it will send whatever you want there. http://blog.atollic.com/cortex-m-debugging-pr...on-to-a-debugger-console-using-swv/itm-part-1


    vp32 wrote:
    Is this data related to the clock somehow, SWCLK?
    NO.

    vp32 wrote:
    2. I use ST-Link. I came across such Connect Under Reset setting. What does it give me? How to take advantage of it? Does it mean the reset button has to be pressed?
    If you turn off the SWD in the program, this connection during the reset gives you access to the SWD interface. And yes, you can hold the reset, but if you don't have a Chinese clone, the programmer will do it for you.

    vp32 wrote:
    Why in cubeMX you can programmatically set two pins to work with SWD?
    Probably because SWD has two signals.
  • ADVERTISEMENT
  • #3 18330935
    BlueDraco
    MCUs specialist
    Posts: 6479
    Help: 939
    Rate: 421
    vp32 wrote:
    2. I use ST-Link. I came across such Connect Under Reset setting. What does it give me? How to take advantage of it? Does it mean the reset button has to be pressed?


    On the contrary. This option means that ST-Link will try to reset the CPU by whatever method it can when attempting to connect. In some uCs the connection fails without it (e.g. often in the F4 series). If you programmed the SWD by mistake and you don't have the RESET line connected to the ST-Link, you can press the RESET button while connecting and it will work without this option, because in this case you reset the uC, not the ST-Link.
  • #4 18330977
    vp32
    Level 11  
    Posts: 505
    Rate: 47
    I read a little, there is light at the end of the tunnel.
    Isn't it a coincidence that after a hundred pins for SWD work are set in CubeMX so that the Debugger can be used during normal operation, otherwise they are ordinary GIPO? And maybe that's why it's connect under reset?
    I also read that SWO also needs SWD. And now I don't understand SWD is two-pin and what about SWO, does it not connect together and actually three lines are needed?
    Also tell me if SWO is activated in the ARM program or is it only activated in the program like Atolic? Because since it is a regular PB3 port, it can be used for other purposes.
    P.S. why there is no ST-LINK programmer selection in Sytem Workbench. No underlined tab? Title: STM32 Serial Wire Viewer & Output, ST-LINK: Beginner Questions on SWO, SWCLK & Reset

    PS> now I've read that SWO is independent of SWD and can also work with JTAG, so what activates SDO?
  • #5 18331006
    Sareph
    Level 24  
    Posts: 638
    Help: 65
    Rate: 378
    vp32 wrote:
    I also read that SWO also needs SWD. And now I don't understand SWD is two-pin and what about SWO, does it not connect together and actually three lines are needed?
    SWD as such is two-pin, "SWD + async trace" is three-pin. So if you want to use ITM then you need 3 lines.

    vp32 wrote:
    Also tell me if SWO is activated in the ARM program or is it only activated in the program like Atolic? Because since it is a regular PB3 port, it can be used for other purposes.
    You can initialize the ITM in the program code on the MCU, the debugger can do it for you. If you're not using it as an SWO, then yes - it can be used as a GPIO.

    And basically you can use trace/ITM even without SWD/JTAG but then *probably* only in synchronous mode.
  • #6 18334507
    vp32
    Level 11  
    Posts: 505
    Rate: 47
    Can you help me?
    I don't understand why I can't connect to uC using ST-LINK.
    SWD pins PA13 and PA14 are free, I don't have them occupied. In Cube they are set as SWDIO and SWCLK. Supposedly, this setting does nothing other than not assigning pins to other purposes.
    In TrueStudio, there is always an error after trying to establish communication
    Error in initializing ST-LINK device.

    When I run ST-Link Utility, it does not connect either, but after pressing RESET, it manages to establish communication with the board.

    What's the deal with Connect Under Reset, why can't I connect via Debbuger in TrueStudio?
  • ADVERTISEMENT
  • #7 18334582
    Sareph
    Level 24  
    Posts: 638
    Help: 65
    Rate: 378
    vp32 wrote:
    SWD pins PA13 and PA14 are free, I don't have them occupied. In Cube they are set as SWDIO and SWCLK. Supposedly, this setting does nothing other than not assigning pins to other purposes.
    Well, it depends, in F1, for example, the default setting is full JTAG. In F0, SWD is enabled by default, but...

    vp32 wrote:
    What's the deal with Connect Under Reset, why can't I connect via Debbuger in TrueStudio?
    ... it would turn out that you turned off the SWD, most likely in GPIO by changing the pin functions in port A.
  • #8 18334653
    vp32
    Level 11  
    Posts: 505
    Rate: 47
    Ok, how to fix it?, The chip is STM32F103C8Tx
    So it comes out that JTAG is the default, but I read that if there is JTAG, SWD switches when trying to establish a connection. or not?

    Tell me what is this Connect Under Reset, why did they separate it specifically?
  • #9 18334678
    Sareph
    Level 24  
    Posts: 638
    Help: 65
    Rate: 378
    vp32 wrote:
    The chip is STM32F103C8Tx

    A, then not in port A, because there DP is configured in AFIO->MAPR

    vp32 wrote:
    So it comes out that JTAG is the default, but I read that if there is JTAG, SWD switches when trying to establish a connection. or not?
    I haven't actually checked, somehow I always have the SWD "manually" selected.

    vp32 wrote:
    Tell me what is this Connect Under Reset, why did they separate it specifically?
    That is why. Because you might want to connect a debugger while the program is running too, like if it crashes, to see what's going on. Or maybe there is a situation like you have, that for some reason the SWD is turned off (or it will stop working because you set the clocks wrong or you do not connect AVCC) and somehow you have to get to the system.

    Check if:

    [syntax=C]AFIO->MAPR = (0b010
  • #10 18334938
    vp32
    Level 11  
    Posts: 505
    Rate: 47
    Title: STM32 Serial Wire Viewer & Output, ST-LINK: Beginner Questions on SWO, SWCLK & Reset No changes unfortunately :-(
    Sareph wrote:
    A, then not in port A, because there DP is configured in AFIO->MAPR

    I don't understand what you mean, if not on A then on what.
    I took PA13 and PA14 from CubeMX. See drawing.

    Sareph wrote:
    I haven't actually checked, somehow I always have the SWD "manually" selected.

    Manually, i.e. by typing in that instruction you posted?

    And if Under Reset is what special initialization is it? Couldn't you write in the note that if you don't want to connect, then cut RESET? I can't figure out the intention of this "special mode".
  • #11 18334959
    Sareph
    Level 24  
    Posts: 638
    Help: 65
    Rate: 378
    vp32 wrote:
    Title: STM32 Serial Wire Viewer & Output, ST-LINK: Beginner Questions on SWO, SWCLK & Reset No changes unfortunately :-(

    Your Cube configuration indicates that you have SWD *disabled*. Go under device SYS, there enable debug on SWD or async trace if you want SWO. Merely marking the pins here does not give you anything. (Orange - pony reserved but device is not turned on).

    vp32 wrote:
    I don't understand what you mean, if not on A then on what.
    On A, but no. :D Because in older STMs, if you want to switch SWD configurations, you change it in AFIO, and if debug is turned on, these pins are "force" intended for debug and you can change their functions in port A (GPIO) and it won't help you anyway.

    vp32 wrote:
    Manually, i.e. by typing in that instruction you posted?
    Yes. At the same time, it will also switch all remapped pins to default ones. Well, that's just for the test.

    vp32 wrote:
    And if Under Reset is what special initialization is it? Couldn't you write in the note that if you don't want to connect, then cut RESET? I can't figure out the intention of this "special mode".
    Reset "presses" the programmer for you, unless you have a Chinese clone, then no, because they do not have a hardware reset and you have to do it manually.
  • #12 18335112
    vp32
    Level 11  
    Posts: 505
    Rate: 47
    Sareph wrote:
    On A, but no. :D Because in older STMs, if you want to switch SWD configurations, you change it in AFIO, and if debug is turned on, these pins are "force" intended for debug and you can change their functions in port A (GPIO) and it won't do you any good.

    You got me so confused I have no idea what you're talking about :(
    Correct me if I misunderstand you. I connect the programmer to these Pins PA13 and 14.
    Bits 26:24 SWJ_CFG[2]: Serial wire JTAG configuration
    These bits are write-only (when read, the value is undefined). They are used to configure the
    SWJ and trace alternate function I/Os. The SWJ (Serial Wire JTAG) supports JTAG or SWD
    access to the Cortex debug port. The default state after reset is SWJ ON without trace. This
    allows JTAG or SW mode to be enabled by sending a specific sequence on the JTMS /
    JTCK pin.
    000: Full SWJ (JTAG-DP + SW-DP): Reset State
    001: Full SWJ (JTAG-DP + SW-DP) but without NJTRST
    010: JTAG-DP Disabled and SW-DP Enabled
    100: JTAG-DP Disabled and SW-DP Disabled
    Other combinations: no effect


    If the value 000, 001, or 010 is written there (in these bits 24:26), these PA13 and PA14 will always be on JATG SWD. And it doesn't matter what he writes to the GPIO port because it doesn't matter then. So for these PA13 and 14 to do anything, those 24:26 bits must be 100? Yes?
    You write in older and newer, how and which, how do I know which is old and which is new?

    But reset isn't meant to be pressed all the time? because if I hold it all the time it doesn't work? or so?

    Now try with SYS, pony turned green

    Added after 7 [minutes]:

    But this setting in SYS SWD gives the same effect as your instruction because according to CubeMX it gives: Title: STM32 Serial Wire Viewer & Output, ST-LINK: Beginner Questions on SWO, SWCLK & Reset
  • ADVERTISEMENT
  • #13 18335216
    Sareph
    Level 24  
    Posts: 638
    Help: 65
    Rate: 378
    vp32 wrote:
    If the value 000, 001, or 010 is written there (in these bits 24:26), these PA13 and PA14 will always be on JATG SWD.
    Well, when JTAG is on, it's not only them, but roughly yes.

    vp32 wrote:
    And it doesn't matter what he writes to the GPIO port because it doesn't matter then. So for these PA13 and 14 to do anything, those 24:26 bits must be 100? Yes?
    Yes.

    vp32 wrote:
    You write in older and newer, how and which, how do I know which is old and which is new?
    From the documentation, the "newer" ones don't have AFIO. And they have other GPIO registers.

    vp32 wrote:
    But reset isn't meant to be pressed all the time? because if I hold it all the time it doesn't work? or so?
    If you have properly configured and connected SWD, then you shouldn't have to hold the reset (unless something with the program goes very wrong).

    vp32 wrote:
    But this setting in SYS SWD has the same effect as your instruction
    Because it's roughly a different way to accomplish the same thing (if you prefer to use Cube). Well, "my instruction" changes the pin remapping settings of other devices, if they are set, Cube does not.
  • #14 18342282
    vp32
    Level 11  
    Posts: 505
    Rate: 47
    Okay, something happened after changing SYS to SW Debug.
    But that gave me another question. Because setting this SW Debug causes the function to be called

    Code: C / C++
    Log in, to see the code


    Which registers AFIO->MAPR 0b010 at [26:24]
    If I don't set SW Debug it will be entered into these registers

    Code: C / C++
    Log in, to see the code



    So they will be ordinary GPIO

    But why, after all the initialization done by CubeIDE, I type it manually

    [syntax=c]AFIO->MAPR =(0b010
  • #15 18344562
    vp32
    Level 11  
    Posts: 505
    Rate: 47
    P.S. thanks for the help :-(
  • #16 18345255
    excray
    Level 41  
    Posts: 5498
    Help: 739
    Rate: 655
    And what does your ST-Link look like? Sareph already mentioned that "Connect under reset" doesn't work in Chinese versions.
  • #17 18359329
    vp32
    Level 11  
    Posts: 505
    Rate: 47
    It's a typical quinol.
    I rather meant the principle of operation of this Connect Under Reset and not whether my programmer works. are these instructions sent when the connection is established, or is it just a matter of giving an additional reset signal and if so, for how long?
  • #18 18359367
    Sareph
    Level 24  
    Posts: 638
    Help: 65
    Rate: 378
    vp32 wrote:
    is it just a matter of giving an additional reset signal and if so, for how long?
    You download the reset to ground, the programmer initiates what it needs in the CPU during this reset, you release the reset (or the programmer does it) and the SWD is restored, so you can program.

Topic summary

✨ The discussion revolves around the STM32 microcontroller's Serial Wire Viewer (SWV) and Serial Wire Output (SWO) functionalities, particularly for beginners. Key points include the nature of SWO as a one-way serial port for data output, its activation through program code, and its relationship with SWD (Serial Wire Debug) and SWCLK (Serial Wire Clock). The "Connect Under Reset" feature of the ST-Link programmer is explained as a method to reset the microcontroller to establish a connection, especially useful for certain STM32 models like the F4 series. The conversation also touches on the configuration of SWD pins in CubeMX, the necessity of proper initialization for SWD and SWO, and the distinction between SWD and JTAG modes. Issues with connecting to the microcontroller and troubleshooting steps are discussed, emphasizing the importance of correct pin assignments and settings in the firmware.
Generated by the language model.

FAQ

TL;DR: SWD uses 2 pins; add SWO for 3‑line async trace — "SWD as such is two‑pin." This FAQ explains enabling SWV/ITM and pin setup. [Elektroda, Sareph, post #18331006]

Why it matters: For STM32 beginners using ST‑LINK, this answers how to enable SWD/SWO and fix "Error in initializing ST‑LINK device" issues.

Quick Facts

What are Serial Wire Viewer (SWV) and Serial Wire Output (SWO)?

SWV is an ARM trace feature that lets firmware stream diagnostic data without halting. SWO is the physical, one‑way pin carrying that stream. Developers often redirect printf or ITM messages to SWO for real‑time logs. Think of it as a debug‑only, single‑wire UART‑like channel. [Elektroda, Sareph, post #18330651]

Is SWO tied to the SWCLK clock?

No. SWO is asynchronous and independent of SWCLK. Configure SWO separately from the SWD port. This separation lets you trace while debugging over SWD. [Elektroda, Sareph, post #18330651]

How many wires do I need for SWD with SWO tracing?

Use two wires for SWD (SWDIO, SWCLK). Add the SWO pin for ITM/SWV tracing, making three lines total. This is often called “SWD + async trace.” [Elektroda, Sareph, post #18331006]

How do I enable SWO/ITM on an STM32?

Enable ITM in firmware or let your debugger do it. Configure the SWO pin to its alternate function. When unused, that pin can act as normal GPIO. Many IDEs (e.g., Atollic TrueSTUDIO/CubeIDE) can auto‑init ITM and SWO during attach. Redirecting printf to ITM/SWO is a common setup. [Elektroda, Sareph, post #18331006]

What does ST‑LINK “Connect Under Reset” actually do?

It holds the MCU in reset while the debugger configures the debug port, then releases reset to attach cleanly. As one expert explains: “You download the reset to ground, the programmer initiates what it needs… and the SWD is restored.” Use it when firmware disabled SWD or misconfigured clocks. [Elektroda, Sareph, post #18359367]

Do I have to hold the RESET button to connect?

You can hold RESET while connecting if needed. However, with genuine ST‑LINK hardware, the tool usually toggles reset for you. Clone adapters may not drive NRST, requiring manual RESET during attach. [Elektroda, Sareph, post #18330651]

Why can’t TrueSTUDIO/CubeIDE connect unless I press RESET?

Your firmware likely disabled SWD or misconfigured clocks, blocking the debug port. Missing power to analog VCC can also break attach. Use Connect Under Reset or hold RESET while connecting, then re‑enable SWD in your firmware settings. [Elektroda, Sareph, post #18334678]

How do I enable SWD on STM32F103 in CubeMX/HAL?

In CubeMX, set SYS->Debug to “Serial Wire.” HAL then calls __HAL_AFIO_REMAP_SWJ_NOJTAG(), setting AFIO->MAPR SWJ_CFG bits [26:24] to 0b010. That disables JTAG‑DP and enables SW‑DP. This restores PA13/PA14 for SWDIO/SWCLK. [Elektroda, vp32, post #18335112]

What’s the STM32F1 default debug state after reset?

Default is Full SWJ (JTAG‑DP + SW‑DP) with trace off. The three SWJ_CFG bits [26:24] control this: 000 is the reset state. A specific JTMS/JTCK sequence can switch between JTAG and SWD. [Elektroda, vp32, post #18335112]

Can I reuse PA13/PA14 as GPIO and still program later?

Yes, but only after disabling debug. Set SWJ_CFG to 100 (JTAG‑DP disabled and SW‑DP disabled). Then PA13 and PA14 operate as normal GPIO. Note that this also disables the debug port until re‑enabled. [Elektroda, vp32, post #18335112]

Why doesn’t “Connect Under Reset” work with my ST‑LINK?

Many clone ST‑LINKs lack hardware NRST control, so the tool cannot assert reset during attach. In that case, press RESET manually while initiating the connection from your IDE or ST‑Link Utility. [Elektroda, excray, post #18345255]

How do I recover a CPU after disabling SWD?

Use manual RESET during connection:
  1. Hold the board’s RESET button low.
  2. Start “Connect” or “Debug” in ST‑Link Utility/IDE.
  3. Release RESET once the tool is trying to attach. This regains access so you can re‑enable SWD. [Elektroda, BlueDraco, post #18330935]

In CubeMX, why isn’t selecting PA13/PA14 as SWD pins enough?

Pin tagging alone does not enable the debug block. You must enable SWD under System Core -> SYS -> Debug (SWD or SWD+Trace). Otherwise, the pins remain regular GPIO and the debugger cannot connect. [Elektroda, Sareph, post #18334959]
Generated by the language model.
ADVERTISEMENT