一、Timer API

struct timer {
clock_time_t start;
clock_time_t interval;
}; CCIF void timer_set(struct timer *t, clock_time_t interval);
void timer_reset(struct timer *t);
void timer_restart(struct timer *t);
CCIF int timer_expired(struct timer *t);
clock_time_t timer_remaining(struct timer *t);

The Contiki timer library provides functions for setting, resetting and restarting timers, and for checking if a timer has expired. An application must "manually" check if its timers have expired; this is not done automatically. The timer library use clock_time() in the clock module to get the current system time.

timer结构体,API中都是通过指针访问声明的timer。

void
timer_set(struct timer *t, clock_time_t interval)
{
t->interval = interval;
t->start = clock_time();
}

The API for the Contiki timer library is shown below. A timer is always initialized by a call totimer_set() which sets the timer to expire the specified delay from current time and also stores the time interval in the timer.

int
timer_expired(struct timer *t)
{
/* Note: Can not return diff >= t->interval so we add 1 to diff and return
t->interval < diff - required to avoid an internal error in mspgcc. */
clock_time_t diff = (clock_time() - t->start) + ;
return t->interval < diff; }

timer_expired用于判断timer是否到期。

void
timer_reset(struct timer *t)
{
t->start += t->interval;
} void
timer_restart(struct timer *t)
{
t->start = clock_time();
}

timer_reset将timer重置为从上次到期(expired)的时间开始计时。

timer_restart将timer重置为从当前时间开始计时。

The difference between these functions is that timer_reset() set the timer to expire at exactly the same time interval while timer_restart() set the timer to expire some time interval from current time, thus allowing time drift.

clock_time_t
timer_remaining(struct timer *t)
{
return t->start + t->interval - clock_time();
}

timer_remaining用于获得离timer到期的间隔(return the time until the timer expires)

注:timer使用的是clock tick,当前时间是通过clock_time()获取的,时间变量类型都是clock_time_t

二、中断中使用timer

The timer library can safely be used from interrupts

例子如下:

 #include "sys/timer.h"

 static struct timer rxtimer;

 void init(void) {
timer_set(&rxtimer, CLOCK_SECOND / );
} interrupt(UART1RX_VECTOR)
uart1_rx_interrupt(void)
{
if(timer_expired(&rxtimer)) {
/* Timeout */
/* ... */
}
timer_restart(&rxtimer);
/* ... */
}

三、Stimer 模块

Stimer模块和Time基本一样,唯一的区别就是,Timer用的是clock ticks,Stimer用的是Second。

struct stimer {
unsigned long start;
unsigned long interval;
}; void stimer_set(struct stimer *t, unsigned long interval);
void stimer_reset(struct stimer *t);
void stimer_restart(struct stimer *t);
int stimer_expired(struct stimer *t);
unsigned long stimer_remaining(struct stimer *t);
unsigned long stimer_elapsed(struct stimer *t);
void
stimer_set(struct stimer *t, unsigned long interval)
{
t->interval = interval;
t->start = clock_seconds();
}

如上所示,stimer结构体中的变量start和interval是unsigned long型的。

以及获取当前时间是用clock_seconds函数。

The stimer library can safely be used from interrupts.

