ARM Cortex-M
System Timer (SysTick)
Embedded Systems with ARM Cortex-M
Microcontrollers in Assembly Language and C
Dr. Yifeng Zhu
Visit book website: web.eece.maine.edu/~zhu/book/
1
System Timer (SysTick)
Generate SysTick interrupts at a fixed time interval
SysTick
interrupts
time
Fixed time interval
Example Usages:
Measuring time elapsed, such as time delay function
Executing tasks periodically, such as periodic polling, and OS CPU scheduling
2
System Timer (SysTick)
System timer is a standard
hardware component built
into ARM Cortex-M. IRQs
Peripherals
This hardware periodically
forces the processor to Processor
NVIC
execute the following ISR: Core
IRQs
I/O pins System
Exceptions
void SysTick_Handler(void){
...
} SysTick IRQ
System Timer
ARM Cortex-M
Microcontroller
3
Diagram of System Timer (SysTick)
Reload Value
copy
Yes
Is it
Counter
zero?
24-bit down counter
4
System Timer
SysTick
Counter SysTick_LOAD = 6
3 Clock
Period SysTick SysTick SysTick
2 Interrupt Interrupt Interrupt
1
0
t
SysTick Interrupt Time Period = (SysTick_LOAD + 1) × Clock Period = 7 × Clock Period
5
Diagram of System Timer (SysTick)
Reload Value
copy
Yes
Is it
Counter Set COUNTFLAG to 1
zero?
24-bit down counter
31 17 16 15 2 1 0
SysTick control and status
Reserved Reserved
register (SysTick_CTRL)
COUNTFLAG Clock Source
TICKINT
ENABLE
6
Diagram of System Timer (SysTick)
Reload Value
External
clock copy
AHB /8 0 Yes
AND
Is it
Clock 1 Counter Set COUNTFLAG to 1
zero?
Processor
clock 24-bit down counter
Clock Source
ENABLE
31 17 16 15 2 1 0
SysTick control and status
Reserved Reserved
register (SysTick_CTRL)
TICKINT
COUNTFLAG AND SysTick Interrupt
7
Registers of System Timer
SysTick control and status register (SysTick_CTRL) SysTick reload value register (SysTick_LOAD)
31 17 16 15 2 1 0 31 24 23 0
Reserved Reserved RELOAD
COUNTFLAG Clock Source
TICKINT
ENABLE
SysTick calibration register (SysTick_CALIB)
SysTick current value register (SysTick_VAL)
31 24 23 0 31 30 23 0
Reserved TENMS
CURRENT
SKEW
NOREF
8
Registers of System Timer
SysTick reload value register (SysTick_LOAD)
31 24 23 0
RELOAD
24 bits, maximum value 0x00FF.FFFF (16,777,215)
Counter counts down from RELOAD value to 0.
Writing RELOAD to 0 disables SysTick, independently of TICKINT
Time interval between two SysTick interrupts
Interval = (RELOAD + 1) × Source_Clock_Period
If 100 clock periods between two SysTick interrupts
RELOAD = 99
9
Registers of System Timer
SysTick current value register (SysTick_VAL)
31 24 23 0
CURRENT
Reading it returns the current value of the counter
When it transits from 1 to 0, it generates an interrupt
Writing to SysTick_VAL clears the counter and COUNTFLAG to zero
Cause the counter to reload on the next timer clock
But, does not trigger an SysTick interrupt
It has random value on reset.
Always clear it before enabling the timer
10
Registers of System Timer
SysTick calibration register (SysTick_CALIB)
31 30 23 0
Reserved TENMS
SKEW
NOREF
A read-only register
TENMS (10 ms) holds the reload value, which will yield a 10ms period
May not be implemented or may be defined differently by chip designers
11
Example Code
void SysTick_Initialize (uint32_t ticks) {
SysTick->CTRL = 0; // Disable SysTick
SysTick->LOAD = ticks - 1; // Set reload register
// Set interrupt priority of SysTick to least urgency (i.e., largest priority value)
NVIC_SetPriority (SysTick_IRQn, (1<<__NVIC_PRIO_BITS) - 1);
SysTick->VAL = 0; // Reset the SysTick counter value
// Select processor clock: 1 = processor clock; 0 = external clock
SysTick->CTRL |= SysTick_CTRL_CLKSOURCE;
// Enables SysTick interrupt, 1 = Enable, 0 = Disable
SysTick->CTRL |= SysTick_CTRL_TICKINT;
// Enable SysTick
SysTick->CTRL |= SysTick_CTRL_ENABLE;
}
12
Implementing Delay Function
volatile int32_t TimeDelay;
int main (void {
SysTick_Initialize(1000); // Interrupt period = 1000 cycles
Delay(100); // Delay 100 ticks
...
}
void SysTick_Handler (void) { // SysTick interrupt service routine
if (TimeDelay > 0) // Prevent it from being negative
TimeDelay--; // TimeDelay is a global volatile variable
}
void Delay (uint32_t nTime) {
// nTime: specifies the delay time length
TimeDelay = nTime; // TimeDelay must be declared as volatile
while(TimeDelay != 0); // Busy wait
}
13
Calculating Reload Value
RELOAD + 1 cycles
Suppose clock source = 80MHz
Goal: SysTick Interval = 10ms Source
Clock
What is RELOAD value?
SysTick
Clock
10 𝑚𝑠
Counter
Period
𝑅𝑒𝑙𝑜𝑎𝑑 = −1 Reload
𝐶𝑙𝑜𝑐𝑘 𝑃𝑒𝑟𝑖𝑜𝑑 Reload - 1
Reload - 2
= 10𝑚𝑠 × 𝐶𝑙𝑜𝑐𝑘 𝐹𝑟𝑒𝑞𝑢𝑒𝑛𝑐𝑦 − 1
1
= 10𝑚𝑠 × 80𝑀𝐻𝑧 − 1 0
t
RELOAD + 1 cycles
= 10 × 10−3 × 80 × 106 −1 SysTick Interrupts
= 800000 − 1
= 799999 10 ms
14