3

I am maintaining some code in C for STM8 using IAR Embedded. what is the way to measure execution time between one part of the code and another? (Take into account that if possible I don't want to stop the execution of the code (a la breakpoint) or write to the console (since I found that this affects heavily the timing of the program).

I ve found something like this Techniques for measuring the elapsed time

but this is usually for ARM processors so many of the methods don't apply to my setting. I am thinking something like Technique #3 might be applicable...

Concretely I am asking if I can do something like that technique

unsigned int cnt1 = 0;
unsigned int cnt2 = 0;

    cnt1 = TIM3->CNT;
    func();
    cnt2 = TIM3->CNT; 
    printf("cnt1:%u cnt2:%u diff:%u \n",cnt1,cnt2,cnt2-cnt1);

for this microcontroller

Any help greatly appreciated

Clifford
  • 88,407
  • 13
  • 85
  • 165
KansaiRobot
  • 7,564
  • 11
  • 71
  • 150
  • 1
    Debugging methods should be as unobtrusive as possible. Probably the best is to toggle a GPIO pin on function entry and exit and use an oscilloscope to measure toggle-to-toggle execution time. – Fiddling Bits Dec 05 '16 at 00:47
  • I don't understand what else info you need, actually the link you posted has everything. – Felipe Lavratti Dec 05 '16 at 00:48
  • By the way, STM8 is an ARM microcontroller. – Fiddling Bits Dec 05 '16 at 00:51
  • @FiddlingBits Yes, I ve consider doing that, flipping a pin and measuring through an oscilloscope but I am looking for another method. (Oh and by the way, I think STM32 is ARM, not sure about STM8 though) – KansaiRobot Dec 05 '16 at 01:02

2 Answers2

1

You cannot call printf each 100us in a 8 bit microprocessor, it has no throughput for that. Instead, increment status variables every time anything behaves unexpectedly.

unsigned int cnt1 = 0;
unsigned int cnt2 = 0;

cnt1 = TIM3->CNT;
func();
cnt2 = TIM3->CNT; 
if ((cnt2 - cnt1) > MAX_DURATION_ALLOWED)
    global_error_func_duration ++;

(Make sure TIM3 counts in microseconds)

CLK_PeripheralClockConfig (CLK_PERIPHERAL_TIMER3 , ENABLE);     
TIM3_DeInit();
TIM3_TimeBaseInit(/* Fill these parameters to get 1us counts at your CPU clock */);
TIM3_Cmd(ENABLE); 

Now, you can make a console function to print this variable, so from time to time you can check if even a single loop took more than 10ms.

Late in the development you will want to monitor these status variables to assess the system runtime integrity and take some action in case of misbehavior.

Felipe Lavratti
  • 2,887
  • 16
  • 34
0

There's plenty of solutions for that, but simple solution would be using hardware pin and toggling pin in places where you want to start/stop measuring time and using oscilloscope or some cheap logic analyser. Software as someone mentioned have some variable start and end assign current timer tick to them and in debug read them. You could aswell print them using i.e uart in runtime but this would aswell slow them down.

koper89
  • 645
  • 3
  • 9