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

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

         }

}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/435255.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

神经网络与深度学习——TensorFlow2.0实战(笔记)(三)(python常量、变量和表达式)

从程序中学习知识点 1. #支持6中表达形式 数字 字符串 列表 元组 字典 集合 #数字 整型(正整数 负整数 零) #Python3中的整数可以任意大&#xff0c;而不用担心位数不够而导致溢出的情况 intnum12345678909999999999999 print(intnum,type(intnum)) #浮点数 小数 floatnum1 …

reentrantlock非公平锁不会随机挂起线程?_程序员必须要知道的ReentrantLock 及 AQS 实现原理...

专注于Java领域优质技术&#xff0c;欢迎关注作者&#xff1a;Float_Luuu提到 JAVA 加锁&#xff0c;我们通常会想到 synchronized 关键字或者是 Java Concurrent Util(后面简称JCU)包下面的 Lock&#xff0c;今天就来扒一扒 Lock 是如何实现的&#xff0c;比如我们可以先提出一…

关于SOAP的几篇文章

转载自&#xff1a;/show-1598-1.shtml PHP操作soap我总觉得是一件非常痛苦的事情&#xff0c;但没有办法&#xff0c;现在很多功能都是基于WebService的&#xff0c;比如那个amazon的&#xff0c;但其实很多 公司都也还是提供了restful之类的接口&#xff0c;使得PHP与其他系统…

VxWorks动态加载

注&#xff1a;最近在做热补丁的功能&#xff0c;看到一篇gateway写的文章&#xff0c;觉得很通俗易懂的&#xff0c;就将搜集到的资料又整理了一下&#xff0c;供大家参考。 使用动态加载目标模块的方式有很多好处&#xff0c;比如可以在不破坏原来的环境下增加调试定位功能&a…

excel办公常用的宏_让领导看傻!精美168套办公常用excel模板免费领

HR们面试的时候&#xff0c;是不是经常看到应聘者的简历上技能那一栏写着精通Excel、PPT等办公技能&#xff1f;你知道Excel用到什么程度才算精通吗&#xff1f;能够用excel做表格就算精通吗&#xff1f;还是要能够熟练使用各种函数&#xff1f;你做出来的Excel报表也许是这样的…

神经网络与深度学习——TensorFlow2.0实战(笔记)(三)(python运算符和表达式)

从程序中学习知识点 1.算术运算符 #运算符&#xff08;Operator&#xff09;&#xff1a;完成不同类型的常量、变量之间的运算 #除法运算 / 结果是一个浮点型的精确数的值&#xff0c;与java等其他语言的不同之处 print(7/2,7.0/2,-7/2) #整除运算 print(7//2,-7//2) print(7…

VxWorks系统BSP配置文件及生成下载

%A VxWorks BSP主要配置文件 config.h , Makefile 注解和 BSP生成下载实例。%A%A 相关内容可参考 VxWorks BSP和启动过程%A%A config.h文件配置%A%A /*%A This file contains the configuration parameters for the CPU evaluation board.%A */%A%A #ifndef INCconfigh%A #defi…

float32精度_PyTorch 1.6来了:新增自动混合精度训练、Windows版开发维护权移交微软...

刚刚&#xff0c;Facebook 通过 PyTorch 官方博客宣布&#xff1a;PyTorch 1.6 正式发布&#xff01;新版本增加了一个 amp 子模块&#xff0c;支持本地自动混合精度训练。Facebook 还表示&#xff0c;微软已扩大了对 PyTorch 社区的参与&#xff0c;现在拥有 PyTorch 在 Windo…

神经网络与深度学习——TensorFlow2.0实战(笔记)(三)(python语句)

1.if语句 #if语句 x,y 3,5 if x<y:print("x<y") elif xy:print("xy") else:print("x>y") 2.条件表达式 x,y3,5 #表达式1(条件为真的结果) if 判断条件 else 表达式2(条件为假的结果) print(x if x>y else y) 3.while语句 #死循环…

建立调试环境

