UCOS挂起与延时

#if OS_TASK_SUSPEND_EN > 0u
INT8U  OSTaskSuspend (INT8U prio)
{
    BOOLEAN    self;
    OS_TCB    *ptcb;
    INT8U      y;
#if OS_CRITICAL_METHOD == 3u                     /* Allocate storage for CPU status register           */
    OS_CPU_SR  cpu_sr = 0u;
#endif

#if OS_ARG_CHK_EN > 0u
    if (prio == OS_TASK_IDLE_PRIO) {                            /* Not allowed to suspend idle task    */
        return (OS_ERR_TASK_SUSPEND_IDLE);
    }
    if (prio >= OS_LOWEST_PRIO) {                               /* Task priority valid ?               */
        if (prio != OS_PRIO_SELF) {
            return (OS_ERR_PRIO_INVALID);
        }
    }
#endif
    OS_ENTER_CRITICAL();
    if (prio == OS_PRIO_SELF) {                                 /* See if suspend SELF                 */
        prio = OSTCBCur->OSTCBPrio;
        self = OS_TRUE;
    } else if (prio == OSTCBCur->OSTCBPrio) {                   /* See if suspending self              */
        self = OS_TRUE;
    } else {
        self = OS_FALSE;                                        /* No suspending another task          */
    }
    ptcb = OSTCBPrioTbl[prio];
    if (ptcb == (OS_TCB *)0) {                                  /* Task to suspend must exist          */
        OS_EXIT_CRITICAL();
        return (OS_ERR_TASK_SUSPEND_PRIO);
    }
    if (ptcb == OS_TCB_RESERVED) {                              /* See if assigned to Mutex            */
        OS_EXIT_CRITICAL();
        return (OS_ERR_TASK_NOT_EXIST);
    }
    y            = ptcb->OSTCBY;
    OSRdyTbl[y] &= (OS_PRIO)~ptcb->OSTCBBitX;                   /* Make task not ready                 */
    if (OSRdyTbl[y] == 0u) {
        OSRdyGrp &= (OS_PRIO)~ptcb->OSTCBBitY;
    }
    ptcb->OSTCBStat |= OS_STAT_SUSPEND;                         /* Status of task is 'SUSPENDED'       */
    OS_EXIT_CRITICAL();
    if (self == OS_TRUE) {                                      /* Context switch only if SELF         */
        OS_Sched();                                             /* Find new highest priority task      */
    }
    return (OS_ERR_NONE);
}

挂起函数原型.判断挂起的是否是自己.如果是,就删除就绪,  状态做挂起记录ptcb->OSTCBStat |= OS_STAT_SUSPEND; 并任务调度.如果挂起的是别的任务 不调度.

 

延时函数原型如下 

void  OSTimeTick (void)
{
    OS_TCB    *ptcb;
#if OS_TICK_STEP_EN > 0
    BOOLEAN    step;
#endif

#if OS_CRITICAL_METHOD == 3                                /* Allocate storage for CPU status register */
    OS_CPU_SR  cpu_sr = 0;
#endif



#if OS_TIME_TICK_HOOK_EN > 0
    OSTimeTickHook();                                      /* Call user definable hook */
#endif

#if OS_TIME_GET_SET_EN > 0
    OS_ENTER_CRITICAL();                                   /* Update the 32-bit tick counter */
    OSTime++;
    OS_EXIT_CRITICAL();
#endif

    if(OSRunning==OS_TRUE) 
	   {
         #if OS_TICK_STEP_EN > 0
             switch(OSTickStepState) 
			    {                         /* Determine whether we need to process a tick  */
                  case OS_TICK_STEP_DIS:                         /* Yes, stepping is disabled */
                           step = OS_TRUE;
                           break;

                  case OS_TICK_STEP_WAIT:                        /* No,  waiting for uC/OS-View to set ... */
                           step = OS_FALSE;                       /* .. OSTickStepState to OS_TICK_STEP_ONCE */
                           break;

                  case OS_TICK_STEP_ONCE:                        /* Yes, process tick once and wait for next ... */
                           step = OS_TRUE;                       /*... step command from uC/OS-View */
                           OSTickStepState = OS_TICK_STEP_WAIT;
                           break;

                  default:                                       /* Invalid case, correct situation */
                           step = OS_TRUE;
                           OSTickStepState = OS_TICK_STEP_DIS;
                           break;
                }
             if(step==OS_FALSE)    /* Return if waiting for step command  */
			    {                           
                  return;
                }
           #endif

             ptcb = OSTCBList;                          /* Point at first TCB in TCB list  */
             while(ptcb->OSTCBPrio!=OS_TASK_IDLE_PRIO)  /* Go through all TCBs in TCB list */
			    {     
                  OS_ENTER_CRITICAL();
                  if(ptcb->OSTCBDly!=0)    /* No, Delayed or waiting for event with TO */
				     {                                  
                       if(--ptcb->OSTCBDly==0) 		    /* Decrement nbr of ticks to end of delay */
					      {                             
                                                           /* Check for timeout */
                            if((ptcb->OSTCBStat&OS_STAT_PEND_ANY)!=OS_STAT_RDY) 
							   {
                                 ptcb->OSTCBStat&=~(INT8U)OS_STAT_PEND_ANY;  /* Yes, Clear status flag */
                                 ptcb->OSTCBStatPend = OS_STAT_PEND_TO;      /* Indicate PEND timeout */
                               } 
							else 
							   {
                                 ptcb->OSTCBStatPend = OS_STAT_PEND_OK;
                               }

                            if((ptcb->OSTCBStat&OS_STAT_SUSPEND)==OS_STAT_RDY) /*Is task suspended? */
							   {  
                                  OSRdyGrp|= ptcb->OSTCBBitY;             /* No, Make ready */
                                  OSRdyTbl[ptcb->OSTCBY]|=ptcb->OSTCBBitX;
                               }
                          }
                      }
                   ptcb = ptcb->OSTCBNext;  /* Point at next TCB in TCB list */
                   OS_EXIT_CRITICAL();
                }
       }
}

其中 

                            if((ptcb->OSTCBStat&OS_STAT_PEND_ANY)!=OS_STAT_RDY) 
							   {
                                 ptcb->OSTCBStat&=~(INT8U)OS_STAT_PEND_ANY;  /* Yes, Clear status flag */
                                 ptcb->OSTCBStatPend = OS_STAT_PEND_TO;      /* Indicate PEND timeout */
                               } 
							else 
							   {
                                 ptcb->OSTCBStatPend = OS_STAT_PEND_OK;
                               }

                            if((ptcb->OSTCBStat&OS_STAT_SUSPEND)==OS_STAT_RDY) /*Is task suspended? */
							   {  
                                  OSRdyGrp|= ptcb->OSTCBBitY;             /* No, Make ready */
                                  OSRdyTbl[ptcb->OSTCBY]|=ptcb->OSTCBBitX;
                               }

可见挂起状态下延时函数继续-1.  并根据延时是否到0,挂起是否恢复.判断是否把挂起任务 添加入就绪表. 当挂起恢复延时结束时才加入.

同样在OSTaskResume() 恢复里面也有判断是否延时等待. 两者都满足 才添加到就绪表中.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

justsure

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值