zoukankan      html  css  js  c++  java
  • 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) + 1;
      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 / 2);
     }
     
     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.

  • 相关阅读:
    webapi Action中跳转到指定内容
    命令行添加防火墙入站规则
    nps简单配置(linux环境)
    C# 执行Javascript脚本
    Docker集群管理(Docker Swarm)
    linux简单的部署frp
    QMap只有insert,而没有append
    SqlServer 数据发布与订阅时一个小问题的处理
    Vue 内部指令缩写
    html中ol li和ul li的区别
  • 原文地址:https://www.cnblogs.com/songdechiu/p/5953015.html
Copyright © 2011-2022 走看看