SYSTICK - Introduction
SYSTICK - Counting
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;
}