when the timer is already late, schedule the ISR manually immediately.
#include "debugpins.h"
#include <headers/hw_ints.h>
+// ========================== define ==========================================
+
+#define TIMERLOOP_THRESHOLD 0xffffff // 511 seconds @ 32768Hz clock
+
// ========================== variable ========================================
typedef struct {
@@ -45,8 +49,14 @@ void sctimer_set_callback(sctimer_cbt cb){
*/
void sctimer_setCompare(uint32_t val){
IntEnable(INT_SMTIM);
+ if (SleepModeTimerCountGet() - val < TIMERLOOP_THRESHOLD){
+ // the timer is already late, schedule the ISR right now manually
+ IntPendSet(INT_SMTIM);
+ } else {
+ // schedule the timer at val
SleepModeTimerCompareSet(val);
}
+}
/**
\brief Return the current value of the timer's counter.
