Hi there,
In a LoRa (not LoRaWAN) app, I want to set the device to sleep for some time or until an Interrupt on a pin occurs. I have the following code:
static volatile bool lowpower = false;
static volatile uint8_t wakeUpInterrupt1 = INVALID_INTERRUPT_NUM;
static volatile uint8_t wakeUpInterrupt2 = INVALID_INTERRUPT_NUM;
static volatile int8_t wakeUpReason = INVALID_INTERRUPT_NUM;
static TimerEvent_t wakeUp;
void onTimerWakeup()
{
lowpower = false;
wakeUpReason = MY_WAKE_UP_BY_TIMER;
}
void onInterrupt1Wakeup()
{
lowpower = false;
wakeUpReason = wakeUpInterrupt1;
ClearPinInterrupt(wakeUpInterrupt1);
delay(10);
};
void onInterrupt2Wakeup()
{
lowpower = false;
wakeUpReason = wakeUpInterrupt2;
ClearPinInterrupt(wakeUpInterrupt2);
delay(10);
};
int8_t hwSleep(uint32_t ms)
{
wakeUpReason = INVALID_INTERRUPT_NUM;
lowpower = true;
wakeUp.Callback = onTimerWakeup;
if (ms > 0)
{
TimerReset(&wakeUp);
TimerSetValue(&wakeUp, ms);
TimerStart(&wakeUp);
}
while (lowpower)
{
lowPowerHandler;
}
return wakeUpReason;
}
int8_t hwSleep(const uint8_t interrupt, const uint8_t mode, uint32_t ms)
{
wakeUpReason = INVALID_INTERRUPT_NUM;
lowpower = true;
wakeUpInterrupt1 = interrupt;
attachInterrupt(wakeUpInterrupt1, onInterrupt1Wakeup, (IrqModes)mode);
wakeUp.Callback = onTimerWakeup;
if (ms > 0)
{
TimerReset(&wakeUp);
TimerSetValue(&wakeUp, ms);
TimerStart(&wakeUp);
}
while (lowpower)
{
lowPowerHandler;
}
//TimerStop(&wakeUp);
detachInterrupt(interrupt);
return wakeUpReason;
}
void setup() {
boardInitMcu();
Serial.begin(115200);
}
void loop() {
Serial.println("Entering sleep");
uint8_t wakeReason = hwSleep(5000);
Serial.printf("Woken up by %d", wakeReason);
delay(5000);
}
hwSleep(5000);
works perfect. But if I do a hwSleep(5000, USER_KEY, BOTH);
the CubeCell crashes after wakeup, no matter if woken by timer or button. I then have to manually reset it bevore I can upload something.
Any Ideas?
Thanks, Edi