cancel
Showing results for 
Search instead for 
Did you mean: 

How to run code and view serial on Nucleo board

acertainfruit
Associate

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.


6 REPLIES 6
LCE
Principal

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?


@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?

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.

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;

 

LCE
Principal

This is actually not documented in any ST material, you have to check the original ARM documentation.
Google helps.

SofLit
ST Employee

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.

 

To give better visibility on the answered topics, please click on "Accept as Solution" on the reply which solved your issue or answered your question.