17.04.2014 Views

Chapter 1 Sample Code Real-Time Programming Two Types of ...

Chapter 1 Sample Code Real-Time Programming Two Types of ...

Chapter 1 Sample Code Real-Time Programming Two Types of ...

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

• A typical real-time multi-tasking program<br />

main( )<br />

{<br />

...<br />

subroutine1( ... );<br />

...<br />

subroutine2( ... );<br />

...<br />

subroutine3( ... );<br />

...<br />

}<br />

Cyclic Executive<br />

execute<br />

every<br />

10 ms<br />

execute<br />

whenever<br />

the CPU<br />

is available<br />

give up<br />

CPU time<br />

main( )<br />

{<br />

...<br />

create( task1, 10, ... );<br />

create( task2, 20, ... );<br />

create( task3, -1, ... );<br />

...<br />

} ....<br />

task2()<br />

{ ....<br />

if( ...) suspend( );<br />

...<br />

}<br />

Multi-tasking<br />

1.04 OS_Enter_Critical() and<br />

OS_Exit_Critical()<br />

#define OS_CRITICAL_METHOD 2<br />

#if OS_CRITICAL_METHOD == 1<br />

#define OS_ENTER_CRITICAL() asm CLI<br />

#define OS_EXIT_CRITICAL() asm STI<br />

#endif<br />

#if OS_CRITICAL_METHOD == 2<br />

#define OS_ENTER_CRITICAL() asm {PUSHF; CLI}<br />

#define OS_EXIT_CRITICAL() asm POPF<br />

#endif<br />

• Disabling interrupts affects interrupt latency, so be<br />

carefully<br />

5<br />

7<br />

Task Scheduling (which one can be serviced<br />

by CPU)<br />

• Round-robin and time-slicing<br />

– What is the difference?<br />

Task No<br />

1<br />

2<br />

3<br />

Task No<br />

1<br />

2<br />

3<br />

(a)<br />

(b)<br />

<strong>Time</strong><br />

<strong>Time</strong><br />

Round-robin<br />

<strong>Time</strong>-sliced<br />

(More fairness)<br />

a quantum<br />

6<br />

1.07 Example 1<br />

• Consist 13 tasks<br />

– <strong>Two</strong> internal tasks: the idle task and a task that determines CPU<br />

usage<br />

– Create 11 tasks: the TaskStart() task is created by main()<br />

– Other 10 tasks is create by TaskStart() task. This 10 tasks are based<br />

on the same code<br />

void main (void)<br />

{<br />

PC_DispClrScr(DISP_FGND_WHITE + DISP_BGND_BLACK); (1)<br />

OSInit(); (2)<br />

PC_DOSSaveReturn(); (3)<br />

PC_VectSet(uCOS, OSCtxSw); // INT 80H 200HZ (4)<br />

RandomSem = OSSemCreate(1); (5)<br />

OSTaskCreate(TaskStart, (6)<br />

(void *)0,<br />

(void *)&TaskStartStk[TASK_STK_SIZE-1],<br />

0);<br />

OSStart(); (7)<br />

}<br />

– OSInit () creates two tasks : an idle task, which executes when no<br />

other task is ready to run and a statistic task, which computes CPU<br />

usage<br />

8

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

Saved successfully!

Ooh no, something went wrong!