主要讲解时间管理的5个系统函数,时间管理最主要的功能是对任务进行延时。
1、时间的获取和设置
1 INT32U OSTimeGet (void) 2 { 3 INT32U ticks; 4 #if OS_CRITICAL_METHOD == 3u /* Allocate storage for CPU status register */ 5 OS_CPU_SR cpu_sr = 0u; 6 #endif 7 8 9 10 OS_ENTER_CRITICAL(); 11 ticks = OSTime; 12 OS_EXIT_CRITICAL(); 13 return (ticks); 14 } 15 16 17 void OSTimeSet (INT32U ticks) 18 { 19 #if OS_CRITICAL_METHOD == 3u /* Allocate storage for CPU status register */ 20 OS_CPU_SR cpu_sr = 0u; 21 #endif 22 23 24 25 OS_ENTER_CRITICAL(); 26 OSTime = ticks; 27 OS_EXIT_CRITICAL(); 28 }
2、任务延时函数OSTimeDly
1 void OSTimeDly (INT32U ticks) 2 { 3 INT8U y; 4 #if OS_CRITICAL_METHOD == 3u /* Allocate storage for CPU status register */ 5 OS_CPU_SR cpu_sr = 0u; 6 #endif 7 8 9 10 if (OSIntNesting > 0u) { /* See if trying to call from an ISR */ 11 return; 12 } 13 if (OSLockNesting > 0u) { /* See if called with scheduler locked */ 14 return; 15 } 16 if (ticks > 0u) { /* 0 means no delay! */ 17 OS_ENTER_CRITICAL(); 18 y = OSTCBCur->OSTCBY; /* Delay current task */ 19 OSRdyTbl[y] &= (OS_PRIO)~OSTCBCur->OSTCBBitX; 20 if (OSRdyTbl[y] == 0u) { 21 OSRdyGrp &= (OS_PRIO)~OSTCBCur->OSTCBBitY; 22 } 23 OSTCBCur->OSTCBDly = ticks; /* Load ticks in TCB */ 24 OS_EXIT_CRITICAL(); 25 OS_Sched(); /* Find next task to run! */ 26 } 27 }
3、任务按分秒延迟函数OSTimeDlyHMSM
1 INT8U OSTimeDlyHMSM (INT8U hours, 2 INT8U minutes, 3 INT8U seconds, 4 INT16U ms) 5 { 6 INT32U ticks; 7 8 9 if (OSIntNesting > 0u) { /* See if trying to call from an ISR */ 10 return (OS_ERR_TIME_DLY_ISR); 11 } 12 if (OSLockNesting > 0u) { /* See if called with scheduler locked */ 13 return (OS_ERR_SCHED_LOCKED); 14 } 15 #if OS_ARG_CHK_EN > 0u 16 if (hours == 0u) { 17 if (minutes == 0u) { 18 if (seconds == 0u) { 19 if (ms == 0u) { 20 return (OS_ERR_TIME_ZERO_DLY); 21 } 22 } 23 } 24 } 25 if (minutes > 59u) { 26 return (OS_ERR_TIME_INVALID_MINUTES); /* Validate arguments to be within range */ 27 } 28 if (seconds > 59u) { 29 return (OS_ERR_TIME_INVALID_SECONDS); 30 } 31 if (ms > 999u) { 32 return (OS_ERR_TIME_INVALID_MS); 33 } 34 #endif 35 /* Compute the total number of clock ticks required.. */ 36 /* .. (rounded to the nearest tick) */ 37 ticks = ((INT32U)hours * 3600uL + (INT32U)minutes * 60uL + (INT32U)seconds) * OS_TICKS_PER_SEC 38 + OS_TICKS_PER_SEC * ((INT32U)ms + 500uL / OS_TICKS_PER_SEC) / 1000uL; 39 OSTimeDly(ticks); 40 return (OS_ERR_NONE); 41 }
4、延时恢复函数OSTimeDlyResume
1 INT8U OSTimeDlyResume (INT8U prio) 2 { 3 OS_TCB *ptcb; 4 #if OS_CRITICAL_METHOD == 3u /* Storage for CPU status register */ 5 OS_CPU_SR cpu_sr = 0u; 6 #endif 7 8 9 10 if (prio >= OS_LOWEST_PRIO) { 11 return (OS_ERR_PRIO_INVALID); 12 } 13 OS_ENTER_CRITICAL(); 14 ptcb = OSTCBPrioTbl[prio]; /* Make sure that task exist */ 15 if (ptcb == (OS_TCB *)0) { 16 OS_EXIT_CRITICAL(); 17 return (OS_ERR_TASK_NOT_EXIST); /* The task does not exist */ 18 } 19 if (ptcb == OS_TCB_RESERVED) { 20 OS_EXIT_CRITICAL(); 21 return (OS_ERR_TASK_NOT_EXIST); /* The task does not exist */ 22 } 23 if (ptcb->OSTCBDly == 0u) { /* See if task is delayed */ 24 OS_EXIT_CRITICAL(); 25 return (OS_ERR_TIME_NOT_DLY); /* Indicate that task was not delayed */ 26 } 27 28 ptcb->OSTCBDly = 0u; /* Clear the time delay */ 29 if ((ptcb->OSTCBStat & OS_STAT_PEND_ANY) != OS_STAT_RDY) { 30 ptcb->OSTCBStat &= ~OS_STAT_PEND_ANY; /* Yes, Clear status flag */ 31 ptcb->OSTCBStatPend = OS_STAT_PEND_TO; /* Indicate PEND timeout */ 32 } else { 33 ptcb->OSTCBStatPend = OS_STAT_PEND_OK; 34 } 35 if ((ptcb->OSTCBStat & OS_STAT_SUSPEND) == OS_STAT_RDY) { /* Is task suspended? */ 36 OSRdyGrp |= ptcb->OSTCBBitY; /* No, Make ready */ 37 OSRdyTbl[ptcb->OSTCBY] |= ptcb->OSTCBBitX; 38 OS_EXIT_CRITICAL(); 39 OS_Sched(); /* See if this is new highest priority */ 40 } else { 41 OS_EXIT_CRITICAL(); /* Task may be suspended */ 42 } 43 return (OS_ERR_NONE); 44 }