The STM32 MCU has been used for several years, and the Systick timers for the arm cores have been used frequently, but have hardly been carefully understood. Recently, just to transplant a new operating system in contact with this piece, according to a more in-depth understanding.
1 , SysTick What the hell is that?
There is no detailed description of the Systick in STM32, which may be due to Systick being an arm kernel thing. In the STM32F10XXX reference manual, the stm32f4xx reference manual, and the STM32F7XX reference manual, the clock is introduced simply by using the tree to draw a hclk clock that is 8-divided and sent to the cortex system clock. The description of the clock is also very simple. In the STM32F10XXX reference manual, only "RCC passes the AHB clock (HCLK) 8 as the external clock of the cortex system timer (SysTick). "Also in the Stm32f4xx reference manual and the STM32F7XX reference manual, it just says:" The RCC feeds the Cortex system timer (SysTick) with a 8-way AHB clock (HCLK). ”
In addition, STM32 in the interrupt section of the Systick also has a sentence description. As mentioned in the "9.1.1 system tick (SysTick) calibration value Register" in the Stm32f10xxx Reference manual: "The system tick calibration value is fixed to 9000, and when the system tick clock is set to 9MHz (the maximum of HCLK/8), a 1ms time base is generated. "The 10.1.2 SysTick Calibration Value Register" section of the Stm32f4xx Reference manual and the STM32F7XX Reference manual "10.1.1 SysTick Calibration Value Register" also include the following: "The SysTick calibration value is set to 18750." When the SysTick clock is set to 18.75 MHz (HCLK/8,HCLK), a 1 ms Time base is generated. ”
Just looking at this makes us feel that Systick seems to be fixed, but in fact it is not, because in the library function there is an operation function of the relevant register. In order to figure this out, we have to look at the manuals for cortex-m3 and M4. In the "cortex-m3 authoritative guide" in the "8th chapter Nvic and Interrupt Control" in a more detailed description. First, it is clear that Systick is a timer, and the second Systick timer is bundled in Nvic, which is used to generate Systick anomalies, and is mainly applicable to the "heartbeat" rhythm of the operating system. The clock source for Systick is ultimately decided by the chip manufacturer.
The Systick timer can produce interrupts and is a separate exception type, and has its place in the vector table. And there are four registers to control the Systick, described in the CORTEX-M3 authoritative guide as follows:
( 1 ), STK_CSR Control Register : 4 bits in the register are meaningful
( 2 ), Stk_load Overloaded Registers
The Systick is a descending timer, and when the timer decrements to 0 o'clock, the value in the Reload register is reloaded and continues to start decreasing. The Stk_load overload register is a 24-bit register with a maximum count of 0xFFFFFF.
( 3 ), Stk_val Current Value Register
It is also a 24-bit register that returns the value of the current inverted count when read, writes it to zero, and clears the COUNTFLAG flag in the Systick control and status register.
( 4 ), STK_CALRB Calibration Value Register
2 , STM32 in the SysTick
As a 24-bit decrement timer for system level, is it processed in STM32? The introduction of St in the manual is relatively simple, but in the library function there is a relatively clear definition. Both the standard library and the HAL Library have a clearer definition, such as the Systick register struct machine operation function defined in the standard library, in misc. The Systick_clksourceconfig function in the C file, which is a clock source configuration function, is defined as follows:
/*@ function: Configure Systick clock source
*@ input parameter: Systick_clksource: Specifies the SysTick clock source.
* This parameter can be one of the following values:
* @ SYSTICK_CLKSOURCE_HCLK_DIV8:AHB Clock 8 divided as SysTick clock source
* @ SYSTICK_CLKSOURCE_HCLK:AHB clock as SysTick clock source. */
void Systick_clksourceconfig (uint32_t Sy stick_clksource)
{
Assert_param (Is_systick_clk_source (Sy sTi ck_clksource));
if (Systick_clksource = = SYSTICK_CLKSOURCE_HCLK)
{
Systick->ctrl |= Systi ck_clksource_hclk;
}
Else
{
Systick->ctrl &= Systick_clksource_hclk_div8;
}
}
Thus, in the STM32 clock can be set to HCLK or HCLK 8, depending on the needs of the use. If not configured by default, the clock is HCLK 8. The same is true in the definition of Hal's difficulties.
There is a register definition in the Core_cm3.h file:
typedef struct
{
__io uint32_t CTRL;
__io uint32_t LOAD;
__io uint32_t VAL;
__i uint32_t calib;
} Systick_type;
Also defines a systick_config function
#if (!defined (__vendor_sy stickconfig)) | | (__vendor_systickconfig = = 0)
/**
* @ function initializes and turns on Sy STick counter and its interrupt
*@ input parameter ticks two interrupts ticks value
*@ return value 1 = failed, 0 = successful
* Initialize the system tick timer and its interrupts and turn on the system tick timer in free running mode to produce a cycle interrupt */
Static __inline uint32_t systick_config (uint32_t ticks)
{
if (Ticks > Systick_load_reload_msk) return (1);/* Reload value exceeds 24 bits, is not possible. Return failure value 0 */
Systick->load = (Ticks & Systick_load_reload_msk)-1; /* Set Reload Register */
Nvic_setpriority (SYSTICK_IRQN, (1<<__nvic_prio_bits)-1); /* Set priority for CORTEX-M0 system interrupt */
Systick->val = 0; /* Load counter value (current count value clear 0) */
Systick->ctrl = Systick_ctrl_clksource_msk |
Sy stick_ctrl_tickint_msk| systick_ctrl_enable_msk;/* Enable SY STick interrupt request and SY sTick timing */
return (0); /* Success, return 0 */
}
#endif
After the above analysis, we need, can be very convenient to operate in their own software systick to achieve some functions.
3 , STM32 in SysTick Application Examples
Since Systick we have already understood his principle and operation, also understand how to operate it in the STM32 library, then we can use him to do what? First we can use it when using the embedded operating system in STM32, and it is more practical to set up. In fact, regardless of the operating system, we can also use Systick to implement the delay timer.
An example of implementation is as follows:
Variables that define the delay count
__io uint32_t Timingdelay;
Const uint16_t DELAYADJUSTMENTS=12;
Function Name: Ms delay
Description: The parameter 1 is 1ms,1000 is 1s, only a few US error;
void Delayms (__io uint32_t ntime)
{
while (Systick_config (systemcoreclock/1000));
Systick->ctrl &= ~ systick_ctrl_enable_msk;//off Tick Timer
Timingdelay = Ntime;
Systick->ctrl |= systick_ctrl_enable_msk;//Enable tick timer
while (Timingdelay! = 0);
systick->ctrl=0x00; Turn off counters
Systick->val =0x00; Empty counter
}
Function name: US delay functions
Description: The parameter 1 is 1us,1000 is 1ms, only a few US error;
void Delayus (__io uint32_t ntime)
{
while (Systick_config (systemcoreclock/1000000));
Systick->ctrl &= ~ systick_ctrl_enable_msk;//off Tick Timer
Timingdelay = Ntime;
Systick->ctrl |= systick_ctrl_enable_msk;//Enable tick timer
while (Timingdelay! = 0);
systick->ctrl=0x00; Turn off counters
Systick->val =0x00; Empty counter
}
Function Name: Delay decrement count
Description: Called by Systick Interrupt function to implement the decrement count of the delay value
void Timingdelay_decrement (void)
{
if (timingdelay! = 0x00)
{
timingdelay--;
}
}
Function name: Delaynus functions of delay-adjusted form
Description: The parameter 1 is 1us,1000 is 1ms, depending on the clock frequency different time
void Delay_nus (uint16_t N)
{
Uint16_t J;
while (n--)
{
j=delayadjustments;//adjusting values According to the instruction period of different clock frequencies
while (j--);
}
}
Function name: DELAYNMS functions of delay-adjusted form
Description: The parameter 1 is 1ms,1000 is 1s, depending on the clock frequency different time
void Delay_nms (uint16_t N)
{
while (n--)
{
Delay_nus (1000);
}
}
4 , References
We have made reference to some classic manuals and some discussions on the web forums, which are not listed here, and are listed in some references:
(1), "cortex-m3 authoritative guide"
(2), "stm32f10x reference manual"
(3), "STM32F4 reference manual"
(4), "STM32F7 reference manual"
(5), "stm32f10x Library function description"
(6), "STM32F1 Hal Library description"
(7), "STM32F4 Hal Library description"
(8), "STM32F7 Hal Library description"
STM32 Study and Application Note one: Systick Timer learning and application