Main Page | Modules | Data Structures | Directories | File List | Data Fields | Globals | Related Pages

# scheduler.c

Go to the documentation of this file.

    00001 /*! \file scheduler.c \brief Simple Task Scheduler. */
    00002 //*****************************************************************************
    00003 //
    00004 // File Name    : 'scheduler.c'
    00005 // Title        : Simple Task Scheduler
    00006 // Author       : Pascal Stang - Copyright (C) 2006
    00007 // Created      : 2006.05.24
    00008 // Revised      : 2006.05.26
    00009 // Version      : 0.1
    00010 // Target MCU   : Atmel AVR Series
    00011 // Editor Tabs  : 4
    00012 //
    00013 // NOTE: This code is currently below version 1.0, and therefore is considered
    00014 // to be lacking in some functionality or documentation, or may not be fully
    00015 // tested.  Nonetheless, you can expect most functions to work.
    00016 //
    00017 // This code is distributed under the GNU Public License
    00018 //      which can be found at http://www.gnu.org/licenses/gpl.txt
    00019 //
    00020 //*****************************************************************************
    00021
    00022 #include "global.h"
    00023 #include "processor.h"
    00024 #include "scheduler.h"
    00025
    00026 // the task list
    00027 struct task TaskList[TASKLIST_SIZE];
    00028
    00029 // functions
    00030 void schedulerInit(void)
    00031 {
    00032     int i;
    00033
    00034     // reset task list
    00035     for(i=0; i<TASKLIST_SIZE; i++)
    00036     {
    00037         // initialize task list
    00038         TaskList[i].runtime = 0;
    00039         TaskList[i].funcptr = 0;
    00040     }
    00041 }
    00042
    00043 void schedulerRun(int systime)
    00044 {
    00045     int i;
    00046
    00047     // locate active task slots
    00048     for(i=0; i<TASKLIST_SIZE; i++)
    00049     {
    00050         if(TaskList[i].funcptr)
    00051         {
    00052             // check systime against runtime
    00053             if(systime >= TaskList[i].runtime)
    00054             {
    00055                 // run task, then update it
    00056                 TaskList[i].funcptr(i,systime);
    00057                 if(TaskList[i].nrepeat)
    00058                 {
    00059                     TaskList[i].runtime += TaskList[i].interval;
    00060                     TaskList[i].nrepeat--;
    00061                     // task has expired, remove it now
    00062                     if(!TaskList[i].nrepeat)
    00063                         schedulerRemoveTask(i);
    00064                 }
    00065                 else
    00066                 {
    00067                     // nrepeat==0 means run forever
    00068                     TaskList[i].runtime += TaskList[i].interval;
    00069                 }
    00070
    00071             }
    00072         }
    00073     }
    00074
    00075     // increment scheduler timer
    00076     //SchedulerTimer++;
    00077 }
    00078
    00079 int schedulerAddTask(int runtime, int nrepeat, int interval, taskfuncptr taskfunc)
    00080 {
    00081     int i;
    00082     // locate empty scheduler slot
    00083     for(i=0; i<TASKLIST_SIZE; i++)
    00084     {
    00085         CRITICAL_SECTION_BEGIN;
    00086         if(!TaskList[i].funcptr)
    00087         {
    00088             // setup entry
    00089             TaskList[i].funcptr = taskfunc;
    00090             TaskList[i].runtime = runtime;
    00091             TaskList[i].nrepeat = nrepeat;
    00092             TaskList[i].interval = interval;
    00093             // return task handle
    00094             CRITICAL_SECTION_END;
    00095             return i;
    00096         }
    00097         CRITICAL_SECTION_END;
    00098     }
    00099     // error - no task slots left
    00100     return -1;
    00101 }
    00102
    00103 int schedulerRemoveTask(int taskhandle)
    00104 {
    00105     // clear the task entry
    00106     TaskList[taskhandle].runtime = 0;
    00107     TaskList[taskhandle].interval = 0;
    00108     TaskList[taskhandle].nrepeat = 0;
    00109     TaskList[taskhandle].funcptr = 0;
    00110     return taskhandle;
    00111 }
    00112
    00113 int schedulerRemoveTaskFunc(taskfuncptr funcptr)
    00114 {
    00115     int i;
    00116     int taskhandle = -1;
    00117     // locate the task
    00118     for(i=0; i<TASKLIST_SIZE; i++)
    00119     {
    00120         // on match, disable the task
    00121         if(TaskList[i].funcptr == funcptr)
    00122             taskhandle = schedulerRemoveTask(i);
    00123     }
    00124     return taskhandle;
    00125 }