VxWorks中Timer机制

来源:互联网 发布:mysql批量update数据 编辑:程序博客网 时间:2024/05/21 10:13

[摘要] 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

将定时器和某个函数相连接

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中并没有系统地讲述其时间机制,而是分散在帮助文件的各个地方,有兴趣者可参见以下章节:

²  Tornado 1.0.1 online Manuals -> VxWorks Programmer’s Guide -> Basic Os -> Watchdog Timers

²  Tornado 1.0.1 online Manuals -> VxWorks Programmer’s Guide-> Basic Os -> POSIX Clock and Timers

²  Tornado 1.0.1 online Manuals -> VxWorks Reference -> Libraries and Drivers -> ClockLib

²  Tornado 1.0.1 online Manuals -> VxWorks Reference -> Libraries and Drivers -> sysLib

²  Tornado 1.0.1 online Manuals -> VxWorks Reference -> Libraries and Drivers -> tickLib

²  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);

         }

}

0
0 0