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

ASSEMBLY FUNCTIONS

These functions need to be modified when porting µexec or if you choose to use a different timer counter instead of TOC4 (Timer Output Compare 4) under ’HC11. They don’t take any arguments or return any value, which should make them easy to port. In total, there are less than 50 assembly source lines, 10 of which deal with the timer registers and can actually be written in C.

UEXC_SystemInterrupt is the timer interrupt handler. After storing the stack pointer to the global variable uexc_current_sp, it calls a function to see if the current task’s timeslice has run out.

If it has, then a new task is scheduled and control transfers to the new task. If the timeslice has not run out, handler returns via ReturnFromInterrupt.

You must arrange for this handler to be invoked for the appropriate timer interrupt. In most cases, this means putting the address of this function in the interrupt vector table. If you are using a debug monitor, typically the monitor provides a RAM-based pseudo-vector table.

The function UEXC_SavregsAndResched is called by UEXC_Defer to create a fake interrupt stack.

UEXC_StartNewTask is called to run a new task. The stack is set up so that if the task function ever returns, then an internal function, UEXC_KillSelf, is called. UEXC_KillSelf is equivalent to UEXC_KillTask (current_task->tid).

The UEXC_Resume is a function that gets the stack pointer from uexc_ current_sp and does a ReturnFromInterrupt.

The function UEXC_StartTimer can be written in C, but we put it in the set of assembler functions because it is microcontroller specific. It needs to set the timer registers to enable the timer interrupt at INTERRUPT_CYCLES later.

TOC4 is used in the supplied code. Typically, a TOC function can be associated with an output pin, but in this case, the output pin function is disabled.

The value given by INTERRUPT_ CYCLES is the system tick and must be adjusted for different system clock speeds. As supplied, it is defined to be 2000 (cycles).

UEXC_RestartTimer can also be written in C. It simply reenables the timer interrupt at INTERRUPT_CYCLES later.