SLAAEK8 August 2024 MSPM0G3507 , MSPM0L1306
The scheduler code is stored in the
modules/scheduler/scheduler.c
file, and includes a list of all
function pointers that the scheduler needs to access in gTasksList
.
Each task can provide a function for getting and resetting the ready flag or pending
flag, and a pointer to the task to be run.
Within the scheduler loop, the
gTasksPendingCounter
value keeps track of how many tasks are
pending. As the loop cycles through each pending task flag, when the loop finds one
that is pending, the scheduler loop decrements this counter. After all tasks are
cleared, the devices enters low power mode via a call to __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();
}
}