Hello All,
i am using custom board based on samd11d14. It is powered by USB.
I am using USB CDC library from Mattiartech.
It is working, but i observe something strange.
When PC goes in sleep, obviously the usb connection stop. And maybe samd11 start working incorectly...
When PC wake up, connection didnt restore automatically, and i have to reboot PC, so my CDC connection start working again.
is there any good way after PC wake up to reboot samd11 and usb connection automatically?
In the setup i have this code:
void setup() {
// // put your setup code here, to run once:
Serial.begin(9600);
Serial.setTimeout(10);
while (!Serial);```
i put this code in loop, but don't know if it will work
Yes, and there is yet no good way to restart the USB Serial connection after sleep. I've tried various libraries (Adafruit has the best, I think) but none of them worked reliably in my tests.
Deep sleep and USB Serial on the SAMD are not yet compatible, so use an alternative hardware serial port instead, which will survive deep sleep. A Serial1 definition is built in the the Adafruit core and it is easy to define others.
Unfortunately native USB requires the host computer to reestablish the connection to the microcontroller. The console on the Arduino IDE version 1 does reconnect, but requires the somewhat unsatisfactory insertion of half second delay to get it working with the SAMD21:
// Program to test COM port receovery after sleep mode
void setup()
{
pinMode(A1, INPUT_PULLUP); // Intialise button input pin and activate internal pull-up resistor
pinMode(LED_BUILTIN, OUTPUT); // Initialise the LED_BUILTIN output
attachInterrupt(A1, dummyFunc, LOW); // Activate a LOW level interrupt on the button pin
NVMCTRL->CTRLB.bit.SLEEPPRM = NVMCTRL_CTRLB_SLEEPPRM_DISABLED_Val; // Prevent the flash memory from powering down in sleep mode
SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk; // Select standby sleep mode
Serial.begin(115200); // Intialise the native USB port
while (!Serial); // Wait for the console to open
}
void loop()
{
digitalWrite(LED_BUILTIN, LOW); // Turn off the LED
Serial.println(F("Sleeping Zzzz...wait for button to wake")); // Send sleep message to the console
USBDevice.detach(); // Detach the native USB port
SysTick->CTRL &= ~SysTick_CTRL_TICKINT_Msk; // Disable SysTick interrupts
__DSB(); // Ensure remaining memory accesses are complete
__WFI(); // Enter sleep mode and Wait For Interrupt (WFI)
SysTick->CTRL |= SysTick_CTRL_TICKINT_Msk; // Enable SysTick interrupts
USBDevice.attach(); // Re-attach the native USB port
digitalWrite(LED_BUILTIN, HIGH); // Turn on the LED
delay(500); // Wait for half a second (seems to be necessary to give time for the USB port to re-attach)
while(!Serial); // Wait for the console to re-open
Serial.println(); // Add a newline
Serial.println(F("Button depress...waking up")); // Send a wake up message
delay(500); // Wait half a second
}
void dummyFunc() {} // Dummy interrupt service routine