STM32 Debugging With ST-Link v2 SWD | Serial Wire Viewer

Previous Tutorial Previous Tutorial Tutorial 6 Next Tutorial Next Tutorial
STM32 Debugging With ST-Link v2 SWD | Serial Wire Viewer
STM32 Course Home Page 🏠

 

STM32 SWD Debugging

 

In this tutorial, we’ll discuss how to debug your projects using the ST-Link debugger SWD and the Serial Wire Viewer (SWV) mode. How to open it and print data to the serial wire console within the IDE itself. We’ll also set up breakpoints to stop the MCU at some points in the code to check live variables while the MCU is running your firmware.


   Required Components For LABs   

 

All the example code/LABs/projects in the course are going to be done using those boards below.

QTY Component Name 🛒 Amazon.com 🛒 eBay.com
2 BreadBoard Amazon eBay
1 LEDs Kit Amazon Amazon eBay
1 Resistors Kit Amazon Amazon eBay
1 Capacitors Kit Amazon Amazon eBay & eBay
2 Jumper Wires Pack Amazon Amazon eBay & eBay
1 9v Battery or DC Power Supply Amazon Amazon Amazon eBay
1 Micro USB Cable Amazon eBay
1 Push Buttons Amazon Amazon eBay

★ Check The Full Course Complete Kit List

Some Extremely Useful Test Equipment For Troubleshooting:

Affiliate Disclosure: When you click on links in this section and make a purchase, this can result in this site earning a commission. Affiliate programs and affiliations include, but are not limited to, the eBay Partner Network (EPN) and Amazon.com.


   STM32 ARM Cortex-M3 Debug Support    

 

The ARM Cortex®-M3 & M4 core contains hardware extensions for advanced debugging features. The debug extensions allow the core to be stopped either on a given instruction fetch (breakpoint) or data access (watchpoint). When stopped, the core’s internal state and the system’s external state may be examined. Once the examination is complete, the core and the system may be restored and program execution resumed.
The debug features are used by the debugger host when connecting to and debugging the STM32 MCUs.

Two interfaces for debugging are available:

  1. Serial Wire Debug (SWD)
  2. JTAG Debug Port

The debug features embedded in the Cortex®-M3 core are a subset of the Arm® CoreSight Design Kit.

We’ll be using the SWD in this tutorial. Down below is a diagram for the ARM Cortex-M3 debugging support hardware. Which includes the ITM for the serial wire trace debugging, and extra more features.

STM32 ARM Cortex-M3 Debug Features

The Arm® Cortex®-M3 core provides integrated on-chip debug support. It is comprised of:

  • SWJ-DP: Serial wire / JTAG debug port
  • AHP-AP: AHB access port
  • ITM: Instrumentation trace macrocell
  • FPB: Flash patch breakpoint
  • DWT: Data watchpoint trigger
  • TPUI: Trace port unit interface (available on larger packages, where the corresponding pins are mapped)
  • ETM: Embedded Trace Macrocell (available on larger packages, where the corresponding pins are mapped)

 


   ST-Link v2 (Hardware Debugger)   

 

For Debugging Projects With The Blue Pill Board

This is the tool you need. Unfortunately, the Chinese clone on the left doesn’t support the serial wire trace debugging as it is. Because it doesn’t expose the SWO pin to its pinout. So you’ll need the original kit on the right, and the SWO pin will be connected to pin B3 of the blue pill.

ST-Link V2 STM32 Programmer ST-Link V2

Alternatively, there is a hack for the ST-Link v2 USB Clone (the one on the left). So you can solder the SWO pin out to any pin on the header. Typically we’re using only 4 out of the 10pins. So you can route the SWO pin out to any other pin. As shown in this article.

5V pin converted into SWO

While configuring the SWD debugging feature in CubeMX, you’ll notice that the SWO pin is B3. Which you’ll need to connect to ST-Link debugger. The steps for SWD debugging are going to be the same regardless of the target microcontroller.

STM32 SWV Debugging

For Debugging With Nucleo Boards (Nucleo32-L432KC)

Nucleo boards come equipped with an integrated ST-Link v2 programmer/debugger chip. It’s another microcontroller on the board itself. In the case of our Nucleo32L432KC, it’s a small microcontroller on the back of the board. It’s hooked up to the USB port on the board. It gives you full debugging capability while flashing the code easily to the board. It even gets detected as a storage device, so you can just drag and drop the binary file (hex code) to the chip directly and it’s been flashed already without any clicks!

