逐字逐句解析ucos ii 源代码-》OS_TIME.C

//BY 简单的元清  

//部分内容引用了其他博主的文章,对这些博主表示感谢,时间关系就不一一指出了。  

//如有转载,请说明,谢谢  
  
/*
*********************************************************************************************************
*                                                uC/OS-II
*                                          The Real-Time Kernel
*                                             TIME MANAGEMENT
*
*                        (c) Copyright 1992-1998, Jean J. Labrosse, Plantation, FL
*                                           All Rights Reserved
*
*                                                  V2.00
*
* File : OS_TIME.C
* By   : Jean J. Labrosse
*********************************************************************************************************
*/

#ifndef  OS_MASTER_FILE
#include "software\includes.h"
#endif

/*
*********************************************************************************************************
*                                DELAY TASK 'n' TICKS   (n from 0 to 65535)
*
* Description: This function is called to delay execution of the currently running task until the 
*              specified number of system ticks expires.  This, of course, directly equates to delaying
*              the current task for some time to expire.  No delay will result If the specified delay is 
*              0.  If the specified delay is greater than 0 then, a context switch will result.
*
* Arguments  : ticks     is the time delay that the task will be suspended in number of clock 'ticks'.  
*                        Note that by specifying 0, the task will not be delayed.
*
* Returns    : none
*********************************************************************************************************
*/

