STM32-库函数分析
void SysTick_ConfigInit(void){
while (SysTick_Config(SystemCoreClock / 10));
SysTick->CTRL &= ~SysTick_CTRL_ENABLE_Msk;
}
追踪SysTick_Config(SystemCoreClock / 10)得:
/**
* @briefInitialize and start the SysTick counter and its interrupt.
*
* @param ticks number of ticks between two interrupts
* @return1 = failed, 0 = successful
*
* Initialise the system tick timer and its interrupt and start the
* system tick timer / counter in free running mode to generate
* periodical interrupts.
*/
static __INLINE uint32_t SysTick_Config(uint32_t ticks)
{
if (ticks > SysTick_LOAD_RELOAD_Msk)return (1); /* Reload value impossible */
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 Interrupts */
SysTick->VAL = 0; /* 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 (0); /* Function successful */
}
分析如下:
一、if (ticks > SysTick_LOAD_RELOAD_Msk)return (1);输入参数检查,
如果参数大于SysTick_LOAD_RELOAD_Msk则返回值“1”,表示 failed既失败;
二、追踪SysTick_LOAD_RELOAD_Msk(位屏蔽宏)得到:
#define SysTick_LOAD_RELOAD_Msk (0xFFFFFFul << SysTick_LOAD_RELOAD_Pos) 这样的宏定义;
再次追踪SysTick_LOAD_RELOAD_Pos(位指示宏)得到:
#define SysTick_LOAD_RELOAD_Pos 0 其后的“0”正好是LOAD在STK_CTRL寄存器中的位置,既Bit0;
三、0xFFFFFF后的ul表示什么呢?
ul表示无符号长整型既unsigned long的缩写;其整体的含义是0xFFFFFF为无符号长整型数据;
总结:第一句的含义为检查输入的参数是否大于0xFFFFFF;因为这是其允许的最大计数值;
四、对SysTick->CTRL= SysTick_CTRL_CLKSOURCE_Msk |
SysTick_CTRL_TICKINT_Msk |
SysTick_CTRL_ENABLE_Msk;
此语句是将寄存器CTRT的各个位(CLKSOURCE/TICKINT/ENABLE)为分别进行位移操作,从而使各个位置
位或复位;
然后再通过位或运算赋值给CTRT寄存器,从而完成寄存器的配置的! 这是我最近学习32时对库函数的一些理解,还请各位大侠指点纠错!谢谢啦!
页:
[1]