Helmut Tschemernjak / SX1276GenericLib

Dependents:   DISCO-L072CZ-LRWAN1_LoRa_PingPong DISCO-L072CZ-LRWAN1_LoRa_PingPong DISCO-L072CZ-LRWAN1_LoRa_PingPong DISCO-L072CZ-LRWAN1_LoRa_USB_Rx ... more

Fork of SX1276Lib by Semtech

Files at this revision

API Documentation at this revision

Comitter:
Helmut Tschemernjak
Date:
Fri Aug 11 15:11:08 2017 +0200
Parent:
83:019da451b283
Child:
85:93832cc77534
Commit message:
Modified InitSerial() to check if the USB Serial is active,
turn off USB if no SerialUSB is being in use to save power

Changed in this revision

Arduino-mbed-APIs/arduino-d21.cpp Show annotated file Show diff for this revision Revisions of this file
Arduino-mbed-APIs/arduino-mbed.cpp Show annotated file Show diff for this revision Revisions of this file
Arduino-mbed-APIs/arduino-mbed.h Show annotated file Show diff for this revision Revisions of this file
--- a/Arduino-mbed-APIs/arduino-d21.cpp	Sun Aug 06 14:23:43 2017 +0200
+++ b/Arduino-mbed-APIs/arduino-d21.cpp	Fri Aug 11 15:11:08 2017 +0200
@@ -30,13 +30,13 @@
         int fa = f1 ^ xorval;
         uint32_t *first = (uint32_t *)fa;
         uint8_t *dst = (uint8_t *)first;
-        for (int i = 0; i < sizeof(uint32_t); i++)
+        for (int i = 0; i < (int)sizeof(uint32_t); i++)
         	*buf++ = *dst++;
         cnt += 4;
         int fb = f2 ^ xorval;
         uint32_t *next = (uint32_t *)fb;
         dst = (uint8_t *)next;
-        for (int i = 0; i < sizeof(uint32_t)*3; i++)
+        for (int i = 0; i < (int)sizeof(uint32_t)*3; i++)
         	*buf++ = *dst++;
         cnt += 12;
         return cnt;
@@ -369,28 +369,28 @@
      * and if the SerialUSB and connected we should
      * not enter into sleep mode because this kills the Arduino USB emulation
      */
-    if (ser && ser == (Stream *)&SerialUSB) {
-        __WFI();
-        return;
+    SysTick->CTRL &= ~SysTick_CTRL_ENABLE_Msk; // disbale SysTick
+    uint32_t saved_ms = ms_getTicker();
+
+    if (SerialUSB_active) {
+        __DSB(); // ensures the completion of memory accesses
+        __WFI(); // wait for interrupt
         // USB->CTRLA.bit.ENABLE = 0;
         // USB->HOST.CTRLA.reg = 0;
         // USB->HOST.CTRLA.bit.ENABLE &= USB_CTRLA_ENABLE;
-    }
-
-    
-#if  1 // (SAMD20 || SAMD21)
-    /* Errata: Make sure that the Flash does not power all the way down
-     * when in sleep mode. */
-    NVMCTRL->CTRLB.bit.SLEEPPRM = NVMCTRL_CTRLB_SLEEPPRM_DISABLED_Val;
+    } else {
+#if  0 // (SAMD20 || SAMD21)
+        /* Errata: Make sure that the Flash does not power all the way down
+         * when in sleep mode. */
+        NVMCTRL->CTRLB.bit.SLEEPPRM = NVMCTRL_CTRLB_SLEEPPRM_DISABLED_Val;
 #endif
-    uint32_t saved_ms = ms_getTicker();
-    SysTick->CTRL &= ~SysTick_CTRL_ENABLE_Msk; // disbale SysTick
-    
-    SCB->SCR &= ~SCB_SCR_SLEEPDEEP_Msk;	// clear deep sleep
-    PM->SLEEP.reg = 2; // SYSTEM_SLEEPMODE_IDLE_2 IDLE 2 sleep mode.
-
-    __DSB(); // ensures the completion of memory accesses
-    __WFI(); // wait for interrupt
+        
+        SCB->SCR &= ~SCB_SCR_SLEEPDEEP_Msk;	// clear deep sleep
+        PM->SLEEP.reg = 2; // SYSTEM_SLEEPMODE_IDLE_2 IDLE 2 sleep mode.
+        
+        __DSB(); // ensures the completion of memory accesses
+        __WFI(); // wait for interrupt
+    }
     
     int count = ms_getTicker() - saved_ms;
     if (count > 0) { // update the Arduino Systicks
--- a/Arduino-mbed-APIs/arduino-mbed.cpp	Sun Aug 06 14:23:43 2017 +0200
+++ b/Arduino-mbed-APIs/arduino-mbed.cpp	Fri Aug 11 15:11:08 2017 +0200
@@ -12,8 +12,25 @@
 #include "arduino-util.h"
 
 Stream *ser;
-void InitSerial(Stream *serial) {
+bool SerialUSB_active = false;
+
+void InitSerial(Stream *serial, int timeout_ms) {
     ser = serial;
+    if (serial == (Stream *)&SerialUSB) {
+        uint32_t start = ms_getTicker();
+
+        SerialUSB_active = true;
+        while(!SerialUSB) {
+            if (ms_getTicker() > start + timeout_ms) {
+                SerialUSB_active = false;
+                break;
+            }
+        }
+        if (!SerialUSB_active) {
+            USB->DEVICE.CTRLA.bit.SWRST = 1; // disconnect the USB Port
+            while (USB->DEVICE.CTRLA.bit.SWRST == 1);
+        }
+    }
 }
 
 static void pinInt00(void);
--- a/Arduino-mbed-APIs/arduino-mbed.h	Sun Aug 06 14:23:43 2017 +0200
+++ b/Arduino-mbed-APIs/arduino-mbed.h	Fri Aug 11 15:11:08 2017 +0200
@@ -27,8 +27,9 @@
  #define MYdigitalPinToInterrupt(x)	(x)
 #endif
 
-void InitSerial(Stream *serial);
+void InitSerial(Stream *serial, int timeout_ms);
 extern Stream *ser;
+extern bool SerialUSB_active;
 
 /*
  * Arduino_d21.cpp