SysTick timer of STM32

SysTick-------The heartbeat of the operating system

SysTick is a system tick timer, which can be said to be the "heartbeat" of the operating system. It is tied to the NVIC and used to generate a SysTick exception (exception number: 15). Once a SysTick exception is generated, a tick interrupt is generated, which is especially important for the operating system. For example, the operating system can allocate different numbers of time slices to multiple tasks, ensure that no one task dominates the system, or assign a certain time range of each timer cycle to a specific task, etc. Various timing functions provided by the operating system are related to this tick timer, so a timer is required to generate periodic interrupts, and it is better that the user program cannot access its registers at will. To maintain the rhythm of the "heartbeat" of the operating system.

    The STM32 core includes a simple timer - SysTick. All CM3 chips have this timer. The clock source of the timer can be an internal clock or an external clock. The SysTick in STM32 uses HCLK (AHB clock) or HCLK/8 as the operating clock.

   The SysTick timer can generate an interrupt, and CM3 has a special exception type for it, and it has its place in the vector table. In addition to serving the operating system, SysTick timers can also be used for other purposes, such as alarms, to measure time.

SysTick is a system tick timer, which can be said to be the "heartbeat" of the operating system. It is tied to the NVIC and used to generate a SysTick exception (exception number: 15). Once a SysTick exception is generated, a tick interrupt is generated, which is especially important for the operating system. For example, the operating system can allocate different numbers of time slices to multiple tasks, ensure that no one task dominates the system, or assign a certain time range of each timer cycle to a specific task, etc. Various timing functions provided by the operating system are related to this tick timer, so a timer is required to generate periodic interrupts, and it is better that the user program cannot access its registers at will. To maintain the rhythm of the "heartbeat" of the operating system.

    The STM32 core includes a simple timer - SysTick. All CM3 chips have this timer. The clock source of the timer can be an internal clock or an external clock. The SysTick in STM32 uses HCLK (AHB clock) or HCLK/8 as the operating clock.

   The SysTick timer can generate an interrupt, and CM3 has a special exception type for it, and it has its place in the vector table. In addition to serving the operating system, SysTick timers can also be used for other purposes, such as alarms, to measure time.

2. SysTick job analysis

 SysTick is a 24-bit timer, that is, it can count up to 2 to 24 clock pulses at a time. This pulse count value is stored in the current count value register STK_VAL, which can only be counted down. Every time a clock pulse STK_VAL is received is decremented by 1 until it reaches 0. When the value of STK_VAL is reduced to 0, the data saved in the reload register STK_LOAD is automatically loaded into STK_VAL by the hardware, and counts down again. When the value of STK_VAL is counted to 0, the timed event can be handled in the interrupt service function.

Time unit conversion: 1s=1000ms=10^6 us=10^9

Let's look at the program below:

/*
* function name: main
* Description: main function
* Enter:
* output:
*/

int main(void)
{
	
    /*LED port initialization*/
	init_led_gpio();
	//system_init();

	/*Configure systick to interrupt once every 10us*/
	SysTick_Init();
	
	for(;;)
  {
  
   turn_led(LED1,ON);
	 Delay_us(50000);
	 turn_led(LED1,OFF);
	 
	 turn_led(LED2,ON);
	 Delay_us(50000);
	 turn_led(LED2,OFF);
	 
	 turn_led(LED3,ON);
	 Delay_us(50000);
	 turn_led(LED3,OFF);
	
  }
}
In the main function, the two functions Systick_Init( ) and Delay_us( ) are relatively unfamiliar. Their functions are to configure the Systick timer and perform precise delay.
The process of the entire main function is to initialize the LED and Systick timer, then enter an infinite loop, turn on LED1, LED2, LED3, and the lighting time is accurate.
500ms。
The next step is to configure and start Systick
The function Systick_Init() is implemented by the user in the file Systick.c. Its function is to start the system tick timer Systick and configure the Systick to interrupt once every 10us.
#include "SysTick.h"
#include "stm32f10x_it.h"
#include "core_cm3.h"
#include "system_stm32f10x.h"
static __IO u32 TimingDelay;
/*
* Function name: SysTick_Init
* Description: Start the system tick timer SYSTICK
* Enter:
* output:
* call: external call
*/

void SysTick_Init(void)
{
   /* systemFrequency /1000 1ms interrupt
	  * systemFrequency /100000 10us interrupt
	  * systemFrequency /1000000 1us interrupt
	  */
	 //if (SysTick_Config(SystemFrenquency /100000)) ST3.0.0 library version
	   if (SysTick_Config(SystemCoreClock /100000)) // ST3.5.0 library version
		 {
			 /* Capture error */
			 while(1);
		 }
		 
		 // close the tick timer
		 SysTick->CTRL &= ~ SysTick_CTRL_ENABLE_Msk;
	 }
	 
	 
	 /*
	  * Function name: Delay_us
	  * Description: us delay program, 10us is a unit
	  * Enter:
	  * Enter:
	  * Call: Delay_us(1) The realized delay is 1*10us = 10us
	  */
	  
	 
	 void Delay_us(__IO u32 nTime)
	 {
		 TimingDelay = nTime;
		 //enable tick timer 1
		 SysTick->CTRL |= SysTick_CTRL_ENABLE_Msk;
		 while(TimingDelay !=0);
	 }
	 
	 
	 /*
	  * Function name: TimingDelay_Decerment
	  * Description: Get the beat program
	  * Enter:
	  * Enter:
	  * Call: Called in the SysTick interrupt function SysTick_Handler()
	  */
	 
	 
	void TimingDelay_Decrement(void)
 {
	 if (TimingDelay != 0x00)
	 {
		 TimingDelay--;
	 }
 }  

The Sys tick_Init() function actually only calls the Systick_Config() function, which is a general function of Cortex_M3 belonging to the kernel layer and located in the Core_cm3.h file. If calling Systick_Config() to configure the Systick is unsuccessful, it will enter an infinite loop and the initialization of the Systick will succeed . After that, turn off the timer first and turn it on again when needed.

enable off timer

// enable tick timer
SysTick->CTRL |= SysTick_CTRL_ENABLE_Msk; //(OR operation)
//enable tick timer
SysTick->CTRL &= ~SysTick_CTRL_ENABLE_Msk;//(与运算)

The next step is to write an interrupt service routine.
Let's go back to the main function, we make the LED work in an infinite loop and call the Delay_us function between the on and off of the LED
/*
	  * Function name: Delay_us
	  * Description: us delay program, 10us is a unit
	  * Enter:
	  * Enter:
	  * Call: Delay_us(1) The realized delay is 1*10us = 10us
	  */
	  
	 
	 void Delay_us(__IO u32 nTime)
	 {
		 TimingDelay = nTime;
		 //enable tick timer 1
		 SysTick->CTRL |= SysTick_CTRL_ENABLE_Msk;
		 while(TimingDelay !=0);
	 }

Once we call the Delay_us() function, the SysTick timer is turned on. According to the set timing cycle decrement technique, when the value of the count register of Systick is reduced to 0, the interrupt function is entered. When the interrupt function is executed, it restarts Timing, and so on, unless it is turned off.
Function to measure time with   Systick
With a little change of usage, we can use systick to measure time. This is the content to be written in the next blog (manual comparison laughing out loud)



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324480715&siteId=291194637