Micro RTOS Scheduler

The Micro RTOS scheduler is basically implemented like a smart function that gets called from one task but returns control to another task. The scheduler does this by using the following:
  1. The scheduler checks if a task having priority higher than current is available and ready. If no such task is available the control returns to the current task normally.
  2. If a higher priority ready task is available then the current tasks registers are saved into the current stack.
  3. The TOS value (i.e. SP value) is saved into the TCB of the current task.
  4. The address of TOS of new stack is obtained from the TCB of the new task (the TCB of the new task in turn can be located using its priority number).
  5. The SP value is replaced by the new tasks TOS value.
  6. The function now returns in the usual way, but as the stack has changed so the control returns to the new task and not the old one.

In order to implement task scheduling in real time, it is necessary to write OS code so that the scheduler is called as soon as a task relinquishes control of the processor (by sleeping) or whenever a new task is created. Thus whenever a task calls a kernel function, the control is passed by the called function to the scheduling function (i.e. Scheduler) before returning to user code, so that the priority structure is honoured, and higher priority tasks can be scheduled immediately without delay. Also the user can write ISRs so that the scheduler is called from within them. This causes context switches even when returning from ISRs.

There are two functions OS_Sched and OS_Interrupt_Exit both of which behave in the same way as described above. OS_Sched is always called from within kernel functions like Sleep, CreateTask etc. Correspondingly, OS_Interrupt_Exit is always called from within an ISR and so does not save CPU context as this has already been done by the ISR.

Back to DocumentationIndex

There are no comments on this page.
Creative Commons License
Micro RTOS Documentation by Aliasgar Lokhandwala is licensed under a Creative Commons Attribution-Non-Commercial-Share Alike 3.0 License.
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki