CanFestival 中定时器讨论

void setTimer(TIMEVAL value)//value 值指的是从调用这个函数开始到下一次alarm的时间ms
/******************************************************************************
Set the timer for the next alarm.
INPUT    value TIMEVAL (unsigned long)
OUTPUT    void
******************************************************************************/

TIMEVAL getElapsedTime(void)//返回 从上一次alarm到调用这个函数的 elapsed 时间ms

/******************************************************************************
Return the elapsed time to tell the Stack how much time is spent since last call.
INPUT    void
OUTPUT    value TIMEVAL (unsigned long) the elapsed time
******************************************************************************/

//函数功能调用到期定时器的回调函数,设置下一次alarm的时间

void TimeDispatch(void)
{
    TIMER_HANDLE i;
    TIMEVAL next_wakeup = TIMEVAL_MAX; /* used to compute when should normaly occur next wakeup */
    /* First run : change timer state depending on time */
    /* Get time since timer signal */
    UNS32 overrun = (UNS32)getElapsedTime();

    TIMEVAL real_total_sleep_time = total_sleep_time + overrun;//real_total_sleep_time  到当前位置的真实时间

//total_sleep_time 上一次alarm的预期时间段,是相对上一次设置alarm的时间

    s_timer_entry *row;

//找出那些定时器时间到期,并标记,找出下一次alarm的时间

    for(i=0, row = timers; i <= last_timer_raw; i++, row++)
    {
        if (row->state & TIMER_ARMED) /* if row is active */
        {
            if (row->val <= real_total_sleep_time) /* to be trigged */
            {
                if (!row->interval) /* if simply outdated */
                {
                    row->state = TIMER_TRIG; /* ask for trig */
                }
                else /* or period have expired */
                {
                    /* set val as interval, with 32 bit overrun correction, */
                    /* modulo for 64 bit not available on all platforms     */
                    row->val = row->interval - (overrun % (UNS32)row->interval);
                    row->state = TIMER_TRIG_PERIOD; /* ask for trig, periodic */
                    /* Check if this new timer value is the soonest */
                    if(row->val < next_wakeup)
                        next_wakeup = row->val;
                }
            }
            else
            {
                /* Each armed timer value in decremented. */
                row->val -= real_total_sleep_time;

                /* Check if this new timer value is the soonest */
                if(row->val < next_wakeup)
                    next_wakeup = row->val;
            }
        }
    }

    /* Remember how much time we should sleep. */
    total_sleep_time = next_wakeup;

    /* Set timer to soonest occurence */
    setTimer(next_wakeup);

    /* Then trig them or not. */ //到期的定时器调用相应的回调函数
    for(i=0, row = timers; i<=last_timer_raw; i++, row++)
    {
        if (row->state & TIMER_TRIG)
        {
            row->state &= ~TIMER_TRIG; /* reset trig state (will be free if not periodic) */
            if(row->callback)
                (*row->callback)(row->d, row->id); /* trig ! */
        }
    }
}

 

你可能感兴趣的:(CanFestival 中定时器讨论)