arduino-libraries / ArduinoLowPower

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

wonky serial connection after wakeup #10

Closed polygamma closed 5 years ago

polygamma commented 5 years ago

Using Arch Linux and a MKR FOX 1200, the following codesnippet yields unwanted output:

#include "ArduinoLowPower.h" // https://www.arduino.cc/en/Reference/ArduinoLowPower
#include "RTCZero.h" // https://www.arduino.cc/en/Reference/RTC

/*
 * initial setup, will be run once at the start of the whole routine
 */
void setup() {
  // init serial connection
  Serial.begin(9600);
  while (!Serial) {}
  RTCZero().begin(false);
}

/*
 * will be run forever, after initial setup() call
 */
void loop() {
  Serial.println("I am being printed, because the device has not been sleeping, yet");

  // deep sleep of 5 seconds
  LowPower.sleep(1000 * 5);

  Serial.println("I am never being printed, because of a deadlock");
}

Expected would be:

I am being printed, because the device has not been sleeping, yet I am never being printed, because of a deadlock I am being printed, because the device has not been sleeping, yet I am never being printed, because of a deadlock I am being printed, because the device has not been sleeping, yet I am never being printed, because of a deadlock I am being printed, because the device has not been sleeping, yet I am never being printed, because of a deadlock I am being printed, because the device has not been sleeping, yet

but I actually get:

I am being printed, because the device has not been sleeping, yet

I am being printed, because the device has not been sleeping, yet I am never being printed, because of a deadlock I am being printed, because the device has not been sleeping, yet

I am being printed, because the device has not been sleeping, yet I am never being printed, because of a deadlock I am being printed, because the device has not been sleeping, yet

I found the cause to be laying here, after the wakeup, the first "print" command times out (sometimes), leading to the call ending here.

Allowing the Low Power library to clear the relevant stuff, as it is already being done here, leads to the Serial connection working properly after the wakeup.

This is the relevant commit for the USBCore to provide the needed functionality.

polygamma commented 5 years ago

Any update?