?? os_time.c
字號:
/*
*********************************************************************************************************
* uC/OS-II
* The Real-Time Kernel
* TIME MANAGEMENT
*
* (c) Copyright 1992-2002, Jean J. Labrosse, Weston, FL
* All Rights Reserved
*
* File : OS_TIME.C
* By : Jean J. Labrosse
*********************************************************************************************************
*/
#ifndef OS_MASTER_FILE
#include "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
*********************************************************************************************************
*/
/*
*********************************************************************************************************
延遲任務N個節拍(N從0到65535)
描述:此函數調用去延遲正在運行的任務直到規定時間期滿,如果延遲0
就等于沒有延遲,如果大于零,將會發生任務轉換
參數:ticks:延遲任務將被掛起的時鐘節拍數,是零的話不延遲
返回:無
*********************************************************************************************************
*/
void OSTimeDly (INT16U ticks)
{
#if OS_CRITICAL_METHOD == 3 /* Allocate storage for CPU status register */
OS_CPU_SR cpu_sr;
#endif
if (ticks > 0) { /* 0 means no delay! *///如果需要延遲
OS_ENTER_CRITICAL();
if ((OSRdyTbl[OSTCBCur->OSTCBY] &= ~OSTCBCur->OSTCBBitX) == 0) { /* Delay current task */
OSRdyGrp &= ~OSTCBCur->OSTCBBitY;//從就緒表中移除這個任務,即刪除當前任務
}
OSTCBCur->OSTCBDly = ticks; /* Load ticks in TCB *///保存延遲節拍數到OS_TCB中
OS_EXIT_CRITICAL();
OS_Sched(); /* 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.
*********************************************************************************************************
*/
/*
*********************************************************************************************************
按時分秒延遲函數
描述:使任務延遲直到一定時間期滿,它可以按照小時,分,秒和毫秒
來延時,而非節拍
參數:hours:延遲小時數(0到255)
minutes:指定分鐘數:0到59
seconds:指定秒鐘數:0到59
milli:毫秒數:0到999
返回: OS_NO_ERR
* OS_TIME_INVALID_MINUTES
* OS_TIME_INVALID_SECONDS
* OS_TIME_INVALID_MS
* OS_TIME_ZERO_DLY
備注:延遲毫秒數取決于節拍頻率,比如:如果節拍是100ms,那么你不能夠
延遲10ms,所以,它將設為0,實際的延遲與最近的節拍相匹配
*********************************************************************************************************
*/
#if OS_TIME_DLY_HMSM_EN > 0
INT8U OSTimeDlyHMSM (INT8U hours, INT8U minutes, INT8U seconds, INT16U milli)
{
INT32U ticks;
INT16U loops;
if (hours > 0 || minutes > 0 || seconds > 0 || milli > 0) {//確實需要延遲
if (minutes > 59) {
return (OS_TIME_INVALID_MINUTES); /* Validate arguments to be within range */
}
if (seconds > 59) {
return (OS_TIME_INVALID_SECONDS);
}
if (milli > 999) {
return (OS_TIME_INVALID_MILLI);
}//分,秒,毫秒出錯
/* 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;
//計算要延遲的時間里的節拍數,采用了四舍五入
loops = (INT16U)(ticks / 65536L); /* Compute the integral number of 65536 tick delays */
//因為ucosII只能延遲65535個節拍,所以如果多于它的話,要分幾次延
ticks = ticks % 65536L; /* Obtain the fractional number of ticks */
//余下不足65535的,作一次延遲
OSTimeDly((INT16U)ticks);//先延不足65535的
while (loops > 0) {//再延loop個65535年節拍。
OSTimeDly(32768);
OSTimeDly(32768);
loops--;
}
return (OS_NO_ERR);
}
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
********************************************************************************************************
*/
/*
*********************************************************************************************************
恢復一個延遲任務
描述:這個函數通過調用OSTimeDly() 或者 OSTimeDlyHMSM()去恢復一個延時的任務,它不能喚醒
等待超時的任務。這個情況任務將把它看成等待超時,除非你指定這種效果,
同時,不能恢復調用OSTimeDlyHMSM() 延時超過65535個時鐘節拍的任務,就是說,如果
時鐘節拍是100Hz,你將不能夠任務OSTimeDlyHMSM(0, 10, 55, 350) 或者更大延遲數
參數:prio:要恢復任務的優先級
返回 : OS_NO_ERR 成功恢復
* OS_PRIO_INVALID 優先級值大于最大值
* (i.e. >= OS_LOWEST_PRIO)
* OS_TIME_NOT_DLY 任務沒有等待時間期滿
* OS_TASK_NOT_EXIST 要恢復的任務沒有創建
********************************************************************************************************
*/
#if OS_TIME_DLY_RESUME_EN > 0
INT8U OSTimeDlyResume (INT8U prio)
{
#if OS_CRITICAL_METHOD == 3 /* Allocate storage for CPU status register */
OS_CPU_SR cpu_sr;
#endif
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 */
//將任務的TCB提取出來
if (ptcb != (OS_TCB *)0) {//要保證不為空
if (ptcb->OSTCBDly != 0) { /* See if task is delayed */
//如果任務被延遲
ptcb->OSTCBDly = 0; /* Clear the time delay */
//取消延遲
if ((ptcb->OSTCBStat & OS_STAT_SUSPEND) == OS_STAT_RDY) { /* See if task is ready to run */
//看任務是否就緒,如果沒有,準備就緒
OSRdyGrp |= ptcb->OSTCBBitY; /* Make task ready to run */
OSRdyTbl[ptcb->OSTCBY] |= ptcb->OSTCBBitX;
OS_EXIT_CRITICAL();
OS_Sched(); /* 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 */
//如果為零,顯示任務沒有延時,
}
}
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
*********************************************************************************************************
*/
/*
*********************************************************************************************************
獲取當前時間
描述:獲得32位跟蹤時鐘節拍數的計數器,獲得當前值
參數:無
返回:OStime的當前值
備注:在訪問OStime的時候,中斷是關的,因為大多數八位處理器上增加或者拷貝
一定32位數,要數條指令,中途不能被打斷
*********************************************************************************************************
*/
#if OS_TIME_GET_SET_EN > 0
INT32U OSTimeGet (void)
{
#if OS_CRITICAL_METHOD == 3 /* Allocate storage for CPU status register */
OS_CPU_SR cpu_sr;
#endif
INT32U ticks;
OS_ENTER_CRITICAL();
ticks = OSTime;//獲取當前值
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
*********************************************************************************************************
*/
/*
*********************************************************************************************************
設置系統時間
描述:此函數設置32位跟蹤系統時鐘節拍的計數器
參數:ticks:OSTime的新值
返回:無
*********************************************************************************************************
*/
#if OS_TIME_GET_SET_EN > 0
void OSTimeSet (INT32U ticks)
{
#if OS_CRITICAL_METHOD == 3 /* Allocate storage for CPU status register */
OS_CPU_SR cpu_sr;
#endif
OS_ENTER_CRITICAL();
OSTime = ticks;//設置好,
OS_EXIT_CRITICAL();
}
#endif
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -