An Introduction to Real Time Operating System with UCOS-II

23
1 J.O. KLEIN [email protected] Institut Universitaire de Technologie de CACHAN Université PARIS SUD - FRANCE An Introduction to Real Time Operating System with UCOS-II www.micrium.com Jean Labrosse Portable Royalty Free Readable Source Simple Well Suited for MCU Well documented

description

Simple. Portable. Royalty Free. Readable Source. Well Suited for MCU. Well documented. Institut Universitaire de Technologie de CACHAN Université PARIS SUD - FRANCE. An Introduction to Real Time Operating System with UCOS-II. www.micrium.com. J.O. KLEIN [email protected]. - PowerPoint PPT Presentation

Transcript of An Introduction to Real Time Operating System with UCOS-II

Page 1: An Introduction  to  Real Time Operating System  with  UCOS-II

1J.O. KLEIN [email protected]

Institut Universitaire de Technologie de CACHANUniversité PARIS SUD - FRANCE

An Introduction to Real Time Operating System

with UCOS-II

www.micrium.com

Jean Labrosse

Portable

Royalty Free

Readable Source

Simple

Well Suited for MCU

Well documented

Page 2: An Introduction  to  Real Time Operating System  with  UCOS-II

2

Background / InterruptModel Limitation

INT

Background

Loop …

TimeBCKGND

INT 1

ISR

ISR

ISRINT

INT

INT 0

ISR < 100 us

ISR > 1 ms Latency

ISR 0

INT 1-Pending

Page 3: An Introduction  to  Real Time Operating System  with  UCOS-II

3

RTOS-Solution

ISR1

TimeIDLE

INT 1

INT 0

Task 1

ISR0

ISR1

Task 1

Task 1

ISR0

Page 4: An Introduction  to  Real Time Operating System  with  UCOS-II

4

Practical Timing Ranges

RTOS Tick = Time unit for delay & timeout

1 us 1 ms 1 s

ISR

RT-Tasks

ProcessingTime

BackGnd-Tasks

T

Page 5: An Introduction  to  Real Time Operating System  with  UCOS-II

5

UC/OS-II RTOS ObjectsSystem

ISR

INT

INTISR

INT

Task 0

T60

Ticks

ISR

T 10

Semaphore

T63IDLE

MessageQueue

Tx-FIFO

Mailbox

Timer INT

T61

T30

MemoryBuffers

Page 6: An Introduction  to  Real Time Operating System  with  UCOS-II

6

Simple Tasksvoid myTask (void*pdata){

while (TRUE){…

OSTimeDly(ticks);}

}

TaskTicks

Task

Task

void myTask (void*pdata){while (TRUE){

…OS???Pend(ticks);}

}

??? := Sem | Mbox | Q

Page 7: An Introduction  to  Real Time Operating System  with  UCOS-II

7

Polling & scheduling

while ((PTH & 8)==0){}

Task

Task

PORTH [3]

while ((PTH & 8)==0){OSTimeDly(1);

}

No context switching :Lower tasks are waiting

Context switching :Lower tasks can run

Page 8: An Introduction  to  Real Time Operating System  with  UCOS-II

8

Interrupt Service Routine

interrupt VECTOR_NUMvoid myISR (void){

OSIntEnter();……OSIntExit();

}

OSIntNesting++

ISR

if (OSIntNesting-- == 0){…OSIntCtxSw();

}

Task 10

Task 5

ISRINT

Preemption

Page 9: An Introduction  to  Real Time Operating System  with  UCOS-II

9

RT Ticks & Timer Interrupt

ISR

RT Timer

1 ms

OSTimeTick

Ticks

OSTimeDly(tick)

OS???Pend( event, tick, &err)

Page 10: An Introduction  to  Real Time Operating System  with  UCOS-II

10

Semaphore

ISR Task

Task

Initial Value = 1

Initial Value = 0

if(mySem=OSSemCreate(InitialVal) != NULL)…

err=OSSemPost(mySem)

OS_EVENT*mySem

OSSemPend(mySem,tick, &err)

OptionalTimeout

tick:

Page 11: An Introduction  to  Real Time Operating System  with  UCOS-II

11

Mail Box

Task

if (myMbox=OSMboxCreate(NULL)) != NULL)…

OptionalTimeout

err=OSMboxPost( myMbox,TxMessage)

RxMessage = OSMboxPend(myMBox,tick,&err)

OS_EVENT*myMbox

void*TxMessage

tick:

void*RxMessage

Page 12: An Introduction  to  Real Time Operating System  with  UCOS-II

12

Message Queue

Task

If (myQ = OSQCreate(Buffer,N)) != NULL)…

…OSQPost(myQ,TxMsg)

RxMsg=OSQPend(myQ, ticks,&err)

OS_EVENT*myQ

…OSQPostFront(myQ,TxMsg)

…OSQFlush(myQ)

void*TxMsg

void*Buffer[N]

OptionalTimeout

tick:

void*RxMsg

err =…

Page 13: An Introduction  to  Real Time Operating System  with  UCOS-II

13

Memory Buffer

If (myMem=OSMemCreate(buf,N,blksize,&err) != NULL)…

OSMemPut(myMem,&err)

OS_MEM*myMem

Buffer

TaskNO

TimeoutNo

wait

Buffer

OSMemGet(myMem,&err)

INT8U buf[N*blksize]

Page 14: An Introduction  to  Real Time Operating System  with  UCOS-II

