- Timer update next alarm bug fixed

This commit is contained in:
Wiesner András 2023-11-04 20:56:23 +01:00
parent c165389369
commit c01907fdde
2 changed files with 11 additions and 5 deletions

12
timer.c
View File

@ -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));
}
}
}

View File

@ -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;
/**