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.
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.
2. SysTick job analysis
/* * 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); } }
#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--; } }
/* * 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.