时间管理相关函数,其实深入根本的理解就是一些对时间任务相关变量,数据结果进行修改的函数这样方便对应任务查找延时等时间相关的任务有没有到期。前面的时间相关的函数是这些操作的基
1.延时函数 OsTImeDly
函数是一个按时钟节拍定时的延时函数 ,默认是将当前执行的任务延时一定时钟节拍。
OS_OPT_TIME_DLY 此选项为相对延时 比如5S后
OS_OPT_TIME_TIMEOUT 同上
OS_OPT_TIME_MATCH 绝对延时 比如延时到系统开始运行后的一个时钟节拍点
OS_OPT_TIME_PERIODIC 周期延时 与相对延时差不多但是相对长时间周期性延时时更准确一些
void OSTimeDly (OS_TICK dly,
OS_OPT opt,
OS_ERR *p_err)
{
CPU_SR_ALLOC();
#ifdef OS_SAFETY_CRITICAL
if (p_err == (OS_ERR *)0) {
OS_SAFETY_CRITICAL_EXCEPTION();
return;
}
#endif
#if OS_CFG_CALLED_FROM_ISR_CHK_EN > 0u
if (OSIntNestingCtr > (OS_NESTING_CTR)0u) { /* Not allowed to call from an ISR */
*p_err = OS_ERR_TIME_DLY_ISR;
return;
}
#endif
if (OSSchedLockNestingCtr > (OS\_NESTING\_CTR)0u) { /\* Can't delay when the scheduler is locked \*/
\*p\_err = OS\_ERR\_SCHED\_LOCKED;
return;
}
switch (opt) {
case OS\_OPT\_TIME\_DLY:
case OS\_OPT\_TIME\_TIMEOUT:
case OS\_OPT\_TIME\_PERIODIC:
if (dly == (OS\_TICK)0u) { /\* 0 means no delay! \*/
\*p\_err = OS\_ERR\_TIME\_ZERO\_DLY;
return;
}
break;
case OS\_OPT\_TIME\_MATCH:
break;
default:
\*p\_err = OS\_ERR\_OPT\_INVALID;
return;
}
OS\_CRITICAL\_ENTER();
OSTCBCurPtr->TaskState = OS\_TASK\_STATE\_DLY;
OS\_TickListInsert(OSTCBCurPtr,
dly,
opt,
p\_err);
if (\*p\_err != OS\_ERR\_NONE) {
OS\_CRITICAL\_EXIT\_NO\_SCHED();
return;
}
OS\_RdyListRemove(OSTCBCurPtr); /\* Remove current task from ready list \*/
OS\_CRITICAL\_EXIT\_NO\_SCHED();
OSSched(); /\* Find next task to run! \*/
*p_err = OS_ERR_NONE;
}
OSTimeDly
2.OSTimeDlyHMSM
函数是一个按时间定时的延时函数 ,默认是将当前执行的任务延时一段时间。
void OSTimeDlyHMSM (CPU_INT16U hours,
CPU_INT16U minutes,
CPU_INT16U seconds,
CPU_INT32U milli,
OS_OPT opt,
OS_ERR *p_err)
{
#if OS_CFG_ARG_CHK_EN > 0u
CPU_BOOLEAN opt_invalid;
CPU_BOOLEAN opt_non_strict;
#endif
OS_OPT opt_time;
OS_RATE_HZ tick_rate;
OS_TICK ticks;
CPU_SR_ALLOC();
#ifdef OS_SAFETY_CRITICAL
if (p_err == (OS_ERR *)0) {
OS_SAFETY_CRITICAL_EXCEPTION();
return;
}
#endif
#if OS_CFG_CALLED_FROM_ISR_CHK_EN > 0u
if (OSIntNestingCtr > (OS_NESTING_CTR)0u) { /* Not allowed to call from an ISR */
*p_err = OS_ERR_TIME_DLY_ISR;
return;
}
#endif
if (OSSchedLockNestingCtr > (OS\_NESTING\_CTR)0u) { /\* Can't delay when the scheduler is locked \*/
\*p\_err = OS\_ERR\_SCHED\_LOCKED;
return;
}
opt\_time = opt & OS\_OPT\_TIME\_MASK; /\* Retrieve time options only. \*/
switch (opt\_time) {
case OS\_OPT\_TIME\_DLY:
case OS\_OPT\_TIME\_TIMEOUT:
case OS\_OPT\_TIME\_PERIODIC:
if (milli == (CPU\_INT32U)0u) { /\* Make sure we didn't specify a 0 delay \*/
if (seconds == (CPU\_INT16U)0u) {
if (minutes == (CPU\_INT16U)0u) {
if (hours == (CPU\_INT16U)0u) {
\*p\_err = OS\_ERR\_TIME\_ZERO\_DLY;
return;
}
}
}
}
break;
case OS\_OPT\_TIME\_MATCH:
break;
default:
\*p\_err = OS\_ERR\_OPT\_INVALID;
return;
}
#if OS_CFG_ARG_CHK_EN > 0u /* Validate arguments to be within range */
opt_invalid = DEF_BIT_IS_SET_ANY(opt, ~OS_OPT_TIME_OPTS_MASK);
if (opt_invalid == DEF_YES) {
*p_err = OS_ERR_OPT_INVALID;
return;
}
opt\_non\_strict = DEF\_BIT\_IS\_SET(opt, OS\_OPT\_TIME\_HMSM\_NON\_STRICT);
if (opt\_non\_strict != DEF\_YES) {
if (milli > (CPU\_INT32U)999u) {
\*p\_err = OS\_ERR\_TIME\_INVALID\_MILLISECONDS;
return;
}
if (seconds > (CPU\_INT16U)59u) {
\*p\_err = OS\_ERR\_TIME\_INVALID\_SECONDS;
return;
}
if (minutes > (CPU\_INT16U)59u) {
\*p\_err = OS\_ERR\_TIME\_INVALID\_MINUTES;
return;
}
if (hours > (CPU\_INT16U)99u) {
\*p\_err = OS\_ERR\_TIME\_INVALID\_HOURS;
return;
}
} else {
if (minutes > (CPU\_INT16U)9999u) {
\*p\_err = OS\_ERR\_TIME\_INVALID\_MINUTES;
return;
}
if (hours > (CPU\_INT16U)999u) {
\*p\_err = OS\_ERR\_TIME\_INVALID\_HOURS;
return;
}
}
#endif
/\* Compute the total number of clock ticks required.. \*/
/\* .. (rounded to the nearest tick) \*/
tick\_rate = OSCfg\_TickRate\_Hz;
ticks = ((OS\_TICK)hours \* (OS\_TICK)3600u + (OS\_TICK)minutes \* (OS\_TICK)60u + (OS\_TICK)seconds) \* tick\_rate
+ (tick\_rate \* ((OS\_TICK)milli + (OS\_TICK)500u / tick\_rate)) / (OS\_TICK)1000u;
if (ticks > (OS\_TICK)0u) {
OS\_CRITICAL\_ENTER();
OSTCBCurPtr->TaskState = OS\_TASK\_STATE\_DLY;
OS\_TickListInsert(OSTCBCurPtr,
ticks,
opt\_time,
p\_err);
if (\*p\_err != OS\_ERR\_NONE) {
OS\_CRITICAL\_EXIT\_NO\_SCHED();
return;
}
OS\_RdyListRemove(OSTCBCurPtr); /\* Remove current task from ready list \*/
OS\_CRITICAL\_EXIT\_NO\_SCHED();
OSSched(); /\* Find next task to run! \*/
\*p\_err = OS\_ERR\_NONE;
} else {
\*p\_err = OS\_ERR\_TIME\_ZERO\_DLY;
}
}
OSTimeDlyHMSM
3.任务插入节拍列表操作
void OS_TickListInsert (OS_TCB *p_tcb,
OS_TICK time,
OS_OPT opt,
OS_ERR *p_err)
{
OS_TICK tick_delta;
OS_TICK tick_next;
OS_TICK_SPOKE *p_spoke;
OS_TCB *p_tcb0;
OS_TCB *p_tcb1;
OS_TICK_SPOKE_IX spoke;
if (opt == OS\_OPT\_TIME\_MATCH) { /\* Task time is absolute. \*/
tick\_delta = time - OSTickCtr - 1u;
if (tick\_delta > OS\_TICK\_TH\_RDY) { /\* If delay already occurred, ... \*/
p\_tcb->TickCtrMatch = (OS\_TICK )0u;
p\_tcb->TickRemain = (OS\_TICK )0u;
p\_tcb->TickSpokePtr = (OS\_TICK\_SPOKE \*)0;
\*p\_err = OS\_ERR\_TIME\_ZERO\_DLY; /\* ... do NOT delay. \*/
return;
}
p\_tcb->TickCtrMatch = time;
p\_tcb->TickRemain = tick\_delta + 1u;
} else if (time > (OS\_TICK)0u) {
if (opt == OS\_OPT\_TIME\_PERIODIC) { /\* Task time is periodic. \*/
tick\_next = p\_tcb->TickCtrPrev + time;
tick\_delta = tick\_next - OSTickCtr - 1u;
if (tick\_delta < time) { /\* If next periodic delay did NOT already occur, ... \*/
p\_tcb->TickCtrMatch = tick\_next; /\* ... set next periodic delay; ... \*/
} else {
p\_tcb->TickCtrMatch = OSTickCtr + time; /\* ... else reset periodic delay. \*/
}
p\_tcb->TickRemain = p\_tcb->TickCtrMatch - OSTickCtr;
p\_tcb->TickCtrPrev = p\_tcb->TickCtrMatch;
} else { /\* Task time is relative to current. \*/
p\_tcb->TickCtrMatch = OSTickCtr + time;
p\_tcb->TickRemain = time;
}
} else { /\* Zero time delay; ... \*/
p\_tcb->TickCtrMatch = (OS\_TICK )0u;
p\_tcb->TickRemain = (OS\_TICK )0u;
p\_tcb->TickSpokePtr = (OS\_TICK\_SPOKE \*)0;
\*p\_err = OS\_ERR\_TIME\_ZERO\_DLY; /\* ... do NOT delay. \*/
return;
}
spoke = (OS\_TICK\_SPOKE\_IX)(p\_tcb->TickCtrMatch % OSCfg\_TickWheelSize);
p\_spoke = &OSCfg\_TickWheel\[spoke\];
if (p\_spoke->NbrEntries == (OS\_OBJ\_QTY)0u) { /\* First entry in the spoke \*/
p\_tcb->TickNextPtr = (OS\_TCB \*)0;
p\_tcb->TickPrevPtr = (OS\_TCB \*)0;
p\_spoke->FirstPtr = p\_tcb;
p\_spoke->NbrEntries = (OS\_OBJ\_QTY)1u;
} else {
p\_tcb1 = p\_spoke->FirstPtr; /\* Point to current first TCB in the list \*/
while (p\_tcb1 != (OS\_TCB \*)0) {
p\_tcb1->TickRemain = p\_tcb1->TickCtrMatch /\* Compute time remaining of current TCB in list \*/
- OSTickCtr;
if (p\_tcb->TickRemain > p\_tcb1->TickRemain) { /\* Do we need to insert AFTER current TCB in list? \*/
if (p\_tcb1->TickNextPtr != (OS\_TCB \*)0) { /\* Yes, are we pointing at the last TCB in the list? \*/
p\_tcb1 = p\_tcb1->TickNextPtr; /\* No, Point to next TCB in the list \*/
} else {
p\_tcb->TickNextPtr = (OS\_TCB \*)0;
p\_tcb->TickPrevPtr = p\_tcb1;
p\_tcb1->TickNextPtr = p\_tcb; /\* Yes, TCB to add is now new last entry in the list \*/
p\_tcb1 = (OS\_TCB \*)0; /\* Break loop \*/
}
} else { /\* Insert before the current TCB \*/
if (p\_tcb1->TickPrevPtr == (OS\_TCB \*)0) { /\* Are we inserting before the first TCB? \*/
p\_tcb->TickPrevPtr = (OS\_TCB \*)0;
p\_tcb->TickNextPtr = p\_tcb1;
p\_tcb1->TickPrevPtr = p\_tcb;
p\_spoke->FirstPtr = p\_tcb;
} else { /\* Insert in between 2 TCBs already in the list \*/
p\_tcb0 = p\_tcb1->TickPrevPtr;
p\_tcb->TickPrevPtr = p\_tcb0;
p\_tcb->TickNextPtr = p\_tcb1;
p\_tcb0->TickNextPtr = p\_tcb;
p\_tcb1->TickPrevPtr = p\_tcb;
}
p\_tcb1 = (OS\_TCB \*)0; /\* Break loop \*/
}
}
p\_spoke->NbrEntries++;
}
if (p\_spoke->NbrEntriesMax < p\_spoke->NbrEntries) { /\* Keep track of maximum # of entries in each spoke \*/
p\_spoke->NbrEntriesMax = p\_spoke->NbrEntries;
}
p\_tcb->TickSpokePtr = p\_spoke; /\* Link back to tick spoke \*/
*p_err = OS_ERR_NONE;
}
OS_TickListInsert
4.取消任务延时
#if OS_CFG_TIME_DLY_RESUME_EN > 0u
void OSTimeDlyResume (OS_TCB *p_tcb,
OS_ERR *p_err)
{
CPU_SR_ALLOC();
#ifdef OS_SAFETY_CRITICAL
if (p_err == (OS_ERR *)0) {
OS_SAFETY_CRITICAL_EXCEPTION();
return;
}
#endif
#if OS_CFG_CALLED_FROM_ISR_CHK_EN > 0u
if (OSIntNestingCtr > (OS_NESTING_CTR)0u) { /* Not allowed to call from an ISR */
*p_err = OS_ERR_TIME_DLY_RESUME_ISR;
return;
}
#endif
#if OS_CFG_ARG_CHK_EN > 0u
if (p_tcb == (OS_TCB *)0) { /* Not possible for the running task to be delayed! */
*p_err = OS_ERR_TASK_NOT_DLY;
return;
}
#endif
CPU\_CRITICAL\_ENTER();
if (p\_tcb == OSTCBCurPtr) { /\* Not possible for the running task to be delayed! \*/
\*p\_err = OS\_ERR\_TASK\_NOT\_DLY;
CPU\_CRITICAL\_EXIT();
return;
}
switch (p\_tcb->TaskState) {
case OS\_TASK\_STATE\_RDY: /\* Cannot Abort delay if task is ready \*/
CPU\_CRITICAL\_EXIT();
\*p\_err = OS\_ERR\_TASK\_NOT\_DLY;
break;
case OS\_TASK\_STATE\_DLY:
OS\_CRITICAL\_ENTER\_CPU\_EXIT();
p\_tcb->TaskState = OS\_TASK\_STATE\_RDY;
OS\_TickListRemove(p\_tcb); /\* Remove task from tick list \*/
OS\_RdyListInsert(p\_tcb); /\* Add to ready list \*/
OS\_CRITICAL\_EXIT\_NO\_SCHED();
\*p\_err = OS\_ERR\_NONE;
break;
case OS\_TASK\_STATE\_PEND:
CPU\_CRITICAL\_EXIT();
\*p\_err = OS\_ERR\_TASK\_NOT\_DLY;
break;
case OS\_TASK\_STATE\_PEND\_TIMEOUT:
CPU\_CRITICAL\_EXIT();
\*p\_err = OS\_ERR\_TASK\_NOT\_DLY;
break;
case OS\_TASK\_STATE\_SUSPENDED:
CPU\_CRITICAL\_EXIT();
\*p\_err = OS\_ERR\_TASK\_NOT\_DLY;
break;
case OS\_TASK\_STATE\_DLY\_SUSPENDED:
OS\_CRITICAL\_ENTER\_CPU\_EXIT();
p\_tcb->TaskState = OS\_TASK\_STATE\_SUSPENDED;
OS\_TickListRemove(p\_tcb); /\* Remove task from tick list \*/
OS\_CRITICAL\_EXIT\_NO\_SCHED();
\*p\_err = OS\_ERR\_TASK\_SUSPENDED;
break;
case OS\_TASK\_STATE\_PEND\_SUSPENDED:
CPU\_CRITICAL\_EXIT();
\*p\_err = OS\_ERR\_TASK\_NOT\_DLY;
break;
case OS\_TASK\_STATE\_PEND\_TIMEOUT\_SUSPENDED:
CPU\_CRITICAL\_EXIT();
\*p\_err = OS\_ERR\_TASK\_NOT\_DLY;
break;
default:
CPU\_CRITICAL\_EXIT();
\*p\_err = OS\_ERR\_STATE\_INVALID;
break;
}
OSSched();
}
#endif
OSTimeDlyResume
TickList数据结构:
μC/OS在一些变量 的处理上用到了一个技巧:
#ifdef OS_GLOBALS
#define OS_EXT
#else
#define OS_EXT extern
#endif
#if OS_CFG_APP_HOOKS_EN > 0u
OS_EXT OS_APP_HOOK_TCB OS_AppTaskCreateHookPtr; /* Application hooks */
OS_EXT OS_APP_HOOK_TCB OS_AppTaskDelHookPtr;
OS_EXT OS_APP_HOOK_TCB OS_AppTaskReturnHookPtr;
OS_EXT OS_APP_HOOK_VOID OS_AppIdleTaskHookPtr;
OS_EXT OS_APP_HOOK_VOID OS_AppStatTaskHookPtr;
OS_EXT OS_APP_HOOK_VOID OS_AppTaskSwHookPtr;
OS_EXT OS_APP_HOOK_VOID OS_AppTimeTickHookPtr;
#endif
/\* IDLE TASK -------------------------------- \*/
OS_EXT OS_IDLE_CTR OSIdleTaskCtr;
OS_EXT OS_TCB OSIdleTaskTCB;
/\* MISCELLANEOUS ---------------------------- \*/
OS_EXT OS_NESTING_CTR OSIntNestingCtr; /* Interrupt nesting level */
#ifdef CPU_CFG_INT_DIS_MEAS_EN
OS_EXT CPU_TS OSIntDisTimeMax; /* Overall interrupt disable time */
#endif
OS_EXT OS_STATE OSRunning; /* Flag indicating that kernel is running */
/\* ISR HANDLER TASK ------------------------- \*/
#if OS_CFG_ISR_POST_DEFERRED_EN > 0u
OS_EXT OS_INT_Q *OSIntQInPtr;
OS_EXT OS_INT_Q *OSIntQOutPtr;
OS_EXT OS_OBJ_QTY OSIntQNbrEntries;
OS_EXT OS_OBJ_QTY OSIntQNbrEntriesMax;
OS_EXT OS_OBJ_QTY OSIntQOvfCtr;
OS_EXT OS_TCB OSIntQTaskTCB;
OS_EXT CPU_TS OSIntQTaskTimeMax;
#endif
示例代码
对于声明了OS_GLOBALS的文件内后面的变量就是定义,否则就是声明;
手机扫一扫
移动阅读更方便
你可能感兴趣的文章