• Contiki Rtimer 模块


    一、rtimer概述

    The Contiki rtimer library provides scheduling and execution of real-time tasks (with predictable execution times)

    数据结构:

    struct rtimer {
      rtimer_clock_t time;
      rtimer_callback_t func;
      void *ptr;
    };
    typedef void (* rtimer_callback_t)(struct rtimer *t, void *ptr);

     1、rtimer_init

    void
    rtimer_init(void)
    {
      rtimer_arch_init();
    }

    rtimer_arch_init是跟平台相关的rtimer_arch_init() is called by the rtimer library to initialize the rtimer architecture code.

    2、rtimer_set

    int
    rtimer_set(struct rtimer *rtimer, rtimer_clock_t time,
           rtimer_clock_t duration,
           rtimer_callback_t func, void *ptr)
    {
      int first = 0;
    
      PRINTF("rtimer_set time %d
    ", time);
    
      if(next_rtimer == NULL) {//空,first置1
        first = 1;
      }
    
      rtimer->func = func;//设置回调函数
      rtimer->ptr = ptr;//回调函数第一个参数
    
      rtimer->time = time;//设置
      next_rtimer = rtimer;//设置next_rtimer
    
      if(first == 1) {//如果是1
        rtimer_arch_schedule(time);
      }
    }

    rtimer用来设置rtimer,即在特定的时间(time)执行特定的回调函数(func)。 

    其中rtimer_arch_schedule函数是跟平台相关的,被底层用来确定调用rtimer_run_next的时间,rtimer_run_next会在特定的时间执行回调函数。

    3、rtimer_run_next

    void
    rtimer_run_next(void)
    {
      struct rtimer *t;
      if(next_rtimer == NULL) {
        return;
      }
      t = next_rtimer;
      next_rtimer = NULL;//重新设置为NULL
      t->func(t, t->ptr);//执行回调函数
      if(next_rtimer != NULL) {//在执行回调函数期间有rtimer set,在调用一次rtimer_arch_schedule
        rtimer_arch_schedule(next_rtimer->time);
      }
      return;
    }

    rtimer_run_next会调用特定的next_rtimer的回调函数。

    rtimer_run_next是在底层被调用的。

    直观的流程:

    rtimer_set(设置rtimer) --->  rtimer_arch_schedule(通知底层在特定时间)    ------>   rtimer_run_next(在特定时间执行回调函数)   ------->   rtimer_set

    注:RTIMER_NOW() RTIMER_SECOND rtimer_arch_init() rtimer_arch_now() rtimer_arch_schedule()等都是跟平台有关的。

  • 相关阅读:
    subprocess(子进程模块)
    logging日志模块,hashlib hash算法相关的库,
    json pickle xml shelve configparser
    os与操作系统进行交互,sys解释器相关,random随机数,shutil解压和压缩
    目录规范+时间模块
    vue的组件
    drf 分页
    包和模块
    docker镜像&nginx配置
    匿名函数 递归 二分法 面向过程编程
  • 原文地址:https://www.cnblogs.com/songdechiu/p/5958046.html
Copyright © 2020-2023  润新知