volatile bool enabled;
} comp_cb_t;
+volatile uint32_t scheduled_comp_counter;
+
watch_cb_t tick_callbacks[8];
comp_cb_t comp_callbacks[WATCH_RTC_N_COMP_CB];
watch_cb_t alarm_callback;
comp_callbacks[index].enabled = false;
}
+ scheduled_comp_counter = 0;
+
NVIC_ClearPendingIRQ(RTC_IRQn);
NVIC_EnableIRQ(RTC_IRQn);
}
}
static void _watch_rtc_schedule_next_comp(void) {
- rtc_disable_compare_interrupt();
+ rtc_counter_t curr_counter = watch_rtc_get_counter();
+ // If there is already a pending comp interrupt for this very tick, let it fire
+ // And this function will be called again as soon as the interrupt fires.
+ if (curr_counter == scheduled_comp_counter) {
+ return;
+ }
- // The soonest we can schedule is the next tick
- rtc_counter_t curr_counter = watch_rtc_get_counter() + 1;
+ // Because of the hardware, the soonest we can schedule is the next tick
+ curr_counter +=1;
bool schedule_any = false;
rtc_counter_t comp_counter;
}
if (schedule_any) {
- rtc_enable_compare_interrupt(comp_counter);
+ // If we are changing the comp counter at the front of the line
+ if (comp_counter != scheduled_comp_counter) {
+ scheduled_comp_counter = comp_counter;
+ rtc_enable_compare_interrupt(comp_counter);
+ }
+ } else {
+ scheduled_comp_counter = curr_counter - 2;
+ rtc_disable_compare_interrupt();
}
}
return;
}
- rtc_disable_compare_interrupt();
-
comp_callbacks[index].counter = counter;
comp_callbacks[index].callback = callback;
comp_callbacks[index].enabled = true;
return;
}
- rtc_disable_compare_interrupt();
-
comp_callbacks[index].enabled = false;
_watch_rtc_schedule_next_comp();
}
static void _watch_rtc_schedule_next_comp(void) {
- scheduled_comp_counter = 0;
+ rtc_counter_t curr_counter = watch_rtc_get_counter();
+ // If there is already a pending comp interrupt for this very tick, let it fire
+ // And this function will be called again as soon as the interrupt fires.
+ if (curr_counter == scheduled_comp_counter) {
+ return;
+ }
// The soonest we can schedule is the next tick
- rtc_counter_t curr_counter = watch_rtc_get_counter() + 1;
+ curr_counter +=1;
bool schedule_any = false;
rtc_counter_t comp_counter;
if (schedule_any) {
scheduled_comp_counter = comp_counter;
+ } else {
+ scheduled_comp_counter = curr_counter - 2;
}
}