查看: 1012|回复: 0

Contiki维持最基本功能的移植部分——clock

[复制链接]

该用户从未签到

发表于 2016-2-25 09:23:32 | 显示全部楼层 |阅读模式
分享到:
clock部分是Contiki维持最基本功能的移植部分,也是唯一必需的部分,一般在以下目录
contiki-2.5\platform\

main 函数可知
1、void clock_init()

  • int main(void)
  • {
  •     init_hw();/*硬件初始化--应该复位为最低功耗的硬件设置*/
  •     /*外设设备初始化*/
  •     leds_init();
  •     /* Clock */
  •     clock_init();

  •     /* Initialize process subsystem */
  •     process_init();

  •     /* etimers must be started before ctimer_init */
  •     process_start(&etimer_process, NULL);//设置etimer_process 进程
  •     ctimer_init();//设置ctimer_process 进程

  •     /* 默认程序 */
  •     autostart_start(autostart_processes);

  •     while(1) {
  •      process_run();
  •     }
  • }
复制代码
使用到的GENERAL-PURPOSE TIMERS



再看代码

  • /*---------------------------------------------------------------------------*/
  • void
  • clock_init()
  • {
  •     ADI_GPT_HANDLE hTimer;
  •     ADI_GPT_RESULT_TYPE result;

  •     result = adi_GPT_Init(ADI_GPT_DEVID_1, &hTimer );

  •     if (ADI_GPT_SUCCESS == result)
  •      result = adi_GPT_SetPrescaler(hTimer, ADI_GPT_PRESCALER_16);

  •     if (ADI_GPT_SUCCESS == result)
  •      result = adi_GPT_SetClockSelect(hTimer, ADI_GPT_CLOCK_SELECT_PCLK);

  •     if (ADI_GPT_SUCCESS == result)
  •      result = adi_GPT_SetPeriodicMode( hTimer, true, CLOCK_CONF_SECOND);
  •     /*CLOCK_CONF_SECOND 这里设置中断频率 由contiki-conf.h 中定义
  •      CLOCK_CONF_SECOND=1000   1/1000 s =1ms
  •      也就是1ms中断一次
  •     */

  •     if (ADI_GPT_SUCCESS == result)
  •      result = adi_GPT_SetCountMode(hTimer, ADI_GPT_COUNT_DOWN);

  •     if (ADI_GPT_SUCCESS == result)
  •      adi_GPT_SetTimerEnable(hTimer, true);
  • }
复制代码
很多都是与硬件相关的,知道中断是1ms一次,即可

2、中断函数

  • /* ADucRF101 general purpose Timer 1 interrupt handler */
  • void GP_Tmr1_Int_Handler(void)
  • {
  • ((ADI_TIMER_TypeDef *)ADI_TM1_ADDR)->CLRI = TCLRI_TMOUT_CLR;//中断函数清除中断

  • current_clock++;//次

  • if(etimer_pending() && etimer_next_expiration_time() <= current_clock) {
  •       //timerlist不为空且还没有etimer到期,则执行etimer_request_poll
  • etimer_request_poll();
  •      /* etimer_process实现,实际上调用process_poll(&etimer_process);
  •      触发etimer_process需要运行,到process_run(); 中才得到真正的运行
  •      */
  • }

  • if (--second_countdown == 0) {
  • current_seconds++;//秒
  • second_countdown = CLOCK_SECOND;
  • }
  • }
复制代码
2.1)CLOCK_SECOND定义了1秒钟产生时钟中断的次数,默认情况是32次,可以通过宏CLOCK_CONF_SECOND来配置
2.2)etimer_pending函数检查timerlist是否为空 (若返回true表示不为空),etimer_next_expiration_time函数返回next_expiration(即到了 next_expiration才有etimer到期。另,若timerlist为空则返回0),并与系统当前时间current_clock比较,若 next_expiration小于等于current_clock,则表明还没有etimer到期,于是把系统进程etimer_process的 needspoll设为1,使其具有更快地再次获得执行。


3、clock 其他相关代码  

  • /*---------------------------------------------------------------------------*/
  • clock_time_t
  • clock_time(void)
  • {
  •     return current_clock;
  • }
  • /*---------------------------------------------------------------------------*/
  • unsigned long
  • clock_seconds(void)
  • {
  •     return current_seconds;
  • }
  • /*---------------------------------------------------------------------------*/
  • void
  • clock_delay(unsigned int d)
  • {   /*一般设置为us 级别 延时, 纯延时
  •     */
  •     /* Does not do anything. */
  •     uint32_t tick = (MCK/1000000)-2;
  •     unsigned int i =0;
  •     for(i=0;i<d;i++)
  •      for(;tick>0;tick--);
  • }
  • /*---------------------------------------------------------------------------*/
  • void
  • clock_time_update ( uint32_t count )
  • {
  •     /*
  •     更新 current_seconds 即可
  •     60s *60 m * 24 h * 365 d = 31536000

  •     unsigned long current_seconds
  •     min =0
  •     max = 2^32-1=4294967295

  •     year = 4294967295/31536000 = 136.19 年
  •     */
  •     current_clock += ( count*CLOCK_SECOND );
  •     current_seconds += count;
  • }

  • void
  • clock_time_update_ms ( uint32_t count )
  • {
  •     while(count) {
  •      current_clock ++;

  •      if (--second_countdown == 0) {
  •          current_seconds++;
  •          second_countdown = CLOCK_SECOND;
  •      }
  •      count--;
  •     }
  • }
复制代码
回复

使用道具 举报

您需要登录后才可以回帖 注册/登录

本版积分规则

关闭

站长推荐上一条 /3 下一条

手机版|小黑屋|与非网

GMT+8, 2024-5-7 18:41 , Processed in 0.135784 second(s), 18 queries , MemCache On.

ICP经营许可证 苏B2-20140176  苏ICP备14012660号-2   苏州灵动帧格网络科技有限公司 版权所有.

苏公网安备 32059002001037号

Powered by Discuz! X3.4

Copyright © 2001-2024, Tencent Cloud.