Problem with serial communication after wake up from sleep m

Please tell us which board you are using.
For CircuitPython issues, ask in the Adafruit CircuitPython forum.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
MartinMu
 
Posts: 5
Joined: Wed Jun 30, 2021 7:50 am

Problem with serial communication after wake up from sleep m

Post by MartinMu »

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.
This is output of my sketch
This is output of my sketch
output.png (19.83 KiB) Viewed 428 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/18r9QDc ... sp=sharing

Code looks like this

Code: Select all

#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 ?

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

Re: Problem with serial communication after wake up from sle

Post by DotaPie »

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.

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

Re: Problem with serial communication after wake up from sle

Post by BryonMiller »

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

User avatar
MartinMu
 
Posts: 5
Joined: Wed Jun 30, 2021 7:50 am

Re: Problem with serial communication after wake up from sle

Post by MartinMu »

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

[attachment=0]fault.png[/attachment]
Attachments
fault.png
fault.png (35.03 KiB) Viewed 371 times

User avatar
adafruit_support_mike
 
Posts: 67446
Joined: Thu Feb 11, 2010 2:51 pm

Re: Problem with serial communication after wake up from sle

Post by adafruit_support_mike »

Use a

Code: Select all

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.

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

Re: Problem with serial communication after wake up from sle

Post by DotaPie »

adafruit_support_mike wrote:Use a

Code: Select all

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

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

Re: Problem with serial communication after wake up from sle

Post by otecfuras »

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?

User avatar
adafruit_support_mike
 
Posts: 67446
Joined: Thu Feb 11, 2010 2:51 pm

Re: Problem with serial communication after wake up from sle

Post by adafruit_support_mike »

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.

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

Re: Problem with serial communication after wake up from sle

Post by otecfuras »

What do you mean by checking Serial object if it is valid? This:

Code: Select all

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/ ... 00748L.pdf in chapter 2.8.3.
My solution was to add these 3 lines to WatchdogSAMD::sleep

Code: Select all

  OSCCTRL->DFLLVAL.reg = OSCCTRL->DFLLVAL.reg;
  while (OSCCTRL->DFLLSYNC.bit.DFLLVAL);
  while (!OSCCTRL->STATUS.bit.DFLLRDY);
after

Code: Select all

__DSB();
__WFI();

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

Return to “Metro, Metro Express, and Grand Central Boards”