A simple Round Robin Task scheduler (a sub minimalistic Operating System) with no inter Task communication implemented (semaphores, mailboxes etc) nor task local varibales can be used. They have to be global (static). But, it's the first step up to an OS.

Dependencies:   mbed

Committer:
Stanislaus
Date:
Fri Jun 11 20:52:03 2010 +0000
Revision:
0:9ce7c170cb66

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Stanislaus 0:9ce7c170cb66 1 #include "mbed.h"
Stanislaus 0:9ce7c170cb66 2 #include "scheduler.h"
Stanislaus 0:9ce7c170cb66 3
Stanislaus 0:9ce7c170cb66 4 #define ON 1
Stanislaus 0:9ce7c170cb66 5 #define OFF 0
Stanislaus 0:9ce7c170cb66 6
Stanislaus 0:9ce7c170cb66 7 DigitalOut myLED(LED1); // LED for task A and B
Stanislaus 0:9ce7c170cb66 8 DigitalOut myLEDTaskC(LED2); // LED for Task C
Stanislaus 0:9ce7c170cb66 9 DigitalOut myLEDTaskD(LED3); // LED for Task D
Stanislaus 0:9ce7c170cb66 10 DigitalOut myLEDTaskE(LED4); // LED for Task E
Stanislaus 0:9ce7c170cb66 11
Stanislaus 0:9ce7c170cb66 12 Timer t;
Stanislaus 0:9ce7c170cb66 13 Ticker keepAlive;
Stanislaus 0:9ce7c170cb66 14
Stanislaus 0:9ce7c170cb66 15 // *******************************************************************************
Stanislaus 0:9ce7c170cb66 16 // * @brief "taskA" task code
Stanislaus 0:9ce7c170cb66 17 // * @param[in] None
Stanislaus 0:9ce7c170cb66 18 // * @param[out] None
Stanislaus 0:9ce7c170cb66 19 // * @retval bool
Stanislaus 0:9ce7c170cb66 20 // * @par Description
Stanislaus 0:9ce7c170cb66 21 // * @details This task use to print message "taskA running". Indicate "taskA"
Stanislaus 0:9ce7c170cb66 22 // * had been executed.
Stanislaus 0:9ce7c170cb66 23 // *******************************************************************************
Stanislaus 0:9ce7c170cb66 24 bool taskA(void* pData)
Stanislaus 0:9ce7c170cb66 25 {
Stanislaus 0:9ce7c170cb66 26 static unsigned int cnt_numA = 0;
Stanislaus 0:9ce7c170cb66 27
Stanislaus 0:9ce7c170cb66 28 myLED = ON;
Stanislaus 0:9ce7c170cb66 29 cnt_numA++;
Stanislaus 0:9ce7c170cb66 30
Stanislaus 0:9ce7c170cb66 31 return true;
Stanislaus 0:9ce7c170cb66 32 }
Stanislaus 0:9ce7c170cb66 33
Stanislaus 0:9ce7c170cb66 34 // *******************************************************************************
Stanislaus 0:9ce7c170cb66 35 // * @brief "taskB" task code
Stanislaus 0:9ce7c170cb66 36 // * @param[in] None
Stanislaus 0:9ce7c170cb66 37 // * @param[out] None
Stanislaus 0:9ce7c170cb66 38 // * @retval bool
Stanislaus 0:9ce7c170cb66 39 // * @par Description
Stanislaus 0:9ce7c170cb66 40 // * @details This task use to print message "taskB running". Indicate "taskB"
Stanislaus 0:9ce7c170cb66 41 // * had been executed.
Stanislaus 0:9ce7c170cb66 42 // *******************************************************************************
Stanislaus 0:9ce7c170cb66 43 bool taskB(void* pData)
Stanislaus 0:9ce7c170cb66 44 {
Stanislaus 0:9ce7c170cb66 45 static unsigned int cnt_numB = 0;
Stanislaus 0:9ce7c170cb66 46
Stanislaus 0:9ce7c170cb66 47 myLED = OFF;
Stanislaus 0:9ce7c170cb66 48 cnt_numB++;
Stanislaus 0:9ce7c170cb66 49
Stanislaus 0:9ce7c170cb66 50 return true;
Stanislaus 0:9ce7c170cb66 51 }
Stanislaus 0:9ce7c170cb66 52
Stanislaus 0:9ce7c170cb66 53 // *******************************************************************************
Stanislaus 0:9ce7c170cb66 54 // * @brief "taskC" task code
Stanislaus 0:9ce7c170cb66 55 // * @param[in] None
Stanislaus 0:9ce7c170cb66 56 // * @param[out] None
Stanislaus 0:9ce7c170cb66 57 // * @retval bool
Stanislaus 0:9ce7c170cb66 58 // * @par Description
Stanislaus 0:9ce7c170cb66 59 // * @details This task use to print message "tasC running". Indicate "taskC"
Stanislaus 0:9ce7c170cb66 60 // * had been executed.
Stanislaus 0:9ce7c170cb66 61 // *******************************************************************************
Stanislaus 0:9ce7c170cb66 62 bool taskC(void* pData)
Stanislaus 0:9ce7c170cb66 63 {
Stanislaus 0:9ce7c170cb66 64 static unsigned int cnt_numC = 0;
Stanislaus 0:9ce7c170cb66 65
Stanislaus 0:9ce7c170cb66 66 myLEDTaskC = !myLEDTaskC.read();
Stanislaus 0:9ce7c170cb66 67 cnt_numC++;
Stanislaus 0:9ce7c170cb66 68
Stanislaus 0:9ce7c170cb66 69 return true;
Stanislaus 0:9ce7c170cb66 70 }
Stanislaus 0:9ce7c170cb66 71
Stanislaus 0:9ce7c170cb66 72 // *******************************************************************************
Stanislaus 0:9ce7c170cb66 73 // * @brief "taskD" task code
Stanislaus 0:9ce7c170cb66 74 // * @param[in] None
Stanislaus 0:9ce7c170cb66 75 // * @param[out] None
Stanislaus 0:9ce7c170cb66 76 // * @retval bool
Stanislaus 0:9ce7c170cb66 77 // * @par Description
Stanislaus 0:9ce7c170cb66 78 // * @details This task use to print message "taskB running". Indicate "taskD"
Stanislaus 0:9ce7c170cb66 79 // * had been executed.
Stanislaus 0:9ce7c170cb66 80 // *******************************************************************************
Stanislaus 0:9ce7c170cb66 81 bool taskD(void* pData)
Stanislaus 0:9ce7c170cb66 82 {
Stanislaus 0:9ce7c170cb66 83 static unsigned int cnt_numD = 0;
Stanislaus 0:9ce7c170cb66 84
Stanislaus 0:9ce7c170cb66 85 myLEDTaskD = !myLEDTaskD.read();
Stanislaus 0:9ce7c170cb66 86 cnt_numD++;
Stanislaus 0:9ce7c170cb66 87
Stanislaus 0:9ce7c170cb66 88 return true;
Stanislaus 0:9ce7c170cb66 89 }
Stanislaus 0:9ce7c170cb66 90
Stanislaus 0:9ce7c170cb66 91 // *******************************************************************************
Stanislaus 0:9ce7c170cb66 92 // * @brief "taskE" task code
Stanislaus 0:9ce7c170cb66 93 // * @param[in] None
Stanislaus 0:9ce7c170cb66 94 // * @param[out] None
Stanislaus 0:9ce7c170cb66 95 // * @retval bool
Stanislaus 0:9ce7c170cb66 96 // * @par Description
Stanislaus 0:9ce7c170cb66 97 // * @details This task use to print message "taskE running". Indicate "taskE"
Stanislaus 0:9ce7c170cb66 98 // * had been executed.
Stanislaus 0:9ce7c170cb66 99 // *******************************************************************************
Stanislaus 0:9ce7c170cb66 100 bool taskE(void* pData)
Stanislaus 0:9ce7c170cb66 101 {
Stanislaus 0:9ce7c170cb66 102 static unsigned int cnt_numE = 0;
Stanislaus 0:9ce7c170cb66 103
Stanislaus 0:9ce7c170cb66 104 myLEDTaskE = !myLEDTaskE.read();
Stanislaus 0:9ce7c170cb66 105 cnt_numE++;
Stanislaus 0:9ce7c170cb66 106
Stanislaus 0:9ce7c170cb66 107 return true;
Stanislaus 0:9ce7c170cb66 108 }
Stanislaus 0:9ce7c170cb66 109
Stanislaus 0:9ce7c170cb66 110 // *******************************************************************************
Stanislaus 0:9ce7c170cb66 111 // * @brief "taskF" task code
Stanislaus 0:9ce7c170cb66 112 // * @param[in] None
Stanislaus 0:9ce7c170cb66 113 // * @param[out] None
Stanislaus 0:9ce7c170cb66 114 // * @retval bool
Stanislaus 0:9ce7c170cb66 115 // * @par Description
Stanislaus 0:9ce7c170cb66 116 // * @details This task use to print message "taskF running". Indicate "taskF"
Stanislaus 0:9ce7c170cb66 117 // * had been executed.
Stanislaus 0:9ce7c170cb66 118 // *******************************************************************************
Stanislaus 0:9ce7c170cb66 119 bool taskF(void* pData)
Stanislaus 0:9ce7c170cb66 120 {
Stanislaus 0:9ce7c170cb66 121 static unsigned int cnt_numF = 0;
Stanislaus 0:9ce7c170cb66 122
Stanislaus 0:9ce7c170cb66 123 cnt_numF++; // task is only running for 30 seconds -> cnt_numF has the value 5 than
Stanislaus 0:9ce7c170cb66 124
Stanislaus 0:9ce7c170cb66 125 return true;
Stanislaus 0:9ce7c170cb66 126 }
Stanislaus 0:9ce7c170cb66 127
Stanislaus 0:9ce7c170cb66 128 // *******************************************************************************
Stanislaus 0:9ce7c170cb66 129 // * @brief "runningTasks" a Ticker routine
Stanislaus 0:9ce7c170cb66 130 // * @param[in] None
Stanislaus 0:9ce7c170cb66 131 // * @param[out] None
Stanislaus 0:9ce7c170cb66 132 // * @retval None
Stanislaus 0:9ce7c170cb66 133 // * @par Description
Stanislaus 0:9ce7c170cb66 134 // * @details This method is used to keep the scheduler alife
Stanislaus 0:9ce7c170cb66 135 // *
Stanislaus 0:9ce7c170cb66 136 // *******************************************************************************
Stanislaus 0:9ce7c170cb66 137 void runningTasks()
Stanislaus 0:9ce7c170cb66 138 {
Stanislaus 0:9ce7c170cb66 139 // you can do this here in running Tasks or in the main routine while loop -> see below
Stanislaus 0:9ce7c170cb66 140 schedulerRun(t.read_ms());
Stanislaus 0:9ce7c170cb66 141 }
Stanislaus 0:9ce7c170cb66 142
Stanislaus 0:9ce7c170cb66 143 int main()
Stanislaus 0:9ce7c170cb66 144 {
Stanislaus 0:9ce7c170cb66 145 schedulerInit(); // init and start scheduler
Stanislaus 0:9ce7c170cb66 146
Stanislaus 0:9ce7c170cb66 147 t.start(); // start task timer
Stanislaus 0:9ce7c170cb66 148
Stanislaus 0:9ce7c170cb66 149 int handleTaskA = schedulerAddTask(t.read_ms(), 0, 500, (taskfuncptr) taskA); // Task A is switching LED1 on
Stanislaus 0:9ce7c170cb66 150 int handleTaskB = schedulerAddTask(t.read_ms(), 0, 1000, (taskfuncptr) taskB); // Task B is switching LED1 off
Stanislaus 0:9ce7c170cb66 151 int handleTaskC = schedulerAddTask(t.read_ms(), 0, 200, (taskfuncptr) taskC); // Task C is switching LED2 on and off - every 200 ms -> five times per sec.
Stanislaus 0:9ce7c170cb66 152 int handleTaskD = schedulerAddTask(t.read_ms(), 0, 100, (taskfuncptr) taskD); // Task D is switching LED3 on anf off - every 100 ms -> ten times per sec.
Stanislaus 0:9ce7c170cb66 153 int handleTaskE = schedulerAddTask(t.read_ms(), 0, 500, (taskfuncptr) taskE); // Task E is switching LED4 on and off - vice versa to LED1 -> starting with on
Stanislaus 0:9ce7c170cb66 154 int handleTaskF = schedulerAddTask(t.read_ms(), 6, 5000, (taskfuncptr) taskF); // Task F finished after 30 seconds
Stanislaus 0:9ce7c170cb66 155
Stanislaus 0:9ce7c170cb66 156
Stanislaus 0:9ce7c170cb66 157 // the address of the function to be attached (runTasks) and the interval (,5 micro seconds)
Stanislaus 0:9ce7c170cb66 158 keepAlive.attach_us(&runningTasks, 500);
Stanislaus 0:9ce7c170cb66 159
Stanislaus 0:9ce7c170cb66 160 while (1)
Stanislaus 0:9ce7c170cb66 161 {
Stanislaus 0:9ce7c170cb66 162 //// we can feed the scheduler here, too
Stanislaus 0:9ce7c170cb66 163 ////schedulerRun(t.read_ms());
Stanislaus 0:9ce7c170cb66 164 ////wait(0.001); //wait one ms
Stanislaus 0:9ce7c170cb66 165 // with a short delay we can power down the controller here...
Stanislaus 0:9ce7c170cb66 166 // or poll very fast variables that are set in interrupt service routines (ISR's) etc.
Stanislaus 0:9ce7c170cb66 167 }
Stanislaus 0:9ce7c170cb66 168 // The code don't reach here -> if so, a great mess occured
Stanislaus 0:9ce7c170cb66 169 }
Stanislaus 0:9ce7c170cb66 170