前面都用 HAL 开发 STM32F030K6T6, 由于空间消耗太快,采用优化编译也无法解决内存不够的问题;只能从来把 HAL 换成 LL 库;
用STM32CUBEMX 生成模板程序后,发现Systick 不能进入中断;
比较HAL 和 LL的区别如下:
标准HAL库是如下的:
__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 */
}
LL 库是如下的:
/**
* @brief This function configures the Cortex-M SysTick source of the time base.
* @param HCLKFrequency HCLK frequency in Hz (can be calculated thanks to RCC helper macro)
* @note When a RTOS is used, it is recommended to avoid changing the SysTick
* configuration by calling this function, for a delay use rather osDelay RTOS service.
* @param Ticks Number of ticks
* @retval None
*/
__STATIC_INLINE void LL_InitTick(uint32_t HCLKFrequency, uint32_t Ticks)
{
/* Configure the SysTick to have interrupt in 1ms time base */
SysTick->LOAD = (uint32_t)((HCLKFrequency / Ticks) - 1UL); /* set reload register */
SysTick->VAL = 0UL; /* Load the SysTick Counter Value */
SysTick->CTRL = SysTick_CTRL_CLKSOURCE_Msk | \
SysTick_CTRL_ENABLE_Msk; /* Enable the Systick Timer */
}
仔细比较二种的区别,发现LL库默认没有设置:enable 中断:
也就是没有设置这个位:SysTick_CTRL_TICKINT_Msk
跟踪调试也可以看到:TICKINT 标志位没有勾上,这时如果认为点上勾,就可以进入中断函数了!
具体解决程序段如下:
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
LL_APB1_GRP2_EnableClock(LL_APB1_GRP2_PERIPH_SYSCFG);
LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_PWR);
/* Configure the system clock 定义自己需要是时钟, 同时设置了Systick =1ms, 但并没有启动systick */
SystemClock_Config();
/* 2024.06.15 必须人工启用Systick enable 中断允许*/
SysTick->CTRL |= SysTick_CTRL_TICKINT_Msk;
NVIC_SetPriority(SysTick_IRQn, 3);
NVIC_EnableIRQ(SysTick_IRQn);