01.06.2013 Views

RTOS Program Models Used in Embedded Systems

RTOS Program Models Used in Embedded Systems

RTOS Program Models Used in Embedded Systems

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

J. Kopják et al. <strong>RTOS</strong> <strong>Program</strong> <strong>Models</strong> <strong>Used</strong> <strong>in</strong> <strong>Embedded</strong> <strong>Systems</strong><br />

<strong>in</strong>t ma<strong>in</strong> ( void )<br />

{<br />

vInitSystem();// Initialization of peripherals<br />

while(1) // Inf<strong>in</strong>itive function call<strong>in</strong>g loop<br />

{<br />

vTask1(); // Call<strong>in</strong>g task function named vTask1<br />

vTask2(); // Call<strong>in</strong>g task function named vTask2<br />

vTask3(); // Call<strong>in</strong>g task function named vTask3<br />

}<br />

}<br />

Develop<strong>in</strong>g source code <strong>in</strong> cooperative multitask model required more preparatory<br />

plann<strong>in</strong>g time than sequential solution. The designer has to divide the whole<br />

program-task to small <strong>in</strong>dividual work<strong>in</strong>g tasks <strong>in</strong> a first phase of development.<br />

Each <strong>in</strong>dividual work<strong>in</strong>g task is communicat<strong>in</strong>g with each other on global<br />

variables or with another name: via mailboxes. The cooperat<strong>in</strong>g task functions<br />

usually read its <strong>in</strong>puts values at the beg<strong>in</strong>n<strong>in</strong>g and writ<strong>in</strong>g they output values at<br />

end of each runn<strong>in</strong>g sequence. The program scans its <strong>in</strong>puts and updates its<br />

outputs values several times dur<strong>in</strong>g a one circle <strong>in</strong> the ma<strong>in</strong> loop.<br />

The task should complete them runn<strong>in</strong>g sequence as soon as possible to got short<br />

program response time. The task should not use block<strong>in</strong>g or wait<strong>in</strong>g loops. Us<strong>in</strong>g<br />

block<strong>in</strong>g loops would drastic <strong>in</strong>cise the response time of runn<strong>in</strong>g system. Some<br />

block<strong>in</strong>g loops examples: Wait<strong>in</strong>g for releas<strong>in</strong>g a push button or wait<strong>in</strong>g for an<br />

UART peripheral register flag. The tasks have to remember the current state of<br />

runn<strong>in</strong>g and return back to the call<strong>in</strong>g function <strong>in</strong> case when a resource is not<br />

available.<br />

The task functions should be structured based on state mach<strong>in</strong>e model due to lack<br />

of a block<strong>in</strong>g waits. In state mach<strong>in</strong>e model the functions are us<strong>in</strong>g static local<br />

variables to store them next states. The backbone of each task function is a switchcase<br />

structure. The task first exam<strong>in</strong>es the value of the state description variable,<br />

and then jumps to the program code of the state. Follow<strong>in</strong>g piece of C-language<br />

source code illustrates the body of task function based on state mach<strong>in</strong>e model.<br />

void vTask( void )<br />

{<br />

/* State description variable. Initial state is:<br />

INIT_STATE */<br />

static enum { INIT_STATE, STATE1, STATE2, STATE3 }<br />

xState = INIT_STATE;<br />

/* Selection of sub-task based on the value of state<br />

variable */<br />

switch ( xState )<br />

{<br />

case INIT_STATE: // Initialization tasks<br />

/* ... */<br />

xState = STATE1; // Selection of next state<br />

break; // End of actual runn<strong>in</strong>g<br />

case STATE1: // First task<br />

/* ... */<br />

xState = STATE2; // Selection of next state<br />

break; // End of actual runn<strong>in</strong>g<br />

/* ... */<br />

default: // Protection state:<br />

– 164 –

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!