Skip to main content
pjn
Associate
June 20, 2018
Question

Execution time in STM32

  • June 20, 2018
  • 7 replies
  • 6012 views
Posted on June 20, 2018 at 13:08

What is the best way to find the execution time of a particular section of code in the STM32 microcontroller? 

Except checking on DSO.

Majerle.Tilen.001

‌

null
    This topic has been closed for replies.

    7 replies

    Tilen MAJERLE
    ST Employee
    June 20, 2018
    Posted on June 20, 2018 at 13:14

    Hello,

    if available, you may use DWT counter, or general purpose timer aswell.

    Best regards,

    Tilen

    Tesla DeLorean
    Guru
    June 20, 2018
    Posted on June 20, 2018 at 14:20

    Here we use the DWT Cycle Count to benchmark code, it's a 32-bit counter, so you'll want to contain test within wrapping limits. I tend to add a secondary measurement of milliseconds around that to provide a sanity check

    volatile unsigned int *DWT_CYCCNT   = (volatile unsigned int *)0xE0001004; //address of the register

    volatile unsigned int *DWT_CONTROL  = (volatile unsigned int *)0xE0001000; //address of the register

    volatile unsigned int *DWT_LAR      = (volatile unsigned int *)0xE0001FB0; //address of the register

    volatile unsigned int *SCB_DEMCR    = (volatile unsigned int *)0xE000EDFC; //address of the register

    ...

      *DWT_LAR = 0xC5ACCE55; // unlock (CM7)

      *SCB_DEMCR |= 0x01000000;

      *DWT_CYCCNT = 0; // reset the counter

      *DWT_CONTROL |= 1 ; // enable the counter

    ...

        x = *DWT_CYCCNT;

    ... Code Under Test

            y = *DWT_CYCCNT;

            x = (y - x); // Elapsed clock ticks, at SystemCoreClock

    Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
    pjn
    pjnAuthor
    Associate
    June 21, 2018
    Posted on June 21, 2018 at 08:44

    I implemented this, just to confirm-

     if my system core clock is operating at 216MHz, and my elapsed clock ticks are 642, then the execution time will be 3.1us? right?

    henry.dick
    Associate II
    June 21, 2018
    Posted on June 21, 2018 at 09:23

    '

    I implemented this,'

    don't implement that: CMSIS has the standard / platform independent definitions for DWT and the struct, DWT_Type. So the counter would be simply DWT->CYCCNT.

    alternatively, you can use this set of coretick.h/.c files here: 

    https://github.com/dannyf00/My-MCU-Libraries---2nd-try/tree/master/STM32F4

     . It works for all ARM chips that support DWT.

    The timing would be done by coreticks(), like this:

      t1=coreticks(); //time stamp t1

      //run user code to be benchmarked

      t1=coreticks() - t1; //measure the time elapsed

    if you replace coreticks() with other ticking functions, like systick or a timer (any timer), the code would work as well.

    henry.dick
    Associate II
    June 20, 2018
    Posted on June 20, 2018 at 14:52

    The simplest would be to flip a pin and observe it on a scope.

    Or use one of the many counters / timers.

    Tesla DeLorean
    Guru
    June 20, 2018
    Posted on June 20, 2018 at 14:53

    >>The simplest would be to flip a pin and observe it on a scope.

    OP specifically said he didn't want to do that. Wants to self quantify speed/elapsed time.

    Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..