You are on page 1of 10

SysTick Interrupt Example

Basic timer included in all


Cortex-M4 devices.

Interrupt Summary

Determine the name of the interrupt


handler (from vector table) & interrupt
index (from derivative.h)
Create interrupt handler function
Create interrupt setup function

11/09/2015

EEE20003 Embedded Microcontrollers

Interrupt handler

Use whatever name is given in the


vector table.
Should clear the interrupt flag (if it
exists) check hardware details.
Carry out whatever function is needed.
May need to do some setup for the
next interrupt e.g. schedule timer.

11/09/2015

EEE20003 Embedded Microcontrollers

Interrupt Setup

Initialise hardware

Do whatever setup is required e.g. set tick


rate etc.
Enable interrupt signal (unmask it) in
hardware

Enable interrupt signal (unmask it) in


NVIC

NVIC_EnableIrq(int interruptIndex)

11/09/2015

EEE20003 Embedded Microcontrollers

IRQ Handler or ISR

The interrupt handler resembles a subroutine


or C function.
Its address must be placed in the appropriate
entry of the vector table so that it may be
called when triggered.
The Cortex-M family uses a interrupt
technique that allows the direct use of C
functions as interrupt handlers.
The correct registers are saved and restored
by hardware (matches ARM-ABI used by C).
11/09/2015

EEE20003 Embedded Microcontrollers

SysTick Timer

This is a basic timer that is included in


all Cortex-M3 & M4 devices.
Real-time Operating Systems (RTOSs)
require at least a basic timer that is
able to generate regular interrupts.
Inclusion in the processor core allows
the creation of chip independent RTOS.
A good (simple) example of a interrupt
source.

11/09/2015

EEE20003 Embedded Microcontrollers

Hardware
SysTick->LOAD
Reference
clock

Reload register

CPU

SysTick->VAL
0
1

24-bit down counter

Core clock

Interrupt Request

ENABLE_MASK

SysTick->CTRL
CLKSOURCE_MASK

Control

TICKINT_MASK

Timer automatically re-loads


when it reaches zero.
11/09/2015

EEE20003 Embedded Microcontrollers

Software Routine
uint32_t SysTick_Config(uint32_t ticks) {
if ((ticks - 1) > SysTick_LOAD_RELOAD_Msk) {
/* Reload value impossible */
return (1);
}
/* Set reload register */
SysTick->LOAD = ticks - 1;

Code to

Set period (interrupt rate)


Enable interrupts
Enable timer

/* Set Priority for Systick Interrupt */


NVIC_SetPriority (SysTick_IRQn, (1<<__NVIC_PRIO_BITS) - 1);
/* Load the SysTick Counter Value */
SysTick->VAL
= 0;

/* Configure Systick */
SysTick->CTRL =
SysTick_CTRL_CLKSOURCE_Msk |
SysTick_CTRL_TICKINT_Msk
|
SysTick_CTRL_ENABLE_Msk;

// Use system core clock


// Enable interrupts
// Enable timer

/* Function successful */
return (0);}
11/09/2015

EEE20003 Embedded Microcontrollers

Vector Table

Weakly assigns to the


default handler
Can be overridden.

...
void SysTick_Handler(void) WEAK_DEFAULT_HANDLER;
...
VectorTable const __vector_table = {
/* omitted rows */
SVC_Handler,
/* System Service Call via SVC instruction */
Vector table entry for
DebugMon_Handler, /* Debug Monitor*/
Timer
0,
/* Reserved SysTick
*/
PendSV_Handler,
/* Pendable request for system service */
SysTick_Handler,
/* System Tick Timer */
/* External Interrupts */
DMA0_IRQHandler,
/* DMA channel 0 interrupt
DMA1_IRQHandler,
/* DMA channel 1 interrupt
/* omitted rows */
};

*/
*/

The name of the actual interrupt handler function


must agree exactly with the vector table entry
It must also have "C" linkage.
11/09/2015

EEE20003 Embedded Microcontrollers

SysTick Handler

Code to

Handle Tick

/*
* This function is used for the
* System Timer interrupt handler.
*/
void SysTick_Handler(void) {
...
}
Name agrees with
Vector Table entry

11/09/2015

EEE20003 Embedded Microcontrollers

10

You might also like