2024-09-19 02:02 AM
Hello,
I am trying to count how many clock cycles pieces of code take to run on my Nucleo board. I am using the following code for timing:
start_cycles = DWT->CYCCNT;
// dummy code
end_cycles = DWT->CYCCNT;
diff_cycles = end_cycles - start_cycles;
printf("Num clocks: %u \n", diff_cycles);
I am using different levels of optimization in the compiler settings and checking using live expressions (but also SWV to check what is printed to console). I had heard that debug mode can make things run slower due to a myriad of different factors. So I am wondering how I can, in the simplest way possible, run this code outside of debug mode and still have SWV or something enabled such that the clocks are printed to console.
Pressing the run button starts debugging but disconnects and shuts down after successfully connecting, and going to run as > C/C++ application does the same.
If anyone has any leads on this, it would be greatly appreciated. Thank you.
2024-09-19 02:15 AM
My crystal ball tells me ... nothing! ;)
First of all, please tell us which MCU / Nucleo board.
Does the MCU actually have the cycle counter (not all ARM types have that, but not sure)?
If yes, have you activated it?
Does your code work without the cycle counter parts?
2024-09-19 02:23 AM
@acertainfruit wrote:run this code outside of debug mode and still have SWV or something enabled such that the clocks are printed to console..
I don't think that SWV depends on being in debug mode?
Anyhow, you could just use a UART?
2024-09-19 02:44 AM - edited 2024-09-19 02:47 AM
Sorry. I am using a NUCLEO-G47RE. I think it does have a cycle counter and I did activate it in the .ioc section (TIM2 > Internal clock). And the code does work without the cycle counter parts. No issues there.
2024-09-19 03:34 AM
> I did activate it in the .ioc section (TIM2 > Internal clock)
> And the code does work without the cycle counter parts.
That should make you suspicious.
I don't know what the cycle counter has to do with TIM2, this is something related to the ARM core, not any peripheral.
So whatever you did there in the ioc section (whatever that is...), throw it out and try to replace it with:
For F7 / H7 - so can't guarantee for G4 - it is activated like this:
/* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
/* CPU cycle count activation for debugging */
CoreDebug->DEMCR |= CoreDebug_DEMCR_TRCENA_Msk;
DWT->LAR = 0xC5ACCE55;
DWT->CYCCNT = 0;
DWT->CTRL |= DWT_CTRL_CYCCNTENA_Msk;
DWT->CTRL |= DWT_CTRL_PCSAMPLENA_Msk;
2024-09-19 03:38 AM
This is actually not documented in any ST material, you have to check the original ARM documentation.
Google helps.
2024-09-19 03:39 AM
Hello @acertainfruit and welcome to the community,
Read this article as it may help you to redirect the printf to the UART.
Hope it helps.