This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Compiler/TM4C1231H6PM: Measuring the time it takes for a function to execute

Part Number: TM4C1231H6PM

Tool/software: TI C/C++ Compiler

Hello this is my code:

int main ()

{

while (1)

get_inputs() ;

some_elaborate_function(); 

}

I want to know how long it takes for "some_elaborate_function()" to execute on my TI launchpad.

The only method I know is to use one of the microcontroller's timer peripherals to start counting one line before the function is called and read the count value when the function returns.

Is there a better / simpler way ?   

  • Hi,
    Your method using the timer will work and many people use this method. You can also toggle a GPIO pin before calling some_elaborate_function and clear pin after the function. In the scope you can measure the time. If you want to base on the delta time to make a decision in your code flow then you need to use the timer.
  • Can you post an example code of the timer method ?
  • This below code is from the TivaWare peripheral driver library user's guide. You can find the same in section 28.3. You just need to add your function before the SysTickValueGet().

    uint32_t ui32Value;
    //
    // Configure and enable the SysTick counter.
    //
    SysTickPeriodSet(1000);
    SysTickEnable();
    //
    // Delay for some time...
    //
    //
    // Read the current SysTick value.
    //
    ui32Value = SysTickValueGet();
    

  • Seems like this code uses the internal ARM CPU timer - and not one of the peripheral timers - am I correct ?
  • Yes, this is using the system tick timer of the ARM CPU. You can use the General Timer if you want but I think the Systick is even easier to setup for what you want to accomplish. 

  • Suppose I call the following 2 functions:
    SysTickPeriodSet(1000);
    SysTickEnable();
    And after (for example) 400 cycles I call SysTickValueGet() - I suppose the value I'll get will be 600 which is correct... But the timer will continue to decrement which I don't want.
    How do I make it stop and return it back to 1000 for another use ?
  • If you want to stop the counter you can call SysTickDisable. However, this only means the current counter is stopped. In your example, the counter will stay at 600. As soon as you re-enable it will continue to count down from 600 to 0 before it reload the 1000. If you want to reload immediately you need to write to the NVIC_ST_CURRENT register directly with your reload value.

    28.2.2.2 SysTickEnable
    Enables the SysTick counter.
    Prototype:
    void
    SysTickEnable(void)
    Description:
    This function starts the SysTick counter. If an interrupt handler has been registered, it is called
    when the SysTick counter rolls over.
    Note:
    Calling this function causes the SysTick counter to (re)commence counting from its current
    value. The counter is not automatically reloaded with the period as specified in a previous call
    to SysTickPeriodSet(). If an immediate reload is required, the NVIC_ST_CURRENT register
    must be written to force the reload. Any write to this register clears the SysTick counter to 0
    and causes a reload with the supplied period on the next clock.

  • Just to clarify again. Writing to the NVIC_ST_CURRENT will force a reload of the counter with the value stored in the Reload register. You can actually write an arbitrary value to NVIC_ST_CURRENT to force a reload. I will suggest you just write 0.
  • "If you want to reload immediately you need to write to the NVIC_ST_CURRENT register directly with your reload value."
    Can you post the syntax of how to reset the timer with the initial reload value ?
  • I already mentioned in my last reply you can write an arbitrary value but I will suggest you write zero. See below.

    HWREG(NVIC_ST_CURRENT) = 0;
  • So the code should be as follows:

    int main
    
    {        
    
       uint32_t ui32Value ;
    
       while ( 1 )
    
       SysTickPeriodSet( 1000 ) ;
    
       SysTickEnable ( ) ;
    
       function_of_interest ( ) ; // This is the function we want to benchmark.
    
       ui32Value = SysTickValueGet ( ) ;
    
       SysTickDisable ( ) ;
    
       HWREG ( NVIC_ST_CURRENT ) = 0 ; // Reset the counter to its default which is = 1000.
    
       some_other_function ( ) ; // Other application code we're not interested in bench-marking.
    
    }

  • yes, that is correct.
  • When trying to reset the counter - I get the following error:
    #20 identifier "NVIC_ST_CURRENT" is undefined

    These are my includes - is something missing ?
    #include <stdbool.h>
    #include <stdint.h>
    #include "driverlib/debug.h"
    #include "driverlib/gpio.h"
    #include "driverlib/pin_map.h"
    #include "driverlib/pwm.h"
    #include "driverlib/rom.h"
    #include "driverlib/sysctl.h"
    #include "inc/hw_gpio.h"
    #include "inc/hw_memmap.h"
    #include "inc/hw_types.h"
  • You need below too.
    #include "inc/hw_nvic.h"