Contiki Timer & Stimer 模块
一、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 模块的更多相关文章
- [置顶] STM32移植contiki进阶之三(中):timer 中文版
鉴于自己英语水平不高,在这里,将上一篇关于contiki 的timer的文章翻译为中文,让自己在学习的时候,更方便点.文中有许多不是很通顺的地方,将就吧. Timers Contiki系统提供了一套时 ...
- Python 3.X 实现定时器 Timer,制作抽象的Timer定时器基类
Python 在不依赖第三方库的前提下,对于定时器的实现并不是很完美,但是这不意味着我们无法实现. 阅读了网上的一些资料,得出一些结论,顺手写了一个基类的定时器(Python3) BaseTimer: ...
- nrf51822裸机教程-硬件timer
该讲介绍51822的Timer/Counter模块工作在timer模式下(定时器模式,还可以工作为计数器模式) 如何操作 51822的Timer/Counter结构如下图所示 Timer模块从PCLK ...
- Python多线程模块
引言 thread threading 1 Thread 11 下面使用threading模块实现与上面相同的功能 12 在创建新线程时还可以给Thread传递可调用类的对象这样使用类本身来保存信息 ...
- python实现定时任务那些你不知道的模块
一.使用time中的sleep 这种方式最简单,在循环里放入要执行的任务,然后sleep一段时间在执行 from datetime import datetime import time # 每n秒执 ...
- Contiki-Timer 概述
Contiki有一个clock模块和一系列timer模块:timer,stimer,ctimer,etimer,和rtimer. 一.clock模块 clock模块提供一些处理系统时间的函数,还有一些 ...
- etimer
Contiki包含一个时钟模型和5个定时器模型(timer, stimer, ctimer, etimer, and rtimer),先学习etimer吧. etimer是一个结构体,(个人用eve ...
- STM32的DMA
什么是DMA?其全称是:Direct Memory Access:根据ST公司提供的相关信息,DMA是STM32中一个独立与Cortex-M3内核的模块,有点类似与ADC.PWM.TIMER等模块:主 ...
- iOS 高仿:花田小憩3.0.1
前言 断断续续的已经学习Swift一年多了, 从1.2到现在的2.2, 一直在语法之间徘徊, 学一段时间, 工作一忙, 再捡起来隔段时间又忘了.思来想去, 趁着这两个月加班不是特别多, 就决定用swi ...
随机推荐
- 选择一个 Python Web 框架:Django vs Flask vs Pyramid
Pyramid, Django, 和 Flask都是优秀的框架,为项目选择其中的哪一个都是伤脑筋的事.我们将会用三种框架实现相同功能的应用来更容易的对比三者.也可以直接跳到框架实战(Framework ...
- tts和字符集的关系--要求源和目的端的数据库字符集必须一样,国家字符集必须一样。
tts和字符集的关系--要求源和目的端的数据库字符集必须一样,国家字符集必须一样. imp sys/as TRANSPORT_TABLESPACE=Y datafiles= C:\oracle\pro ...
- Notification(二)——PendingIntent的flag导致数据同样的问题
MainActivity例如以下: package cc.cu; import android.os.Bundle; import android.view.View; import android. ...
- GTD实用指南
以前通过余弦大牛博客接触到了GTD, 后来我自己接触之后呢, 我是非常讨厌GTD的, 因为太功利化了 反人类 我还是比较懒得··· 可是最近事情真的比较多,不得不做GTD了 = = 郁闷! 时间管理 ...
- Shell脚本之:替换
转义字符 #!/bin/bash a= echo -e "Value of a is $a \n" 使用-e表示对转义字符进行替换,默认情况是不转义的 命令替换 命令替换的语法,注 ...
- 微信小程序实战 购物车功能
代码地址如下:http://www.demodashi.com/demo/12400.html 一.准备工作 软件环境:微信开发者工具 官方下载地址:https://mp.weixin.qq.com/ ...
- 当Design Support Library遇上RecycleView
近期对Design Support Library中的一些新组件做了些研究,当中涉及到CoordinatorLayout.AppBarLayout.CollapsingToolbarLayout,为了 ...
- XXE攻击
1.背景 现在很多应用都存在XXE(XML External Entity attack)漏洞,就是xml外部实体攻击,比如facebook,很多XML的解析器默认是含有XXE漏洞的. 2.xml的定 ...
- Bootstrp--一个导航面板切换的实用例子
<!--导航区开始--> <ul class="nav nav-tabs nav-stacked" role="tablist"> &l ...
- asp.net core 系列之Reponse caching之cache in-memory (2)
这篇文章(主要翻译于官网,水平有限,见谅)讲解asp.net core 中的 Cache in-memory (内存缓存). Cache in-memory in ASP.NET Core Cachi ...