An Introduction to Real Time Operating System by yxv76130

VIEWS: 0 PAGES: 23

									      Institut Universitaire de Technologie de CACHAN
      Université PARIS SUD - FRANCE




Simple

Portable

Royalty Free

Readable Source                                                       www.micrium.com

Well Suited for MCU                                                   Jean Labrosse

Well documented

            An Introduction to Real Time
           Operating System with UCOS-II
               J.O. KLEIN
                                                                                        1
                                                        JOK@IEF.U-PSUD.FR
         Background / Interrupt
            Model Limitation
                 INT                           INT
                         ISR   Background
                 INT                    ISR
 ISR < 100 us                 Loop
                         ISR    …



 INT 0                 ISR 0           ISR > 1 ms
                                       Latency 
 INT 1
                  INT 1-Pending
BCKGND
                                        Time         2
          RTOS-Solution
INT 0           ISR0
                                       ISR0

         ISR1          ISR1
INT 1

                                               Task 1

Task 1                        Task 1


IDLE
                                        Time
                                                        3
  Practical Timing Ranges
ISR


         RT-Tasks

                    BackGnd-Tasks

                                             Processing
                                             Time
  1 us      1 ms           1s
                T
           RTOS Tick
           = Time unit for delay & timeout                4
            UC/OS-II RTOS Objects
                                                          System
INT                                    Timer INT
      ISR          Semaphore
                   Task 0                 T 10            Ticks


                             Message
       Memory                                      T63
                             Queue        T61
INT    Buffers                                     IDLE             INT

                            T60
      ISR                                                     ISR
                                          T30
             Tx-FIFO                               Mailbox
                                                                          5
Task
                Simple Tasks
                  void myTask (void*pdata){
                           while (TRUE){
                            …
 Ticks   Task
                           OSTimeDly(ticks);
                           }
                  }
                  void myTask (void*pdata){
                           while (TRUE){
         Task
                            …
                           OS???Pend(ticks);
                           }
                  }
                                               6
                  ??? := Sem | Mbox | Q
        Task
                 Polling & scheduling
                            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
                                                            7
                   Interrupt
   ISR
                Service Routine
interrupt VECTOR_NUM
void myISR (void){       OSIntNesting++
   OSIntEnter();
   …
   …
   OSIntExit();         if (OSIntNesting-- == 0){
}                         …
                          OSIntCtxSw();
                        }
          INT    ISR

     Task 5              Preemption
                                                    8
     Task 10
      RT Ticks & Timer Interrupt
RT Timer


                        OSTimeDly(tick)
 1 ms

ISR        OSTimeTick



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




            Ticks                                         9
                              Semaphore
                                               OS_EVENT*
                                               mySem

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


   ISR                                Task

   Task                            OSSemPend(mySem,
                                     tick, &err)
err=OSSemPost(mySem)



                                        tick:         Optional
                                                      Timeout

          Initial Value = 0            Initial Value = 1         10
                   Mail Box
                                       OS_EVENT*
                                       myMbox



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

                               RxMessage =
err=OSMboxPost(         Task   OSMboxPend(myMBox,
 myMbox,                       tick,&err)
 TxMessage)
                                    tick:   Optional
                                            Timeout


  void*TxMessage
                                void*RxMessage         11
         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
                               tick:
                                          Timeout


      void*TxMsg            void*RxMsg
                                                     12
           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      No
 Timeout            Task
            wait                                  13
              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        RTOS_Checking
 Configuration)
                                                              14
Task
           Creating a 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);
                                                15
Code Generator
                XXX.H

        XXX.C


        XXX_EVENTS_INIT



        MYTASK_CREATE



        MYTASK

                          16
                   Training Course
Lecture     1H30                 Labs    3H00

 Introduction to RTOS            Getting Start     1H00
     Multitasking, Scheduling       Load the demo
     Task States                    Add a commented task
     Preemptive/Cooperative         Debug !
     Task Stacks
                                                          2H00
 Introduction to uCOSII          Writing an Application
     Task, Semaphore,               Use Code Generator
       Mbox,Q,Mem                    Adding the Files to project
     Starting the OS                Adding the task to the App.
     Code Architecture              Debug
     Code Generator

                                           ISR             Task 0
                                                                    17
                          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
                                                              18
                            Monitoring
5   Monitor
                                   60 RS232
     Send Sensors &
       Variables values                     Wait for RS232 Q
       through RS232                        Constitute Frames
     Print To LCD buffer                   Copy msg to TxFIFO
     Delay 100 ms                          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                19
                    Application Starsky
    1       LineControl   /                    4     USSensor   
                                                         Emit Ultrasound
            Wait for LineCtrlSem
                                                         Delay 5ms
            ShortCut & Crossing Detection
                                                         Sample echo
            End Detection
                                                         Delay 30 ms
            Line Tracking

2       MotorControl      
                                                 5     Monitor   
                                                       Emit variable values
            Wait for MotorCtrlSem                      via RS232 & LCD
            Read Speed                                Delay 100 ms
            PI Controller or Open loop
            Setting PWM Values              6       Function (Main Task)   
3       LineSensor                                  Select Mode
                                                     (Stop,demo,normal…)
     Read line position /I2C                        Delay (variable)
     Delay 1 ms                                     Set Motor Order or        20
                                                     post LineCtrlSem
HC12 Enhanced Capture Timer
                                            Frequency =
Free running Timer                          Periodic PA
                                             sampling
 16                                                           PA Overflow
                     Timer Capture
                     Register
                                               Pulse Accumulator

                     Timer Capture                        8
                     Holding Register

                              Read ICHReg
Period =                                       Pulse Accumulator
TC - TCH                                       Holding Register


                                                                        21
       Frequency Measurement
 Modulus Down Counter                                                   Period =
 5 ms                                                                   TC - TCH
                                              UINT16 ECT_MOTOR_PT1_speed ( void ){
                                                UINT16 Speed, Delta;
  ISR              MotorCtrlSem                 Delta = (TC1 - TC1H);
                                               TotalPa1 += PA1H ;
                      Motor                    Speed_mm_per_sec = DiffPA1 * 21;
                     Control                    return ( (Speed> 100) ?
1/10                                                       (3272490UL / Delta) : Speed);
                                              }
          void ECT_MOTOR_update(void) {
  50 ms     Delta_ic1 = (TC1 - TC1H); // Tfer PA -> PAH         The choice
            Delta_ic3 = (TC3 - TC3H); // Tfer PA -> PAH
            TotalPa1 += PA1H;
            TotalPa3 += PA3H;
            DiffPA1 = TotalPa1 - TotalPa1_old;             Frequency =
            DiffPA3 = TotalPa3 - TotalPa3_old;             Periodic PA
            TotalPa1_old = TotalPa1;
            TotalPa3_old = TotalPa3;                        sampling                       22
          }
               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


                                                 23

								
To top