From afa7fc7cb0de079581a58740067fbc7a38a7f7eb Mon Sep 17 00:00:00 2001 From: Clement_B Date: Tue, 28 Jan 2025 13:02:34 +0100 Subject: [PATCH] SerialUSBHost Lib WIP --- .vscode/settings.json | 6 +++ lib/SerialUSBHost/include/SerialUSBHost.h | 37 +++++++++++++ lib/SerialUSBHost/src/SerialUSBHost.cpp | 50 ++++++++++++++++++ src/main.cpp | 63 +++-------------------- 4 files changed, 99 insertions(+), 57 deletions(-) create mode 100644 .vscode/settings.json create mode 100644 lib/SerialUSBHost/include/SerialUSBHost.h create mode 100644 lib/SerialUSBHost/src/SerialUSBHost.cpp diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..c6b368b --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,6 @@ +{ + "files.associations": { + "memory": "cpp", + "vector": "cpp" + } +} \ No newline at end of file diff --git a/lib/SerialUSBHost/include/SerialUSBHost.h b/lib/SerialUSBHost/include/SerialUSBHost.h new file mode 100644 index 0000000..ae487be --- /dev/null +++ b/lib/SerialUSBHost/include/SerialUSBHost.h @@ -0,0 +1,37 @@ +#ifndef SERIAL_USB_HOST_H +#define SERIAL_USB_HOST_H + +#include "esp_log.h" + +#include "freertos/FreeRTOS.h" +#include "freertos/semphr.h" +#include "freertos/task.h" + +#include "usb/cdc_acm_host.h" +#include "usb/vcp_ch34x.hpp" +#include "usb/vcp_cp210x.hpp" +#include "usb/vcp_ftdi.hpp" +#include "usb/vcp.hpp" +#include "usb/usb_host.h" + +#include + + +class SerialUSBHost +{ +public: + SerialUSBHost(/* args */); + SemaphoreHandle_t device_disconnected_sem; +private: + /** + * @brief USB Host library handling task + * + * @param arg unused but required for task + */ + static void usb_lib_task(void* arg); +}; + + + + +#endif \ No newline at end of file diff --git a/lib/SerialUSBHost/src/SerialUSBHost.cpp b/lib/SerialUSBHost/src/SerialUSBHost.cpp new file mode 100644 index 0000000..d235471 --- /dev/null +++ b/lib/SerialUSBHost/src/SerialUSBHost.cpp @@ -0,0 +1,50 @@ +#include "../include/SerialUSBHost.h" + + + + +void SerialUSBHost::usb_lib_task(void* arg) { + const String TAG = "SerialUSBHost_usb_lib_task"; + while (1) { + // Start handling system events + uint32_t event_flags; + usb_host_lib_handle_events(portMAX_DELAY, &event_flags); + if (event_flags & USB_HOST_LIB_EVENT_FLAGS_NO_CLIENTS) { + ESP_ERROR_CHECK(usb_host_device_free_all()); + } + if (event_flags & USB_HOST_LIB_EVENT_FLAGS_ALL_FREE) { + ESP_LOGI(TAG.c_str(), "USB: All devices freed"); + // Continue handling USB events to allow device reconnection + } + } +} + + + +SerialUSBHost::SerialUSBHost() { + const String TAG = "SerialUSBHost_constructor"; + this->device_disconnected_sem = xSemaphoreCreateBinary(); + assert(device_disconnected_sem); + + // Install USB Host driver. Should only be called once in entire application + ESP_LOGI(TAG.c_str(), "Installing USB Host"); + const usb_host_config_t host_config = { + .skip_phy_setup = false, + .intr_flags = ESP_INTR_FLAG_LEVEL1, + .enum_filter_cb = nullptr, + }; + ESP_ERROR_CHECK(usb_host_install(&host_config)); + + // Create a task that will handle USB library events + BaseType_t task_created = xTaskCreate(SerialUSBHost::usb_lib_task, "usb_lib", 4096, NULL, 10, NULL); + assert(task_created == pdTRUE); + + ESP_LOGI(TAG.c_str(), "Installing CDC-ACM driver"); + ESP_ERROR_CHECK(cdc_acm_host_install(NULL)); + + // Register VCP drivers to VCP service + esp_usb::VCP::register_driver(); + esp_usb::VCP::register_driver(); + esp_usb::VCP::register_driver(); + +} diff --git a/src/main.cpp b/src/main.cpp index c9adb6d..4d5ac65 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -3,17 +3,10 @@ #include #include -#include "esp_log.h" -#include "freertos/FreeRTOS.h" -#include "freertos/task.h" -#include "freertos/semphr.h" -#include "usb/cdc_acm_host.h" -#include "usb/vcp_ch34x.hpp" -#include "usb/vcp_cp210x.hpp" -#include "usb/vcp_ftdi.hpp" -#include "usb/vcp.hpp" -#include "usb/usb_host.h" +#include "SerialUSBHost.h" + +SerialUSBHost* serialUSB; using namespace esp_usb; @@ -26,8 +19,6 @@ using namespace esp_usb; namespace { static const char *TAG = "VCP example"; -static SemaphoreHandle_t device_disconnected_sem; - /** * @brief Data received callback * @@ -62,7 +53,7 @@ static void handle_event(const cdc_acm_host_dev_event_data_t *event, void *user_ break; case CDC_ACM_HOST_DEVICE_DISCONNECTED: ESP_LOGI(TAG, "Device suddenly disconnected"); - xSemaphoreGive(device_disconnected_sem); + xSemaphoreGive(serialUSB->device_disconnected_sem); break; case CDC_ACM_HOST_SERIAL_STATE: ESP_LOGI(TAG, "Serial state notif 0x%04X", event->data.serial_state.val); @@ -71,55 +62,13 @@ static void handle_event(const cdc_acm_host_dev_event_data_t *event, void *user_ default: break; } } - -/** - * @brief USB Host library handling task - * - * @param arg Unused - */ -static void usb_lib_task(void *arg) -{ - while (1) { - // Start handling system events - uint32_t event_flags; - usb_host_lib_handle_events(portMAX_DELAY, &event_flags); - if (event_flags & USB_HOST_LIB_EVENT_FLAGS_NO_CLIENTS) { - ESP_ERROR_CHECK(usb_host_device_free_all()); - } - if (event_flags & USB_HOST_LIB_EVENT_FLAGS_ALL_FREE) { - ESP_LOGI(TAG, "USB: All devices freed"); - // Continue handling USB events to allow device reconnection - } - } -} } void setup() { - device_disconnected_sem = xSemaphoreCreateBinary(); - assert(device_disconnected_sem); - - // Install USB Host driver. Should only be called once in entire application - ESP_LOGI(TAG, "Installing USB Host"); - const usb_host_config_t host_config = { - .skip_phy_setup = false, - .intr_flags = ESP_INTR_FLAG_LEVEL1, - }; - ESP_ERROR_CHECK(usb_host_install(&host_config)); - - // Create a task that will handle USB library events - BaseType_t task_created = xTaskCreate(usb_lib_task, "usb_lib", 4096, NULL, 10, NULL); - assert(task_created == pdTRUE); - - ESP_LOGI(TAG, "Installing CDC-ACM driver"); - ESP_ERROR_CHECK(cdc_acm_host_install(NULL)); - - // Register VCP drivers to VCP service - VCP::register_driver(); - VCP::register_driver(); - VCP::register_driver(); + serialUSB = new SerialUSBHost(); } void loop() { @@ -168,5 +117,5 @@ void loop() { // We are done. Wait for device disconnection and start over ESP_LOGI(TAG, "Done. You can reconnect the VCP device to run again."); - xSemaphoreTake(device_disconnected_sem, portMAX_DELAY); + xSemaphoreTake(serialUSB->device_disconnected_sem, portMAX_DELAY); }