Quellcode durchsuchen

RP2MCU: Check USB serial connection only once per second

This avoids putting the USB overhead into every loop through
platform_poll() if USB is not connected.

When USB is connected, it still gets served every loop.
Petteri Aimonen vor 7 Monaten
Ursprung
Commit
7238526d55
1 geänderte Dateien mit 27 neuen und 1 gelöschten Zeilen
  1. 27 1
      lib/ZuluSCSI_platform_RP2MCU/ZuluSCSI_platform.cpp

+ 27 - 1
lib/ZuluSCSI_platform_RP2MCU/ZuluSCSI_platform.cpp

@@ -645,6 +645,28 @@ void isr_hardfault(void)
 /* Debug logging and watchdog            */
 /*****************************************/
 
+static bool usb_serial_connected()
+{
+#ifdef PIO_FRAMEWORK_ARDUINO_NO_USB
+    return false;
+#endif
+
+    static bool connected;
+    static uint32_t last_check_time;
+
+#ifdef PLATFORM_MASS_STORAGE
+    if (platform_msc_lock_get()) return connected; // Avoid re-entrant USB events
+#endif
+
+    if (last_check_time == 0 || (uint32_t)(millis() - last_check_time) > 1000)
+    {
+        connected = bool(Serial);
+        last_check_time = millis();
+    }
+
+    return connected;
+}
+
 // Send log data to USB UART if USB is connected.
 // Data is retrieved from the shared log ring buffer and
 // this function sends as much as fits in USB CDC buffer.
@@ -659,6 +681,8 @@ static void usb_log_poll()
 #ifndef PIO_FRAMEWORK_ARDUINO_NO_USB
     static uint32_t logpos = 0;
 
+    if (!usb_serial_connected()) return;
+
 #ifdef PLATFORM_MASS_STORAGE
     if (platform_msc_lock_get()) return; // Avoid re-entrant USB events
 #endif
@@ -686,7 +710,9 @@ static void usb_log_poll()
 // Grab input from USB Serial terminal
 static void usb_input_poll()
 {
-    #ifndef PIO_FRAMEWORK_ARDUINO_NO_USB
+#ifndef PIO_FRAMEWORK_ARDUINO_NO_USB
+
+    if (!usb_serial_connected()) return;
 
 #ifdef PLATFORM_MASS_STORAGE
     if (platform_msc_lock_get()) return; // Avoid re-entrant USB events