om-Scheduler
(c) 2004 Stephan Le Meunier 

Scheduler for the 8051 architecture.

om-sched.c: scheduler source
om-sched.h: scheduler prototypes
test.c    : example programm (main)
makefile  : to compile (first take a look at the 'stack-loc' option)

Features:
- om-sched provides a REAL multitask system
  Multiple functions (Tasks) can be run asynchronously.
- Task priority levels 0-255
- P() and V() semaphore used to access shared resources
- nextTask() for premature task switching

Requirements:
- enough extra ram
- sdcc compiler

Extra functions:
P():disables timer0 to access shared ressources
V():enables timer 0 after accessed shared ressources
nextTask():switch to next task

The idea:
The scheduler uses the timer0 interrupt to switch the task.
When a interrupt occures, the interrupt routine first saves all important registers onto the stack.
The interrupt routine (SchedulerHandler) itself just copy the hole stack,stack pointer and frame pointer in the extra ram.
That way the context of the running task is saved.
Then the SchedulerHandler select the new task to run by the highest priority.
It loads the before saved stack,stack pointer and frame pointer of the new task.
When leaving the interrupt routine (reti) it restores the registers of the new task from the changed stack and returns to the new task.
So each time the interrupt routine returns (reti), the tasks are switched.
An exception is the first call of a task function when there is no stack to load yet (look onto the end of SchedulerHandler() and mkjump()).
For better understanding you have to look at the generated asm file.


Be aware:
The interrupt service routine (SchedulerHandler) should not be interrupted while manipulating the stack.
Also the interrupt service routine (SchedulerHandler) should not interrupt another interrupt routine.
Therefore when using other interrupts, modifie the source code or just put them all in the same priority level.
Modifie the defines MAX_TASK_NB and STACK_SIZE for your controler and be sure the stack becomes not too big.



Good luck!!!