Contiki Timer & Stimer 模块的更多相关文章

  1. [置顶] STM32移植contiki进阶之三(中):timer 中文版

    鉴于自己英语水平不高,在这里,将上一篇关于contiki 的timer的文章翻译为中文,让自己在学习的时候,更方便点.文中有许多不是很通顺的地方,将就吧. Timers Contiki系统提供了一套时 ...

  2. Python 3.X 实现定时器 Timer,制作抽象的Timer定时器基类

    Python 在不依赖第三方库的前提下,对于定时器的实现并不是很完美,但是这不意味着我们无法实现. 阅读了网上的一些资料,得出一些结论,顺手写了一个基类的定时器(Python3) BaseTimer: ...

  3. nrf51822裸机教程-硬件timer

    该讲介绍51822的Timer/Counter模块工作在timer模式下(定时器模式,还可以工作为计数器模式) 如何操作 51822的Timer/Counter结构如下图所示 Timer模块从PCLK ...

  4. Python多线程模块

    引言 thread threading 1 Thread 11 下面使用threading模块实现与上面相同的功能 12 在创建新线程时还可以给Thread传递可调用类的对象这样使用类本身来保存信息 ...

  5. python实现定时任务那些你不知道的模块

    一.使用time中的sleep 这种方式最简单,在循环里放入要执行的任务,然后sleep一段时间在执行 from datetime import datetime import time # 每n秒执 ...

  6. Contiki-Timer 概述

    Contiki有一个clock模块和一系列timer模块:timer,stimer,ctimer,etimer,和rtimer. 一.clock模块 clock模块提供一些处理系统时间的函数,还有一些 ...

  7. etimer

     Contiki包含一个时钟模型和5个定时器模型(timer, stimer, ctimer, etimer, and rtimer),先学习etimer吧. etimer是一个结构体,(个人用eve ...

  8. STM32的DMA

    什么是DMA?其全称是:Direct Memory Access:根据ST公司提供的相关信息,DMA是STM32中一个独立与Cortex-M3内核的模块,有点类似与ADC.PWM.TIMER等模块:主 ...

  9. iOS 高仿:花田小憩3.0.1

    前言 断断续续的已经学习Swift一年多了, 从1.2到现在的2.2, 一直在语法之间徘徊, 学一段时间, 工作一忙, 再捡起来隔段时间又忘了.思来想去, 趁着这两个月加班不是特别多, 就决定用swi ...

随机推荐

  1. 2016.7.12 eclipse和IDEA中mybatis generator插件的安装与使用

    Eclipse中的安装 http://jingyan.baidu.com/article/9faa7231506ed8473c28cbee.html 1.下载插件 2.将插件generator的fea ...

  2. maven仓库中有jar包pom还报错

    maven仓库中有jar包pom还报错 就报错,咋啦? 这个包来源不明,自己拷贝进来的吧?你当我mvn是傻子?我要去网上验证一下: 我自己有个_remote.respositories文件,如果自己用 ...

  3. HDU 4927 大数

    题意非常easy: 对于长度为n的数.做n-1遍.生成的新数列: b1=a2-a1   b2=a3-a2  b3=a4-a3 c1=b2-b1   c2=b3-b2 ans=c2-c1 最后推出公式: ...

  4. Python & Django & Pycharm 安装

    一.下载安装Python 从https://www.python.org/上下载 Python 2.7.6,双击安装包开始安装: 单击“Next”按钮,进入Python安装组件选择界面.这里我们安装全 ...

  5. UNP学习笔记(第二十五章 信号驱动式I/O)

    信号驱动式I/O是指进程预先告知内核,使得当某个描述符发生某事时,内核使用信号通知相关进程. 套接字的信号驱动式I/O 针对一个套接字使用信号驱动式I/O(SIGIO)要求进程执行以下3个步骤: 1. ...

  6. JVM内存最大能调多大分析【经典】

       http://hi.baidu.com/suofang/blog/item/49c637c71c0afbd0d1006028.html  上次用weblogic 把 -XmxXXXX 设成2G, ...

  7. [读书笔记] learn python the hard way书中 有关powershell 的一些小问题

    ex46中,创建自己的python,  当你激活环境时 .\.venvs\lpthw\ Scripts\activate 会报一个错误 此时需要以管理员身份运行PowerShell,(当前的PS不用关 ...

  8. nginx 代理模式下,获取客户端真实IP

    最近做博友推荐,发现个小问题,用$_SERVER['REMOTE_ADDR'];得到的都是服务器的地址192.168.96.52,搜索了一下,发现问题,改为$_SERVER['HTTP_X_REAL_ ...

  9. html5 cocos2d js Access-Control-Allow-Origin

    1.No 'Access-Control-Allow-Origin' header is present on the requested 近期在接html5的渠道,遇到了跨域的问题,使用 js 的 ...

  10. jquery瀑布流布局插件,兼容ie6不支持下拉加载,用于制作分类卡

    调用方法 $('需要布局的块').sault() 如果要在图片加载后调用需要使用$(window).load(function(fx){});函数,即等待图片加载完成再调用 3个参数 1.left:左 ...