Timers

A timer provides the ability to execute an application-defined function upon expiration of a specified time interval. A timer can be started, stopped, reset, or restarted at any time from any code context, including from an interrupt service routine (ISR).

A timer has the resolution of the kernel tick rate. When specifying a time interval, the accuracy is +1/-0 of a system tick as the tick ISR is not synchronized with the starting time of a timer.

The graph below illustrates the remaining time before expiration for a timer as a function of run-time. The graph shows how the different API calls affect the timer's remaining time.

Basic Usage

A timer can be used to repeatedly call an application-defined function. The code snippet below creates a timer that expires every 100 kernel ticks (milliseconds by default). The timer is created with the options to have the timer started and to have the timer automatically reset upon each expiration which causes repeated calls to the timer callback function.

#include "Kernel/kernel_timer.h"
#include <assert.h>

static TIMER timer1;                                    /* Allocate a timer */

void APP_Example(void)
{
    STATUS status;


    status = TIMER_Create(&timer1,                      /* Create a timer */
                          "Timer1",                     /* A name for the timer (debugging purposes) */
                          APP_TimerCallback,            /* A function to be called when the timer expires */
                          NULL,                         /* An optional argument (not used here) */
                          100,                          /* Interval, in kernel ticks */
                          OPT_START | OPT_AUTORESET);   /* Start and configure as a repeating timer */

    assert(status == SUCCESS);
}

void APP_TimerCallback(TIMER* timer, void* arg)
{
    /* Called repeatedly every 100 kernel ticks (milliseconds by default) */
}

API Reference

STATUS TIMER_Create(TIMER* timer, const CHAR* name, TIMERCALLBACK callback, void* arg, UINT32 interval, UINT32 opt)
Creates and initializes a timer.
PARAMETERS
timer A pointer to a caller allocated timer object to be initialized.
name A pointer to a NULL terminated string that represents a name for the timer.
callback A function that will receive a callback when the timer has expired.
arg (optional) A user defined argument object that is passed as the callback function argument. Can be NULL.
interval The amount of time, in kernel ticks, between the timer's expiration events.
opt Additional options to be applied to the timer.
OPT_NONE No options. Create a stopped timer in one-shot mode.
OPT_START Use to have the timer started.
OPT_AUTORESET Use to have the timer automatically reset after each expiration.
RETURNS
SUCCESS The timer has been created and initialized.
ERR_NULLREFERENCE The argument 'timer' or 'callback' was found to be NULL.
ERR_INVALIDCONTEXT The operation is not supported from the context of an interrupt service routine (ISR).
ERR_ALREADYINITIALIZED The specified timer has already been created and initialized.
STATUS TIMER_Destroy(TIMER* timer)
Destroys and removes a timer from the kernel.
PARAMETERS
timer A pointer to the timer to be destroyed and removed.
RETURNS
SUCCESS The timer has been destroyed and removed from the kernel.
ERR_NULLREFERENCE The argument 'timer' was found to be NULL.
ERR_INVALIDCONTEXT The operation is not supported from the context of an interrupt service routine (ISR).
ERR_NOTINITIALIZED The specified timer has not been created and initialized.
ERR_ACCESSDENIED The specified timer is currently executing it's callback function.
STATUS TIMER_SetInterval(TIMER* timer, UINT32 interval)
Sets a new timing interval, in kernel ticks, for a timer.
PARAMETERS
timer A pointer to the timer to receive the new interval value.
interval The new interval value for the timer. Must be greater than zero.
RETURNS
SUCCESS The timer has been updated with the new interval value.
ERR_NULLREFERENCE The argument 'timer' was found to be NULL.
ERR_INVALIDARGUMENT The argument 'interval' was invalid. Must be greater than zero.
ERR_NOTINITIALIZED The specified timer has not been created and initialized.
STATUS TIMER_Start(TIMER* timer)
Starts a timer.
PARAMETERS
timer A pointer to the timer to be started.
RETURNS
SUCCESS The timer has been started.
ERR_NULLREFERENCE The argument 'timer' was found to be NULL.
ERR_NOTINITIALIZED The specified timer has not been created and initialized.
REMARKS
If the timer is not expired, it will resume where it has last been stopped. Use TIMER_Restart() if starting the timer over with a fresh complete timing period is the desired operation.
STATUS TIMER_Stop(TIMER* timer)
Stops a timer.
PARAMETERS
timer A pointer to the timer to be stopped.
RETURNS
SUCCESS The timer has been stopped.
ERR_NULLREFERENCE The argument 'timer' was found to be NULL.
ERR_NOTINITIALIZED The specified timer has not been created and initialized.
REMARKS
The timer's remaining time interval is left unchanged.
STATUS TIMER_Reset(TIMER* timer)
Resets a timer by giving the timer a new full time interval.
PARAMETERS
timer A pointer to the timer to be reset.
RETURNS
SUCCESS The timer has been reset.
ERR_NULLREFERENCE The argument 'timer' was found to be NULL.
ERR_NOTINITIALIZED The specified timer has not been created and initialized.
REMARKS
This will leave the timer in it's current state.
STATUS TIMER_Restart(TIMER* timer)
Resets and starts a timer.
PARAMETERS
timer A pointer to the timer to be restarted.
RETURNS
SUCCESS The timer has been restarted.
ERR_NULLREFERENCE The argument 'timer' was found to be NULL.
ERR_NOTINITIALIZED The specified timer has not been created and initialized.
BOOLEAN TIMER_IsRunning(TIMER* timer)
Returns an indication of whether a timer is currently running.
PARAMETERS
timer A pointer to the timer to be checked for running status.
RETURNS
TRUE if the timer is currently running; otherwise FALSE.