Embedded study notes - SysTick (system tick)

foreword

In the previous article, I introduced the clock system of STM32F407. After understanding the clock of the system, the most important thing is to get the operation of the timer. This article starts from the most basic timer, which is also a timer that comes with the kernel— —SysTick (system tick) for introduction. The purpose is to figure out what is system tick, what is the use of system tick, and how to use system tick.

SysTickOverview

The word SysTick has actually appeared before. When introducing interrupts, it is the following picture, and SysTick appears. Look at his position, in the shaded part of the picture, that is to say, SysTick is a part of the NVIC in the kernel; It is not an on-chip peripheral like USART and GPIO, but a peripheral in the core; there is an arrow pointing to the NVIC in the picture, indicating that it can generate interrupts like the EXTI and USART used before.
insert image description here

What is SysTick

As for the question of what it is, it is really difficult to express it, so let's let the official answer it.
insert image description here
After reading the above description, there will be a general concept. First, it is a programmable system timer. Second, it is used for delay and timing operations, and then it can also trigger interrupts. One thing needs to be corrected. It is said that it is a 32-bit auto-decrement counter, which is wrong. In STM32F407, it is a 24-bit auto-decrement counter.
It has been said here that the system tick is a timer, so what is the timer? To put it bluntly, the timer is a counter that increases or decreases according to the law of time. In STM32, this time law is the clock. For example, we assume that the system ticks The clock is 168MHZ; then the system ticks the timer and it will increase from 0 to 168 000 000 within one second; in the same way, it means that the counter counts to 168000 000 and it takes 1 second. As for decrement and increment, decrement means that the initial value of the counter is given by us, and then the counter starts to decrement from this value; while self-increment means that we give a value, and then the counter increments from 0 to this value number.

Well, after having a general image, let's analyze its structure and function in detail.

SysTick structure block diagram

Since the system tick is an internal timer, it cannot be found in the ST company's Chinese reference manual. The relevant description can only be found in the ARM authoritative guide. The specific location is in Chapter 9, Section 5 of the M3 and M4 authoritative guide. .
insert image description here
You can see the system block diagram by pulling down:
still follow the old routine, first add the part that can be omitted, here you can clearly see that the bottom red box has nothing to do with the above things, so it can be edited, its function is Calibration of SysTick, generally speaking, SysTick is the system clock used, if this is not accurate, then most likely this MCU will die soon, so this thing can be ignored directly.
insert image description here
Get rid of the ones you don’t need to read, and then introduce them module by module.

1. Clock selection

As shown in the figure below, the red box on the left represents the clock input selection part of the system tick; the green box is a two-to-one data selector, and the two inputs are the processor clock and the reference clock after rising edge detection; the execution selection It is "the second bit of the control and status register" below, and the specific selection process will be introduced in detail in the register section. Then the clock is given to the counter.
insert image description here
Since there are two input clocks, what exactly do these two clocks refer to?
One is the processor clock, which is what we call the main frequency, which corresponds to 168MHZ for STM32F407; so what is the other reference clock? In fact, this clock also appeared in yesterday's clock tree introduction. As shown in the figure below, the Cortex system timer in the orange box is the reference clock here. It can be found that it has passed through a frequency divider by 8, that is to say, the frequency of this clock should be 168/8=21Mhz .

insert image description here

2. Counter part

The counter is simplified as shown in the figure below. This is the most basic structure of a counter. First, there are three parts of input:
1. Clock reference: This clock directly determines how long the counter performs a count;
2. Reload value: The upper reload The value directly determines the maximum counting value of the counter;
3. Control part: The control part directly determines when the counter starts counting and when it stops counting. Bit 0 here is used to control whether the counter counts.
insert image description here
Then there is the output part, the output has only one direction, which is the position of 4, note the description: when the counter decreases from 1 to 0, it will trigger, and this trigger points to the "control and status register", which means that when counting When it is completed, there will be a corresponding bit in the "Control and Status Register", allowing us to judge whether the timing is complete.
Finally, the most important part is the 24-bit down counter in the orange box. Its function is to decrease the value by one every once in a while. Of course, the Mingzi here is called a down counter, so there must be a corresponding up counter and a center-aligned counter. This will be encountered in the basic general-purpose and advanced timers later, and we will talk about it when we encounter it.