Nucleo32L432KC

The Target Microcontroller STM32L432KC On The Top Side OF The Board

NUCLEO-L432KC-ST-Link V2

The ST-Link v2 Programmer / Debugger On The Bottom Side OF The Board. It’s The One Connected To The USB Port On The Board As Well.

Obviously, no extra hardware is needed when you’re using Nucleo Boards. And the debugging steps are the same for all Nucleo boards and the blue pill with ST-Link v2. However, the Nucleo32 boards (the 32-pins small version) like the shown above, doesn’t have the SWO pin connected to the integrated ST-Link debugger 🙁

You’ll have to solder a bridge wire across the board with the help of the board schematic to enable this serial wire trace feature. And I won’t do this. I’ll give you the steps to debug with the serial wire viewer just in case you could hack your ST-Link clone or you have a discovery board or a Nucleo-64 maybe that doesn’t lack the connection for the SWO pin.


   Create Demo Project & SWV Debug   

 

Step1: Open CubeMX & Create New Project

STM32 CubeMX Create New Project

Step2: Choose The Target MCU & Double-Click Its Name

STM32 SWV Debugging Tutorial

Step3: Configure All The Peripherals & GPIOs You’re Willing To Use In Your Project!

Whatever your project is. Just do the normal configurations as per the application. To enable the serial wire debugging, you only have to add an extra step which is the one below.

Step4: Set The Trace Serial Wire Debugging

Note: the pin B3 is now the SWO and needs to be connected to the respective pin on the ST-Link v2 debugger

PCBgogo Ad

STM32 SWV Debugging

Step6: Go To The Clock Configuration

Step7: Set The System Clock To Be 72MHz Or Whatever You Want

STM32 Clock Tree Configuration CubeMX

Step8: Name & Generate The Project Initialization Code For CubeIDE or The IDE You’re Using

Then, open the project in the IDE you’re using. And head over to the main.c file.

You’ll need to add this function to your main.c file before the main function. In case you’re not including the syscalls in your project yet. It gives the implementation for the printf() function so as to print the data over the ITM trace.

Step9: Open The Debug Configurations & Enable SWV

STM32 SWV Debug Configurations

Step10: Build The Project & Start Debugging Session

Step11: Open The SWV ITM Data Console Screen

STM32 SWV ITM Debugging

Step12: Click The Configure Trace Button & Enable Port0 Then Click OK .. And Don’t forget to click the red button pointed to with arrow! To start the trace .. And resume the debug so the microcontroller continues printing the data

STM32 SWV ITM Debugging Configuration

 

Nothing will show up in my IDE as I don’t have the SWO pin connected at all. So, in the upcoming tutorials, I’ll show you a couple of other ways to debug your projects in case you couldn’t have the SWV starting with no problems.

 


   Live Expressions & Variables   

 

Now, I’ve added an integer counter variable and a constant string. I did also place a BreakPoint in the main while loop just before the counter increment statement.

 

 

 

Previous Tutorial Previous Tutorial Tutorial 6 Next Tutorial Next Tutorial

 

Share This Page With Your Network!

Khaled Magdy

I'm an embedded systems engineer doing both Software & Hardware. I'm an EE guy who studied Computer Engineering, But I'm also passionate about Computer Science. I love reading, writing, creating projects and Technical training. A reader by day a writer by night, it's my lifestyle. You can view my profile or follow me via contacts.

You may also like...

2 Responses

  1. yerih says:

    Hey bro! Thanks for the tuto. I’ve a doubt, I’ve done all the tuto but when I start tracing (click the red button), the stlink is freezed and my PC lose the connection. I plug a 100ohms resistor to SWO pin in the debugger side. Do you have any idea ?

    • Khaled Magdy says:

      The fact that it freezes and the PC loses connection makes me think it can be a configuration issue. Make sure you’re chosing the right port from the device manager you can check its number. Also make sure that serial wire debug in cubemx and the swo pin is activated and highlighted in green as usual.
      I also had a limited success with the swd trace using the nucleo32 and blue pill. The discovery board works just fine.

Leave a Reply

%d bloggers like this: