cancel
Showing results for 
Search instead for 
Did you mean: 

Execution time in STM32

pjn
Associate II
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
7 REPLIES 7
Tilen MAJERLE
ST Employee
Posted on June 20, 2018 at 13:14

Hello,

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

Best regards,

Tilen

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
Up vote any posts that you find helpful, it shows what's working..
henry.dick
Senior II
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.

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
Up vote any posts that you find helpful, it shows what's working..
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?

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.

Posted on June 21, 2018 at 16:49

Math here suggests 2.97us

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