AUTONOMO: SerialUSB use freezes after resuming from deep sleep

I encountered another issue in my project: When the SerialUSB-object is used (for example to println(“some text”) after resuming from deep sleep with an RTC alarm, the execution hangs.

I could reproduce this behavior using the WakeNoISR code example
by just adding in the following lines in the setup method
SerialUSB.println("begin of sketch");

and adding the following line just after blinking the led in the example sketch
SerialUSB.println("this line stops executing the sketch");

Additionally adding the “while(!SerialUSB);” line before the println-line does not resolve this problem.

Is there another method to reinit the SerialUSB?

Are you sure the execution is hanging, or is it that you are not seeing anymore output in the Serial Monitor?

The problem is that we are connecting directly to the main chip via the native USB. So when the board goes to sleep (or resets) the virtual COM port is closed. You have to manually re-open it. (I’m curious if you know of a terminal program that will automatically attempt to reconnect?)

This differs from the AVR board where the connection is via a UART to USB chip (FTDI or similar). If the main CPU is reset or goes to sleep, the FTDI chip still maintains the connection with the PC. So when it wakes or reboots, you start seeing data again.

Also note that the “while(!SerialUSB)” does not test if a USB cable is connected, but tests whether there is an active virtual COM port open.

I double checked and am sure the sketch freezes. The purpose of the line SerialUSB.println(“this line…”); is not really to see its output, because it is almost impossible to see this with the arduino integrated serial port monitor. (Because the USB port is gone during sleep) The purpose is just to show you the loop of the example sketch (which would normally blink the LED every minute) really stops after the first blink if you use that line. Put the println in remarks and the sketch will blink the led every minute.

I believe it is waiting for you to open the Serial Monitor again.
Can you try closing and re-opening the Serial Monitor to see if it resumes?

Ok, did some more testing and your suggestion is true. It is waiting for the port to be opened again. However, this is not really desirable.

Also adding SerialUSB.end() just before disabling the USB did not help. From the first moment SerialUSB.println is used, the sketch is frozen…

Can we change this? This is not really useable… I use the SerialUSB port for occasional debugging/monitoring/communication of/with my sketch and this situation results in a sketch that is depending on a connection to be setup again on the PC…

Ok, doing some more testing, it seems like if you never have setup a serial USB connection (did not open the serial monitor of the arduino IDE during startup) the sketch works as expected. Still, this issue is rather blocking…

This might help, it is a bit more advanced sleep routine that I have developed.

void setupSleep()
  // Set the sleep mode

void systemSleep()
    // Disable USB

    // SAMD sleep

    // Enable USB and wait for resume if attached
    USB->DEVICE.CTRLB.bit.UPRSM = 0x01u;
    while (USB->DEVICE.CTRLB.bit.UPRSM);

Effectively, it tests if the USB cable is attached and does not enter sleep mode if it is. When it wakes it attempts to resume the USB connection. If the cable is not connected it will continue after about 5ms. If the cable is connected, the host machine will recognise the device again.