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

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

}

}

VxWorks中Timer机制相关推荐

  1. VxWorks中信号量实现任务间通信与同步机制分析

    引 言 多任务内核.任务调度机制.任务间通信和中断处理机制,这些都是VxWorks运行环境的核心.多任务处理和任务间通信是实时操作系统的基石.一个多任务环境允许将一个实时应用构造成一套独立任务的集合, ...

  2. Linux内核中锁机制之完成量、互斥量

    在上一篇博文中笔者分析了关于信号量.读写信号量的使用及源码实现,接下来本篇博文将讨论有关完成量和互斥量的使用和一些经典问题. 八.完成量 下面讨论完成量的内容,首先需明确完成量表示为一个执行单元需要等 ...

  3. matlab中的timer模块,[转载]Matlab中Timer的使用

    Matlab中Timer的使用 鉴于Matlab中缺乏多线程机制,使用Timer无疑是一个很重要的工具,Matlab中Timer是一个Java对象. (1)Timer 的定义 t=timer(); 设 ...

  4. 关于C#中timer类 在C#里关于定时器类就有3个

    ·关于C#中timer类  在C#里关于定时器类就有3个   1.定义在System.Windows.Forms里   2.定义在System.Threading.Timer类里   3.定义在Sys ...

  5. C#中timer类的用法

    C#中timer类的用法 关于C#中timer类  在C#里关于定时器类就有3个   1.定义在System.Windows.Forms里   2.定义在System.Threading.Timer类 ...

  6. 关于C#中timer类

    关于C#中timer类  在C#里关于定时器类就有3个   1.定义在System.Windows.Forms里   2.定义在System.Threading.Timer类里   /?" ...

  7. PCI总线在VxWorks中的实现

    8D Spaces Reliability & Stability & Efficiency 目录视图 摘要视图 订阅 PCI总线在VxWorks中的实现 2013-01-22 23: ...

  8. VxWorks中的任务间通信(信号量、共享内存、消息队列、管道、信号、事件、套接字socket)

    文章目录 信号量 共享内存 消息队列 管道 信号 事件 套接字(socket) 总结 VxWorks5.5中,为了保证各个独立的任务可以协同工作,提供了一整套任务间的通信机制,主要包括信号量,共享内存 ...

  9. Java程序中Timer的用法

    Java程序中Timer的用法 import java.io.IOException; import java.util.Timer; public class CheckTimer {/*** @p ...

最新文章

  1. 7 个日常实用的 Shell 拿来就用脚本实例!
  2. mysql sql 1到10_(1.10)SQL优化——mysql 常见SQL优化
  3. 检查型异常(Checked Exception)与非检查型异常(Unchecked Exception)
  4. 求点到直线的最短距离及垂足
  5. 【转】NuGet.org 无法访问的解决方法
  6. linux mount命令参数及用法详解
  7. 【linux】学习笔记
  8. thinkphp3.2.3版本的数据库增删改查实例
  9. Android开发之修改Chrome书签
  10. 00 后 CEO 何以“将马云和马化腾两家一网打尽”
  11. 定位的特殊特性(HTML、CSS)
  12. 高中信息技术——Flash动画制作刷题点整理(二)
  13. s7-200plc编程及应用_西门子S7200PLC编程应用15个为什么
  14. AHP(层次分析法)学习笔记及多层权重Python实践
  15. 浪潮服务器键盘自动输空格,键盘空格键的常用技巧分享
  16. linux 构建ios_如何使用SlackTextViewController构建iOS聊天应用
  17. 案例(一)爬取优美图库风景壁纸
  18. Linux运维笔记-日常操作命令总结(1)
  19. 异常处理(六)--------SpringBoot+Maven项目运行异常:Unable to find a single main class from the following candidat
  20. android ems具体意义?

热门文章

  1. jQuery复选框多选问题
  2. 高级数据库,建库,建表,建约束
  3. 【C++深度剖析教程2】C++经典问题解析之二 this指针与成员函数
  4. maven 解决冲突
  5. NOI2004郁闷的出纳员
  6. 创建好centos7虚拟机之后连xshell连不上虚机
  7. cookie和session 以及Django中应用
  8. Linux下,C++编程论坛题目抽取
  9. Mdi悬浮子窗体不超过主窗体边界
  10. 用BST解决729. My Calendar I 731. My Calendar II 732. My Calendar III