⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 os_task.lst

📁 uCOS 嵌入式操作系统的改进版,增加了网络通讯.
💻 LST
📖 第 1 页 / 共 5 页
字号:
 200          *********************************************************************************************************
 201          *                                     CREATE A TASK (Extended Version)
 202          *
 203          * Description: This function is used to have uC/OS-II manage the execution of a task.  Tasks can either
 204          *              be created prior to the start of multitasking or by a running task.  A task cannot be
 205          *              created by an ISR.  This function is similar to OSTaskCreate() except that it allows
 206          *              additional information about a task to be specified.
 207          *
 208          * Arguments  : task     is a pointer to the task's code
 209          *
 210          *              pdata    is a pointer to an optional data area which can be used to pass parameters to
 211          *                       the task when the task first executes.  Where the task is concerned it thinks
 212          *                       it was invoked and passed the argument 'pdata' as follows:
 213          *
 214          *                           void Task (void *pdata)
 215          *                           {
 216          *                               for (;;) {
 217          *                                   Task code;
 218          *                               }
 219          *                           }
 220          *
 221          *              ptos     is a pointer to the task's top of stack.  If the configuration constant
 222          *                       OS_STK_GROWTH is set to 1, the stack is assumed to grow downward (i.e. from high
 223          *                       memory to low memory).  'ptos' will thus point to the highest (valid) memory
 224          *                       location of the stack.  If OS_STK_GROWTH is set to 0, 'ptos' will point to the
 225          *                       lowest memory location of the stack and the stack will grow with increasing
 226          *                       memory locations.  'ptos' MUST point to a valid 'free' data item.
 227          *
 228          *              prio     is the task's priority.  A unique priority MUST be assigned to each task and the
 229          *                       lower the number, the higher the priority.
 230          *
 231          *              id       is the task's ID (0..65535)
 232          *
 233          *              pbos     is a pointer to the task's bottom of stack.  If the configuration constant
 234          *                       OS_STK_GROWTH is set to 1, the stack is assumed to grow downward (i.e. from high
 235          *                       memory to low memory).  'pbos' will thus point to the LOWEST (valid) memory
 236          *                       location of the stack.  If OS_STK_GROWTH is set to 0, 'pbos' will point to the
 237          *                       HIGHEST memory location of the stack and the stack will grow with increasing
 238          *                       memory locations.  'pbos' MUST point to a valid 'free' data item.
 239          *
 240          *              stk_size is the size of the stack in number of elements.  If OS_STK is set to INT8U,
