0

Sleep SAMD51 - USB doesn't come back
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Sleep SAMD51 - USB doesn't come back

by YoheiStitch on Thu Jul 30, 2020 11:27 am

Hello everyone!

I'm using a SAMD51 board and currently trying to make it sleep to save some power. I'm using Adafruit_SleepyDog library to make it sleep and wakeup, with a basic code. The Built in LED is blinking like it should, meaning that the board is sleeping and waking it up. However, I can't access the USB after it wakes up.


Code: Select all | TOGGLE FULL SIZE
// just to make sure that this isn't the problem
#define __SAMD51__
#define ARDUINO_ARCH_SAMD

#include <Adafruit_SleepyDog.h>


int times = 0;
int sleepMS = 0;

void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, HIGH); // Show we're awake

  Serial.begin(115200);
  while(!Serial); // wait for Arduino Serial Monitor (native USB boards)
  Serial.println("Adafruit Watchdog Library Sleep Demo!");
  Serial.println();

}

void loop() {
  Serial.println("Going to sleep in one second...");
  delay(3000);

//    Serial.println("USBDevice.detach");
//    USBDevice.detach();
  digitalWrite(LED_BUILTIN, LOW); // Show we're asleep
  sleepMS = Watchdog.sleep(4000);
  digitalWrite(LED_BUILTIN, HIGH); // Show we're awake again
    // Try to reattach USB connection on "native USB" boards (connection is
    // lost on sleep). Host will also need to reattach to the Serial monitor.
    // Seems not entirely reliable, hence the LED indicator fallback.
//    USBDevice.init();
//    USBDevice.attach();

  Serial.print("I'm awake now! I slept for ");
  Serial.print(sleepMS, DEC);
  Serial.println(" milliseconds.");
  Serial.println();
  times += 0;
}


I added some comments in the SleepyDog function to know where the USB problem happens. The USB connection happens after the __WFI(), that is when the board goes to sleep, and I do agree that this should happen. The problem is that the USB doesn't come back after it.

Code: Select all | TOGGLE FULL SIZE
sleep: int WatchdogSAMD::sleep(int maxPeriodMS)
    enable: int WatchdogSAMD::enable(int maxPeriodMS, bool isForSleep)
        _initialize_wdt: Before USB->DEVICE.CTRLA.bit.ENABLE = 0;
        _initialize_wdt: After USB->DEVICE.CTRLA.bit.ENABLE = 1;
    enable: #if defined(__SAMD51__)
    enable: After maxPeriodMS and cycles
    enable: #if defined(__SAMD51__)
    enable: if (isForSleep)
sleep: #if defined(__SAMD51__)
sleep: before __DSB() and __WFI()


The next step was to add USBDevice.detach() before the board goes to sleep and USBDevice.init(); USBDevice.attach(); after it awakes. The only difference is that after the board awakes, it stops running and never sleeps again.

Code: Select all | TOGGLE FULL SIZE
    Serial.println("USBDevice.detach");
    USBDevice.detach();
  digitalWrite(LED_BUILTIN, LOW); // Show we're asleep
  sleepMS = Watchdog.sleep(4000);
  digitalWrite(LED_BUILTIN, HIGH); // Show we're awake again
    // Try to reattach USB connection on "native USB" boards (connection is
    // lost on sleep). Host will also need to reattach to the Serial monitor.
    // Seems not entirely reliable, hence the LED indicator fallback.
   USBDevice.init();
   USBDevice.attach();



To make sure that the problem isn't with the USBDevice, I removed the Watchdog and used a simple delay and it worked, the USB "goes out", wait for the delay and them "comes back" and print in the Serial Monitor.
Code: Select all | TOGGLE FULL SIZE
  Serial.println("Going to sleep in three second...");
  delay(2500);
 
  digitalWrite(LED_BUILTIN, LOW); // Show we're asleep
  USBDevice.detach();
  delay(2500);
  digitalWrite(LED_BUILTIN, HIGH); // Show we're awake again
  USBDevice.init();
  USBDevice.attach();
  delay(5000);

  Serial.print("I'm awake now! I slept for ");
  Serial.print(sleepMS, DEC);
  Serial.println(" milliseconds.");
  Serial.println();
  times += 0;

YoheiStitch
 
Posts: 10
Joined: Fri Mar 27, 2020 10:39 am

Re: Sleep SAMD51 - USB doesn't come back

by YoheiStitch on Tue Nov 10, 2020 5:00 pm

Hello everyone,

Anyone knows how to solve this? I still can't get my USB back after the board sleeps with the sleepy dog library...

YoheiStitch
 
Posts: 10
Joined: Fri Mar 27, 2020 10:39 am

Re: Sleep SAMD51 - USB doesn't come back

by BryonMiller on Tue Nov 10, 2020 5:22 pm

You could try looking here. This is discussing a SAMD21 rather than the 51 but it might still apply.

Bryon

BryonMiller
 
Posts: 214
Joined: Fri Mar 04, 2016 10:34 am

Re: Sleep SAMD51 - USB doesn't come back

by YoheiStitch on Wed Nov 11, 2020 12:16 pm

@bryan
BryonMiller wrote:You could try looking here. This is discussing a SAMD21 rather than the 51 but it might still apply.

Bryon


This topic made me realize that I should try to run it on Windows, maybe it would ""work" (opening and closing the serial) as with my MacOS even opening and closing didn`t work. And guess what? This method "worked", thank you. Although this is not the best solution, neither the good, at least it kind of works so its better than before.

BTW, i tried your program and it helped a lot, thank you again.

YoheiStitch
 
Posts: 10
Joined: Fri Mar 27, 2020 10:39 am

Re: Sleep SAMD51 - USB doesn't come back

by BryonMiller on Wed Nov 11, 2020 3:15 pm

Glad I could help.

BTW thanks to the suggestion by @danhalbert in the same thread I started using Tera Term. Works very well and is much more professional than the program I wrote.

BryonMiller
 
Posts: 214
Joined: Fri Mar 04, 2016 10:34 am

Re: Sleep SAMD51 - USB doesn't come back

by YoheiStitch on Thu Nov 12, 2020 9:46 am

BryonMiller, I tested the Terra Form and is indeed a good program to do this. Again, thank you for the suggestion.

YoheiStitch
 
Posts: 10
Joined: Fri Mar 27, 2020 10:39 am

Please be positive and constructive with your questions and comments.