一、freeRTOS定时器配置需要注意的地方
1、在STM32CubeMX 首先找到Config parameters这个选项下的Software timer definitions,然后将USE_TIMERS设置为Enable
2、这个时候将其它的配置保持默认 ,将时钟配置好后 就能够生成代码了。(我是在这里配置了两颗 LED灯,1颗使用系统默认的一个线程,让它每200ms进行闪烁一次,另一个则是使用定时器定时每200ms闪烁一次)。
3、生成代码后,我们需要自己创建一个定时器使用的系统API函数为:
a、创建定时器的API函数 ** * name name of the timer object. * param function name of the timer call back function. */ osTimerDef(name, function) osTimerDef(LEDTimer, osTimerCallback); ** * @brief Create a timer. * @param timer_def timer object referenced with ref osTimer. * @param type osTimeronce for one-shot or osTimerPeriodic for periodic behavior. * @param argument argument to the timer call back function. * @retval timer ID for reference by other functions or NULL in case of error. * @note MUST REMAIN UNCHANGED: b osTimerCreate shall be consistent in every CMSIS-RTOS. */ osTimerId osTimerCreate (const osTimerDef_t *timer_def, os_timer_type type, void *argument) b、创建定时器句柄 osTimerId osTimer = osTimerCreate (osTimer(LEDTimer), osTimerPeriodic, NULL);
创建完定时器后,我们现在就可以来设置定时器的周期和开启定时器了(这里我设置的是定时器周期100ms) 。
osTimerStart(osTimer, 100);
4、下面我们在主函数main.c前面声明该定时器的回调函数
static void osTimerCallback(void const *argument);
5、在回调函数中的实现,其实这就很简单了
static void osTimerCallback (void const *argument)
{
(void) argument;
HAL_GPIO_TogglePin(GPIOF,GPIO_PIN_9);
}
6、默认线程中的也是将LED进行闪烁
void StartDefaultTask(void const * argument)
{
for(;;)
{
osDelay(200);
HAL_GPIO_TogglePin(GPIOF,GPIO_PIN_10);
}
}



