os_core.lst

来自「一个基于C51指令系统的简易uCOS示范程序,有完整的代码分析」· LST 代码 · 共 882 行 · 第 1/4 页

LST
882
字号
 206   1          for (i = 0; i < OS_EVENT_TBL_SIZE; i++) {
 207   2              pevent->OSEventTbl[i] = 0x00;
 208   2          }
 209   1      }
 210          #endif
 211          /*$PAGE*/
 212          /*
 213          *********************************************************************************************************
 214          *                                             INITIALIZATION
 215          *
 216          * Description: This function is used to initialize the internals of uC/OS-II and MUST be called prior to
 217          *              creating any uC/OS-II object and, prior to calling OSStart().
 218          *
 219          * Arguments  : none
 220          *
 221          * Returns    : none
 222          *********************************************************************************************************
 223          */
 224          
 225          void OSInit (void) reentrant
 226          {
 227   1          INT16U i;
 228   1      
 229   1      
 230   1          OSTime        = 0L;                                    /* Clear the 32-bit system clock            */
 231   1          OSIntNesting  = 0;                                     /* Clear the interrupt nesting counter      */
 232   1          OSLockNesting = 0;                                     /* Clear the scheduling lock counter        */
 233   1      #if OS_TASK_CREATE_EN  || OS_TASK_CREATE_EXT_EN || OS_TASK_DEL_EN
 234   1          OSTaskCtr     = 0;                                     /* Clear the number of tasks                */
 235   1      #endif
 236   1          OSRunning     = FALSE;                                 /* Indicate that multitasking not started   */
 237   1          OSIdleCtr     = 0L;                                    /* Clear the 32-bit idle counter            */
 238   1      #if OS_TASK_STAT_EN && OS_TASK_CREATE_EXT_EN
                  OSIdleCtrRun  = 0L;
                  OSIdleCtrMax  = 0L;
C51 COMPILER V7.07   OS_CORE                                                               05/31/2008 20:36:05 PAGE 5   

                  OSStatRdy     = FALSE;                                 /* Statistic task is not ready              */
              #endif
 243   1          OSCtxSwCtr    = 0;                                     /* Clear the context switch counter         */
 244   1          OSRdyGrp      = 0;                                     /* Clear the ready list                     */
 245   1          for (i = 0; i < OS_RDY_TBL_SIZE; i++) {
 246   2              OSRdyTbl[i] = 0;
 247   2          }
 248   1                      
 249   1          OSPrioCur     = 0;
 250   1          OSPrioHighRdy = 0;                                           
 251   1          OSTCBHighRdy  = (OS_TCB *)0;                                 /* TCB Initialization                 */
 252   1          OSTCBCur      = (OS_TCB *)0;
 253   1          OSTCBList     = (OS_TCB *)0;
 254   1          for (i = 0; i < (OS_LOWEST_PRIO + 1); i++) {                 /* Clear the priority table           */
 255   2              OSTCBPrioTbl[i] = (OS_TCB *)0;
 256   2          }
 257   1          for (i = 0; i < (OS_MAX_TASKS + OS_N_SYS_TASKS - 1); i++) {  /* Init. list of free TCBs            */
 258   2              OSTCBTbl[i].OSTCBNext = &OSTCBTbl[i + 1];
 259   2          }
 260   1          OSTCBTbl[OS_MAX_TASKS + OS_N_SYS_TASKS - 1].OSTCBNext = (OS_TCB *)0;    /* Last OS_TCB             */
 261   1          OSTCBFreeList                                         = &OSTCBTbl[0];
 262   1      
 263   1      #if OS_MAX_EVENTS >= 2
 264   1          for (i = 0; i < (OS_MAX_EVENTS - 1); i++) {            /* Init. list of free EVENT control blocks  */
 265   2              OSEventTbl[i].OSEventPtr = (OS_EVENT *)&OSEventTbl[i + 1];
 266   2          }
 267   1          OSEventTbl[OS_MAX_EVENTS - 1].OSEventPtr = (OS_EVENT *)0;
 268   1          OSEventFreeList                          = &OSEventTbl[0];    
 269   1      #endif
 270   1      
 271   1      #if OS_Q_EN && (OS_MAX_QS >= 2)
 272   1          OSQInit();                                             /* Initialize the message queue structures  */
 273   1      #endif
 274   1      
 275   1      #if OS_MEM_EN && OS_MAX_MEM_PART >= 2
                  OSMemInit();                                           /* Initialize the memory manager            */
              #endif    
 278   1      
 279   1      #if OS_STK_GROWTH == 1
                  #if OS_TASK_CREATE_EXT_EN
                  OSTaskCreateExt(OSTaskIdle, 
                                  (void *)0,                                 /* No arguments passed to OSTaskIdle()  */
                                  &OSTaskIdleStk[OS_TASK_IDLE_STK_SIZE - 1], /* Set Top-Of-Stack                     */
                                  OS_IDLE_PRIO,                              /* Lowest priority level                */
                                  OS_TASK_IDLE_ID,
                                  &OSTaskIdleStk[0],                         /* Set Bottom-Of-Stack                  */
                                  OS_TASK_IDLE_STK_SIZE, 
                                  (void *)0,                                 /* No TCB extension                     */
                                  OS_TASK_OPT_STK_CHK | OS_TASK_OPT_STK_CLR);/* Enable stack checking + clear stack  */
                  #else
                  OSTaskCreate(OSTaskIdle, (void *)0, &OSTaskIdleStk[OS_TASK_IDLE_STK_SIZE - 1], OS_IDLE_PRIO);
                  #endif
              #else
 294   1          #if OS_TASK_CREATE_EXT_EN
                  OSTaskCreateExt(OSTaskIdle, 
                                  (void *)0,                                 /* No arguments passed to OSTaskIdle()  */
                                  &OSTaskIdleStk[0],                         /* Set Top-Of-Stack                     */
                                  OS_IDLE_PRIO,                              /* Lowest priority level                */
                                  OS_TASK_IDLE_ID,
                                  &OSTaskIdleStk[OS_TASK_IDLE_STK_SIZE - 1], /* Set Bottom-Of-Stack                  */
                                  OS_TASK_IDLE_STK_SIZE, 
                                  (void *)0,                                 /* No TCB extension                     */
C51 COMPILER V7.07   OS_CORE                                                               05/31/2008 20:36:05 PAGE 6   

                                  OS_TASK_OPT_STK_CHK | OS_TASK_OPT_STK_CLR);/* Enable stack checking + clear stack  */
                  #else
 305   1          OSTaskCreate(OSTaskIdle, (void *)0, &OSTaskIdleStk[0], OS_IDLE_PRIO);
 306   1          #endif
 307   1      #endif
 308   1      
 309   1      #if OS_TASK_STAT_EN 
                  #if OS_TASK_CREATE_EXT_EN
                      #if OS_STK_GROWTH == 1
                      OSTaskCreateExt(OSTaskStat, 
                                      (void *)0,                                /* No args passed to OSTaskStat()    */
                                      &OSTaskStatStk[OS_TASK_STAT_STK_SIZE - 1],/* Set Top-Of-Stack                  */
                                      OS_STAT_PRIO,                             /* One higher than the idle task     */
                                      OS_TASK_STAT_ID,
                                      &OSTaskStatStk[0],                        /* Set Bottom-Of-Stack               */
                                      OS_TASK_STAT_STK_SIZE, 
                                      (void *)0,                                /* No TCB extension                  */
                                      OS_TASK_OPT_STK_CHK | OS_TASK_OPT_STK_CLR);  /* Enable stack checking + clear  */
                      #else
                      OSTaskCreateExt(OSTaskStat, 
                                      (void *)0,                                /* No args passed to OSTaskStat()    */
                                      &OSTaskStatStk[0],                        /* Set Top-Of-Stack                  */
                                      OS_STAT_PRIO,                             /* One higher than the idle task     */
                                      OS_TASK_STAT_ID,
                                      &OSTaskStatStk[OS_TASK_STAT_STK_SIZE - 1],/* Set Bottom-Of-Stack               */
                                      OS_TASK_STAT_STK_SIZE, 
                                      (void *)0,                                /* No TCB extension                  */
                                      OS_TASK_OPT_STK_CHK | OS_TASK_OPT_STK_CLR);  /* Enable stack checking + clear  */
                      #endif
                  #else
                      #if OS_STK_GROWTH == 1
                      OSTaskCreate(OSTaskStat, 
                                   (void *)0,                                   /* No args passed to OSTaskStat()    */
                                   &OSTaskStatStk[OS_TASK_STAT_STK_SIZE - 1],   /* Set Top-Of-Stack                  */
                                   OS_STAT_PRIO);                               /* One higher than the idle task     */
                      #else
                      OSTaskCreate(OSTaskStat, 
                                   (void *)0,                                   /* No args passed to OSTaskStat()    */
                                   &OSTaskStatStk[0],                           /* Set Top-Of-Stack                  */
                                   OS_STAT_PRIO);                               /* One higher than the idle task     */
                      #endif
                  #endif
              #endif
 346   1      #ifdef MCS_51
 347   1          InitTimer0();
 348   1      #endif
 349   1      }
 350          /*$PAGE*/
 351          /*
 352          *********************************************************************************************************
 353          *                                              ENTER ISR
 354          *
 355          * Description: This function is used to notify uC/OS-II that you are about to service an interrupt
 356          *              service routine (ISR).  This allows uC/OS-II to keep track of interrupt nesting and thus
 357          *              only perform rescheduling at the last nested ISR.
 358          *
 359          * Arguments  : none
 360          *
 361          * Returns    : none
 362          *
 363          * Notes      : 1) Your ISR can directly increment OSIntNesting without calling this function because 
 364          *                 OSIntNesting has been declared 'global'.  You MUST, however, be sure that the increment
C51 COMPILER V7.07   OS_CORE                                                               05/31/2008 20:36:05 PAGE 7   

 365          *                 is performed 'indivisibly' by your processor to ensure proper access to this critical
 366          *                 resource.
 367          *              2) You MUST still call OSIntExit() even though you increment OSIntNesting directly.
 368          *              3) You MUST invoke OSIntEnter() and OSIntExit() in pair.  In other words, for every call
 369          *                 to OSIntEnter() at the beginning of the ISR you MUST have a call to OSIntExit() at the
 370          *                 end of the ISR.
 371          *********************************************************************************************************
 372          */
 373          
 374          void OSIntEnter (void) reentrant
 375          {
 376   1          OS_ENTER_CRITICAL();
 377   1          OSIntNesting++;                              /* Increment ISR nesting level                        */
 378   1          OS_EXIT_CRITICAL();
 379   1      }
 380          /*$PAGE*/
 381          /*
 382          *********************************************************************************************************
 383          *                                               EXIT ISR
 384          *
 385          * Description: This function is used to notify uC/OS-II that you have completed serviving an ISR.  When 
 386          *              the last nested ISR has completed, uC/OS-II will call the scheduler to determine whether
 387          *              a new, high-priority task, is ready to run.
 388          *
 389          * Arguments  : none
 390          *
 391          * Returns    : none
 392          *
 393          * Notes      : 1) You MUST invoke OSIntEnter() and OSIntExit() in pair.  In other words, for every call
 394          *                 to OSIntEnter() at the beginning of the ISR you MUST have a call to OSIntExit() at the
 395          *                 end of the ISR.
 396          *              2) Rescheduling is prevented when the scheduler is locked (see OSSchedLock())
 397          *********************************************************************************************************
 398          */
 399          
 400          void OSIntExit (void) reentrant
 401          {
 402   1          OS_ENTER_CRITICAL();
 403   1          if ((--OSIntNesting | OSLockNesting) == 0) { /* Reschedule only if all ISRs completed & not locked */
 404   2              OSIntExitY    = OSUnMapTbl[OSRdyGrp];
 405   2              OSPrioHighRdy = (INT8U)((OSIntExitY << 3) + OSUnMapTbl[OSRdyTbl[OSIntExitY]]);
 406   2              if (OSPrioHighRdy != OSPrioCur) {        /* No context switch if current task is highest ready */
 407   3                  OSTCBHighRdy  = OSTCBPrioTbl[OSPrioHighRdy];
 408   3                  OSCtxSwCtr++;                        /* Keep track of the number of context switches       */
 409   3                  OSIntCtxSw();                        /* Perform interrupt level context switch             */
 410   3              }
 411   2          }
 412   1          OS_EXIT_CRITICAL();
 413   1      }
 414          /*$PAGE*/
 415          /*
 416          *********************************************************************************************************
 417          *                                              SCHEDULER
 418          *
 419          * Description: This function is called by other uC/OS-II services to determine whether a new, high
 420          *              priority task has been made ready to run.  This function is invoked by TASK level code

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?