1.5.27 System timer (SysTick)

The ARM Cortex CPU includes an optional system timer (SysTick) that provides a simple, 24-bit clear-on-write, decrementing, wrap-on-zero counter with a flexible control mechanism.

When enabled, the 24-bit timer counter counts down from the value in VAL. When the counter reaches zero, it reloads the value in LOAD on the next clock edge. It then decrements on subsequent clocks. This reloading when the counter reaches zero is called wrapping.

The timer interrupt is activated on the transition from 1 to 0, therefore it activates every n+1 clock ticks. If a period of 100 is required 99 should be written to the SysTick Reload Value register.

The SysTick can be polled by software or can be configured to generate an interrupt.

Using The Library

When the timer is enabled, it decrements by one on every rising edge of the input clock, and generates an interrupt when the timer value transitions to zero (Timer expiry). This is used to generate periodic interrupt generation.

The SysTick can be polled by software or can be configured to generate an interrupt.

  • With polling, the application will need to continuously poll to check if the timer has expired.

  • With interrupt, the registered callback function will be called when the timer expires. This means the application do not have to poll continuously.

Callback method

This example demonstrates how to use SysTick to generate periodic callback.

/* This function is called after Timer expires */
void SYSTICK_EventHandler(uintptr_t context)
{
    /* Toggle LED */
    LED_Toggle();
}

int main(void)
{
    /* Initialize all modules */
    SYS_Initialize ( NULL );

    /* Register Callback */
    SYSTICK_TimerCallbackSet(SYSTICK_EventHandler, (uintptr_t) NULL);

    /* Start the Timer */
    SYSTICK_TimerStart();
}

Library Interface

System timer peripheral library provides the following interfaces:

Functions

Name Description
SYSTICK_TimerInitialize Initializes SysTick
SYSTICK_TimerRestart Restarts SysTick
SYSTICK_TimerStart Starts System Timer
SYSTICK_TimerStop Stops System Timer
SYSTICK_TimerPeriodSet Set the SysTick Load Value
SYSTICK_TimerPeriodGet Get the SysTick Load Value
SYSTICK_TimerCounterGet Get the SysTick current Value
SYSTICK_TimerFrequencyGet Get the SysTick frequency Value
SYSTICK_TimerPeriodHasExpired Returns the current status of the systick
SYSTICK_DelayMs Blocking function to generate delay in milliseconds
SYSTICK_DelayUs Blocking function to generate delay in micro seconds
SYSTICK_TimerCallbackSet Sets the pointer to the function (and it's context) to be called when the given systick reaches 0

Data types and constants

Name Type Description
SYSTICK_CALLBACK Typedef Defines the data type and function signature for the SysTick callback function