arduino-libraries / ArduinoLowPower

Powersave features for SAMD boards
GNU Lesser General Public License v2.1
80 stars 57 forks source link

attaching an InterruptWakeup before and after deepSleep breaks RTCZero::getEpoch #58

Open usbpc opened 1 year ago

usbpc commented 1 year ago

I tested the following code on an MKR 1310:

#include <RTCZero.h>
#include <ArduinoLowPower.h>

void noop() {}

void setup() {
    Serial1.begin(115200);
    RTCZero rtc;
    rtc.begin();
    LowPower.attachInterruptWakeup(LORA_IRQ, noop, RISING);
    LowPower.deepSleep(1000);
    LowPower.attachInterruptWakeup(LORA_IRQ, noop, RISING);
    Serial1.println("This gets printed!");
    Serial1.println(rtc.getEpoch());
    Serial1.println("This never gets printed!");
}

void loop() {}

The first Serial1.println works and I get the message. But then reading the Epoch just results in an Endless Loop right here: https://github.com/arduino-libraries/RTCZero/blob/master/src/RTCZero.cpp#L488

So neither the Epoch nor the last Serial1.println get transmitted over Serial.

The code is just a minimal example of something that works around another problem where another library is overwriting the wakeup interrupt handler with a wrong one.

usbpc commented 1 year ago

I found a workaround, if I deep sleep before attatching the first Interrupt it works as I would expect.

usbpc commented 1 year ago

The problem is not only with RTCZero, but also other parts of the system. I can get the Arduino stuck with this code aswell:

#include <LoRa.h>
#include <ArduinoLowPower.h>

void noop() {}

void setup() {
    Serial1.begin(115200);
    LowPower.deepSleep(1000);
    LowPower.attachInterruptWakeup(LORA_IRQ, noop, RISING);
    LowPower.deepSleep(1000);
    LoRa.begin(869587500);
    LowPower.attachInterruptWakeup(LORA_IRQ, noop, RISING);
    Serial1.println("This gets printed!");
    LoRa.receive();
    Serial1.println("This never gets printed!");
}

void loop() {}

This time it gets stuck in the LoRa.receive() call, some more investigation seems to indicate that in this case it is a problem with the SPI communication specifically it seems to loop indefinitely in this loop: https://github.com/arduino/ArduinoCore-samd/blob/a5d52dbf7bcf338de2042eb74cfba33993729c81/cores/arduino/SERCOM.cpp#L333

Any ideas on how I can debug this further to get the problem fixed quicker would be awesome.