I have been using SWO with STM32 chips for debugging for a number of years. I have managed to side-step this issue until now. I would like to be able to use SWO for debugging while varying the SysClock frequency.

This is an issue because OpenOCD requires that I specify the SysClock when setting up SWO:

tpiu config internal swv uart off 48000000

This works fine at a fixed HCLK frequency of 48MHz. But as soon as I change HCLK, either up or down, the data coming from the port is garbled.

In hardware, I am taking SWO straight from the CPU into an ST/Link SWD interface. The documentation for openocd does not mention how to deal with a varying HCLK (or, if it does, I have not found it). I have found hints online that using Manchester encoding might work.

I have tried using both UART and Manchester modes. Both work at fixed frequencies and neither appear to work when changing the clock speed.

Is it possible to configure openocd for SWO with a varying HCLK? If so, how does one do it?


2 Answers 2


It has been almost 3 years since I asked this question and, after some recent experimentation, I finally found something that works.

This was made more difficult because the register used to make this work is not documented in the STM32L4+ reference manual. I discovered it when reading through some relevant STMicro Community posts (see referances below). The register is documented in the Cortex-M4 reference manual from ARM.

I am using STM32CubeIDE rather than Eclipse/OpenOCD at this point. In the STM32CubeIDE, the SWO can be limited with the "Limit SWO Clock" option. My system clock varies from 2MHz to 72MHz. What worked for me is to limit the SWO clock to 2000kHz (2MHz). Then, after each clock change, I update the TPIU_ACPR register (the trace port asynchronous prescaler) to keep the SWO output speed constant.

TPI->ACPR = (SystemCoreClock / 2000000) - 1;

This sets the asynchronous prescaler to match the 2MHz SWO speed. This works for me because all of the clock speeds I use are even multiples of 2MHz.

With this change, I get the complete SWO output regardless of the the Sysclock speed. I do see small glitches in the output when switching speeds. This may be due in part to switching between MSI and HSE as the clock source.



The question didn't specify the type of STM32 in use. I checked documentation for STM32L4x5/STM32L4x6 as an example: Section 48.17.8 of the Reference Manual states that

TRACECLKIN input is internally connected to HCLK. This means that when in asynchronous trace mode, the application is restricted to use time frames where the CPU frequency is stable.

SWO (the Asynchronous Mode of TPIU) is a asynchronous one-wire protocol that transmits symbols, either as a Manchester code or NRZ. In either case, the receiver must know (the order of magnitude of) the baud rate in order to distinguish bit changes/repetitions from signal noise.

Therefore, I think that changing SWO baudrate on-the-fly can not be implemented in any useful way.

Edit: I might have misunderstood the question. Is the goal to change the HCLK frequency during runtime "every now and then" or only once during power-up? In the latter case, the trace adapter can be configured at the target frequency (through OpenOCD, too, I recon), and tracing will work from when HCLK has reached that stable frequency.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.