VxWorks中Timer机制
[摘要] Timer是實時操作系統的一個重要組成部分。本文結合近階段的學習和實驗情況,對VxWorks中的時間函數和定時器作了一些探討。主要介紹了Timer的機制,相關的函數,并給出了一些具體的例子。
?
一. Tick
Tick是指每秒中定時器中斷的次數。POSIX標準中,tick等于50,即每20ms定時器中斷一次。VxWorks中,tick的缺省設置為60。因為實時操作系統中,任務的調度和定時器密切相關,tick設置是否合理對整個系統性能的影響是很明顯的。如果tick太小,則系統實時響應能力較差;反之,如果tick太大,則會使得系統的絕大多數資源浪費在不斷的任務管理和調度中。
Tick的次數在userconfig.c文件中設置,其語句為sysClkRateSet (60)。用戶可以更改這個文件,然后重新編譯BSP庫,也可以在應用程序中更改。
和tick相關的函數主要有:
?
| sysClkRateGet: | 得到每秒系統的tick數 |
| sysClkRateSet: | 設置系統的tick數 |
?
二. 看門狗時鐘(Watchdog Timer)
Watchdog Timer 提供了這樣一種機制,它使一個C函數和一個時間延遲聯系起來。當該時間延遲到達以后,系統會調用該C函數。Watchdog Timer采用了中斷服務進程(ISR)的機理,當C函數被激活時,它是作為ISR運行的。
和Watchdog Timer相關的函數如下:
?
| wdCreate: | 創建Watchdog Timer |
| wdDelete: | 刪除Watchdog Timer |
| wdStart: | 啟動一個Watchdog Timer |
| wdCancel: | 取消一個正在記數的Watchdog Timer |
?
Watchdog使用過程如下:首先調用wdCreate創建一個Watchdog Timer, 然后通過wdStart啟動該Timer。當tick累計到設定的數字時,和它相聯系的C函數被激活作為ISR運行。下面是一個例子,該例子在延遲3秒后輸出一句話:“Watchdog timer just expired”。
例:
#include "VxWorks.h"
#include "logLib.h"
#include "wdLib.h"
#include "taskLib.h"
?
/* defines */
#define? SECONDS (3)
?
WDOG_ID myWatchDogId;
myTask (void)
{?
?????? /* Create watchdog */
?
?????? if ((myWatchDogId = wdCreate()) == NULL)
??????????????? return (ERROR);
?
?????? /* Set timer to go off in SECONDS - printing a message to stdout */
? ?? if (wdStart (myWatchDogId, sysClkRateGet() * SECONDS, logMsg,
?????? ???????????? "Watchdog timer just expired\n") == ERROR)
?????? taskDelay(200);
?????? return (ERROR);
}?
?
三.POSIX Timer
VxWorks提供了和POSIX1003.1b兼容的時間機制。和POSIX Timer相關的主要函數如下:
?
| clock_gettime: | 取得當前時間 |
| clock_settime: | 設置當前時間 |
| timer_create: | 創建定時器 |
| timer_connect: | 將定時器和某個C 函數相連接 |
| timer_cancel: | 取消某個定時器 |
| timer_delete: | 刪除定時器 |
| timer_settime: | 設置Timer的中斷周期 |
?
下面是POSIX Timer的例子。該例子中,myTimer()用來初始化Timer,將myHandler()和tmID Timer相關聯。每隔1秒,myHandler()被調用一次。當myHandler()被調用10次后,它取消并刪除定時器tmID。
POSIX Timer中,定義了兩個重要的結構,它們都在time.h文件中定義。其定義如下:
struct timespec
{
??? ?????????????????????????????????????? /* interval = tv_sec*10**9 + tv_nsec */
??? time_t tv_sec;???????????????? /* seconds */
??? long tv_nsec;??????? ??? /* nanoseconds (0 - 1,000,000,000) */
};
struct itimerspec
??? ????{
????? ??????struct timespec it_interval;????????? /* timer period (reload value) */
????? ??????struct timespec it_value;???????????? /* timer expiration*/
??????? };
?
例子見附錄。
?
四.小結:
VxWorks目前并沒有向我們提供系統的文檔及開發資料,所有的資料只有連機幫助。幫助中對各個系統函數也只作了一個簡單的介紹,對函數中的輸入、輸出、返回值以及函數中用到的各種結構、宏定義都沒有說明。本文中提供的例子及對函數的理解都是通過實驗得出的,可能會有曲解或錯誤的地方,歡迎大家批評指正。
為了測試系統函數,編制了一些小程序,如有興趣者可和我聯系。
VxWorks中并沒有系統地講述其時間機制,而是分散在幫助文件的各個地方,有興趣者可參見以下章節:
2? Tornado 1.0.1 online Manuals -> VxWorks Programmer’s Guide -> Basic Os -> Watchdog Timers
2? Tornado 1.0.1 online Manuals -> VxWorks Programmer’s Guide-> Basic Os -> POSIX Clock and Timers
2? Tornado 1.0.1 online Manuals -> VxWorks Reference -> Libraries and Drivers -> ClockLib
2? Tornado 1.0.1 online Manuals -> VxWorks Reference -> Libraries and Drivers -> sysLib
2? Tornado 1.0.1 online Manuals -> VxWorks Reference -> Libraries and Drivers -> tickLib
2? Tornado 1.0.1 online Manuals -> VxWorks Reference -> Libraries and Drivers -> timerLib
?
五.附錄 vxtimer.c
?
/******************************************************************************
?
???????? 標題: vxtimer.c
???????? 功能: VxWorks timer test programm.
???????? 說明:
?????????????????? This is a test program for VxWorks. In function myTimer, the timer
???????? handler--myHandler is connect to the timer tmId. Once the timer reaches
???????? the set time, myHandler will be called. It will display some infomration
???????? on the screen.
?????????????????? To run this program, just compile it and do as follows:
??????????????????????????? ld<vxtimer.o
??????????????????????????? sp myTimer
???????? 作者: An Yunbo
???????? 日期: 1999/7/14??????????
******************************************************************************/
?
?
#include "VxWorks.h"
#include "time.h"
#include "timers.h"
#include "syslib.h"
#include "logLib.h"
#include "stdio.h"
?
#define COUNT 10
?
/******************************************************************************
?
???????? 標題: myhandler
???????? 功能: the timer handler. it will be called once the set time is
???????? reachted.
???????? 格式:void myHandler(timer_t tmId, int arg).
???????? 輸入:
?????????????????? timer_t tmId: the Id of the set timer.
?????????????????? int arg. A user parameter.
???????? 輸出:
???????? 返回值:
******************************************************************************/
void myHandler(timer_t tmId,int arg)
{
???????? static int iCount=0;
???????? int iRet;
????????
???????? iCount++;
???????? printf("myHandler is called. the arg is????? %d,count is %d\n",arg,iCount);
????????
???????? /* When this funciton is called COUNT times, cancle the timer and
???????? ?? delete it.
???????? */
???????? if(iCount>=COUNT)
???????? {
?????????????????? iRet=timer_cancel(tmId);
?????????????????? if(iRet!=0)
?????????????????? {
??????????????????????????? printf("time_cancel error.\n");
??????????????????????????? return;
?????????????????? }
?????????????????? printf("Timer cancled\n");
?????????????????? timer_delete(tmId);
?????????????????? if(iRet!=0)
?????????????????? {
??????????????????????????? printf("time_delete error.\n");
?????????????????? ???????? return;
?????????????????? }
???????? }
}
?
?
/*************************************************************************
???????? 標題:myTimer
???????? 功能:init timId and connect it with function myHandler.
???????? 格式:void myTimer().
???????? 輸入:
???????? 輸出:
???????? 返回值:
*************************************************************************/
void myTimer()
{
???????? int iRet;
???????? struct timespec???? stTp0,stTp1;
???????? struct itimerspec stValue;
???????? timer_t tmId;
????????
?
???????? /* set current time to 0 second and o nsecond*/
???????? stTp0.tv_sec=0;
???????? stTp0.tv_nsec=0;
???????? iRet=clock_settime(CLOCK_REALTIME, &stTp0);
???????? if(iRet!=0)
???????? {
?????????????????? printf("clock_settime error.\n");
?????????????????? return;
???????? }
????????
???????? iRet=timer_create(CLOCK_REALTIME,NULL,&tmId);
???????? iRet=timer_create(0,NULL,&tmId);
???????? if(iRet!=0)
???????? {
?????????????????? printf("timer_create error.\n");
?????????????????? return;
???????? }
????????
?
???????? /* connect tmId with myHandler*/?
???????? iRet=timer_connect(tmId,myHandler,10);
???????? if(iRet!=0)
???????? {
?????????????????? printf("timer_connect error.\n");
?????????????????? return;
???????? }
????????
???????? /* set interrupt time: 1 second and the first interrup time will be 2
???????? ?? second later
???????? */
???????? stValue.it_interval.tv_sec=1;
???????? stValue.it_interval.tv_nsec=0;
???????? stValue.it_value.tv_sec=2;
???????? stValue.it_value.tv_nsec=0;
???????? iRet=timer_settime(tmId,0,&stValue,0);
???????? if(iRet!=0)
???????? {
?????????????????? printf("timer_settime error.\n");
?????????????????? return;
???????? }
?
???????? /* sleep 10 second and print the remind time when the time interrupt come*/
???????? stTp0.tv_sec=10;
???????? stTp0.tv_nsec=0;
???????? while(1)
???????? {
?????????????????? nanosleep(&stTp0,&stTp1);
?????????????????? printf("tv_sec %ld tv_nsec %ld\n",stTp1.tv_sec,stTp1.tv_nsec);
???????? }
}
總結
以上是生活随笔為你收集整理的VxWorks中Timer机制的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 苹果M2正式发布:8+10核心、2.3倍
- 下一篇: WinCE中得Catalog Items