Premium Content

New Signal Chain Resources from Texas Instruments:

EiED Online>> Single-Stack RTOS

Date Posted: December 06, 2004 12:00 AM
Author: William Wong

How A Single-Stack Operating System Works
Programmers that have dealt with embedded microcontrollers without an operating system have probably implemented their own simple single-stack operating system. It could look as simple as the following code:

	void main () {
		for (;;) {
			try () {
			task1();
				task2();
				task3();
			}
catch ( exception e )
{}
		}
	}

The stack usage includes the minimal operating system support and each task/function uses the stack as necessary and returns upon completion (Fig. 1). Of course, this assumes that all tasks run each cycle. A more dynamic implementation such as the following employs a scheduler that allows tasks to wait on semaphores or other interprocess communication primitives.

void main () {
		for (;;) {
			try () {
			scheduler();
			}
catch ( exception e )
{}
		}
	}

This approach allows a more arbitrary execution graph (Fig. 2). There are a variety of simple ways to implement a scheduler. The scheduler does not have to worry about being interrupted by another task and execution is simply a matter of calling the appropriate function. This might be accomplished via a table or list of function pointers. Some very basic 8-bit microcontrollers may not be able to do this. In the latter case, a scheduler could use a task dispatch function such as the following:

void dispatch (int n) {
		switch (n) {
		case 1: task1();break;
		case 2: task2();break;
		case 3: task3();break;
	}
	}

Of course, the task functions must be predefined. However, this is a minor issue for an embedded system where tasks are rarely downloaded for execution.

Programming Differences
Programmers will notice a couple of differences when dealing with a single-stack operating system. Task creation is different. Sometimes it requires defining a dispatch function or table and there is obviously no stack allocation required. Also, more complex tasks are typically implemented using a state machine approach. Remember, you cannot simply wait on a semaphore and continue executing after the wait function returns as in a multiple-stack operating system. Instead, a state will typically perform some operations, call an operating system primitive to setup the task to be restarted when an event occurs, and then return. Alternatively, the task may perform part of its function, change its state, and then return, allowing other tasks to run. The task steps through each state as it gets another turn at the processor, such as in the following example:

void task1() {
		static int state = 0 ;
		switch (state) {
		case 0: state0();state=1;break;
		case 1: state1();state=2;break;
		case 2: state2();state=3;break;
		case 3: state3();state=0;break;
	}
	}

The next time you consider a system design, look at single-stack operating systems. It takes a slightly different mind set to utilize but the pay off can be considerable. It may also allow a simpler processor to handle an application.

Although not always the best solution, single-stack operating systems are sometimes the right choice. Single-stack operating systems have found a home in a wide variety of applications from digital signal processing to robotics.

Related Links
Quadros
www.quadros.com

OSEK/VDX
www.osek-vdx.org

microcontrollers
Part Inventory
Go
powered by:
 

 
You must log on before posting a comment.

Are you a new visitor? Register Here
    There are no comments to display. Be the first one!