STM32 knowledge: Systick timer

1. Introduction to Systick timer

     The Systick timer is also called the system tick clock. It is located in the Cortex-Mx core and is "built-in". Therefore, all MCUs with the Cortex-Mx core have a Systick timer.

     Systick is a 24-bit down counter (^{^{^{^{^{^{_{}}}}}}}The maximum set reload value is2^{24}=2^{4}M=16M=16777215). Every time 1 system clock cycle passes, The count value is reduced by 1. The "system clock" here is the Systick clock, and its frequency is up to HCLK/8, where HCLK is the AHB bus clock and also the CUP system clock. For stm32f103, the maximum is 72MHz, and the corresponding Systick clock is up to 9MHz. If the Systick reload value is set to 9000 and the clock is set to 9MHz, Systick will generate an interrupt of 1ms (the maximum delay can be set to 16M/9M=1.8s).

      Systick timer function:

       1) Can produce precise delay;

       2) A separate heartbeat (clock) beat can be provided to the operating system.

2. Systick register

   (1) CTRL: Systick control register 

            Bit 16: COUNTFLAG. If SysTick has counted to 0 after the last time this register was read, this bit is 1. This bit is automatically cleared if read.

            Bit 2: CLKSOURCE, clock source selection bit. 0: AHB/8; 1: AHB.

            Bit 1: TICKINT: SysTick interrupt response bit.

                        0: The counter is reduced to 0 without interruption; 1: The counter is reduced to 0 to generate interruption

            ​​​​​Bit 0: ENABLE, counter enable bit, 0: off; 1: on. When ENABLE is set to 1, the counter reloads the register and then counts down. When it reaches 0, it sets COUNTFLAG to 1 and then chooses whether to generate an interrupt based on the value of TICKINT. It will then reload and calculate again and start counting.​ 

            [Original link: https://blog.csdn.net/thebestleo/article/details/109675976]

   (2) LOAD: Systick reload register, 24 bits

   (3) VAL: Systick current value register, 24 bits

   (4) CALIB: Systick calibration value register (not commonly used, used for calibration)

3. ST library functions use Systick

      1. Call SysTick_CounterCmd(SysTick_Counter_Disable); turn off the SysTick counter

      2. Call SysTick_ITConfig(DISABLE); turn off SysTick interrupt

      3. Call SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK_DIV8); set the SysTick clock source, 72M/8=9MHz

      4. Call SysTick_SetReload(9000*N); set the reload value

      5. Call SysTick_ITConfig(ENABLE); turn on SysTick interrupt

      ​ 6. Call SysTick_CounterCmd(SysTick_Counter_ENABLE); turn on the SysTick counter

    [PS: There are also: SysTick_GetCounter, to get the counter value; Systick_GetFlagStatus, whether the flag bit is set or not]

      It should be noted that because SysTick is in the kernel and is an "internal device", there is no need to turn on the peripheral clock through RCC.

Guess you like

Origin blog.csdn.net/chn_zx/article/details/131684781