Author Topic: Measuring funtion usage on microcontroller  (Read 2667 times)

0 Members and 1 Guest are viewing this topic.

Offline KaramelTopic starter

  • Regular Contributor
  • *
  • Posts: 178
  • Country: tr
Measuring funtion usage on microcontroller
« on: September 21, 2016, 12:53:57 am »
Hi everyone,

I am wondering, How can I do microcontroller usage monitor for any mcu? (could be stm32 arm microcontrollers)

Let's write some codes?


Code: [Select]
multiplication(int x, int y){
       
       int result=0;
       
       while(y>0){
            result += x;
            y--;
       }
       return result;
}

main()
{
       int a = 1;
       int b = 1;

       while(1)
       {
            multiplication(a,b);
            Delay_ms(100);
       }
}

Yes, I have written manual multiplication for understanding mcu usage. If a and b little numbers like 1,10 etc. multiplication function returns quickly but if there are so much bigger then, function must take so much time. Well, I am intelligent with while(1) loop and I want to learn multiplication function how much does it use my microcontroller operation in percentage.

Best regards,
Karamel  ^-^

p.s: we will work as if function takes 1msn and delay 100 msn we will say that mcu usage %0.99 
« Last Edit: September 21, 2016, 01:03:56 am by Karamel »
 

Online ataradov

  • Super Contributor
  • ***
  • Posts: 11238
  • Country: us
    • Personal site
Re: Measuring funtion usage on microcontroller
« Reply #1 on: September 21, 2016, 01:36:58 am »
What kind of tools do you have? The most reliable way is to toggle a pin before and after function call. Then measure pulse width the scope.

Another method is to do something like:
Code: [Select]
  set_pin_high();
  for (int i = 0; i < 1000000; i++)
    function();
  set_pin_low();

Measure how long it takes with a stopwatch and divide that time by 1000000.
« Last Edit: September 21, 2016, 01:38:46 am by ataradov »
Alex
 

Online ataradov

  • Super Contributor
  • ***
  • Posts: 11238
  • Country: us
    • Personal site
Re: Measuring funtion usage on microcontroller
« Reply #2 on: September 21, 2016, 01:40:58 am »
Keil's RTOS has built in dash board (in debugging mode) that allows you to see each task's resource usage.
But it will take way more effort to rig that for a singe function measurement.
Alex
 

Offline rrinker

  • Super Contributor
  • ***
  • Posts: 2046
  • Country: us
Re: Measuring funtion usage on microcontroller
« Reply #3 on: September 21, 2016, 01:48:44 am »
I was reminded of this post by Jack Ganssel.
http://www.ganssle.com/video/episode12-measuring-idle-time.html

Not sure it will be useful for your needs, but it sounded so simple that I mentally filed it away.

 Before I even finished reading the OP, I thought of exactly the same thing. Strange how odd things like that stick with you. I guess because it's one of those elegant hacks that once you're told about it, it makes so much sense but you would never have thought to do that yourself.

 

Offline tszaboo

  • Super Contributor
  • ***
  • Posts: 7369
  • Country: nl
  • Current job: ATEX product design
Re: Measuring funtion usage on microcontroller
« Reply #4 on: September 21, 2016, 10:40:56 am »
Just use a real time tracer to find out. They are not cheap though. So debugging hiearchy:
-ICP or in circuit programmer, lets you program the chip.
-JTAG lets you do jtaggy things, like stop the execution, and then read registers, memory, and io pins. It is intrusive.
-Trace will spit some information into your computer, so you will have real time information, what happens on the chip. it is not intrusive, unless AFAIK some commands will send information to the PC deliberately.
-In circuit emulator will run the entire code on your pc, real time and you have all the information.

A good compromise is the pin flipping and reading it with a scope or logic analiser. Although pin flipping can be much slower than the other parts of your microcontroller.
 

Offline Kalvin

  • Super Contributor
  • ***
  • Posts: 2145
  • Country: fi
  • Embedded SW/HW.
Re: Measuring funtion usage on microcontroller
« Reply #5 on: September 21, 2016, 02:36:16 pm »
You can use one of the on-chip timers to count the instruction cycles. If your timers are only 16 bits, probably you can daisychain two timers as one 32-bit counter. If your algorithm takes less than 65000 cycles, you can get away with a single 16-bit timer. Configure the timer to increment at each instruction clock. At the start of the algorithm enable the timer (typically set one bit), and at the end of the algorithm stop the timer. Read the timer value, add it to the total cycle count, possibly reset the timer if you are running 16-bit timers. Pretty simple.
 

Offline Jeroen3

  • Super Contributor
  • ***
  • Posts: 4078
  • Country: nl
  • Embedded Engineer
    • jeroen3.nl
Re: Measuring funtion usage on microcontroller
« Reply #6 on: September 21, 2016, 02:43:56 pm »
If you don't own a Ulink Pro. There are methods.

On Cortex M0:
- Using a timer or GPIO pin.
On Cortex M3+:
- Using the DWT_CYCCNT.
- Using a timer or GPIO pin.

Simple way to do this is to use wfi().
1. Each time you exit wfi (that could be in any interrupt) you store the CYCCNT somewhere.
2. Then before you enter wfi, you look at CCYCNT to see how many cycles have passed.
3. Divide by core speed.

If you have an RTOS you just count the time in the idle loop.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf