In this post I will talk about SWV. How to set up SWV function and how to use printf and logic analyzer.
This week a friend send me a video where a guy uses printf directly inside uVision debugger without VPC or UART. This is called SWV(serial wire viwer), a real-time trace technology that uses SWD(serial wire debug) port and the SWO(serial wire output) pin. Serial Wire Viewer provides advanced system analysis and real-time tracing without the need to halt the processor to extract certain types of debug information. You can read more in . Besides the printf, what caught my attention was the logic analyzer built in the SWV debugger.
If you use printf to debug your code you can choose between SWV, UART and VPC. SWV just needs the SWO pin(PB3), UART needs 3 pins(TX,RX,GND), and VPC needs 2 pins(USB_DP, USB_DM). Using SWV will save some pins to your application.
I am currently working with the Blue Pill Dev. Board (figure 2), it has a STM32F103 (Cortex-M3 core) uController. I tested SWV on this board and nothing happened. After click on Run in the debugger session the only message was “Trace: No Synchronization”. I found a lot of things about SWV but nothing helped in this time.
After some time I gave up and tried on the MCBSTM32 Dev. Kit (picture 2). This board has a STM32F103RBT6 microcontroller a use a ULINK-ME debugger. SWV worked great with this board with no problem.
I decided to use the STM32F429I-DISC1 to test SWV, and nothing happend too. This time was easy to find the problem. The STM32F429I-DISC1 has a STM32F429ZIT6U microcontroller and has a ST-LINK V2 debugger built-in the board. The ST-LINK comes by default without the connection between pin PB3 of the microcontroller and SWO of the debugger. To fix this the Solder Bridge SB9 must be shorted in order for SWV to operate. You can see the solder bridge in the figures below and know more about this in the datasheets.
I did not understand why the Blue Pill still not working. After some time, I do not know how, I found a video from a Russian channel, ElectroHobby, that solve my problem, Thanks friend.
The ST-LINK V2 that I have is the cheap ones. I bought it on Aliexpress with my blue pill for U$10,00 I think. Figure 7 shows two ST-LINK V2 that you can buy on Aliexpress, they are almost the same if you use Blue Pill. The ST-LINK on the left of Figure 7 has only SWD connection, and the ST-LINK on the right has SWD, SWIM, and JTAG connection.
The video of ElectroHobby shows who to hack the ST-LINK on the left side of the Figure 7. Here I will show how to hack the ST-LINK on the right side.
I was connecting the SWO(PB3) pin from the Blue Pin on the JTWO pin (I thought that was the same because of the labels on Figure 7) (-.-).
If you have the left ST-LINK V2 of Figure 7 you can follow the video.
My hack was a little cleaner that presented on the video. After you open the case of the programmer you will see some thing like Figure 8. There is a SWIM pin on ST-LINK V2 that is used to STM8 microcontrollers, you can see this pin labeled on Figure 7, I probably will not use this microcontrollers then I soldered a wire from PA10 in the 220 ohm resistor that is connect between PB3 to the pin 5 referent to SWIM.
Figures 8 and 9 show the positions where you need to solder the wire. Figures 10, 11, and 12 show how my ST-LINK looks now. Figure 13 shows the connectors labeled of my ST-LINK now.
I don’t know if there is a problem to connect PB3 and PA10 directly, but this worked (This is gambiarra). You can cut/scratch the path from the resistor to the PB3 pin from the microcontroller of the ST-LINK V2 if you want.
Now the Blue Pill has the SWV functions too. The next steps explain how to set up this board to work with SWV.
First you need to create a new project on STM32CUBEMX, select the STM32F103C8Tx microcontroller, select Crystal to HSC in RCC, and Serial Wire to debug in SYS (Figure 14). After this you need to set the clock in the Clock Configuration tab. Just type 8 in the Input Frequency text field, and 72 in the HCLK to AHB bus, core text field (Figure 15). Now you can generate the source code to Keil uVision.
I just wrote a simple code to demonstrate this features, figure 16 shows the code. To use a variable on watchdog or logic analyzer you need to declare it globally.
Now you need to enable STDIN/STDOUT with ITM in Manage Run-Time Environment window like figure 17. In Options for target window, go to Debug tab, and click in Settings beside the name of your programmer device. Here you need to configure the debugger. You need to copy figure 19. Change the core frequency to the value that you use in the HCLK to AHB bus, core text field of STM32CubeMx.
Compile and download the program, and click in Start Debug Session. Click in View-> Serial Window->Debug (printf) Viewer to add a terminal. Click in View->Analysis Windows->Logic Analyzer to add a logic analyzer window. Figures 20 and 21 show how to add and set a variable to the logic analyzer.
Now you just click in Run or press F5.
Figure 15. STM32CubeMx Clock Configuration Tab
Figure 16. Keil uVision5 Main File
Figure 17. Keil uVision5 Manage Run-Time Enviroment Window
OFF-TOPIC: I want to thank the Russian Blue Pill community, this guys is working hard with this board.
 STM32F7: Debug(printf) Viwer y Logic Analyzer en Keil 5 – https://www.youtube.com/watch?v=ji1MbxQJSc8
 Serial Wire Viewer (SWV) for ARM Cortex-M3 Processors ARM Real-Time Trace aids debugging – http://www.keil.com/download/files/swv_on_cortex-m3.pdf
 Logic Analyzer – http://www.keil.com/support/man/docs/uv4/uv4_db_dbg_logicanalyzer.htm
 STM32F429I-DISC1 datasheet – http://www.disca.upv.es/aperles/arm_cortex_m3/llibre/st/en.DM00093903.pdf
 STM32 Serial Wire Output SWO – https://www.youtube.com/watch?v=anop2wrf5rU
 STMF103xx Datasheet – http://www.st.com/content/ccc/resource/technical/document/reference_manual/59/b9/ba/7f/11/af/43/d5/CD00171190.pdf/files/CD00171190.pdf/jcr:content/translations/en.CD00171190.pdf