概要
本篇主要介绍如何使用HAL库自带的定时器和任务的使用;在最后会补充一些在开发当中容易踩坑的地方。
在使用STM32CubeMX导出工程之后,已经默认使用了一套伪操作系统,因此我们应该使用操作系统的开发方式来开发,其中最为主要是定时器和任务的创建。
定时器
...
任务
/** * @brief This function registers a task in the sequencer. * * @param TaskId_bm The Id of the task * @param Flags Flags are reserved param for future use * @param Task Reference of the function to be executed * * @note It may be called from an ISR. * */ void UTIL_SEQ_RegTask( UTIL_SEQ_bm_t TaskId_bm, uint32_t Flags, void (*Task)( void ) );/** * @brief This function requests a task to be executed * * @param TaskId_bm The Id of the task * It shall be (1<<task_id) where task_id is the number assigned when the task has been registered * @param Task_Prio The priority of the task * It shall a number from 0 (high priority) to 31 (low priority) * The priority is checked each time the sequencer needs to select a new task to execute * It does not permit to preempt a running task with lower priority * * @note It may be called from an ISR * */ void UTIL_SEQ_SetTask( UTIL_SEQ_bm_t TaskId_bm, uint32_t Task_Prio );使用步骤:
首先使用UTIL_SEQ_RegTask创建任务
- 第一个参数是任务的ID,这个需要在app_conf.h中的结构体CFG_Task_Id_With_HCI_Cmd_t定义中添加,例如:
/** * These are the lists of task id registered to the scheduler * Each task id shall be in the range [0:31] * This mechanism allows to implement a generic code in the API TL_BLE_HCI_StatusNot() to comply with * the requirement that a HCI/ACI command shall never be sent if there is already one pending */ /**< Add in that list all tasks that may send a ACI/HCI command */ typedef enum { CFG_TASK_ADV_UPDATE_ID, CFG_TASK_MEAS_REQ_ID, CFG_TASK_HCI_ASYNCH_EVT_ID, /* USER CODE BEGIN CFG_Task_Id_With_HCI_Cmd_t */ CFG_TASK_MY_ID, /*自行在此添加即可*/ /* USER CODE END CFG_Task_Id_With_HCI_Cmd_t */ CFG_LAST_TASK_ID_WITH_HCICMD, /**< Shall be LAST in the list */ } CFG_Task_Id_With_HCI_Cmd_t;- 第二个参数是保留的参数直接填UTIL_SEQ_RFU即可,可以看到它的值是0
- 第三个参数是任务的实现,无需多言。
启动任务
使用UTIL_SEQ_SetTask即可,例如;
UTIL_SEQ_SetTask( 1<<CFG_TASK_MY_ID, CFG_SCH_PRIO_0);其他的函数可以在,stm32_seq.h中可以找到。
其他资料汇总
https://zhuanlan.zhihu.com/p/401022982https://zhuanlan.zhihu.com/p/401022982
https://wiki.stmicroelectronics.cn/stm32mcu/wiki/Category:STM32WB_Serieshttps://wiki.stmicroelectronics.cn/stm32mcu/wiki/Category:STM32WB_Series