How long the piece of code is executed?

Hello,
Does anyone know how long piece of code is executed? Today I will teach you how to check it.
To measure time there are needed two timestamps t1 and t2. Timestamp t1 describes time when the measurment is started, t2 when the measurment is finished. For this puprpose one gpio will be used. Speaking more precisely:
t1 is when the rising edge occured
t2 is when the falling edged occured
Time of execution doSomething2 function equal is t2 - t1. It is showed at the picture below:


Time will be measured by logic analyzer like this:

This is one of the cheapest logic analyzer, but for this purpose is excellent. The oscilloscope can be used also.

What will happen if the interrupt occurred during measurement?
An interrupt breaks the execution of instructions and diverts its execution to interrupt service routine(ISR). When code is done CPU go back into the normal execution of instruction, as showed below:

Almost every embedded system use interrupts. The interrupt service routine should only execute the critical code. The code should be very simple and fast(without any delays). That is why the time spent in ISR can be omitted. Let's assume the ISR doesn't cause any deviation.

Please be aware this method doesn't work correctly in concurrent programming for the similar reason as interrupts. In concurrent programming it is more complicated.

In next posts i will show you usage of logic analyzer in real cases.

See you later :)

Comments

Popular posts from this blog

Semihosting

Two ways to compare a variable in a condition statement