Sleep SAMD51 - USB doesn't come back

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
YoheiStitch
 
Posts: 10
Joined: Fri Mar 27, 2020 10:39 am

Sleep SAMD51 - USB doesn't come back

Post by YoheiStitch »

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

// 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

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

    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

  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;

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

Re: Sleep SAMD51 - USB doesn't come back

Post by YoheiStitch »

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...

User avatar
BryonMiller
 
Posts: 222
Joined: Fri Mar 04, 2016 10:34 am

Re: Sleep SAMD51 - USB doesn't come back

Post by BryonMiller »

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

Bryon

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

Re: Sleep SAMD51 - USB doesn't come back

Post by YoheiStitch »

@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.

User avatar
BryonMiller
 
Posts: 222
Joined: Fri Mar 04, 2016 10:34 am

Re: Sleep SAMD51 - USB doesn't come back

Post by BryonMiller »

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.

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

Re: Sleep SAMD51 - USB doesn't come back

Post by YoheiStitch »

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

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”