ZHCAER2A August 2024 – August 2025 MSPM0C1105 , MSPM0C1106 , MSPM0G3507 , MSPM0L1306
调度器代码存储在 modules/scheduler/scheduler.c 文件中,包括调度器需要在 gTasksList 中访问的所有函数指针的列表。每个任务可提供一个用于获取和复位就绪标志或挂起标志的函数,以及一个指向要运行的任务的指针。
在调度器循环中,gTasksPendingCounter 值会跟踪有多少任务处于挂起状态。当循环遍历每个挂起任务标志时,如果循环发现一个挂起的任务,则调度器循环就会使该计数器递减。清除所有任务后,器件会通过调用 __WFI 进入低功耗模式。
#include "scheduler.h"
#define NUM_OF_TASKS 2 /* Update to match required number of tasks */
volatile extern int16_t gTasksPendingCounter;
/*
* Update gTasksList to include function pointers to the
* potential tasks you want to run. See DAC8Driver and
* switchDriver code and header files for examples.
*
*/
static struct task gTasksList[NUM_OF_TASKS] =
{
{ .getRdyFlag = getSwitchFlag, .resetFlag = resetSwitchFlag, .taskRun = runSwitchTask },
{ .getRdyFlag = getDACFlag, .resetFlag = resetDACFlag, .taskRun = runDACTask },
/* {.getRdyFlag = , .resetFlag = , .taskRun = }, */
};
void scheduler() {
/* Iterate through all tasks and run them as necessary */
while(1) {
/*
* Iterate through tasks list until all tasks are completed.
* Checking gTasksPendingCounter prevents us from going to
* sleep in the case where a task was triggered after we
* checked its ready flag, but before we went to sleep.
*/
while(gTasksPendingCounter > 0)
{
for(uint16_t i=0; i < NUM_OF_TASKS; i++)
{
/* Check if current task is ready */
if(gTasksList[i].getRdyFlag())
{
/* Execute current task */
gTasksList[i].taskRun();
/* Reset ready for for current task */
gTasksList[i].resetFlag();
/* Disable interrupts during read, modify, write. */
__disable_irq();
/* Decrement pending tasks counter */
(gTasksPendingCounter)--;
/* Re-enable interrupts */
__enable_irq();
}
}
}
/* Sleep after all pending tasks are completed */
__WFI();
}
}