C51 COMPILER V7.06   OS_TASK                                                               07/18/2003 11:06:04 PAGE 5   

 241          *                       'stk_size' corresponds to the number of bytes available.  If OS_STK is set to
 242          *                       INT16U, 'stk_size' contains the number of 16-bit entries available.  Finally, if
 243          *                       OS_STK is set to INT32U, 'stk_size' contains the number of 32-bit entries
 244          *                       available on the stack.
 245          *
 246          *              pext     is a pointer to a user supplied memory location which is used as a TCB extension.
 247          *                       For example, this user memory can hold the contents of floating-point registers
 248          *                       during a context switch, the time each task takes to execute, the number of times
 249          *                       the task has been switched-in, etc.
 250          *
 251          *              opt      contains additional information (or options) about the behavior of the task.  The
 252          *                       LOWER 8-bits are reserved by uC/OS-II while the upper 8 bits can be application
 253          *                       specific.  See OS_TASK_OPT_??? in uCOS-II.H.
 254          *
 255          * Returns    : OS_NO_ERR        if the function was successful.
 256          *              OS_PRIO_EXIT     if the task priority already exist
 257          *                               (each task MUST have a unique priority).
 258          *              OS_PRIO_INVALID  if the priority you specify is higher that the maximum allowed
 259          *                               (i.e. > OS_LOWEST_PRIO)
 260          *********************************************************************************************************
 261          */
 262          /*$PAGE*/
 263          #if OS_TASK_CREATE_EXT_EN > 0
 264          INT8U  OSTaskCreateExt (void   (*task)(void *pd),
 265                                  void    *pndata,
 266                                  OS_STK  *ptos,
 267                                  INT8U    prio,
 268                                  INT16U   id,
 269                                  OS_STK  *pbos,
 270                                  INT32U   stk_size,
 271                                  void    *pext,
 272                                  INT16U   opt) reentrant //using 0
 273          {
 274   1      #if OS_CRITICAL_METHOD == 3                  /* Allocate storage for CPU status register               */
 275   1          OS_CPU_SR  cpu_sr;
 276   1      #endif
 277   1          OS_STK    *psp;
 278   1          INT8U      err;
 279   1      
 280   1      
 281   1      #if OS_ARG_CHK_EN > 0
 282   1          if (prio > OS_LOWEST_PRIO) {             /* Make sure priority is within allowable range           */
 283   2              return (OS_PRIO_INVALID);
 284   2          }
 285   1      #endif
 286   1          OS_ENTER_CRITICAL();
 287   1          if (OSTCBPrioTbl[prio] == (OS_TCB *)0) { /* Make sure task doesn't already exist at this priority  */
 288   2              OSTCBPrioTbl[prio] = (OS_TCB *)1;    /* Reserve the priority to prevent others from doing ...  */
 289   2                                                   /* ... the same thing until task is created.              */
 290   2              OS_EXIT_CRITICAL();
 291   2      
 292   2              OS_TaskStkClr(pbos, stk_size, opt);                    /* Clear the task stack (if needed)     */
 293   2      
 294   2              psp = (OS_STK *)OSTaskStkInit(task, pndata, ptos, opt); /* Initialize the task's stack          */
 295   2              err = OS_TCBInit(prio, psp, pbos, id, stk_size, pext, opt);
 296   2              if (err == OS_NO_ERR) {
 297   3                  OS_ENTER_CRITICAL();
 298   3                  OSTaskCtr++;                                       /* Increment the #tasks counter         */
 299   3                  OS_EXIT_CRITICAL();
 300   3                  if (OSRunning == TRUE) {                           /* Find HPT if multitasking has started */
 301   4                      OS_Sched();
 302   4                  }
C51 COMPILER V7.06   OS_TASK                                                               07/18/2003 11:06:04 PAGE 6   

 303   3              } else {
 304   3                  OS_ENTER_CRITICAL();
 305   3                  OSTCBPrioTbl[prio] = (OS_TCB *)0;                  /* Make this priority avail. to others  */
 306   3                  OS_EXIT_CRITICAL();
 307   3              }
 308   2              return (err);
 309   2          }
 310   1          OS_EXIT_CRITICAL();
 311   1          return (OS_PRIO_EXIST);
 312   1      }
 313          #endif
 314          /*$PAGE*/
 315          /*
 316          *********************************************************************************************************
 317          *                                            DELETE A TASK
 318          *
 319          * Description: This function allows you to delete a task.  The calling task can delete itself by
 320          *              its own priority number.  The deleted task is returned to the dormant state and can be
 321          *              re-activated by creating the deleted task again.
 322          *
 323          * Arguments  : prio    is the priority of the task to delete.  Note that you can explicitely delete
 324          *                      the current task without knowing its priority level by setting 'prio' to
 325          *                      OS_PRIO_SELF.
 326          *
 327          * Returns    : OS_NO_ERR           if the call is successful
 328          *              OS_TASK_DEL_IDLE    if you attempted to delete uC/OS-II's idle task
 329          *              OS_PRIO_INVALID     if the priority you specify is higher that the maximum allowed
 330          *                                  (i.e. >= OS_LOWEST_PRIO) or, you have not specified OS_PRIO_SELF.
 331          *              OS_TASK_DEL_ERR     if the task you want to delete does not exist
 332          *              OS_TASK_DEL_ISR     if you tried to delete a task from an ISR
 333          *
 334          * Notes      : 1) To reduce interrupt latency, OSTaskDel() 'disables' the task:
 335          *                    a) by making it not ready
 336          *                    b) by removing it from any wait lists
 337          *                    c) by preventing OSTimeTick() from making the task ready to run.
 338          *                 The task can then be 'unlinked' from the miscellaneous structures in uC/OS-II.
 339          *              2) The function OS_Dummy() is called after OS_EXIT_CRITICAL() because, on most processors,
 340          *                 the next instruction following the enable interrupt instruction is ignored.  
 341          *              3) An ISR cannot delete a task.
 342          *              4) The lock nesting counter is incremented because, for a brief instant, if the current
 343          *                 task is being deleted, the current task would not be able to be rescheduled because it
 344          *                 is removed from the ready list.  Incrementing the nesting counter prevents another task
 345          *                 from being schedule.  This means that an ISR would return to the current task which is
 346          *                 being deleted.  The rest of the deletion would thus be able to be completed.
 347          *********************************************************************************************************
 348          */
 349          /*$PAGE*/
 350          #if OS_TASK_DEL_EN > 0
 351          INT8U  OSTaskDel (INT8U prio) reentrant //using 0
 352          {
 353   1      #if OS_CRITICAL_METHOD == 3                      /* Allocate storage for CPU status register           */
 354   1          OS_CPU_SR     cpu_sr;
 355   1      #endif
 356   1      
 357   1      #if OS_EVENT_EN > 0
 358   1          OS_EVENT     *pevent;
 359   1      #endif    
 360   1      #if (OS_VERSION >= 251) && (OS_FLAG_EN > 0) && (OS_MAX_FLAGS > 0)
 361   1          OS_FLAG_NODE *pnode;
 362   1      #endif
 363   1          OS_TCB       *ptcb;
 364   1              INT8U         y;
C51 COMPILER V7.06   OS_TASK                                                               07/18/2003 11:06:04 PAGE 7   

 365   1      
 366   1      
 367   1      
 368   1          if (OSIntNesting > 0) {                                     /* See if trying to delete from ISR    */
 369   2              return (OS_TASK_DEL_ISR);
 370   2          }
 371   1      #if OS_ARG_CHK_EN > 0
 372   1          if (prio == OS_IDLE_PRIO) {                                 /* Not allowed to delete idle task     */
 373   2              return (OS_TASK_DEL_IDLE);
 374   2          }
 375   1          if (prio >= OS_LOWEST_PRIO && prio != OS_PRIO_SELF) {       /* Task priority valid ?               */
 376   2              return (OS_PRIO_INVALID);
 377   2          }
 378   1      #endif
 379   1          OS_ENTER_CRITICAL();
 380   1          if (prio == OS_PRIO_SELF) {                                 /* See if requesting to delete self    */
 381   2              prio = OSTCBCur->OSTCBPrio;                             /* Set priority to delete to current   */
 382   2          }
 383   1          ptcb = OSTCBPrioTbl[prio];
 384   1          if (ptcb != (OS_TCB *)0) {                                  /* Task to delete must exist           */
 385   2                  y            =  ptcb->OSTCBY;
 386   2                      OSRdyTbl[y] &= ~ptcb->OSTCBBitX;
 387   2              if (OSRdyTbl[y] == 0x00) {                              /* Make task not ready                 */
 388   3                  OSRdyGrp &= ~ptcb->OSTCBBitY;
 389   3              }
 390   2      #if OS_EVENT_EN > 0
 391   2              pevent = ptcb->OSTCBEventPtr;
 392   2              if (pevent != (OS_EVENT *)0) {                          /* If task is waiting on event         */
 393   3                          pevent->OSEventTbl[y] &= ~ptcb->OSTCBBitX;
 394   3                  if (pevent->OSEventTbl[y] == 0) {                   /* ... remove task from ...            */
 395   4                      pevent->OSEventGrp &= ~ptcb->OSTCBBitY;         /* ... event ctrl block                */
 396   4                  }
 397   3              }
 398   2      #endif
 399   2      #if (OS_VERSION >= 251) && (OS_FLAG_EN > 0) && (OS_MAX_FLAGS > 0)
 400   2              pnode = ptcb->OSTCBFlagNode;
 401   2              if (pnode != (OS_FLAG_NODE *)0) {                       /* If task is waiting on event flag    */
 402   3                  OS_FlagUnlink(pnode);                               /* Remove from wait list               */
 403   3              }
 404   2      #endif
 405   2              ptcb->OSTCBDly  = 0;                                    /* Prevent OSTimeTick() from updating  */
 406   2              ptcb->OSTCBStat = OS_STAT_RDY;                          /* Prevent task from being resumed     */
 407   2                      if (OSLockNesting < 255u) {
 408   3                  OSLockNesting++;

⌨️ 快捷键说明

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