建立调试环境 Tornado采用支持主机/目标机开发模式。本节以x86系列目标机为例介绍调试环境的建立 。 7.1.1 配置文件config.h 目标机运行的程序包括两部分&#xff1a;引导文件bootrom.sys和操作系统影像文件VxWorks。 引导文件bootrom.sys的主要作用类似于BIOS&#xf…

天气预报的获取

好久没有写技术文章了&#xff0c;2010年工作太忙&#xff0c;奔波在国内各地&#xff0c;也没什么时间关注一些技术方面的事情&#xff0c;最近有一个项目封闭开发&#xff0c;有些时间来写些琐碎的东西&#xff0c;就当是整理下最近的东西吧&#xff0c;没什么技术价值&#…

神经网络与深度学习——TensorFlow2.0实战(笔记)(四)(python列表与元组)

序列数据结构 1.成员是有序排列的 2.每个元素的位置称为下标或索引 3.通过索引访问序列中的成员 4.Python中的序列数据类型有字符串、列表、元组 "abc" ≠ "bca" 5.Python中的列表和元组&#xff0c;可以存放不同类型的数据 列表使用方括号[ ]表示&a…

apktoolkit apk反编译没有文件_[工具] Mac下一键APK逆向环境

安装apktool和dex2jar,jd-guihomebrew安装&#xff1a; brew install apktool brew install dex2jar JD-GUI去http://jd.benow.ca/下载 dmg可能不支持最新版本的mac用不了&#xff0c;打开就报错反编译流程执行脚本apktool d xxx.apk 注&#xff1a;xxx.apk为你要反编译的apk…

搭建你的嵌入式Vxworks开发环境

3.1 最常见的开发环境配置使用串口和网络连接&#xff08;host和target之间&#xff09;。串口连接用于和boot loader之间的通信&#xff08;如输出信息在host上的显示&#xff09;&#xff0c;网络连接用于传输文件&#xff0c;包括Vxworks system image。默认情况下使用网络连…

python开发应用程序错误_Python 程序员经常犯的 10 个错误

常见错误 #6: 不明白Python在闭包中是如何绑定变量的 看下面这个例子&#xff1a; >>> def create_multipliers(): ... return [lambda x : i * x for i in range(5)] >>> for multiplier in create_multipliers(): ... print multiplier(2) ... 你也许希望获…

神经网络与深度学习——TensorFlow2.0实战(笔记)(四)(python字典和集合)

字典和集合 字典 每个字典元素都是一个键(关键字)/值(关键字对应的取值)对 #创建字典 dic_score{"语文":80,"数学":99} #打印 print(dic_score) print(dic_score["语文"]) #长度 print(dic_score.__len__)#错误写法 print(len(dic_score)) #…

VxWorks动态加载.out文件

//Device.cpp #include "other.h" #ifdef __cplusplus extern "C" { #endif int initDevice(char *arg); #ifdef __cplusplus } #endif int initDevice(char *arg) { printf("%s\n", arg); } 生成的.out文件需对其使用如下命令 chmod.exe arx…

[Project Euler]加入欧拉 Problem 9

A Pythagorean triplet is a set of three natural numbers, a b c, for which, a2 b2 c2 For example, 32 42 9 16 25 52. There exists exactly one Pythagorean triplet for which a b c 1000.Find the product abc. 意思很明白找出这样的a, b, c abc 1000 a …

java 在已有的so基础上封装jni_[干货]再见,Android JNI 封装

1 前言2 JNI 速查表2.1 Java 和 Native 数据类型映射表2.2 引用类型3 JNI 理论基础速览4 JNI 常用场景示例4.1 字符串传递(java->native)4.2 字符串返回(native->java)4.3 数组传递(java->native)4.4 其他复杂对象传递(java->native)4.5 复杂对象返回(native->j…

神经网络与深度学习——TensorFlow2.0实战(笔记)(四)(python函数)

函数&#xff08;function) &#xff1a;实现某种特定功能的代码块 优点&#xff1a;程序简洁&#xff0c;可重复调用、封装性好、便于共享 类别&#xff1a;系统函数和用户自定义函数 Python内置函数 数学运算函数 print(abs(-1)) print(pow(2,3)) print(round(3.1415926,…