3. Interrupt part

Then the last part of this figure is related to interrupts. There is an AND gate here. One of the inputs of the AND gate comes from the flag after the counter technology is completed, and the other comes from the first bit of the "Control and Status Register", which is the interrupt. Enable, indicating that in the process of using the interrupt, this bit needs to be enabled to enable the interrupt.

insert image description here

The maximum delay time for one count cycle (from reload value to 0)

After figuring out the above structure, you can calculate the longest time it takes for the counter to work for one cycle at two frequencies.
The maximum reload value: 2^24=16777216
system ticks has two clock sources:
core clock: the main frequency provides a clock
of 168MHZ the maximum delay time: 1S 16777216/168 000 000=0.09986S
0.09986s---->99.8 ms
External clock: 21MHZ provided by AHB line
Maximum delay time: 1S
16777216/21 000 000=0.7989 S
0.7989s-----》798.9ms

work process

According to the analysis of the block diagram, the initialization process of the system tick can be roughly summarized as follows:

{
    
    
	①选择时钟;
	②根据自己所需时间计算出重装载值;
	③使能计数器;
	④判断对应的标志位是否到了,到了说明计时到了,没到说明计时还没到
}

SysTick register

In fact, according to the block diagram, the registers have already been guessed. Let’s take a closer look. There are four registers for the system tick.
insert image description here

1. Control and status register SysTick->CTRL

insert image description here
Writing method: SysTick->CTRL
Function: Control the system tick timer and read the corresponding status
Bit 0: ENALEB
set to 1: Enable the counter to work repeatedly
Set to 0: Disable the counter

Bit 1: Interrupt enable bit count flag must be set to 1/interrupt flag
Set 1: Enable interrupt
Set 0: Disable interrupt

Bit 2: Select the clock source Default 1
Set 1: Select the core clock 168MHZ
Set 0: External reference clock 21MHZ

The 16th bit: the flag bit is read-only
1: the counter returns
to 0 when it reaches 0, and it is 0: it is cleared when
reading. The specific writing method when reading:

while(! (SysTick->CTRL & (1<<16)) );

2. Reload value register SysTick->LOAD

insert image description here
Writing: SysTick->LOAD
Function: Provide the maximum value of the counter
Usage: Directly write the maximum count value that needs to be written
Can not exceed the maximum reload value range (0-1667216)
SysTick->LOAD=arr-1;
this value is specific How much to write, the size should be calculated according to the demand

3. Current value register SysTick->VAL

Writing: SysTick->VAL
Function: Store the current value of the counter
Read this register: You can get the current value of the counter
Write this register: Any value can clear the counting flag bit

4. Calibration value register

As mentioned when analyzing the block diagram, this is generally not used.

configuration process

The configuration process here is divided into two categories:
one is to implement a delay function. The delay function only requires the timer to work for one cycle, that is, a process of reducing the reload value to one. After executing it once, the timer needs to be turned off. Let him keep reducing the reload value to 0 and then reduce the reload value to 0 in an infinite loop.
pseudocode:

实现系统的us延时(参数)
{
    
    
   //选择时钟 建议选择外部时钟
   //写入重装载值  21*参数
   //当前值清零
   //打开计数器
   //等待标志位置1
   //关闭计数器
}

The second is to use interrupts to enter an interrupt at a certain time, so as to realize a time slice polling operation mode. At this time, the timer needs to keep counting, so the counter cannot be turned off after the counting is completed. The pseudo code is as follows:

系统滴答的初始化代码
{
    
    
   //选择系统滴答的时钟
   //配置系统抵达的重装载值
   //当前值清零
   //打开中断使能
   
   //NVIC控制器

   //开启定时器   
}
中断服务函数
{
    
    
	判断标志;
	清楚标志;
	执行操作。
}

the code

#include "SysTick.h"
u16 SysTick_us;
u16 SysTick_ms;


