2018-06-20 04:08 AM
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
‌null2018-06-20 04:14 AM
Hello,
if available, you may use DWT counter, or general purpose timer aswell.
Best regards,
Tilen
2018-06-20 05:20 AM
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 registervolatile unsigned int *DWT_LAR = (volatile unsigned int *)0xE0001FB0; //address of the registervolatile 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
2018-06-20 05:52 AM
The simplest would be to flip a pin and observe it on a scope.
Or use one of the many counters / timers.
2018-06-20 07:53 AM
>>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.
2018-06-21 01:44 AM
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?
2018-06-21 02:23 AM
'
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.
2018-06-21 09:49 AM
Math here suggests 2.97us