0

Problem with serial communication after wake up from sleep m
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Problem with serial communication after wake up from sleep m

by MartinMu on Wed Jun 30, 2021 9:38 am

Hello,

I am using board Adafruit Grand Central M4 with this chip ATSAMD51P20.
In my sketch I am using this library https://github.com/adafruit/Adafruit_SleepyDog.

The problem occurs when board wakes up from sleep. I want to print some text to serial monitor but there are printed only garbage.
But before first executing sleep function everything work correctly.
As you can see in picture below text "Starting program" and text "test" are written before first sleep.
output.png
This is output of my sketch
output.png (19.83 KiB) Viewed 259 times

Then board sleeps for 3000ms and then after wakes up it should writes again "test" into serial monitor, but from this time there is only garbage.

You can find my sketch also with library and picture of my setup at this link https://drive.google.com/file/d/18r9QDcWGosvV_m6uUr-2uDmKo9FuWC0f/view?usp=sharing

Code looks like this
Code: Select all | TOGGLE FULL SIZE
#include <Adafruit_SleepyDog.h>

void setup() {
 delay(3000);
 
 Serial1.begin(9600);
 Serial1.println("Starting program");
}

void loop() {
  Serial1.println("test");
 
  Serial1.flush();
  Watchdog.sleep(3000);
}


Does anyone have experience with this problem or do you know how to solve it ?

MartinMu
 
Posts: 2
Joined: Wed Jun 30, 2021 7:50 am

Re: Problem with serial communication after wake up from sle

by DotaPie on Thu Jul 01, 2021 12:42 pm

Hey, i am having the same problem, but with different board(s), featuring samd51j19a (metro & itsy bitsy). Seems the result is the same for all the mcu-s in samd51 family.
I am getting the same results as you with your sketch.
Everything (arduino ide, cmsis, boards, library, ...) is up to date.

This really look like some kind of bug indeed.

DotaPie
 
Posts: 2
Joined: Thu Jul 01, 2021 12:38 pm

Re: Problem with serial communication after wake up from sle

by BryonMiller on Fri Jul 02, 2021 8:44 am

take a look at this thread. Might help both of you.

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

Re: Problem with serial communication after wake up from sle

by MartinMu on Fri Jul 02, 2021 10:09 am

Thank you for reply.
I have tried it but it didn't help me. It looks like I have problem with something else, because if I manually close and reopen serial monitor, still I see there only garbage.
Code: Select all | TOGGLE FULL SIZE
[attachment=0]fault.png[/attachment]
Attachments
fault.png
fault.png (35.03 KiB) Viewed 202 times

MartinMu
 
Posts: 2
Joined: Wed Jun 30, 2021 7:50 am

Re: Problem with serial communication after wake up from sle

by adafruit_support_mike on Fri Jul 02, 2021 11:10 pm

Use a

Code: Select all | TOGGLE FULL SIZE
while ( ! Serial ) { delay( 1 ); }
loop before trying to use Serial communication.

The SAMD51 handles USB internally, and if it's sleeping it can't respond to the keep-alive packets from the USB host. The host will assume the device has been disconnected and stop listening. When the microcontroller wakes up, it will have to negotiate a new USB connection with the USB host before it can create a working Serial interface again.

adafruit_support_mike
 
Posts: 63698
Joined: Thu Feb 11, 2010 2:51 pm

Re: Problem with serial communication after wake up from sle

by DotaPie on Mon Jul 05, 2021 4:59 pm

adafruit_support_mike wrote:Use a

Code: Select all | TOGGLE FULL SIZE
while ( ! Serial ) { delay( 1 ); }
loop before trying to use Serial communication.

The SAMD51 handles USB internally, and if it's sleeping it can't respond to the keep-alive packets from the USB host. The host will assume the device has been disconnected and stop listening. When the microcontroller wakes up, it will have to negotiate a new USB connection with the USB host before it can create a working Serial interface again.


I see where you are heading, but as I mentioned before, I am NOT using built-in USB for serial communication, rather external USB TTL. My issue is related to the very basic Serial (let´s assume Serial1 in arduino core world).

DotaPie
 
Posts: 2
Joined: Thu Jul 01, 2021 12:38 pm

Re: Problem with serial communication after wake up from sle

by otecfuras on Fri Jul 23, 2021 5:45 pm

I'm experiencing same issue with my Grand Central. Like guys above I'm not using USBSerial.
I've noticed that if I configure DFLL to keep running in stand-by mode (OSCCTRL->DFLLCTRLA.bit.RUNSTDBY = 1), the problem after wakeup from stand-by (0x4) sleep mode is gone. I guess it has to do something with heavily inaccurate DFLL output after wakeup, which is source for 48Mhz Generic Clock Generator #1, which is also used in SERCOM modules.
This also explains another experiment, that I tried before applying this workaround, where I managed to get it work after wakeup using different!!! baudrates on the UART communication endpoints - for example it "worked" when GC was communicating with baudrate 64000 but PC was set up on 57600.
Can I do something on my GC to make DFLL work accurately after wakeup from stand-by sleep mode?

otecfuras
 
Posts: 2
Joined: Fri Jul 23, 2021 5:23 pm

Re: Problem with serial communication after wake up from sle

by adafruit_support_mike on Sat Jul 31, 2021 7:44 pm

UARTs have to synchronize their clocks before they can communicate. Then each RX side uses its clock to decide when to read the next bit.

Putting a microcontroller in sleep mode breaks clock synchronization. When the microcontroller wakes up it has to re-sync.

Checking the Serial object to see if it’s valid, then calling Serial.begin() once you have a valid object, does that synchronization.

adafruit_support_mike
 
Posts: 63698
Joined: Thu Feb 11, 2010 2:51 pm

Re: Problem with serial communication after wake up from sle

by otecfuras on Fri Aug 06, 2021 9:17 am

What do you mean by checking Serial object if it is valid? This:
Code: Select all | TOGGLE FULL SIZE
class Uart : public HardwareSerial
{
  public:
...
    operator bool() { return true; }
...
? That would hardly help.
And calling SerialX.begin(...) did not help neither.

Problem was in DFLL as I wrote before and finally I found the correct explanation and workaround here https://ww1.microchip.com/downloads/en/DeviceDoc/SAM_D5x_E5x_Family_Silicon_Errata_DS80000748L.pdf in chapter 2.8.3.
My solution was to add these 3 lines to WatchdogSAMD::sleep
Code: Select all | TOGGLE FULL SIZE
  OSCCTRL->DFLLVAL.reg = OSCCTRL->DFLLVAL.reg;
  while (OSCCTRL->DFLLSYNC.bit.DFLLVAL);
  while (!OSCCTRL->STATUS.bit.DFLLRDY);
after
Code: Select all | TOGGLE FULL SIZE
__DSB();
__WFI();

otecfuras
 
Posts: 2
Joined: Fri Jul 23, 2021 5:23 pm

Please be positive and constructive with your questions and comments.