/*******************************
函数名:SysTick_Init
函数功能:初始化系统滴答,选择外部时钟
函数形参:u32 sysclk 系统时钟168(MHZ)
函数返回值:void
备注:开启1ms中断

********************************/
void SysTick_Init(u32 sysclk) //168MHZ
{
    
    
	u32 pri;//存储优先级合成函数返回的优先级
	
	SysTick->CTRL &=~(1<<2); //选择外部时钟,必须清零默认是1内核时钟
	SysTick_us=sysclk/8;           //21     1us//外部时钟8分频
	SysTick_ms=SysTick_us*1000;            //21 000  1ms
	
	SysTick->LOAD = SysTick_ms-1;//重装载值21000-1
	SysTick->VAL=0;    //清空计数器,清标志位
	SysTick->CTRL |=1<<1;   //使能中断 

/*-----------------------配置NVIC---------------------------------------------*/	
	pri=NVIC_EncodePriority(7-2,1,2);
	NVIC_SetPriority(SysTick_IRQn,pri);
	NVIC_EnableIRQ(SysTick_IRQn);
	
		SysTick->CTRL |=1<<0;   //使能计数器  
}


/*******************************
函数名:SysTick_Delay_us
函数功能:系统滴答实现us延时
函数形参:u32 nus
函数返回值:void
备注:
//因为LOAD为24位,所以最大重装载值16,777,216
最长时间:形参最大值,798,915us

********************************/
void SysTick_Delay_us(u32 nus)//1us
{
    
    
	SysTick->LOAD =nus*SysTick_us;//传进来的参数*21  nus 传多少就是多少微秒
	SysTick->VAL=0;    //清空计数器,清标志位
	SysTick->CTRL |=1<<0;   //使能  
	while(!(SysTick->CTRL & 1<<16));//等待计数完成
	SysTick->CTRL &=~(1<<0);  //关闭计数器
	SysTick->VAL=0;     //清空计数器,清标志位
}
/*******************************
函数名:SysTick_Delay_ms
函数功能:系统滴答实现ms延时
函数形参:u32 nms
函数返回值:void
备注:
形参最大值798ms
********************************/
void SysTick_Delay_ms(u32 nms)
{
    
    
	SysTick->LOAD =nms*SysTick_ms;//传进来的参数*21  nms 传多少就是多少毫秒
	SysTick->VAL=0;    //清空计数器,清标志位
	SysTick->CTRL |=1<<0;   //使能  
	while(!(SysTick->CTRL & 1<<16));//等待标志位到
	SysTick->CTRL &=~(1<<0);  //关闭计数器
	SysTick->VAL=0;     //清空计数器,清标志位
}
//中断服务函数:

/*******************************
函数名:SysTick_Handler
函数功能:系统滴答的中断服务函数函数
函数形参:无
函数返回值:void
备注:1ms进一次中断
********************************/
void SysTick_Handler(void)
{
    
    
	static u8 i=100;
	while(SysTick->CTRL &(1<<16))//检测中断标志,同时也是清除标志位
		mtime--;
		Led_cnt++;
		_TIMER_1MS++;
		i--;
	if(i==0){
    
    
		i = 100;
		_TIMER_100MS ++;
	}
}


Realize time slice polling by using system tick

Programming with time slice polling can solve the blocking problem encountered before, define the corresponding timing variable in the system tick, and then perform the required operations according to this timing variable.
As shown in the figure below: Here, the author has selected three time variables to count 1S, 100ms, and 200ms respectively, and the timing of one second corresponds to a serial port printout; 100ms and 200ms correspond to the flickering of LED1 and LED2 respectively; There is a situation where polling is 0 to store tasks that do not require strict timing refresh.
insert image description here

Effect

The final effect is as follows: It can be seen from the timestamp that the timing of SysTick is relatively accurate.
insert image description here

Summarize

System tick is a timer in the system. Its main function is to provide precise delay and timing functions, which can be used to realize the code framework of time slice polling.

Guess you like

Origin blog.csdn.net/qq_41954556/article/details/129673789