生活随笔
收集整理的這篇文章主要介紹了
软定时器的启动与停止
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
文章目錄
1 軟定時(shí)器的啟動(dòng)與停止
軟定時(shí)器單獨(dú)開一個(gè)定時(shí)器任務(wù)來處理,硬定時(shí)器直接放在Systick中斷中進(jìn)行處理。
定時(shí)器啟動(dòng):
定時(shí)器停止:
具體的實(shí)現(xiàn)代碼如下:
#include "tinyOS.h"
#include "tTimer.h"
static tList tTimerHardList
;
static tList tTimerSoftList
;
static tSem tTimerProtectSem
;
static tSem tTimerTickSem
;
void tTimerInit
(tTimer
* timer
, uint32_t delayTicks
, uint32_t durationTicks
,void (*timerFunc
) (void * arg
), void * arg
, uint32_t config
)
{tNodeInit(&timer
->linkNode
);timer
->startDelayTicks
= delayTicks
;timer
->durationTicks
= durationTicks
;timer
->timerFunc
= timerFunc
;timer
->arg
= arg
;timer
->config
= config
;if (delayTicks
== 0){timer
->delayTicks
= durationTicks
;}else{timer
->delayTicks
= timer
->startDelayTicks
;}timer
->state
= tTimerCreated
;
}
void tTimerStart
(tTimer
* timer
)
{switch (timer
->state
){case tTimerCreated
:case tTimerStopped
:timer
->delayTicks
= timer
->startDelayTicks
? timer
->startDelayTicks
: timer
->durationTicks
;timer
->state
= tTimerStarted
;if (timer
->config
& TIMER_CONFIG_TYPE_HARD
){uint32_t status
= tTaskEnterCritical();tListAddLast(&tTimerHardList
, &timer
->linkNode
);tTaskExitCritical(status
);}else{tSemWait(&tTimerProtectSem
, 0);tListAddLast(&tTimerSoftList
, &timer
->linkNode
);tSemNotify(&tTimerProtectSem
);}break;default:break;}
}
void tTimerStop
(tTimer
* timer
)
{switch (timer
->state
){case tTimerStarted
:case tTimerRunning
:if (timer
->config
& TIMER_CONFIG_TYPE_HARD
){uint32_t status
= tTaskEnterCritical();tListRemove(&tTimerHardList
, &timer
->linkNode
);tTaskExitCritical(status
);}else{tSemWait(&tTimerProtectSem
, 0);tListRemove(&tTimerSoftList
, &timer
->linkNode
);tSemNotify(&tTimerProtectSem
);}timer
->state
= tTimerStopped
;break;default:break;}
}
static void tTimerCallFuncList
(tList
* timerList
)
{tNode
* node
;for (node
= timerList
->headNode
.nextNode
; node
!= &(timerList
->headNode
); node
= node
->nextNode
){tTimer
* timer
= tNodeParent(node
, tTimer
, linkNode
);if ((timer
->delayTicks
== 0) || (--timer
->delayTicks
== 0)){timer
->state
= tTimerRunning
;timer
->timerFunc(timer
->arg
);timer
->state
= tTimerStarted
;if (timer
->durationTicks
> 0){timer
->delayTicks
= timer
->durationTicks
;}else{tListRemove(timerList
, &timer
->linkNode
);timer
->state
= tTimerStopped
;}}}
}
static tTask tTimeTask
;
static tTaskStack tTimerTaskStack
[TINYOS_TIMERTASK_STACK_SIZE
];static void tTimerSoftTask
(void * param
)
{for (;;){tSemWait(&tTimerTickSem
, 0);tSemWait(&tTimerProtectSem
, 0);tTimerCallFuncList(&tTimerSoftList
);tSemNotify(&tTimerProtectSem
);}
}
void tTimerModuleTickNotify
(void)
{uint32_t status
= tTaskEnterCritical();tTimerCallFuncList(&tTimerHardList
);tTaskExitCritical(status
);tSemNotify(&tTimerTickSem
);
}
void tTimerModuleInit
(void)
{tListInit(&tTimerHardList
);tListInit(&tTimerSoftList
);tSemInit(&tTimerProtectSem
, 1, 1);tSemInit(&tTimerTickSem
, 0, 0);#if TINYOS_TIMERTASK_PRIO >= (TINYOS_PRO_COUNT - 1)#error "The proprity of timer task must be greater then (TINYOS_PRO_COUNT - 1)"
#endiftTaskInit(&tTimeTask
, tTimerSoftTask
, (void *)0,TINYOS_TIMERTASK_PRIO
, &tTimerTaskStack
[TINYOS_TIMERTASK_STACK_SIZE
]);
}
tTimerModuleTickNotify 需要在中斷函數(shù)中進(jìn)行調(diào)用。
參考資料:
【李述銅】從0到1自己動(dòng)手寫嵌入式操作系統(tǒng)
總結(jié)
以上是生活随笔為你收集整理的软定时器的启动与停止的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。