μC/OSII中軟件定時(shí)器的優(yōu)缺點(diǎn)與改進(jìn)
2.2 與軟件定時(shí)器相關(guān)的函數(shù)函數(shù)與任務(wù)的改進(jìn)
2.2.1 軟件定時(shí)器創(chuàng)建函數(shù)OSTmrCreate
在創(chuàng)建函數(shù)OSTmrCreate的參數(shù)中加入優(yōu)先級參數(shù)prio。調(diào)用創(chuàng)建函數(shù)時(shí),對定時(shí)器控制塊中的成員變量賦值,并給回調(diào)函數(shù)數(shù)組的相應(yīng)單元賦值,形式如下:
OSTmrCallbackTbl [prio].OSTmrCallback = callback;
OSTmrCallbackTbl [prio].OSTmrCallbackArg = callback_arg;
OSTmrCallbackTbl [prio].OSTmr = ptmr;
2.2.2 對定時(shí)器任務(wù)OSTmr_Task的改進(jìn)
當(dāng)有定時(shí)器定時(shí)完成,把定時(shí)器優(yōu)先級寫入軟件定時(shí)器就緒表中,并根據(jù)就緒表前后的值判斷時(shí)候發(fā)送信號量OSTmrSemSignal,以激活回調(diào)函數(shù)任務(wù)。任務(wù)OSTmr_Task的流程如圖1所示。
圖1 OSTmr_Task的流程
把定時(shí)器優(yōu)先級寫入定時(shí)器就緒表的代碼如下所示:
if (OSTmrTime == ptmr>OSTmrMatch) {
prio = ptmr>OSTmrPrio;
pfnct =OSTmrCall[prio].OSTmrCallback;
if (pfnct != (OS_TMR_CALLBACK)0) { /*加入定時(shí)器回調(diào)函數(shù)就緒表*/
OSTmrRdyGrp|= (INT8U)(1 《 (INT8U)(prio 》 0x03));
OSTmrRdyTbl[prio >> 0x03]|= (INT8U)(1 《 (INT8U)(prio 0x07));
}
}
2.2.3 對定時(shí)器停止函數(shù)OSTmrStop()的修改
函數(shù)OSTmrStop只需修改與回調(diào)函數(shù)執(zhí)行相關(guān)的部分即可,例如,case OS_TMR_OPT_CALLBACK_ARG: 部分的代碼如下:
case OS_TMR_OPT_CALLBACK_ARG:
prio = ptmr>OSTmrPrio;
pfnct = OSTmrCall[prio].OSTmrCallback;
if (pfnct != (OS_TMR_CALLBACK)0) {
……/*prio加入定時(shí)器就緒表*/
OSTmrCall[prio].OSTmrCallbackArg =(void *)callback_arg;
OSSemPost(OSTmrSemCallback); /*發(fā)送回調(diào)函數(shù)執(zhí)行信號量*/
}else {
*perr = OS_ERR_TMR_NO_CALLBACK;
}
而case OS_TMR_OPT_CALLBACK:部分的代碼同上,只是回調(diào)函數(shù)的參數(shù)不需要重新賦值。
評論