Skip to main content
Associate
September 19, 2024
Question

How to run code and view serial on Nucleo board

  • September 19, 2024
  • 4 replies
  • 1453 views

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.


    4 replies

    LCE
    Principal II
    September 19, 2024

    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?

    Associate
    September 19, 2024

    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.

    LCE
    Principal II
    September 19, 2024

    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;

     

    Andrew Neil
    Super User
    September 19, 2024

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

    A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.
    LCE
    Principal II
    September 19, 2024

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

    mƎALLEm
    Technical Moderator
    September 19, 2024

    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."