STM32 Study Notes 3——Systick

Publisher:陈晨5566Latest update time:2018-07-17 Source: eefocusKeywords:STM32  Systick Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

For STM32, there is a tool that is often used but rarely mentioned in the data sheet, that is Systick. This tool is common to all microcontrollers with the cortex-M0 core. It is a system timer, and its main purpose is to provide a 100Hz (10ms) timing beat for the embedded operating system. Of course, it can also be used for other purposes such as ordinary timing. Next, let's start to understand this timer.

register

The system timer includes four registers, SYST_CSR, SYST_RVR, SYST_CVR, SYST_CALIB. The definitions are as follows: 
Write the picture description here
For these registers, we can translate:

  • SYST_CSR register, system timer control and status register

  • SYST_RVR register, system timer reload value register

  • SYST_CVR register, system timer current value register

  • SYST_CALIB register, system timer calibration register 
    This is very clear, let's look at these registers one by one. 
    First is the SYST_CSR register: 
    Write the picture description here
    The CSR register uses 4 bits, bit0 is used to start the timer, bit1 is used to generate an interrupt, bit2 is used to select whether the timer clock source is equal to the main clock or equal to half of the main clock, and bit16 is the status of the timer.

The SYST_RVR register 
Write the picture description here
RELOAD value can be any value in the range of 0x00000001-0x00FFFFFF. You can set the RELOAD value to 0, which will not have any effect, because the SysTick exception request and COUNTFLAG are both activated when the count value changes from 1 to 0. If you want to generate a multi-trigger timer with a period of N processor clock cycles, you can set the RELOAD value to N-1. For example, if you require a SysTick interrupt to be triggered every 100 clock pulses, RELOAD is set to 99.

SYST_CVR register 
Write the picture description here
The CVR register uses bits 0 to 23, which is a 24-bit number. This is a status register. When the timer starts running, this value is constantly changing. After getting the initial value from the RVR register, it counts down to 0. 
SYST_CALIB register 
The calibration register SYST_CALIB does not need to be considered by us, as it is configured before leaving the factory.

function

The system's own Systick function is provided by CMSIS (for more information about what CMSIS is, go to Baidu and search it), and is located in the core_cm0.h file. In the 656th line, there is the following function

__STATIC_INLINE uint32_t SysTick_Config(uint32_t ticks)

{

  if ((ticks - 1) > SysTick_LOAD_RELOAD_Msk)  return (1);      /* Reload value impossible */


  SysTick->LOAD  = ticks - 1;                                  /* set reload register */

  NVIC_SetPriority (SysTick_IRQn, (1<<__NVIC_PRIO_BITS) - 1);  /* set Priority for Systick Interrupt */

  SysTick->VAL   = 0;                                          /* Load the SysTick Counter Value */

  SysTick->CTRL  = SysTick_CTRL_CLKSOURCE_Msk |

                   SysTick_CTRL_TICKINT_Msk   |

                   SysTick_CTRL_ENABLE_Msk;                    /* Enable SysTick IRQ and SysTick Timer */

  return (0);                                                  /* Function successful */

}


From the statement “SysTick->LOAD = ticks – 1;” inside the function, we know that ticks is the LOAD value, that is, the reload value, which represents the count of two interrupts. 

For example, to generate a 10ms interrupt, you can call the function in the program as follows: Systick_Config(SystemCoreClock/100); SystemCoreClock in the function parameter is the value of the current main frequency. If the current main frequency is 48MHz, SystemCoreClock is 48 000 000, 48 000 000/100=480 000. After we bring in the parameters, LOAD=479 999, that is, after the timer starts running, the timer value will decrease from 479 999 to 0, enter the interrupt function, and then decrease from 479 999 to 0 again, and so on. As for why it takes 10ms to decrease from 479 999 to 0? Knowing how long it takes to decrease by one value, then how long it takes to decrease by 480 000, you will know. To know how long it takes to decrease by one value, you need to know the clock of the current timer. From the register CSR, we know that there are two types of timer clocks, one is equal to the main frequency, and the other is equal to half of the main frequency, which is determined by bit 2 in the CSR register. 

In the function, bits 0, 1 and 2 of the control register are all set to 1. By comparing the previous register definition, the clock is set to be equal to the main frequency, the system timer interrupt is turned on, and the timer is allowed to run. 

Now that we know the clock, we know the time required for the timer to decrement a value, which is 1/SystemCoreClock seconds. Converted into milliseconds, it is (1/SystemCoreClock)*1000=1000/SystemCoreClock milliseconds, which means that it takes 1000/SystemCoreClock milliseconds to decrement a value. So if you want to make the timing 10ms, that is, 10/(1000/SystemCoreClock)=SystemCoreClock/100, so our settings are correct. Similarly, you can calculate how long the timing needs to be, and bring it in with a parameter. It should be noted that the LOAD value is a 24-bit number, and the number brought in should not exceed the maximum value of a 24-bit number. Another thing to note is that the minimum LOAD value is 255. When you bring in a value less than 255 for the LOAD value, LOAD will automatically become 255. Since the timer SYST_RVR register is 24 bits, the maximum value is hexadecimal FFFFFF, which is decimal 16777215, so the maximum data that can be stored is 16777215+1=16777216;


Timer delay


Using the functions in CMSIS we can write the following functions:


__IO uint32_t msTick;

void SysTick_Handler(void) // interrupt function  

{

    msTick++;

}


void delay_ms(IO uint32_t ms)

{

  uint32_t endTime=msTicks+ms;

  SysTick_Config((SystemCoreClock/1000)*ms);  

  while(msTicks

}


It can delay the time with an accuracy of 1ms. In addition, SystemCoreClock/1000 can be modified to delay the time with different time.


Keywords:STM32  Systick Reference address:STM32 Study Notes 3——Systick

Previous article:STM32 SysTick (system timer)
Next article:STM32 tick timer (Systick) detailed analysis

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号