//将一个任务延时若干个时钟节拍
void OSTimeDly (INT16U ticks) reentrant
{
    if (ticks > 0)
		{                                                      /* 0 means no delay!         */
	//确保延时时间大于0
        OS_ENTER_CRITICAL();
		//进入临界区
        if ((OSRdyTbl[OSTCBCur->OSTCBY] &= ~OSTCBCur->OSTCBBitX) == 0) {  /* Delay current task        */
		
            OSRdyGrp &= ~OSTCBCur->OSTCBBitY;
			//从就绪表组里面删除就绪任务
        }
        OSTCBCur->OSTCBDly = ticks;                                       /* Load ticks in TCB         */
		//改写延时时间到TCB块
        OS_EXIT_CRITICAL();
		//退出临界区
        OSSched();            
          //进行一次调度                                  /* Find next task to run!    */
    }
}
/*$PAGE*/
/*
*********************************************************************************************************
*                                     DELAY TASK FOR SPECIFIED TIME
*
* Description: This function is called to delay execution of the currently running task until some time
*              expires.  This call allows you to specify the delay time in HOURS, MINUTES, SECONDS and
*              MILLISECONDS instead of ticks.
*
* Arguments  : hours     specifies the number of hours that the task will be delayed (max. is 255)
*              minutes   specifies the number of minutes (max. 59)    
*              seconds   specifies the number of seconds (max. 59)
*              milli     specifies the number of milliseconds (max. 999)
*
* Returns    : OS_NO_ERR
*              OS_TIME_INVALID_MINUTES
*              OS_TIME_INVALID_SECONDS
*              OS_TIME_INVALID_MS
*              OS_TIME_ZERO_DLY
*
* Note(s)    : The resolution on the milliseconds depends on the tick rate.  For example, you can't do
*              a 10 mS delay if the ticker interrupts every 100 mS.  In this case, the delay would be
*              set to 0.  The actual delay is rounded to the nearest tick.
*********************************************************************************************************
*/
#if OS_TIME_DLY_HMSM_EN > 0
//延时函数
INT8U OSTimeDlyHMSM (INT8U hours, INT8U minutes, INT8U seconds, INT16U milli) reentrant
{
    INT32U ticks;
    INT16U loops;


    if (hours > 0 || minutes > 0 || seconds > 0 || milli > 0) {
		//时/分/秒/微秒/不能小于0
        if (minutes > 59) {
			//分钟不能大于50
            return (OS_TIME_INVALID_MINUTES);    /* Validate arguments to be within range              */
        }
        if (seconds > 59) {
            return (OS_TIME_INVALID_SECONDS);
			//秒不能大于59
        }
        if (milli > 999) {
            return (OS_TIME_INVALID_MILLI);
			//毫秒不能大于999
        }
                                                 /* Compute the total number of clock ticks required.. */
                                                 /* .. (rounded to the nearest tick)  

        												 */
        ticks = ((INT32U)hours * 3600L + (INT32U)minutes * 60L + (INT32U)seconds) * OS_TICKS_PER_SEC
              + OS_TICKS_PER_SEC * ((INT32U)milli + 500L / OS_TICKS_PER_SEC) / 1000L;
			  //把24小时制时间转化为RTC时间戳
			  
        loops = ticks / 65536L;                  /* Compute the integral number of 65536 tick delays   */
        ticks = ticks % 65536L;                  /* Obtain  the fractional number of ticks             */
		//定时器最大计数2^16 为65536L
		//
        OSTimeDly(ticks); 
		
        while (loops > 0) {
            OSTimeDly(32768);
            OSTimeDly(32768);
            loops--;
        }
		//延时时间为ticks
        return (OS_NO_ERR);
		//返回返回值
		
    } else {
        return (OS_TIME_ZERO_DLY);
		//
    }
}
#endif
/*$PAGE*/
/*
*********************************************************************************************************
*                                         RESUME A DELAYED TASK
*
* Description: This function is used resume a task that has been delayed through a call to either
*              OSTimeDly() or OSTimeDlyHMSM().  Note that you MUST NOT call this function to resume a 
*              task that is waiting for an event with timeout.  This situation would make the task look 
*              like a timeout occurred (unless you desire this effect).  Also, you cannot resume a task 
*              that has called OSTimeDlyHMSM() with a combined time that exceeds 65535 clock ticks.  In 
*              other words, if the clock tick runs at 100 Hz then, you will not be able to resume a 
*              delayed task that called OSTimeDlyHMSM(0, 10, 55, 350) or higher.  
*
*                  (10 Minutes * 60 + 55 Seconds + 0.35) * 100 ticks/second.
*
* Arguments  : prio      specifies the priority of the task to resume
*
* Returns    : OS_NO_ERR                 Task has been resumed
*              OS_PRIO_INVALID           if the priority you specify is higher that the maximum allowed 
*                                        (i.e. >= OS_LOWEST_PRIO)
*              OS_TIME_NOT_DLY           Task is not waiting for time to expire
*              OS_TASK_NOT_EXIST         The desired task has not been created
*********************************************************************************************************
*/
#if OS_TIME_DLY_RESUME_EN > 0
//可以通过调用OSTimeDlyResume()和指定要恢复的任务的优先级来完成。
INT8U OSTimeDlyResume (INT8U prio) reentrant
{
    OS_TCB *ptcb;


    if (prio >= OS_LOWEST_PRIO) {
        return (OS_PRIO_INVALID);
    }
	//检测优先级是否在范围内
    OS_ENTER_CRITICAL();
	//进入临界区
    ptcb = (OS_TCB *)OSTCBPrioTbl[prio];                   /* Make sure that task exist                */
	//把任务赋值给ptcb指针
    if (ptcb != (OS_TCB *)0) {
		//如果ptcb指针有效
        if (ptcb->OSTCBDly != 0) {                         /* See if task is delayed                   */
		
            ptcb->OSTCBDly  = 0;                           /* Clear the time delay                     */
			//清除等待时间
            if (!(ptcb->OSTCBStat & OS_STAT_SUSPEND)) {    /* See if task is ready to run              */
			//判断任务是否被挂起
                OSRdyGrp               |= ptcb->OSTCBBitY; /* Make task ready to run                   */
                OSRdyTbl[ptcb->OSTCBY] |= ptcb->OSTCBBitX;
				//把任务注册进等待列表
                OS_EXIT_CRITICAL();
				//退出临界区
                OSSched();                                 /* See if this is new highest priority      */
            } else {
                OS_EXIT_CRITICAL();                        /* Task may be suspended                    */
				//退出临界区 
            }
            return (OS_NO_ERR);
        } else {
            OS_EXIT_CRITICAL();
				//退出临界区 
            return (OS_TIME_NOT_DLY);                      /* Indicate that task was not delayed       */
        }
    } else {
        OS_EXIT_CRITICAL();
			//退出临界区 
        return (OS_TASK_NOT_EXIST);                        /* The task does not exist                  */
    }
}
#endif
/*$PAGE*/
/*
*********************************************************************************************************
*                                         GET CURRENT SYSTEM TIME
*
* Description: This function is used by your application to obtain the current value of the 32-bit 
*              counter which keeps track of the number of clock ticks.
*
* Arguments  : none
*
* Returns    : The current value of OSTime
*********************************************************************************************************
*/
#if OS_TIME_GET_SET_EN > 0
//获取系统时间函数
INT32U OSTimeGet (void) reentrant
{
    INT32U ticks;


    OS_ENTER_CRITICAL();
	//进入临界区
    ticks = OSTime;
	//把时间赋值给ticks然后返回
    OS_EXIT_CRITICAL();
    return (ticks);
}
#endif
/*
*********************************************************************************************************
*                                            SET SYSTEM CLOCK
*
* Description: This function sets the 32-bit counter which keeps track of the number of clock ticks.
*
* Arguments  : ticks      specifies the new value that OSTime needs to take.
*
* Returns    : none
*********************************************************************************************************
*/
#if OS_TIME_GET_SET_EN > 0
//设置系统时间函数
void OSTimeSet (INT32U ticks) reentrant
{
    OS_ENTER_CRITICAL();
	//把设置的值赋值给系统
    OSTime = ticks;
    OS_EXIT_CRITICAL();
}
#endif

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值