arduino-libraries / ArduinoLowPower

Powersave features for SAMD boards
GNU Lesser General Public License v2.1
81 stars 57 forks source link

SERCOM stops receiving after sleep #15

Closed ldab closed 5 years ago

ldab commented 5 years ago

Hi,

The sleeping works fine, and after wake up the next function and Loop is called as well the timer Ticker function.

However on the below implementation of a serial bridge the SERCOM2 does not transmit anymore after external wake, my assumption is that it does not get initialized again.

Could it be something that kept running during sleep and overflowed?

ATSAMD21E18A MCU on a Adafruit Trinket M0

#include "Arduino.h"
#include "wiring_private.h"
#include "Ticker.h"
#include "ArduinoLowPower.h"

#define PIN_INT1          (7u)
#define PIN_INT2          (8u)
#define BATT              (23ul)

bool blink  = true;
uint32_t up = 0;

void blinky( void );
void goodNight( void );
void wakefromsleep( void );

Uart Serial_GNSS(&sercom2, 22, 21, SERCOM_RX_PAD_3, UART_TX_PAD_2);

void SERCOM2_Handler()
{
  Serial_GNSS.IrqHandler();
}

Ticker blinkIt( blinky, 800 );
Ticker faster( blinkD, 200 );

void wakefromsleep()
{
  faster.start();
}

void blinky()
{
  digitalWrite( 12, blink );
  blink = !blink;
  up++;

  if( up > 10 )
  {
    up = 0;
    blinkIt.stop();
    Serial_GNSS.end();
    LowPower.sleep();
    Serial_GNSS.begin(9600);
  }

}

void blinkD()
{
  digitalWrite( 12, blink );
  blink = !blink;
}

void setup() {

  blinkIt.start();

  pinMode( 12, OUTPUT );

  // Interrupt 1 to detect when it's moving -> PA00 pin 8
  pinMode(1, INPUT_PULLDOWN);
  LowPower.attachInterruptWakeup(1, wakefromsleep, RISING);

  pinPeripheral(21, PIO_SERCOM);
  pinPeripheral(22, PIO_SERCOM);

  Serial.begin( 9600 );

  Serial_GNSS.begin( 9600 );

}

void loop()
{
  blinkIt.update();
  faster.update();

  // If anything comes in Serial (USB),
  if (Serial.available()) 
  {
    Serial_GNSS.write(Serial.read());   // read it and send it out Serial_GNSS
  }

  // If anything comes in Serial_GNSS
  if (Serial_GNSS.available()) 
  {
    Serial.write(Serial_GNSS.read());   // read it and send it out Serial (USB)
  }

}
ldab commented 5 years ago

I don't think this is working on my case. https://github.com/arduino-libraries/ArduinoLowPower/blob/b38a5bc50c06da903f6614f773d2a676203c7bf2/src/samd/ArduinoLowPower.cpp#L21

Implemented:

    USBDevice.detach();
    LowPower.sleep();
    USBDevice.attach();

And works.