Take the 2-minute tour ×
Electrical Engineering Stack Exchange is a question and answer site for electronics and electrical engineering professionals, students, and enthusiasts. It's 100% free, no registration required.

I would like to write a simple profiler for my STM32F100VE microcontroller. I have seen that I can include time.h and based on the c standard functions I can use it, but I was confused about _CLOCKS_PER_SEC_ 1000. I'm not sure if that approach will work or not.

How would one try measure the time a function took in micro-controller application?

share|improve this question
    
Do you need to know how much time the function needs to be executed, or do you need to know how much time the mcu spends in the function, ie how often it's called? –  Vladimir Cravero Apr 12 at 15:47
    
@VladimirCravero I want to know how much time the mcu spends in that function. Basically I'm running a game loop, and would like to know how much that function "ate" from that game loop which is sync to 30FPS. –  Ahmed Saleh Apr 12 at 15:59
3  
One method I've used for verification of design is to toggle a port pin, you can then look at it on an oscilloscope. Usually I know what it's supposed to be from simulation. –  Spehro Pefhany Apr 12 at 16:05
    
Spehro idea is great, I used to do that too. Just assert a pin when you enter the function and deassert it when you exit. You can check how many times it's called, how long does it take to return and so on. Keep in mind: is the function interruptible? –  Vladimir Cravero Apr 12 at 16:27
2  
Lacking an oscilloscope you could put and RC filter on the toggle pin and see what the (averaged) output is with a multimeter. If you know the loop frequency you can calculate back to the (average!) time spend in the routine. –  Wouter van Ooijen Apr 12 at 18:02
show 5 more comments

3 Answers 3

up vote 3 down vote accepted

The most basic maneuver for complicated functions is to utilize the onboard timers.

Depending on how you set them up, you can get them to count exactly the number of clock cycles it takes for a certain function to execute. Simply read the timer before the function call and then again after the function call.

You will have to look through the assembly code to know how much time to take off the timer for the read calls but if it is a lengthy function or if you don't mind being by a couple of clock cycles then it is ok.

The only error in the conversion from clock cycles back to time is the possible error in the system clock's oscillating frequency. Again, this will be negligible for functions which take significant time to execute.

It sounds like in your case you want something around 30Hz (30 FPS) which is a very long time compared time cycle of the processor clock.

share|improve this answer
    
Good answer, I just want to add that if all of the code runs from the same system clock then the exact clock frequency probably doesn't matter in this case. It's usually the number of clock cycles required to execute a function that is of most interest. –  Joe Hass Apr 12 at 17:59
    
@MathEE, can you explain the approach of starting the timer and stopping it ? I would start the timer to count, then call my function then stop it, the timer's value would be proportional the time spent in the function? but how would I convert the value from the timer to milliseconds that are spent ? –  Ahmed Saleh Apr 12 at 19:01
    
You can either start it and stop it or you can just keep one running then read off the values (permitted it doesn't overflow). The system clock has a frequency \$F_{CY}\$ and lets say you set your timer to increment at each system clock tick then the elapsed time is \$(\text{ change in timer})/F_{CY}\$. –  SomeEE Apr 12 at 21:45
add comment

If it is a small function you could just look at the disassembly and count the instructions.

share|improve this answer
    
Unfortunately, I do not have an ICD. –  Ahmed Saleh Apr 12 at 16:04
    
You can look at the generated assembler without an ICD. Which tools are you using? Also is it a small function? –  Will Apr 12 at 16:05
    
It's a triangle rasterization function, with calls to LCD framebuffer to write pixels there. –  Ahmed Saleh Apr 12 at 16:07
2  
@Will Your short answer has valuable insight, but it would work better as a comment. You have enough reputation to post comments. –  Nick Alexeev Apr 12 at 16:12
    
Some inexpensive hand-held meters can measure frequency and duty cycle. F/ex: TekPower TP4000ZC. (Note that one user claimed this one's impedance wasn't very high. I'm happy enough with mine...) –  JRobert Apr 12 at 16:56
add comment

Do you need to use that information in your software as well, or do you only need it for debugging/development purposes? If it's the second one (I assume it is, as your main goal is profiling), just toggling an output at the beginning and the end of the function, and measuring the time with an oscilloscope might be the quickest, easiest and most accurate measurement.

share|improve this answer
    
This is what I do all the time. Much simpler than setting up another timer. –  tcrosley Apr 13 at 8:05
    
While using a scope might be the quickest and easiest I don't think it is the most accurate. Using the on-chip timers allows you to exercise the function over a broad set of input conditions over a long period of time. You can then find the maximum execution time, in actual clock cycles. For code that has significant variability it would be hard to catch that on a scope. –  Joe Hass Apr 13 at 12:06
1  
@JoeHass : a lot of modern scopes have a feature called "persistence mode", where successive signals are superimposed. –  vsz Apr 13 at 17:21
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.