Time Triggered OS

  1. include "mbed.h"
  2. define NumTask (1) change for number of array spaces.
  3. define ArrayFull (-1) If SCHAddTask runs out of room before adding task.
  4. define NothingFound (-2) If the remove fails due to lack of something to remove. DigitalOut Led1(LED1); void SCHDispatch(void); void Led1Init(void); void LED1Run(void); int SCHRemoveTask(void); int SCHAddTask(void(*fP)(void), int P, int D); void SCHUpdate(void); void InterruptInit(void); Ticker Interrupt; int i = 0; possible errors global shared variable?

typedef struct { void(*fP)(void); int Delay; int Period; int RunMe; }pTask; The Task List array structure

pTask TaskList[NumTask];

int main() { void Led1Init(); void InterrupInit(); SCHAddTask(LED1Run,50,50); adding our LED task

while(1) { SCHDispatch(); } }

void InterruptInit() { Interrupt.attach(&SCHUpdate, 1.0); Starting up the ticker for SCHUpdate. }

void SCHUpdate() { for (i = 0; i < NumTask; i++) run through the whole tasklist array. { TaskList[i].Delay; if (TaskList[i].Delay <=0) { TaskList[i].RunMe++; Add 1 to runme per timeout. TaskList[i].Delay = TaskList[i].Period; reset the timeout to the period. } } }

int SCHAddTask(void(*fP)(void), int P, int D) { int i = 0; while(i <= NumTask && TaskList[i].fP != 0) { i++; } if (i > NumTask) { return ArrayFull; } TaskList[i].fP = fP; TaskList[i].Delay = D; TaskList[i].Period = P; TaskList[i].RunMe = 0;

return i; }

int SCHRemoveTask(int i) { if (TaskList[i].fP == 0) { return NothingFound; } TaskList[i].fP = 0; TaskList[i].Delay = 0; TaskList[i].Period = 0; TaskList[i].RunMe = 0; return i; }

void SCHDispatch() { for (i = 0; i <NumTask; i++) { if (TaskList[i].RunMe > 0) { TaskList[i].fP(); TaskList[i].RunMe; } } }

void LED1Init() { Led1 = 0; }

void LED1Run() { Led1 = !Led1; }


Please log in to post comments.