From ad53f65e64e643ed26839fcd568c1a871eaf5326 Mon Sep 17 00:00:00 2001
From: Jonni Westphalen <jonny.westphalen@googlemail.com>
Date: Fri, 12 Oct 2018 22:44:48 +0200
Subject: [PATCH 1/2] fix wrong assumption

pin > PINS_COUNT may yield valid pins for interrupts
---
 src/samd/ArduinoLowPower.cpp | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)

diff --git a/src/samd/ArduinoLowPower.cpp b/src/samd/ArduinoLowPower.cpp
index d756ed6..20680f8 100644
--- a/src/samd/ArduinoLowPower.cpp
+++ b/src/samd/ArduinoLowPower.cpp
@@ -57,7 +57,9 @@ void ArduinoLowPowerClass::setAlarmIn(uint32_t millis) {
 
 void ArduinoLowPowerClass::attachInterruptWakeup(uint32_t pin, voidFuncPtr callback, uint32_t mode) {
 
-	if (pin > PINS_COUNT) {
+    EExt_Interrupts in = g_APinDescription[pin].ulExtInt;
+
+    if (in == NOT_AN_INTERRUPT || in == EXTERNAL_INT_NMI) {
 		// check for external wakeup sources
 		// RTC library should call this API to enable the alarm subsystem
 		switch (pin) {
@@ -69,10 +71,6 @@ void ArduinoLowPowerClass::attachInterruptWakeup(uint32_t pin, voidFuncPtr callb
 		return;
 	}
 
-	EExt_Interrupts in = g_APinDescription[pin].ulExtInt;
-	if (in == NOT_AN_INTERRUPT || in == EXTERNAL_INT_NMI)
-    		return;
-
 	//pinMode(pin, INPUT_PULLUP);
 	attachInterrupt(pin, callback, mode);
 

From d39fa7ed5e5c71cf771087865f8c7fe5406dcb2a Mon Sep 17 00:00:00 2001
From: Jonni Westphalen <jonny.westphalen@googlemail.com>
Date: Sun, 14 Oct 2018 00:22:31 +0200
Subject: [PATCH 2/2] this commit fixes broken serial connections after
 sleeping

together with https://github.com/polygamma/ArduinoCore-samd/commit/037b770928c9632f91e5e3f793eb0f1fc021915a
---
 src/samd/ArduinoLowPower.cpp | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/src/samd/ArduinoLowPower.cpp b/src/samd/ArduinoLowPower.cpp
index 20680f8..ba63b50 100644
--- a/src/samd/ArduinoLowPower.cpp
+++ b/src/samd/ArduinoLowPower.cpp
@@ -18,6 +18,7 @@ void ArduinoLowPowerClass::idle(uint32_t millis) {
 void ArduinoLowPowerClass::sleep() {
 	bool restoreUSBDevice = false;
 	if (SERIAL_PORT_USBVIRTUAL) {
+		SERIAL_PORT_USBVIRTUAL.flush();
 		USBDevice.standby();
 	} else {
 		USBDevice.detach();
@@ -29,6 +30,9 @@ void ArduinoLowPowerClass::sleep() {
 	if (restoreUSBDevice) {
 		USBDevice.attach();
 	}
+	if (SERIAL_PORT_USBVIRTUAL) {
+		SERIAL_PORT_USBVIRTUAL.clear();
+	}
 }
 
 void ArduinoLowPowerClass::sleep(uint32_t millis) {