From 0dcbae7ec620b6eb7cd9b712aa19e0136ba55a66 Mon Sep 17 00:00:00 2001 From: Joe Kearney Date: Sat, 22 Nov 2025 16:51:33 -0600 Subject: [PATCH] Better USB plug/unplug handling. --- components/NVM/USB.c | 35 ++++++++++++++++++++++------------- components/NVM/USB.h | 2 +- main/main.c | 11 +++++++++-- 3 files changed, 32 insertions(+), 16 deletions(-) diff --git a/components/NVM/USB.c b/components/NVM/USB.c index c5ab98b..85f4d1f 100644 --- a/components/NVM/USB.c +++ b/components/NVM/USB.c @@ -57,6 +57,7 @@ static const char *OTA_FILE = "/usb/esp/OTA_URL.txt"; const char *CONFIG_FILE = "/usb/esp/config.txt"; __NOINIT_ATTR uint_fast8_t Restarts; +static bool Device_Connected_This_Power_Cycle = false; typedef enum { @@ -274,8 +275,6 @@ static void usb_host_task(void *args) static void app_usb_task(void *args) { - SemaphoreHandle_t init_complete = args; - static USB_State_T Current_State = USB_STATE_UNINITIALIZED; static msc_host_device_handle_t msc_device = NULL; static msc_host_vfs_handle_t vfs_handle = NULL; @@ -311,20 +310,31 @@ static void app_usb_task(void *args) { device_address = msg.data.new_dev_address; Current_State = USB_STATE_DEVICE_CONNECTED; + Device_Connected_This_Power_Cycle = true; + Restarts = 0; KLOG_INFO(TAG, "Device connected."); } } else { - if (Restarts <= 3) + if (Device_Connected_This_Power_Cycle == false) { - KLOG_ERROR(TAG, "No flash drive detected--rebooting."); - vTaskDelay(pdMS_TO_TICKS(100)); - esp_restart(); - } - else - { - KLOG_WARN(TAG, "No flash drive detected after multiple attempts."); + if (Restarts <= 3) + { + KLOG_ERROR(TAG, "No flash drive detected--rebooting."); + vTaskDelay(pdMS_TO_TICKS(100)); + esp_restart(); + } + else + { + static bool warning_sent = false; + + if (warning_sent == false) + { + KLOG_WARN(TAG, "No flash drive detected after multiple attempts."); + warning_sent = true; + } + } } } } @@ -356,7 +366,6 @@ static void app_usb_task(void *args) { Current_State = USB_STATE_VFS_REGISTERED; xEventGroupSetBits(Get_System_Events(), SYS_USB_FS_PRESENT); - xSemaphoreGive(init_complete); } } } @@ -401,7 +410,7 @@ static void app_usb_task(void *args) } } -void Initialize_USB(SemaphoreHandle_t init_complete) +void Initialize_USB(void) { KLOG_INFO(TAG, "Initializing USB file system..."); @@ -416,7 +425,7 @@ void Initialize_USB(SemaphoreHandle_t init_complete) app_usb_task, // Function that implements the task. "USB NVM", // Text name for the task. STACK_SIZE, // Stack size in bytes, not words. - (void *)init_complete, // Parameter passed into the task. + NULL, // Parameter passed into the task. tskIDLE_PRIORITY + 2, // Priority at which the task is created. xStack, // Array to use as the task's stack. &xTaskBuffer, // Variable to hold the task's data structure. diff --git a/components/NVM/USB.h b/components/NVM/USB.h index f56e9fa..d64a531 100644 --- a/components/NVM/USB.h +++ b/components/NVM/USB.h @@ -21,7 +21,7 @@ #pragma once -void Initialize_USB(SemaphoreHandle_t init_complete); +void Initialize_USB(void); extern const char *CONFIG_FILE; diff --git a/main/main.c b/main/main.c index f6a7b41..a1741cd 100644 --- a/main/main.c +++ b/main/main.c @@ -93,8 +93,15 @@ void app_main(void) KLOG_ERROR(TAG, "Timeout initializing SPIFFS!"); } - Initialize_USB(init_complete_semaphore); - if (xSemaphoreTake(init_complete_semaphore, pdMS_TO_TICKS(INITIALIZATION_TIMEOUT_IN_ms)) != pdTRUE) + Initialize_USB(); + EventBits_t bits = xEventGroupWaitBits( + Get_System_Events(), + SYS_USB_FS_PRESENT, + pdFALSE, + pdTRUE, + pdMS_TO_TICKS(INITIALIZATION_TIMEOUT_IN_ms)); + + if ((bits & SYS_USB_FS_PRESENT) == 0) { KLOG_ERROR(TAG, "Timeout initializing USB!"); }