RTOS之SysTick_Config()解析

Posted 旭日初扬

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了RTOS之SysTick_Config()解析相关的知识,希望对你有一定的参考价值。

一、SysTick_Config()解析

  • 定期性的中断或者异常来作为系统的时基 --时钟节拍
  •   SysTick中断频率设置  CM3内核时钟 SystemCoreClock = 25M 
/*----------------------------------------------------------------------------
  System Core Clock Variable
 *----------------------------------------------------------------------------*/
uint32_t SystemCoreClock = SYSTEM_CLOCK;  /* System Core Clock Frequency */

/*----------------------------------------------------------------------------
  Define clocks
 *----------------------------------------------------------------------------*/
#define  XTAL            (50000000UL)     /* Oscillator frequency */

#define  SYSTEM_CLOCK    (XTAL / 2U)     
  • ticks =  SystemCoreClock / RT_TICK_PER_SECOND  =25M/100   =250 000       
  •  中断一次的时间 =   重装载值/时钟频率=250 000/25M = 0.01s = 10ms.
  •  计数一次的时间:T=1/内核时钟频率       内核时钟=25Mhz
  •  ticks形参的值最终是写到重装载寄存器 LOAD 中
  • 定时器中断一次的时间:重装载寄存器的值/系统时钟频率 =250 000/25M=0.01s =10ms 
  •  72Mhz等于720 000 00hz。hz是一个频率的单位,它表示物体在一秒钟之内振动一次,它的频率就是1hz。
#define RT_TICK_PER_SECOND 100
 

SysTick_Config(SystemCoreClock / RT_TICK_PER_SECOND);

形参 ticks 用来设 置重装载寄存器的值,最大不能超过重装载寄存器的值 2的24次方 当重装载寄存器的值递减到 0 的时候产生中断,然后重装载寄存器的值又重新装载往下递减计数,以此循环往复。紧随 其后设置好中断优先级,最后配置系统定时器的时钟等于 AHBCLK=72M 使能定时器和 定时器中断
/**
  \\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 */
  

//  设置重装载寄存器值  ticks-1
  SysTick->LOAD  = (uint32_t)(ticks - 1UL);                         /* set reload register */
  //  设置中断优先级
  NVIC_SetPriority (SysTick_IRQn, (1UL << __NVIC_PRIO_BITS) - 1UL); /* set Priority for Systick Interrupt */
 //  加载sysTick计数器值
  SysTick->VAL   = 0UL;                                             /* Load the SysTick Counter Value */
//  设置系统定时器的时钟源为内核时钟
//  使能Systick定时器中断
//  使能SysTick定时器
  SysTick->CTRL  = SysTick_CTRL_CLKSOURCE_Msk |
                   SysTick_CTRL_TICKINT_Msk   |
                   SysTick_CTRL_ENABLE_Msk;                         /* Enable SysTick IRQ and SysTick Timer */
  return (0UL);                                                     /* Function successful */

二、SysTick时钟源选择

  • 位0:CTRL使能位。
  • 2U     0000 0000 0000 0010。
  • 1UL << 2U   = 0000 0000 0000 0100   内核时钟。
/*!< SysTick CTRL: CLKSOURCE Position */
#define SysTick_CTRL_CLKSOURCE_Pos          2U   

/*!< SysTick CTRL: CLKSOURCE Mask */                                         
#define SysTick_CTRL_CLKSOURCE_Msk         (1UL << SysTick_CTRL_CLKSOURCE_Pos)     

// 时钟源选择位,0=外部时钟;1=内核时钟       
  • U表示该常数用无符号整型方式存储,相当于 unsigned int 。
  •  在32位操作系统与64位操作系统中占4字节   32位。
  • 2U   无符号整型2     表示  0000 0000 0000 0000 0000 0000 0000 0010      倒数第二位为1。
  • 0UL:无符号0
  • 1UL:无符号1
  • L表示该常数用长整型方式存储,相当于 long
  • F表示该常数用浮点方式存储,相当于 float

以上是关于RTOS之SysTick_Config()解析的主要内容,如果未能解决你的问题,请参考以下文章

RTOS内功修炼记 | 深度解析RTOS内核上下文切换机制

RTOS内功修炼记 | 深度解析RTOS内核上下文切换机制

STM32中 Systick问题 while(SysTick_Config(SystemFrequency / 1000)); 是啥意思,实现啥功能,求大神

CMSIS-RTOS 时间管理之时间延迟Time Delay

ARM官方《CMSIS-RTOS教程》之线程Threads

RTOS基础之线程间同步