SYSTICK
SYSTICK - Introduction
- Found in all ARM Cortex-Microcontrollers, regardless of silicon manufacturer.
Used for taking actions periodically. - Often used as time-base for real-time operating systems.
- The Systick is a 24-bit down counter driven by the processor clock.
SYSTICK - Counting
- Counts from initial value down to zero
- 24-bits imply maximum initial value of:
2²⁴ = 0xFFFFFF = 16,777,216 - Initial value can be set to a value between 0x000000 to 0xFFFFFF
SYSTICK - Registers
-
Systick Current Value Register (STCVR)
This register contains the current count value
-
Systick Control & Status Register (STCSR)
This register allows us to configure the systick clock source, enable/disable interrupts and enable/disable the systick counter
-
Systick Reload Value Register (STRVR)
This is where the initial count value is placed
SYSTICK - Count value computations
Compute the delay achieved by loading 10 in the Systick Reload Value Register (STRVR) given system clock = 16Mhz
Written as:
Systick->LOAD = 9
written in the CMSIS standard
we write 9 although we want 10 because the counter starts counting from 0
Solution
System clock = 16MHz = 16 000 000 cycles/second
If 1 second executes 16 000 000 cycles,
how many seconds execute 1 cycle?
⇒ 1 / 16 000 000 = 62.5ns = 62.5 x 10⁻⁹ s
Then 10 cycles ⇒ 10 x 62.5 x 10⁻⁹ s = 625 x 10⁻⁹ s = 625ns


#include "stm32f4xx.h"
#define SYSTICK_LOAD_VAL 16000
#define CTRL_ENABLE (1U<<0)
#define CTRL_CLKSR (1U<<2)
#define CTRL_COUNTFLAG (1U<<16)
void systickDelayMs(int delay)
{
/*Reload with number of clocks per millisecond*/
SysTick->LOAD = SYSTICK_LOAD_VAL;
/*Clear systick current value register */
SysTick->VAL = 0;
/*Enable systick and select internal clk src*/
SysTick->CTRL = CTRL_ENABLE | CTRL_CLKSR;
for(int i=0; i<delay; i++)
{
/*Wait until the COUNTFLAG is set*/
while((SysTick->CTRL & CTRL_COUNTFLAG) == 0){}
}
SysTick->CTRL = 0;
}
Systic Intr
// ...existing code...
#define CTRL_TICKINT (1U << 1)
#define ONE_SEC_LOAD 16000000
void systick_1hz_interrupt(void)
{
/*Reload with number of clocks per second*/
SysTick->LOAD = ONE_SEC_LOAD - 1;
/*Clear systick current value register */
SysTick->VAL = 0;
/*Enable systick and select internal clk src*/
SysTick->CTRL = CTRL_ENABLE | CTRL_CLKSRC;
/*Enable systick interrupt*/
SysTick->CTRL |= CTRL_TICKINT;
/***don't need NVIC_ENABLE()*/
}
void SysTick_Handler(void)
{
}