Measuring funtion usage on microcontroller - Page 1
Forum section: electronics / beginners

author: Karamel
author_rank: Regular Contributor
timestamp: September 21, 2016, 12:53:57 am
content: 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: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,Karamelp.s: we will work as if function takes 1msn and delay 100 msn we will say that mcu usage %0.99

author: ataradov
author_rank: Super Contributor
timestamp: September 21, 2016, 01:36:58 am
content: 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: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.

author: ataradov
author_rank: Super Contributor
timestamp: September 21, 2016, 01:40:58 am
content: Quote from: blueskull on September 21, 2016, 01:39:50 amBut it will take way more effort to rig that for a singe function measurement.

author: rrinker
author_rank: Super Contributor
timestamp: September 21, 2016, 01:48:44 am
content: Quote from: wilfred on September 21, 2016, 01:15:39 amBefore 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.

author: tszaboo
author_rank: Super Contributor
timestamp: September 21, 2016, 10:40:56 am
content: 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.

author: Kalvin
author_rank: Super Contributor
timestamp: September 21, 2016, 02:36:16 pm
content: 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.

author: Jeroen3
author_rank: Super Contributor
timestamp: September 21, 2016, 02:43:56 pm
content: 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 inanyinterrupt) 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.