diff --git a/timer.c b/timer.c index 3daf79c..c6806a2 100644 --- a/timer.c +++ b/timer.c @@ -69,8 +69,11 @@ static void timer_update_nearest_alarm(Timer *tmr) { AlarmAssignment *nearest = NULL; // nearest alarm for (uint32_t i = 0; i < tmr->maxSched; i++) { AlarmAssignment * iter = tmr->alarms + i; + if (iter->id == 0) { // do not consider empty slots + continue; + } int64_t t_i_us = time_to_us(&(iter->time)); - if ((nearest == NULL) && (iter->id != 0)) { // if it's the first one + if (nearest == NULL) { // if it's the first one nearest = iter; min_delta_t_us = t_i_us - t_c_us; } else { // if it's not the first one @@ -103,6 +106,7 @@ uint32_t timer_sched(Timer *tmr, const TimePoint *t, TimerAlarmCb cb, AlarmUserD // increase allocation count tmr->nSched++; + MSG("%d %s\n", tmr->nSched, __FUNCTION__); // replace nearest if needed if (tmr->nSched > 1) { @@ -132,6 +136,7 @@ void timer_unsched(Timer *tmr, uint32_t id) { if (alarm != NULL) { memset(alarm, 0, sizeof(AlarmAssignment)); tmr->nSched--; + MSG("%d %s\n", tmr->nSched, __FUNCTION__); timer_update_nearest_alarm(tmr); } } @@ -176,12 +181,11 @@ void timer_tick(Timer *tmr, int64_t us) { // decrease scheduled alarm count tmr->nSched--; + MSG("%d %s\n", tmr->nSched, __FUNCTION__); // update nearest alarm timer_update_nearest_alarm(tmr); - ETHLIB_OS_MTX_UNLOCK(&(tmr->tabMtx)); - // invoke callback if (alarm.cb != NULL) { alarm.cb(tmr, alarm.params); @@ -190,6 +194,8 @@ void timer_tick(Timer *tmr, int64_t us) { if (tmr->nextAlarm != NULL) { t_alarm = time_to_us(&(tmr->nextAlarm->time)); } + + ETHLIB_OS_MTX_UNLOCK(&(tmr->tabMtx)); } } } diff --git a/timer.h b/timer.h index 4878dfc..5c697f6 100644 --- a/timer.h +++ b/timer.h @@ -10,8 +10,8 @@ * A single point in time */ typedef struct { - uint32_t s; ///< Seconds - uint32_t us; ///< Microseconds + int64_t s; ///< Seconds + int64_t us; ///< Microseconds } TimePoint; /**