AnsweredAssumed Answered

ST Link V2, SWO speed weirdness

Question asked by zhao.frank on Feb 13, 2014
I have a bit of code to output some debug using SWO

void swo_init()
     uint32_t SWOSpeed = SWO_SPEED;
     uint32_t SWOPrescaler = (SystemCoreClock / SWOSpeed) - 1; // SWOSpeed in Hz
     CoreDebug->DEMCR = CoreDebug_DEMCR_TRCENA_Msk;
     *((volatile unsigned *)(ITM_BASE + 0x400F0)) = 0x00000002; // "Selected PIN Protocol Register": Select which protocol to use for trace output (2: SWO)
     *((volatile unsigned *)(ITM_BASE + 0x40010)) = SWOPrescaler; // "Async Clock Prescaler Register". Scale the baud rate of the asynchronous output
     *((volatile unsigned *)(ITM_BASE + 0x00FB0)) = 0xC5ACCE55; // ITM Lock Access Register, C5ACCE55 enables more write access to Control Register 0xE00 :: 0xFFC
     ITM->TCR = ITM_TCR_TraceBusID_Msk | ITM_TCR_TXENA_Msk | ITM_TCR_SYNCENA_Msk | ITM_TCR_ITMENA_Msk; // ITM Trace Control Register
     ITM->TPR = ITM_TPR_PRIVMASK_Msk; // ITM Trace Privilege Register
     ITM->TER = 0x00000001; // ITM Trace Enable Register. Enabled tracing on stimulus ports. One bit per stimulus port.
     *((volatile unsigned *)(ITM_BASE + 0x01000)) = 0x400003FE; // DWT_CTRL
     *((volatile unsigned *)(ITM_BASE + 0x40304)) = 0x00000100; // Formatter and Flush Control Register

notice the constant SWO_SPEED

For J-Link, the JLinkSWOViewer.exe program says the speed should be 6000KHz

For ST Link Utility's Serial Wire Viewer, the speed should be 2000KHz

So when I compile, I change it depending on which debugger I am attaching.

But if I accidentally set it to 6000KHz and try to use the ST Link, it will first output gibberish (which is expected), but if I perform a reconnect without updating the code, it magically works!

What's going on? How did the speed change without me changing the code? Is the ST Link V2 speeding itself up or is my microcontroller slowing itself down?

This isn't really an issue or problem, I'm just curious.