Pārlūkot izejas kodu

Merge pull request #478 from ZuluSCSI/feature/switch-to-blink-polling

Switch activity LED blinking from delay to polling
Alex Perez 11 mēneši atpakaļ
vecāks
revīzija
a9d8879c7e

+ 23 - 0
lib/ZuluSCSI_platform_GD32F205/ZuluSCSI_platform.cpp

@@ -44,6 +44,7 @@ static bool g_enable_apple_quirks = false;
 bool g_direct_mode = false;
 ZuluSCSIVersion_t g_zuluscsi_version = ZSVersion_unknown;
 bool g_moved_select_in = false;
+static bool g_led_blinking = false;
 // hw_config.cpp c functions
 #include "platform_hw_config.h"
 
@@ -459,6 +460,28 @@ void platform_post_sd_card_init()
     #endif
 }
 
+void platform_write_led(bool state)
+{
+    if (g_led_blinking) return;
+    if (state)
+        gpio_bit_reset(LED_PORT, LED_PINS);
+    else
+        gpio_bit_set(LED_PORT, LED_PINS);
+}
+
+void platform_set_blink_status(bool status)
+{
+    g_led_blinking = status;
+}
+
+void platform_write_led_override(bool state)
+{
+    if (state)
+        gpio_bit_reset(LED_PORT, LED_PINS);
+    else
+        gpio_bit_set(LED_PORT, LED_PINS);
+}
+
 void platform_disable_led(void)
 {   
     gpio_init(LED_PORT, GPIO_MODE_IPU, 0, LED_PINS);

+ 11 - 0
lib/ZuluSCSI_platform_GD32F205/ZuluSCSI_platform.h

@@ -84,6 +84,17 @@ void platform_late_init();
 // Initialization after the SD Card has been found
 void platform_post_sd_card_init();
 
+// Set the status LED only if it is not in a blinking routine
+void platform_write_led(bool state);
+#define LED_ON()  platform_write_led(true)
+#define LED_OFF() platform_write_led(false)
+// Used by the blinking routine
+void platform_set_blink_status(bool status);
+// LED override will set the status LED regardless of the blinking routine
+void platform_write_led_override(bool state);
+#define LED_ON_OVERRIDE()  platform_write_led_override(true)
+#define LED_OFF_OVERRIDE()  platform_write_led_override(false)
+
 // Disable the status LED
 void platform_disable_led(void);
 

+ 0 - 2
lib/ZuluSCSI_platform_GD32F205/ZuluSCSI_v1_0_gpio.h

@@ -146,8 +146,6 @@
 #define LED_I_PIN    GPIO_PIN_4
 #define LED_E_PIN    GPIO_PIN_5
 #define LED_PINS     (LED_I_PIN | LED_E_PIN)
-#define LED_ON()     gpio_bit_reset(LED_PORT, LED_PINS)
-#define LED_OFF()    gpio_bit_set(LED_PORT, LED_PINS)
 
 // Ejection buttons are available on expansion header J303.
 // PE5 = channel 1, PE6 = channel 2

+ 0 - 2
lib/ZuluSCSI_platform_GD32F205/ZuluSCSI_v1_1_gpio.h

@@ -292,8 +292,6 @@
 #define LED_I_PIN    GPIO_PIN_4
 #define LED_E_PIN    GPIO_PIN_5
 #define LED_PINS     (LED_I_PIN | LED_E_PIN)
-#define LED_ON()     gpio_bit_reset(LED_PORT, LED_PINS)
-#define LED_OFF()    gpio_bit_set(LED_PORT, LED_PINS)
 #define LED_EJECT_PORT  GPIOA
 #define LED_EJECT_PIN   GPIO_PIN_1
 #define LED_EJECT_ON()  gpio_bit_reset(LED_EJECT_PORT, LED_EJECT_PIN)

+ 24 - 0
lib/ZuluSCSI_platform_GD32F450/ZuluSCSI_platform.cpp

@@ -39,6 +39,7 @@ extern "C" {
 
 const char *g_platform_name = PLATFORM_NAME;
 static bool g_enable_apple_quirks = false;
+static bool g_led_blinking = false;
 
 /*************************/
 /* Timing functions      */
@@ -301,6 +302,29 @@ void platform_late_init()
 
 void platform_post_sd_card_init() {}
 
+void platform_write_led(bool state)
+{
+    if (g_led_blinking) return;
+    if (state)
+        gpio_bit_reset(LED_PORT, LED_PINS);
+    else
+        gpio_bit_set(LED_PORT, LED_PINS);
+}
+
+void platform_set_blink_status(bool status)
+{
+    g_led_blinking = status;
+}
+
+void platform_write_led_override(bool state)
+{
+    if (state)
+        gpio_bit_reset(LED_PORT, LED_PINS);
+    else
+        gpio_bit_set(LED_PORT, LED_PINS);
+}
+
+
 void platform_disable_led(void)
 {   
     gpio_mode_set(LED_PORT, GPIO_MODE_INPUT, GPIO_PUPD_PULLUP, LED_PINS);

+ 11 - 0
lib/ZuluSCSI_platform_GD32F450/ZuluSCSI_platform.h

@@ -76,6 +76,17 @@ void platform_post_sd_card_init();
 // Hooks
 void platform_end_of_loop_hook(void);
 
+// Set the status LED only if it is not in a blinking routine
+void platform_write_led(bool state);
+#define LED_ON()  platform_write_led(true)
+#define LED_OFF() platform_write_led(false)
+// Used by the blinking routine
+void platform_set_blink_status(bool status);
+// LED override will set the status LED regardless of the blinking routine
+void platform_write_led_override(bool state);
+#define LED_ON_OVERRIDE()  platform_write_led_override(true)
+#define LED_OFF_OVERRIDE()  platform_write_led_override(false)
+
 // Disable the status LED
 void platform_disable_led(void);
 

+ 0 - 2
lib/ZuluSCSI_platform_GD32F450/ZuluSCSI_v1_4_gpio.h

@@ -170,8 +170,6 @@
 #define LED_I_PIN    GPIO_PIN_4
 #define LED_E_PIN    GPIO_PIN_5
 #define LED_PINS     (LED_I_PIN | LED_E_PIN)
-#define LED_ON()     gpio_bit_reset(LED_PORT, LED_PINS)
-#define LED_OFF()    gpio_bit_set(LED_PORT, LED_PINS)
 
 // User LEDs
 #define USER_LED_PORT       GPIOC

+ 19 - 0
lib/ZuluSCSI_platform_RP2MCU/ZuluSCSI_platform.cpp

@@ -63,6 +63,7 @@ const char *g_platform_name = PLATFORM_NAME;
 static bool g_scsi_initiator = false;
 static uint32_t g_flash_chip_size = 0;
 static bool g_uart_initialized = false;
+static bool g_led_blinking = false;
 /***************/
 /* GPIO init   */
 /***************/
@@ -487,6 +488,24 @@ bool platform_is_initiator_mode_enabled()
     return g_scsi_initiator;
 }
 
+void platform_write_led(bool state)
+{
+    if (g_led_blinking) return;
+
+    gpio_put(LED_PIN, state);
+}
+
+void platform_set_blink_status(bool status)
+{
+    g_led_blinking = status;
+}
+
+void platform_write_led_override(bool state)
+{
+    gpio_put(LED_PIN, state);
+
+}
+
 void platform_disable_led(void)
 {
     //        pin      function       pup   pdown  out    state fast

+ 11 - 0
lib/ZuluSCSI_platform_RP2MCU/ZuluSCSI_platform.h

@@ -90,6 +90,17 @@ void platform_late_init();
 // Initialization after the SD Card has been found
 void platform_post_sd_card_init();
 
+// Set the status LED only if it is not in a blinking routine
+void platform_write_led(bool state);
+#define LED_ON()  platform_write_led(true)
+#define LED_OFF() platform_write_led(false)
+// Used by the blinking routine
+void platform_set_blink_status(bool status);
+// LED override will set the status LED regardless of the blinking routine
+void platform_write_led_override(bool state);
+#define LED_ON_OVERRIDE()  platform_write_led_override(true)
+#define LED_OFF_OVERRIDE()  platform_write_led_override(false)
+
 // Disable the status LED
 void platform_disable_led(void);
 

+ 0 - 2
lib/ZuluSCSI_platform_RP2MCU/ZuluSCSI_platform_gpio_BS2.h

@@ -60,8 +60,6 @@
 
 // Status LED pins
 #define LED_PIN      25
-#define LED_ON()     sio_hw->gpio_set = 1 << LED_PIN
-#define LED_OFF()    sio_hw->gpio_clr = 1 << LED_PIN
 
 // SD card pins in SDIO mode
 #define SDIO_CLK 10

+ 0 - 3
lib/ZuluSCSI_platform_RP2MCU/ZuluSCSI_platform_gpio_Pico.h

@@ -71,9 +71,6 @@
 
 // Status LED pins
 #define LED_PIN      16
-#define LED_ON()    sio_hw->gpio_set = 1 << LED_PIN
-#define LED_OFF()   sio_hw->gpio_clr = 1 << LED_PIN
-
 
 // SD card pins in SDIO mode
 #define SDIO_CLK 10

+ 0 - 3
lib/ZuluSCSI_platform_RP2MCU/ZuluSCSI_platform_gpio_Pico_2.h

@@ -71,9 +71,6 @@
 
 // Status LED pins
 #define LED_PIN      16
-#define LED_ON()    sio_hw->gpio_set = 1 << LED_PIN
-#define LED_OFF()   sio_hw->gpio_clr = 1 << LED_PIN
-
 
 // SD card pins in SDIO mode
 #define SDIO_CLK 10

+ 0 - 2
lib/ZuluSCSI_platform_RP2MCU/ZuluSCSI_platform_gpio_RP2040.h

@@ -71,8 +71,6 @@
 
 // Status LED pins
 #define LED_PIN      25
-#define LED_ON()     sio_hw->gpio_set = 1 << LED_PIN
-#define LED_OFF()    sio_hw->gpio_clr = 1 << LED_PIN
 
 // SD card pins in SDIO mode
 #define SDIO_CLK 18

+ 0 - 2
lib/ZuluSCSI_platform_RP2MCU/ZuluSCSI_platform_gpio_RP2350A.h

@@ -71,8 +71,6 @@
 
 // Status LED pins
 #define LED_PIN      25
-#define LED_ON()     sio_hw->gpio_set = 1 << LED_PIN
-#define LED_OFF()    sio_hw->gpio_clr = 1 << LED_PIN
 
 // SD card pins in SDIO mode
 #define SDIO_CLK 18

+ 58 - 25
src/ZuluSCSI.cpp

@@ -80,19 +80,57 @@ static bool g_sdcard_present;
 #define BLINK_DIRECT_MODE      4
 #define BLINK_ERROR_NO_SD_CARD 5
 
-void blinkStatus(int count)
+static uint16_t blink_count = 0;
+static uint32_t blink_start = 0;
+static uint32_t blink_delay = 0;
+static uint32_t blink_end_delay= 0;
+
+bool blink_poll()
 {
-  uint8_t blink_delay = 250;
-  if (count == BLINK_DIRECT_MODE)
-    blink_delay = 100;
+    bool is_blinking = true;
 
-  for (int i = 0; i < count; i++)
-  {
-    LED_ON();
-    delay(blink_delay);
-    LED_OFF();
-    delay(blink_delay);
-  }
+    if (blink_count == 0)
+    {
+        is_blinking = false;
+    }
+    else if (blink_count == 1 && ((uint32_t)(millis() - blink_start)) > blink_end_delay )
+    {
+        LED_OFF_OVERRIDE();
+        blink_count = 0;
+        is_blinking = false;
+    }
+    else if (blink_count > 1 && ((uint32_t)(millis() - blink_start)) > blink_delay)
+    {
+        if (1 & blink_count)
+            LED_ON_OVERRIDE();
+        else
+            LED_OFF_OVERRIDE();
+        blink_count--;
+        blink_start = millis();
+    }
+
+    if (!is_blinking)
+        platform_set_blink_status(false);
+    return is_blinking;
+}
+
+void blink_cancel()
+{
+    blink_count = 0;
+    platform_set_blink_status(false);
+}
+
+void blinkStatus(uint8_t times, uint32_t delay = 500, uint32_t end_delay = 1250)
+{
+    if (!blink_poll() && blink_count == 0)
+    {
+        blink_start = millis();
+        blink_count = 2 * times + 1;
+        blink_delay = delay / 2;
+        blink_end_delay =  end_delay;
+        platform_set_blink_status(true);
+        LED_OFF_OVERRIDE();
+    }
 }
 
 extern "C" void s2s_ledOn()
@@ -772,9 +810,7 @@ static void reinitSCSI()
       {
         logmsg("---- Using device preset: ", g_scsi_settings.getDevicePresetName(scsiId));
       }
-      blinkStatus(BLINK_STATUS_OK);
     }
-    delay(250);
     blinkStatus(BLINK_DIRECT_MODE);
   }
   else
@@ -784,13 +820,7 @@ static void reinitSCSI()
     findHDDImages();
 
     // Error if there are 0 image files
-    if (scsiDiskCheckAnyImagesConfigured())
-    {
-      // Ok, there is an image, turn LED on for the time it takes to perform init
-      LED_ON();
-      delay(100);
-    }
-    else
+    if (!scsiDiskCheckAnyImagesConfigured())
     {
   #ifdef RAW_FALLBACK_ENABLE
       logmsg("No images found, enabling RAW fallback partition");
@@ -948,10 +978,12 @@ static void zuluscsi_setup_sd_card()
     do
     {
       blinkStatus(BLINK_ERROR_NO_SD_CARD);
-      delay(1000);
       platform_reset_watchdog();
       g_sdcard_present = mountSDCard();
     } while (!g_sdcard_present);
+    blink_cancel();
+    LED_OFF();
+
     logmsg("SD card init succeeded after retry");
   }
 
@@ -1023,8 +1055,7 @@ static void zuluscsi_setup_sd_card()
     }
   }
 
-  // Counterpart for the LED_ON in reinitSCSI().
-  LED_OFF();
+  blinkStatus(BLINK_STATUS_OK);
 }
 
 extern "C" void zuluscsi_setup(void)
@@ -1061,6 +1092,7 @@ extern "C" void zuluscsi_main_loop(void)
   platform_reset_watchdog();
   platform_poll();
   diskEjectButtonUpdate(true);
+  blink_poll();
 
 #ifdef ZULUSCSI_NETWORK
   platform_network_poll();
@@ -1120,16 +1152,17 @@ extern "C" void zuluscsi_main_loop(void)
 
       if (g_sdcard_present)
       {
+        blink_cancel();
+        LED_OFF();
         logmsg("SD card reinit succeeded");
         print_sd_info();
-
         reinitSCSI();
         init_logfile();
+        blinkStatus(BLINK_STATUS_OK);
       }
       else if (!g_romdrive_active)
       {
         blinkStatus(BLINK_ERROR_NO_SD_CARD);
-        delay(1000);
         platform_reset_watchdog();
         platform_poll();
       }