From 5fa3d89af818cbad7ae550b0d6e270cd116673ce Mon Sep 17 00:00:00 2001 From: Clement_B Date: Tue, 28 Jan 2025 18:18:50 +0100 Subject: [PATCH] WIP: usb host lib --- lib/SerialUSBHost/include/SerialUSBHost.h | 13 +++-- lib/SerialUSBHost/src/SerialUSBHost.cpp | 57 +++++++++++++++++++-- src/main.cpp | 60 +++-------------------- 3 files changed, 68 insertions(+), 62 deletions(-) diff --git a/lib/SerialUSBHost/include/SerialUSBHost.h b/lib/SerialUSBHost/include/SerialUSBHost.h index ae487be..65586ef 100644 --- a/lib/SerialUSBHost/include/SerialUSBHost.h +++ b/lib/SerialUSBHost/include/SerialUSBHost.h @@ -17,11 +17,16 @@ #include -class SerialUSBHost -{ +class SerialUSBHost { + public: - SerialUSBHost(/* args */); - SemaphoreHandle_t device_disconnected_sem; + SerialUSBHost() = delete; + static void init(); + + static void handle_event(const cdc_acm_host_dev_event_data_t *event, void *user_ctx); + static bool handle_rx(const uint8_t *data, size_t data_len, void *arg); + + static void takeSem();//XXX private: /** * @brief USB Host library handling task diff --git a/lib/SerialUSBHost/src/SerialUSBHost.cpp b/lib/SerialUSBHost/src/SerialUSBHost.cpp index d235471..9973a92 100644 --- a/lib/SerialUSBHost/src/SerialUSBHost.cpp +++ b/lib/SerialUSBHost/src/SerialUSBHost.cpp @@ -1,7 +1,53 @@ #include "../include/SerialUSBHost.h" +static SemaphoreHandle_t device_disconnected_sem;//XXX +static const char *TAG = "VCP example"; + +/** + * @brief Data received callback + * + * Just pass received data to stdout + * + * @param[in] data Pointer to received data + * @param[in] data_len Length of received data in bytes + * @param[in] arg Argument we passed to the device open function + * @return + * true: We have processed the received data + * false: We expect more data + */ +bool SerialUSBHost::handle_rx(const uint8_t *data, size_t data_len, void *arg) +{ + printf("%.*s", data_len, data); + return true; +} + +/** + * @brief Device event callback + * + * Apart from handling device disconnection it doesn't do anything useful + * + * @param[in] event Device event type and data + * @param[in] user_ctx Argument we passed to the device open function + */ +void SerialUSBHost::handle_event(const cdc_acm_host_dev_event_data_t *event, void *user_ctx) +{ + switch (event->type) { + case CDC_ACM_HOST_ERROR: + ESP_LOGE(TAG, "CDC-ACM error has occurred, err_no = %d", event->data.error); + break; + case CDC_ACM_HOST_DEVICE_DISCONNECTED: + ESP_LOGI(TAG, "Device suddenly disconnected"); + xSemaphoreGive(device_disconnected_sem); + break; + case CDC_ACM_HOST_SERIAL_STATE: + ESP_LOGI(TAG, "Serial state notif 0x%04X", event->data.serial_state.val); + break; + case CDC_ACM_HOST_NETWORK_CONNECTION: + default: break; + } +} void SerialUSBHost::usb_lib_task(void* arg) { const String TAG = "SerialUSBHost_usb_lib_task"; @@ -19,11 +65,9 @@ void SerialUSBHost::usb_lib_task(void* arg) { } } - - -SerialUSBHost::SerialUSBHost() { +void SerialUSBHost::init() { const String TAG = "SerialUSBHost_constructor"; - this->device_disconnected_sem = xSemaphoreCreateBinary(); + device_disconnected_sem = xSemaphoreCreateBinary(); assert(device_disconnected_sem); // Install USB Host driver. Should only be called once in entire application @@ -48,3 +92,8 @@ SerialUSBHost::SerialUSBHost() { esp_usb::VCP::register_driver(); } + + +void SerialUSBHost::takeSem(){//XXX + xSemaphoreTake(device_disconnected_sem, portMAX_DELAY); +} \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 4d5ac65..400f503 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -6,8 +6,6 @@ #include "SerialUSBHost.h" -SerialUSBHost* serialUSB; - using namespace esp_usb; // Change these values to match your needs @@ -16,68 +14,21 @@ using namespace esp_usb; #define EXAMPLE_PARITY (0) // 0: None, 1: Odd, 2: Even, 3: Mark, 4: Space #define EXAMPLE_DATA_BITS (8) - -namespace { static const char *TAG = "VCP example"; -/** - * @brief Data received callback - * - * Just pass received data to stdout - * - * @param[in] data Pointer to received data - * @param[in] data_len Length of received data in bytes - * @param[in] arg Argument we passed to the device open function - * @return - * true: We have processed the received data - * false: We expect more data - */ -static bool handle_rx(const uint8_t *data, size_t data_len, void *arg) -{ - printf("%.*s", data_len, data); - return true; -} - -/** - * @brief Device event callback - * - * Apart from handling device disconnection it doesn't do anything useful - * - * @param[in] event Device event type and data - * @param[in] user_ctx Argument we passed to the device open function - */ -static void handle_event(const cdc_acm_host_dev_event_data_t *event, void *user_ctx) -{ - switch (event->type) { - case CDC_ACM_HOST_ERROR: - ESP_LOGE(TAG, "CDC-ACM error has occurred, err_no = %d", event->data.error); - break; - case CDC_ACM_HOST_DEVICE_DISCONNECTED: - ESP_LOGI(TAG, "Device suddenly disconnected"); - 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); - break; - case CDC_ACM_HOST_NETWORK_CONNECTION: - default: break; - } -} -} - - void setup() { - serialUSB = new SerialUSBHost(); + SerialUSBHost::init(); } + void loop() { const cdc_acm_host_device_config_t dev_config = { .connection_timeout_ms = 5000, // 5 seconds, enough time to plug the device in or experiment with timeout .out_buffer_size = 512, .in_buffer_size = 512, - .event_cb = handle_event, - .data_cb = handle_rx, + .event_cb = SerialUSBHost::handle_event, + .data_cb = SerialUSBHost::handle_rx, .user_arg = NULL, }; @@ -117,5 +68,6 @@ 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(serialUSB->device_disconnected_sem, portMAX_DELAY); + + SerialUSBHost::takeSem(); }