RadioOperator / STM32F103C8T6_CMSIS-DAP_SWO

CMSIS-DAP SWO CDC STM32F103C8T6 BluePill STLINK ARM Debugger
Apache License 2.0
387 stars 193 forks source link

SWO output? #11

Closed RuStrannik closed 3 years ago

RuStrannik commented 3 years ago

I am trying to figure out how to view the output from the SWO, could you please give a hint? It is quite obvious how achieve that on ST-LinkV2 (GUI) or BlackMagicProbe (dedicated virtual COM port), but I could not find any kind of viewer that would work via USB-HID / CMSIS DAP interface. And thank you so much for sharing this open-source mod! UPD: I assume Keil uVision might have a built-in viewer, but could you suggest an external tool for that?

RadioOperator commented 3 years ago

@RuStrannik Hi, On CMSIS-DAP, SWO info combined into HID/WinUSB data, no individual software Viewer for it. But many IDEs, like Keil/IAR.... already have ready-to-use SWO built-in viewer. To setup SWO working correctly, it's not easy, I think the important is "Speed match".

An alternative method, please try to use Segger RTT + CMSIS-DAP for printf, refer my repo: https://github.com/RadioOperator/STM32_HAL_retarget_printf The RTT Viewer for DAP: https://github.com/XIVN1987/DRTTView/releases/tag/20190423 No need the SWO hardware wire to do almost same jobs.

RuStrannik commented 3 years ago

Thank you so much for quick reply! Will definitely try DRTTViewer, looks like this is exactly what I need. Regarding the remapped printf() output, I agree, this is the simplest and the most efficient way, which I use all the time (99.8% of cases). Unfortunately, there are still some cases when there is simply no other way of getting minimally invasive debug output (i.e. without stopping the MCU core on breakpoints to inspect memory/vars), for instance, on a finished production board, where USARTs are either not wired at all or busy communicating with some essential peripherals, so cannot be disconnected/reused and only SWD (including SWO) pads are exposed for emergency firmware upgrade + debug.

RadioOperator commented 3 years ago

@RuStrannik Hi, If you really want to get the SWO benefits and not use "official" SWO viewer, you could create a swo Viewer by yourself, easy:

SWO data from the target IC, is UART readable data. I use PC terminal software captured some here: 01 43 01 6F 01 75 01 6E 01 74 01 65 01 72 01 20 01 3D 01 20 01 38 01 33 01 0D 01 0A 01 41 01 44 01 20 01 76 01 61 01 6C 01 75 01 65 01 20 01 3D 01 20 01 30 01 78 01 46 01 30 01 35 01 33 01 0D 01 0A 00 00 00 00 00 80 01 43 01 6F 01 75 01 6E 01 74 01 65 01 72 01 20 01 3D 01 20 01 38 01 34 01 0D 01 0A 01 41 01 44 01 20 01 76 01 61 01 6C 01 75 01 65 01 20 01 3D 01 20 01 30 01 78 01 46 01 30 01 35 01 34 01 0D 01 0A 01 43 01 6F 01 75 01 6E 01 74 01 65 01 72 01 20 01 3D 01 20 01 38 01 35 01 0D 01 0A 01 41 01 44 01 20 01 76 01 61 01 6C 01 75 01 65 01 20 01 3D 01 20 01 30 01 78 01 46 01 30 01 35 01 35 01 0D 01 0A 00 00 00 00 00 80 01 43 01 6F 01 75 01 6E 01 74 01 65 01 72 01 20 01 3D 01 20 01 38 01 36 01 0D 01 0A 01 41 01 44 01 20 01 76 01 61 01 6C 01 75 01 65 01 20 01 3D 01 20 01 30 01 78 01 46 01 30 01 35 01 36 01 0D 01 0A 01 43 01 6F 01 75 01 6E 01 74 01 65 01 72 01 20 01 3D 01 20 01 38 01 37 01 0D 01 0A 01 41 01 44 01 20 01 76 01 61 01 6C 01 75 01 65 01 20 01 3D 01 20 01 30 01 78 01 46 01 30 01 35 01 37 01 0D 01 0A

After ignored all "01", "00", "80": 43 6F 75 6E 74 65 72 20 3D 20 38 33 0D 0A 41 44 20 76 61 6C 75 65 20 3D 20 30 78 46 30 35 33 0D 0A 43 6F 75 6E 74 65 72 20 3D 20 38 34 0D 0A 41 44 20 76 61 6C 75 65 20 3D 20 30 78 46 30 35 34 0D 0A 43 6F 75 6E 74 65 72 20 3D 20 38 35 0D 0A 41 44 20 76 61 6C 75 65 20 3D 20 30 78 46 30 35 35 0D 0A 43 6F 75 6E 74 65 72 20 3D 20 38 36 0D 0A 41 44 20 76 61 6C 75 65 20 3D 20 30 78 46 30 35 36 0D 0A 43 6F 75 6E 74 65 72 20 3D 20 38 37 0D 0A 41 44 20 76 61 6C 75 65 20 3D 20 30 78 46 30 35 37 0D 0A

Translated to ASCII, we got what we wanted: Counter = 83 AD value = 0xF053 Counter = 84 AD value = 0xF054 Counter = 85 AD value = 0xF055 Counter = 86 AD value = 0xF056 Counter = 87 AD value = 0xF057

So modify a UART terminal source code, ignore all "01", "00", "80" data, then swo Viewer is done.

"Speed match" is important. if setup swo data rate is 1Mbps, the UART would be 1Mbps also.

RadioOperator commented 3 years ago

@RuStrannik Hi, One new question is How to start ITM/SWO functions without IDE support, just from IC internal initial. I do not have a solution on this.

RuStrannik commented 3 years ago

Ah, this is awesome! Once again, thank you very much for sharing the knowledge! Since SWO was used so rarely I never studied it carefully to find that it is actually possible to configure it to run in USART 8N1 frame compatible mode.

Just checked, and also wanted to share my findings. In my example, I took STM32F1. By default, SWO output format is configured in Manchester encoding mode: TPI->SPPR (0xE00400F0) = 0x01 and is not readable by USART. [1] Nevertheless, it could be configured to enable async NRZ (USART) mode, there is a good example of automating that, given here: https://github.com/PetteriAimonen/STM32_Trace_Example Moreover, this is the mode that is used by the ST-Link Utility with their SWO viewer. Regarding the baudrate, as you have mentioned, it depends on the MCU's core clocking frequency and also on SWO Clock Prescaler, and both could be configured. Taking the same example of ST-Link SWO Viewer, when launched, it starts debugger and fully configures the MCU core to output from SWO at 2Mbps.

Also, I think it is definitely worth mentioning explicitly, that on Cortex-M3 family processors, such as STM32F1xx, there is no way to configure the SWO or output anything to it while the DBGMCU->CR (0xE00400F0) does not have bit C_DEBUGEN (0x01) set, which the MCU Core not allowed to set itself, but it can be set externally, from the AHB-AP master bus, controlled by the connected debugger. [2]

References: [1] STM32F1 Reference Manual, p.1108 [2] Cortex-M3 Technical Reference Manual, Revision: r1p1, p.211

RadioOperator commented 3 years ago

@RuStrannik Hi, thanks for the info, I did not study the details about swo configs clearly, because I'm using Keil only.

Regarding the Manchecter mode, my another repo: STLINKv3DAPv2_WinUSB, already enabled decode/encode for Manchecter STREAM mode, but I never test on it. https://github.com/RadioOperator/CMSIS-DAP_for_STLINK-V3MINI/tree/master/STLINKv3DAPv2_WinUSB