14

Code Architecture

XXX.C

XXX.H

XXX_EVENTS_INIT

MYTASK_CREATE

MYTASK

MAIN.C ALL_INCLUDES.H

Task

MAIN

INIT

StartRTOS.CSTAR12_Init

APP_Task_CREATE

RTOS_Checking

ALL_Events_Init

(HardwareConfiguration)

Page 15: An Introduction  to  Real Time Operating System  with  UCOS-II

15

Creating a Task

err = OSTaskCreateExt (myTask, pdata, ptos, prio, id, pbos, MYTASK_stk_size, pext, opt);

Task

void myTask (void*pdata);static OS_STK MYTASK_stack[MYTASK_stk_size];void * ptos =

(void*)&MYTASK_stack[MYTASK_stk_size-1];void * pbos = (void*)&MYTASK_stack[0];INT8U prio = … ; // 0(higher) to 63(idle)INT8U id = 0; // unusedINT16U opt = OS_TASK_OPT_STK_CHK |

OS_TASK_OPT_STK_CLR; void*pdata = NULL; //optional data ptr void*pext = NULL; // optional ptr

Page 16: An Introduction  to  Real Time Operating System  with  UCOS-II

16

XXX.H

Code Generator

XXX.C

XXX_EVENTS_INIT

MYTASK_CREATE

MYTASK

Page 17: An Introduction  to  Real Time Operating System  with  UCOS-II

17

Training Course

Introduction to RTOS Multitasking,

Scheduling Task States Preemptive/

Cooperative Task Stacks

Introduction to uCOSII Task, Semaphore,

Mbox,Q,Mem Starting the OS Code Architecture Code Generator

Getting Start Load the demo Add a commented task Debug !

Writing an Application Use Code Generator Adding the Files to

project Adding the task to the

App. Debug

1H30

Lecture Labs

1H00

2H00

3H00

ISR Task 0

Page 18: An Introduction  to  Real Time Operating System  with  UCOS-II

18

Starsky

1 LineControl

2 MotorControl

3 LineSensor (I2C)

4 USSensor

5 Monitor (RS232)

6 Function

Application Tasks

60 RS232

61 LCD

62 Init (+RTOS Checking)

63 Idle

Service & System Tasks

Page 19: An Introduction  to  Real Time Operating System  with  UCOS-II

19

Monitoring5 Monitor

60 RS232

62 Init (+RTOS Checking) CPU Usage

Stack Usage Delay 1s

Wait for RS232 Q

Constitute Frames

Copy msg to TxFIFO

Enable SCI Tx ISR

Send Sensors & Variables values through RS232

Print To LCD buffer

Delay 100 ms

61 LCD Copy LCD buffer

to LCD controller

Delay 100 ms

Page 20: An Introduction  to  Real Time Operating System  with  UCOS-II

20

Application Starsky1 LineControl

2 MotorControl

Wait for MotorCtrlSem

Read Speed PI Controller or Open

loop Setting PWM Values

4 USSensor

Emit Ultrasound Delay 5ms Sample echo Delay 30 ms

3 LineSensor

Read line position /I2C

Delay 1 ms

Wait for LineCtrlSem ShortCut & Crossing

Detection End Detection Line Tracking

6 Function (Main Task)Select Mode (Stop,demo,normal…)Delay (variable)Set Motor Order or post LineCtrlSem

5 Monitor

Emit variable values via RS232 & LCD

Delay 100 ms

/

Page 21: An Introduction  to  Real Time Operating System  with  UCOS-II

21

HC12 Enhanced Capture Timer

Free running Timer

Timer Capture Register

Timer CaptureHolding Register

Pulse Accumulator

Pulse AccumulatorHolding Register

PA Overflow

Read ICHReg

16

8

Frequency = Periodic PA sampling

Period =

TC - TCH

Page 22: An Introduction  to  Real Time Operating System  with  UCOS-II

22

Frequency = Periodic PA sampling

Period =

TC - TCH

The choice

Frequency Measurement

UINT16 ECT_MOTOR_PT1_speed ( void ){ UINT16 Speed, Delta; Delta = (TC1 - TC1H); TotalPa1 += PA1H ; Speed_mm_per_sec = DiffPA1 * 21; return ( (Speed> 100) ?

(3272490UL / Delta) : Speed);}

Modulus Down Counter

5 ms

MotorCtrlSem

MotorControl

ISR

1/10

50 msvoid ECT_MOTOR_update(void) { Delta_ic1 = (TC1 - TC1H); // Tfer PA -> PAH Delta_ic3 = (TC3 - TC3H); // Tfer PA -> PAH TotalPa1 += PA1H; TotalPa3 += PA3H; DiffPA1 = TotalPa1 - TotalPa1_old; DiffPA3 = TotalPa3 - TotalPa3_old; TotalPa1_old = TotalPa1; TotalPa3_old = TotalPa3;}

Page 23: An Introduction  to  Real Time Operating System  with  UCOS-II

23

Starsky Project• Professor

– STAR 12 Board design 3 months– STAR12 Software Lib 2 months– UCOS-II Port to STAR12 Board 1 month– UCOS-II/Star12 Service 1 month

• 20 Students group– Frequency, Motor controller,

H-bridge, RS232 Monitor, I2C 6 weeks– All together, Line tracking 6 weeks

• 3 students – Starsky & Hutch design 8 weeks