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 /*! \file scheduler.c \brief Simple Task Scheduler. */
Stanislaus 0:9ce7c170cb66 2 //*****************************************************************************
Stanislaus 0:9ce7c170cb66 3 //
Stanislaus 0:9ce7c170cb66 4 // File Name : 'scheduler.c'
Stanislaus 0:9ce7c170cb66 5 // Title : Simple Task Scheduler
Stanislaus 0:9ce7c170cb66 6 // Author : Pascal Stang - Copyright (C) 2006
Stanislaus 0:9ce7c170cb66 7 // Created : 2006.05.24
Stanislaus 0:9ce7c170cb66 8 // Revised : 2006.05.26
Stanislaus 0:9ce7c170cb66 9 // Version : 0.1
Stanislaus 0:9ce7c170cb66 10 // Target MCU : Atmel AVR Series
Stanislaus 0:9ce7c170cb66 11 // Editor Tabs : 4
Stanislaus 0:9ce7c170cb66 12 //
Stanislaus 0:9ce7c170cb66 13 // NOTE: This code is currently below version 1.0, and therefore is considered
Stanislaus 0:9ce7c170cb66 14 // to be lacking in some functionality or documentation, or may not be fully
Stanislaus 0:9ce7c170cb66 15 // tested. Nonetheless, you can expect most functions to work.
Stanislaus 0:9ce7c170cb66 16 //
Stanislaus 0:9ce7c170cb66 17 // This code is distributed under the GNU Public License
Stanislaus 0:9ce7c170cb66 18 // which can be found at http://www.gnu.org/licenses/gpl.txt
Stanislaus 0:9ce7c170cb66 19 //
Stanislaus 0:9ce7c170cb66 20 //*****************************************************************************
Stanislaus 0:9ce7c170cb66 21
Stanislaus 0:9ce7c170cb66 22 #include "scheduler.h"
Stanislaus 0:9ce7c170cb66 23
Stanislaus 0:9ce7c170cb66 24 // the task list
Stanislaus 0:9ce7c170cb66 25 struct task TaskList[TASKLIST_SIZE];
Stanislaus 0:9ce7c170cb66 26
Stanislaus 0:9ce7c170cb66 27 // functions
Stanislaus 0:9ce7c170cb66 28 void schedulerInit(void)
Stanislaus 0:9ce7c170cb66 29 {
Stanislaus 0:9ce7c170cb66 30 int i;
Stanislaus 0:9ce7c170cb66 31
Stanislaus 0:9ce7c170cb66 32 // reset task list
Stanislaus 0:9ce7c170cb66 33 for(i=0; i<TASKLIST_SIZE; i++)
Stanislaus 0:9ce7c170cb66 34 {
Stanislaus 0:9ce7c170cb66 35 // initialize task list
Stanislaus 0:9ce7c170cb66 36 TaskList[i].runtime = 0;
Stanislaus 0:9ce7c170cb66 37 TaskList[i].funcptr = 0;
Stanislaus 0:9ce7c170cb66 38 }
Stanislaus 0:9ce7c170cb66 39 }
Stanislaus 0:9ce7c170cb66 40
Stanislaus 0:9ce7c170cb66 41 void schedulerRun(int systime)
Stanislaus 0:9ce7c170cb66 42 {
Stanislaus 0:9ce7c170cb66 43 int i;
Stanislaus 0:9ce7c170cb66 44
Stanislaus 0:9ce7c170cb66 45 // locate active task slots
Stanislaus 0:9ce7c170cb66 46 for(i=0; i<TASKLIST_SIZE; i++)
Stanislaus 0:9ce7c170cb66 47 {
Stanislaus 0:9ce7c170cb66 48 if(TaskList[i].funcptr)
Stanislaus 0:9ce7c170cb66 49 {
Stanislaus 0:9ce7c170cb66 50 // check systime against runtime
Stanislaus 0:9ce7c170cb66 51 if(systime >= TaskList[i].runtime)
Stanislaus 0:9ce7c170cb66 52 {
Stanislaus 0:9ce7c170cb66 53 // run task, then update it
Stanislaus 0:9ce7c170cb66 54 TaskList[i].funcptr(i,systime);
Stanislaus 0:9ce7c170cb66 55
Stanislaus 0:9ce7c170cb66 56 if(TaskList[i].nrepeat)
Stanislaus 0:9ce7c170cb66 57 {
Stanislaus 0:9ce7c170cb66 58 TaskList[i].runtime += TaskList[i].interval;
Stanislaus 0:9ce7c170cb66 59 TaskList[i].nrepeat--;
Stanislaus 0:9ce7c170cb66 60 // task has expired, remove it now
Stanislaus 0:9ce7c170cb66 61 if(!TaskList[i].nrepeat)
Stanislaus 0:9ce7c170cb66 62 schedulerRemoveTask(i);
Stanislaus 0:9ce7c170cb66 63 }
Stanislaus 0:9ce7c170cb66 64 else
Stanislaus 0:9ce7c170cb66 65 {
Stanislaus 0:9ce7c170cb66 66 // nrepeat==0 means run forever
Stanislaus 0:9ce7c170cb66 67 TaskList[i].runtime += TaskList[i].interval;
Stanislaus 0:9ce7c170cb66 68 }
Stanislaus 0:9ce7c170cb66 69
Stanislaus 0:9ce7c170cb66 70 }
Stanislaus 0:9ce7c170cb66 71 }
Stanislaus 0:9ce7c170cb66 72 }
Stanislaus 0:9ce7c170cb66 73
Stanislaus 0:9ce7c170cb66 74 // increment scheduler timer
Stanislaus 0:9ce7c170cb66 75 //SchedulerTimer++;
Stanislaus 0:9ce7c170cb66 76 }
Stanislaus 0:9ce7c170cb66 77
Stanislaus 0:9ce7c170cb66 78 int schedulerAddTask(int runtime, int nrepeat, int interval, taskfuncptr taskfunc)
Stanislaus 0:9ce7c170cb66 79 {
Stanislaus 0:9ce7c170cb66 80 int i;
Stanislaus 0:9ce7c170cb66 81
Stanislaus 0:9ce7c170cb66 82 // locate empty scheduler slot
Stanislaus 0:9ce7c170cb66 83 for(i=0; i<TASKLIST_SIZE; i++)
Stanislaus 0:9ce7c170cb66 84 {
Stanislaus 0:9ce7c170cb66 85 //CRITICAL_SECTION_BEGIN;
Stanislaus 0:9ce7c170cb66 86 if(!TaskList[i].funcptr)
Stanislaus 0:9ce7c170cb66 87 {
Stanislaus 0:9ce7c170cb66 88 // setup entry
Stanislaus 0:9ce7c170cb66 89 TaskList[i].funcptr = taskfunc;
Stanislaus 0:9ce7c170cb66 90 TaskList[i].runtime = runtime;
Stanislaus 0:9ce7c170cb66 91 TaskList[i].nrepeat = nrepeat;
Stanislaus 0:9ce7c170cb66 92 TaskList[i].interval = interval;
Stanislaus 0:9ce7c170cb66 93
Stanislaus 0:9ce7c170cb66 94 // return task handle
Stanislaus 0:9ce7c170cb66 95 return i;
Stanislaus 0:9ce7c170cb66 96 }
Stanislaus 0:9ce7c170cb66 97 //CRITICAL_SECTION_END;
Stanislaus 0:9ce7c170cb66 98 }
Stanislaus 0:9ce7c170cb66 99 // error - no task slots left
Stanislaus 0:9ce7c170cb66 100 return -1;
Stanislaus 0:9ce7c170cb66 101 }
Stanislaus 0:9ce7c170cb66 102
Stanislaus 0:9ce7c170cb66 103 int schedulerRemoveTask(int taskhandle)
Stanislaus 0:9ce7c170cb66 104 {
Stanislaus 0:9ce7c170cb66 105 // clear the task entry
Stanislaus 0:9ce7c170cb66 106 TaskList[taskhandle].runtime = 0;
Stanislaus 0:9ce7c170cb66 107 TaskList[taskhandle].interval = 0;
Stanislaus 0:9ce7c170cb66 108 TaskList[taskhandle].nrepeat = 0;
Stanislaus 0:9ce7c170cb66 109 TaskList[taskhandle].funcptr = 0;
Stanislaus 0:9ce7c170cb66 110
Stanislaus 0:9ce7c170cb66 111 return taskhandle;
Stanislaus 0:9ce7c170cb66 112 }
Stanislaus 0:9ce7c170cb66 113
Stanislaus 0:9ce7c170cb66 114 int schedulerRemoveTaskFunc(taskfuncptr funcptr)
Stanislaus 0:9ce7c170cb66 115 {
Stanislaus 0:9ce7c170cb66 116 int i;
Stanislaus 0:9ce7c170cb66 117 int taskhandle = -1;
Stanislaus 0:9ce7c170cb66 118
Stanislaus 0:9ce7c170cb66 119 // locate the task
Stanislaus 0:9ce7c170cb66 120 for(i=0; i<TASKLIST_SIZE; i++)
Stanislaus 0:9ce7c170cb66 121 {
Stanislaus 0:9ce7c170cb66 122 // on match, disable the task
Stanislaus 0:9ce7c170cb66 123 if(TaskList[i].funcptr == funcptr) taskhandle = schedulerRemoveTask(i);
Stanislaus 0:9ce7c170cb66 124 }
Stanislaus 0:9ce7c170cb66 125 return taskhandle;
Stanislaus 0:9ce7c170cb66 126 }