280 likes | 533 Views
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 JOK@IEF.U-PSUD.FR.
E N D
Simple Portable Royalty Free Readable Source Well Suited for MCU Well documented InstitutUniversitaire de Technologie de CACHAN Université PARIS SUD - FRANCE An Introduction to Real Time Operating System with UCOS-II www.micrium.com J.O. KLEIN JOK@IEF.U-PSUD.FR Jean Labrosse
Background / InterruptModel Limitation INT INT Background Loop … ISR ISR INT • ISR < 100 us ISR • ISR > 1 ms • Latency INT 0 ISR 0 INT 1 INT 1-Pending BCKGND Time
RTOS-Solution ISR0 INT 0 ISR0 ISR1 ISR1 INT 1 Task 1 Task 1 Task 1 IDLE Time
Practical Timing Ranges ISR RT-Tasks BackGnd-Tasks Processing Time 1 us 1 ms 1 s T RTOS Tick = Time unit for delay & timeout
UC/OS-II RTOS Objects System INT Timer INT ISR Semaphore Task 0 T 10 Ticks Message Queue Memory Buffers T61 T63 IDLE INT INT T60 ISR ISR T30 Tx-FIFO Mailbox
Simple Tasks Task void myTask (void*pdata){ while (TRUE){ … OSTimeDly(ticks); } } Task Ticks void myTask (void*pdata){ while (TRUE){ … OS???Pend(ticks); } } Task ??? := Sem | Mbox | Q
Polling & scheduling Task while ((PTH & 8)==0){} PORTH [3] Task No context switching : Lower tasks are waiting while ((PTH & 8)==0){ OSTimeDly(1); } Context switching : Lower tasks can run
Interrupt Service Routine ISR interrupt VECTOR_NUM void myISR (void){ OSIntEnter(); … … OSIntExit(); } OSIntNesting++ if (OSIntNesting-- == 0){ … OSIntCtxSw(); } INT ISR Task 5 Preemption Task 10
RT Ticks & Timer Interrupt RT Timer OSTimeDly(tick) 1 ms ISR OSTimeTick OS???Pend( event, tick, &err) Ticks
Semaphore OS_EVENT* mySem if(mySem=OSSemCreate(InitialVal) != NULL)… ISR Task Task OSSemPend(mySem,tick, &err) err=OSSemPost(mySem) Optional Timeout tick: Initial Value = 0 Initial Value = 1
Mail Box OS_EVENT* myMbox if (myMbox=OSMboxCreate(NULL)) != NULL)… RxMessage = OSMboxPend(myMBox,tick,&err) Task err=OSMboxPost( myMbox,TxMessage) Optional Timeout tick: void*TxMessage void*RxMessage
Message Queue OS_EVENT* myQ void*Buffer[N] If (myQ = OSQCreate(Buffer,N)) != NULL)… err =… …OSQPost(myQ,TxMsg) RxMsg=OSQPend(myQ, ticks,&err) …OSQPostFront(myQ,TxMsg) Task …OSQFlush(myQ) Optional Timeout tick: void*TxMsg void*RxMsg
Memory Buffer OS_MEM* myMem INT8U buf[N*blksize] If (myMem=OSMemCreate(buf,N,blksize,&err) != NULL)… OSMemGet(myMem,&err) OSMemPut(myMem,&err) Buffer Buffer NO Timeout No wait Task
Code Architecture MAIN.C ALL_INCLUDES.H XXX.H MAIN XXX.C INIT XXX_EVENTS_INIT ALL_Events_Init StartRTOS.C MYTASK_CREATE STAR12_Init APP_Task_CREATE Task MYTASK (Hardware Configuration) RTOS_Checking
Creating a Task 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; // unused INT16U opt = OS_TASK_OPT_STK_CHK | OS_TASK_OPT_STK_CLR; void*pdata = NULL; //optional data ptr void*pext = NULL; // optional ptr err = OSTaskCreateExt (myTask, pdata, ptos, prio, id, pbos, MYTASK_stk_size, pext, opt);
Code Generator XXX.H XXX.C XXX_EVENTS_INIT MYTASK_CREATE MYTASK
Training Course Lecture Labs 1H30 3H00 • 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 1H00 2H00 ISR Task 0
Starsky Application Tasks Service & System Tasks 1 LineControl 60 RS232 2 MotorControl 61 LCD 3 LineSensor (I2C) 62 Init (+RTOS Checking) 4 USSensor 63 Idle 5 Monitor (RS232) 6 Function
Monitoring 5 Monitor 60 RS232 • Send Sensors & Variables values through RS232 • Print To LCD buffer • Delay 100 ms • Wait for RS232 Q • Constitute Frames • Copy msg to TxFIFO • Enable SCI Tx ISR 61 LCD • Copy LCD buffer to LCD controller • Delay 100 ms 62 Init (+RTOS Checking) • CPU Usage • Stack Usage • Delay 1s
Application Starsky / 1 LineControl 4 USSensor • Emit Ultrasound • Delay 5ms • Sample echo • Delay 30 ms • Wait for LineCtrlSem • ShortCut & Crossing Detection • End Detection • Line Tracking 5 Monitor 2 MotorControl • Emit variable values via RS232 & LCD • Delay 100 ms • Wait for MotorCtrlSem • Read Speed • PI Controller or Open loop • Setting PWM Values 6 Function (Main Task) • Select Mode (Stop,demo,normal…) • Delay (variable) • Set Motor Order or post LineCtrlSem 3 LineSensor • Read line position /I2C • Delay 1 ms
HC12 Enhanced Capture Timer Frequency = Periodic PA sampling Free running Timer 16 PA Overflow Timer Capture Register Pulse Accumulator 8 Timer Capture Holding Register Read ICHReg Period = TC - TCH Pulse Accumulator Holding Register
Frequency Measurement Modulus Down Counter Period = TC - TCH 5 ms 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); } ISR MotorCtrlSem Motor Control 1/10 void 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; } The choice 50 ms Frequency = Periodic PA sampling
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