《零死角玩转STM32》-18-SysTick系统滴答定时器

参考资料:《STM32F4XX内核参考手册》:4.4.8-SHPRX(System Handler Priority Register)与4.5- System Tick Timer.

1. 简介

SysTick是一个24Bit的系统定时器,属于CM4内核的外设,相关寄存器与部分库函数中core_cm4.h中定义。SysTick一般用于操作系统,用于产生时基,维持OS的心跳。

如下所示:当计数器被使能(STC_CTRL: Bit0-ENABLE)并且计数器到0时,STK_LOAD中的预设值会被重新装载到STK_VAL中,并作为计数器的起始值(Start Value),之后开始递减计数。

 "STK_VAL" ---   ---count down---  -----> Zero?

     |                                                    |

    /|\                                                 \|/

     |                                                    |

     -<-----Relaod Value from STK_LOAD-

2. SysTick寄存器介绍

  

 

STK_CTRL,主要涉及

  计数器的使能(Bit0-ENABLE),

  计数到0的时候是否产生系统异常(Bit1-TICKINT:SysTick exception enable);当设置为1时,counting down to zero asserts the SysTick exception request.

  时钟源的选择(Bit2-CLKSOURCE)

  以及计数标志位(Bit16-COUNTFLAG)

STK_LOAD:计数器的预设值

STK_VAL:计数器当前值寄存器,即为:The current value of the SysTick counter.

  注意:读取该寄存器,返回:系统滴答计数器的当前值

     写入“任何数值”到该寄存器都会将该寄存器清零!,同时会清除STK_CTRL寄存器中的COUNTFLAG位!

 

关于SysTick Handler的优先级:

 Ref:

 

附录:SysTick唯一的固件库函数(core_cm4.h),其中SysTick_IRQn是在stm32f4xx.h中定义

  SysTick_IRQn                = -1,     /*!< 15 Cortex-M4 System Tick Interrupt                                */
 1 /* ##################################    SysTick function  ############################################ */
 2 /** \ingroup  CMSIS_Core_FunctionInterface
 3     \defgroup CMSIS_Core_SysTickFunctions SysTick Functions
 4     \brief      Functions that configure the System.
 5   @{
 6  */
 7 
 8 #if (__Vendor_SysTickConfig == 0)
 9 
10 /** \brief  System Tick Configuration
11 
12     The function initializes the System Timer and its interrupt, and starts the System Tick Timer.
13     Counter is in free running mode to generate periodic interrupts.
14 
15     \param [in]  ticks  Number of ticks between two interrupts.
16 
17     \return          0  Function succeeded.
18     \return          1  Function failed.
19 
20     \note     When the variable <b>__Vendor_SysTickConfig</b> is set to 1, then the
21     function <b>SysTick_Config</b> is not included. In this case, the file <b><i>device</i>.h</b>
22     must contain a vendor-specific implementation of this function.
23 
24  */
25 __STATIC_INLINE uint32_t SysTick_Config(uint32_t ticks)
26 {
27   if ((ticks - 1) > SysTick_LOAD_RELOAD_Msk)  return (1);      /* Reload value impossible */
28 
29   SysTick->LOAD  = ticks - 1;                                  /* set reload register */
30   NVIC_SetPriority (SysTick_IRQn, (1<<__NVIC_PRIO_BITS) - 1);  /* set Priority for Systick Interrupt */
31   SysTick->VAL   = 0;                                          /* Load the SysTick Counter Value */
32   SysTick->CTRL  = SysTick_CTRL_CLKSOURCE_Msk |
33                    SysTick_CTRL_TICKINT_Msk   |
34                    SysTick_CTRL_ENABLE_Msk;                    /* Enable SysTick IRQ and SysTick Timer */
35   return (0);                                                  /* Function successful */
36 }
37 
38 #endif
39 
40 /*@} end of CMSIS_Core_SysTickFunctions */

 

Set Interrput Priority Function

 1 /** \brief  Set Interrupt Priority
 2 
 3     The function sets the priority of an interrupt.
 4 
 5     \note The priority cannot be set for every core interrupt.
 6 
 7     \param [in]      IRQn  Interrupt number.
 8     \param [in]  priority  Priority to set.
 9  */
10 __STATIC_INLINE void NVIC_SetPriority(IRQn_Type IRQn, uint32_t priority)
11 {
12   if(IRQn < 0) {
13     SCB->SHP[((uint32_t)(IRQn) & 0xF)-4] = ((priority << (8 - __NVIC_PRIO_BITS)) & 0xff); } /* set Priority for Cortex-M  System Interrupts */
14   else {
15     NVIC->IP[(uint32_t)(IRQn)] = ((priority << (8 - __NVIC_PRIO_BITS)) & 0xff);    }        /* set Priority for device specific Interrupts  */
16 }

 

SysTick的初始化相当简单:

 1 void SystemTickConfiguration(){
 2     /** \brief  System Tick Configuration
 3 
 4         The function initializes the System Timer and its interrupt, and starts the System Tick Timer.
 5         Counter is in free running mode to generate periodic interrupts.
 6 
 7         \param [in]  ticks  Number of ticks between two interrupts.
 8 
 9         \return          0  Function succeeded.
10         \return          1  Function failed.
11     */    
12     /*  T = (f/fn) = (180M/100000)/(180M) = 1/100000 s = 10 us*/
13     if(1 == SysTick_Config(SystemCoreClock/1000)){
14         while(1);
15     }
16     
17     return ;     
18 }

 

posted on 2017-04-09 10:24  Alexboo  阅读(569)  评论(0)    收藏  举报