circuitcellar.com
Magazine Support   Digital Library   Products & Services   Suppliers Directory 
 
 





 

December 1998, Issue 101

A Minimalist Multitasking Executive


by Richard Man & Christina Willrich
Start Task Function Scheduling and Timeslice Task Context Task Control Block Global Variables Memory Management API Assembly Functions Give it a Try Enhancements References, Sources & PDF

SCHEDULING AND TIMESLICE

The scheduler chooses which task to run. The period of time a task is allowed to run is called its timeslice.

Under 畫xec, tasks are run to completion of their timeslices unless they are explicitly yielding control to the system or hogging the processor for another timeslice. Although 畫xec doesn’t assign priorities, tasks can have different timeslice lengths, so you can cause a compute-intensive task to run longer if necessary.

The main purpose of the 畫xec control system, then, is to interrupt the processor at the end of a task’s timeslice and invoke the scheduler to choose another task to run. 畫xec uses a timer to interrupt the CPU at regular intervals.

Almost all microcontrollers have timer interrupt functions that can be used for this purpose. For example, on the ’HC11, there are five timer-registers, each one of which can be set up to generate an interrupt when the timer-register’s value matches the value of the free-running timer counter.

The time period when such an interrupt occurs is called a tick, and its timeslice is some multiple of ticks. A tick’s value should be chosen such that the processor is not interrupted too frequently, but it shouldn’t be so long that other tasks do not get to execute in a timely manner.

For the ’HC11, we chose a value of 2000 cycles per tick, or about 25 盜 for a ’HC11 running with an 8-MHz clock. The default timeslice is five ticks, or 125 盜. You may want different values for your system.

INTERRUPT DRIVER

Sometimes your program may need to perform some function at a regular interval, and task scheduling may be too slow and unpredictable. 畫xec gives a hook to the timer-interrupt handler—if the global variable UEXC_InterruptDiver is nonnull, then it’s assumed to contain the address of a function.

The system timer-interrupt handler then calls this function before it performs other work. To use this feature, assign the address of your function to be called to this variable.