Systick定时器是系统滴答定时器,一个24位的倒计数的定时器,计到0时,将从RELOAD寄存器中自动重装载定时初值。只要不把它在Systick控制及状态寄存器中的使能位清除,就永不停息,即使在睡眠模式下也能工作。
SysTick定时器被捆绑在NVIC中,用于产生SYSTICK异常(异常号:15)。
SysTick中断的优先级也是可以设置。
4个Systick寄存器
CTRL SysTick控制和状态寄存器
LOAD SysTick重装载数值寄存器
VAL SysTick当前值寄存器
CALIB SysTick校准值寄存器
寄存器是在core_cm4.h或core_cm7.h中定义的
typedef struct
{
__IOM uint32_t CTRL;
__IOM uint32_t LOAD;
__IOM uint32_t VAL;
__IM uint32_t CALIB;
}SysTick_Type;
HAL库中的Systick相关的函数:
stm32f4xx_hal_cortex.h/stm32f7xx_hal_cortex.h文件中:
void HAL_SYSTICK_CLKSourceConfig(uint32_t CLKSource); //Systick时钟源选择
core_cm4.h/core_cm4.h文件中:
SysTick_Config(uint32_t ticks); //初始化systick,时钟为HCLK,并开启中断
/* ################################## SysTick function ############################################ */
/**
\ingroup CMSIS_Core_FunctionInterface
\defgroup CMSIS_Core_SysTickFunctions SysTick Functions
\brief Functions that configure the System.
@{
*/
#if (__Vendor_SysTickConfig == 0U)
/**
\brief System Tick Configuration
\details Initializes the System Timer and its interrupt, and starts the System Tick Timer.
Counter is in free running mode to generate periodic interrupts.
\param [in] ticks Number of ticks between two interrupts.
\return 0 Function succeeded.
\return 1 Function failed.
\note When the variable <b>__Vendor_SysTickConfig</b> is set to 1, then the
function <b>SysTick_Config</b> is not included. In this case, the file <b><i>device</i>.h</b>
must contain a vendor-specific implementation of this function.
*/
__STATIC_INLINE uint32_t SysTick_Config(uint32_t ticks)
{
if ((ticks - 1UL) > SysTick_LOAD_RELOAD_Msk)
{
return (1UL); /* Reload value impossible */
}
SysTick->LOAD = (uint32_t)(ticks - 1UL); /* set reload register */
NVIC_SetPriority (SysTick_IRQn, (1UL << __NVIC_PRIO_BITS) - 1UL); /* set Priority for Systick Interrupt */
SysTick->VAL = 0UL; /* 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 (0UL); /* Function successful */
}
#endif
/*@} end of CMSIS_Core_SysTickFunctions */
Systick中断服务函数:
void SysTick_Handler(void);