osalTimeUpdate()解释

void osalTimeUpdate( void )
{
  uint16 tmp;                //为暂存变量,用于临时存放时间值
  uint16 ticks320us;      //用于存放timer2的溢出次数,每次溢出为320us,也就是说ticks320us代表了320us的个数
  uint16 elapsedMSec = 0;  //也是用来存放时间值的,只是它存放的值是上一次操作所保留下来的值,它最终存放的是时间的ms值

  // Get the free-running count of 320us timer ticks
  tmp = macMcuPrecisionCount();   //这个函数就是用来读取timer2溢出次数的,溢出次数存放在T2MOVF2([23:16]),T2MOVF2([15:8]), T2MOVF0([7:0]), 一共有24bit(具体信息请参照cc2530的user guide TIMER2)
  
  if ( tmp != previousMacTimerTick )  //判断时间是否有变化(正常情况下,随着程序的运行,tmp 这个值一直增大(16-bit))
  {
    // Calculate the elapsed ticks of the free-running timer.
    ticks320us = tmp - previousMacTimerTick;  //当前的时间值减去上一次的值,也就是代码再一次运行到这里所消耗的时间(注意单位为320us)
  
    // Store the MAC Timer tick count for the next time through this function.
    previousMacTimerTick = tmp;   //将当前值存储起来,为程序下一次运行到这里作准备
  
    /* It is necessary to loop to convert the usecs to msecs in increments so as 
     * not to overflow the 16-bit variables.
     */
//下面红色部分的代码,都只是实现了一个功能,就是将ticks320us个320us 转换成 elapsedMSec 个ms(ms的整数部分)和remUsTicks 个us(ms的小数部分),“ *8 / 25” 等价于 “ *320 / 1000”(分子分母同时扩大40倍),同理“ *8 % 25” 等价于 “*320 % 1000”  至于MAXCALCTICKS ,程序中有一下两段注释,可以帮助我们理解 
// (MAXCALCTICKS * 8) + (max remainder) must be <= (uint16 max), 
// so: (8188 * 8) + 24 <= 65535
在 OSAL_Clock.c的最上面,宏定义MAXCALCTICKS 为 8188 之前


   while (ticks320us> MAXCALCTICKS )
    {
      ticks320us -= MAXCALCTICKS;
      elapsedMSec += MAXCALCTICKS * 8 / 25;       //这个while是对溢出的操作
      remUsTicks += MAXCALCTICKS * 8 % 25;
    }
  
    // update converted number with remaining ticks from loop and the 
    // accumulated remainder from loop
    tmp = (ticks320us * 8) + remUsTicks;
      
    // Convert the 320 us ticks into milliseconds and a remainder
    elapsedMSec += tmp / 25;                              //这里对的 /25 也相当于 /1000,因为前面已经乘以了个8
    remUsTicks = tmp % 25;                                //如果觉得不容易理解可以代几个数值进去换算一下,有助于理解
  
        
    // Update OSAL Clock and Timers
    if ( elapsedMSec )  //判断时间是否到了1ms,如果等于或者超过1ms(elapsedMSec  >= 1),则需要轮询任务列表
    {
      osalClockUpdate( elapsedMSec );    //更新osal操作系统的系统时间
      osalTimerUpdate( elapsedMSec );   //这个函数和任务的轮询调度有关,即和timeout有关,一句话说不清除,这里先不做具体讲解
    }
  }
}
  • 6
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值