WIP: usb host lib

This commit is contained in:
Clement_B 2025-01-28 18:18:50 +01:00
parent afa7fc7cb0
commit 5fa3d89af8
3 changed files with 68 additions and 62 deletions

View File

@ -17,11 +17,16 @@
#include <Arduino.h> #include <Arduino.h>
class SerialUSBHost class SerialUSBHost {
{
public: public:
SerialUSBHost(/* args */); SerialUSBHost() = delete;
SemaphoreHandle_t device_disconnected_sem; 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: private:
/** /**
* @brief USB Host library handling task * @brief USB Host library handling task

View File

@ -1,7 +1,53 @@
#include "../include/SerialUSBHost.h" #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) { void SerialUSBHost::usb_lib_task(void* arg) {
const String TAG = "SerialUSBHost_usb_lib_task"; const String TAG = "SerialUSBHost_usb_lib_task";
@ -19,11 +65,9 @@ void SerialUSBHost::usb_lib_task(void* arg) {
} }
} }
void SerialUSBHost::init() {
SerialUSBHost::SerialUSBHost() {
const String TAG = "SerialUSBHost_constructor"; const String TAG = "SerialUSBHost_constructor";
this->device_disconnected_sem = xSemaphoreCreateBinary(); device_disconnected_sem = xSemaphoreCreateBinary();
assert(device_disconnected_sem); assert(device_disconnected_sem);
// Install USB Host driver. Should only be called once in entire application // Install USB Host driver. Should only be called once in entire application
@ -48,3 +92,8 @@ SerialUSBHost::SerialUSBHost() {
esp_usb::VCP::register_driver<esp_usb::CH34x>(); esp_usb::VCP::register_driver<esp_usb::CH34x>();
} }
void SerialUSBHost::takeSem(){//XXX
xSemaphoreTake(device_disconnected_sem, portMAX_DELAY);
}

View File

@ -6,8 +6,6 @@
#include "SerialUSBHost.h" #include "SerialUSBHost.h"
SerialUSBHost* serialUSB;
using namespace esp_usb; using namespace esp_usb;
// Change these values to match your needs // 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_PARITY (0) // 0: None, 1: Odd, 2: Even, 3: Mark, 4: Space
#define EXAMPLE_DATA_BITS (8) #define EXAMPLE_DATA_BITS (8)
namespace {
static const char *TAG = "VCP example"; 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() { void setup() {
serialUSB = new SerialUSBHost(); SerialUSBHost::init();
} }
void loop() { void loop() {
const cdc_acm_host_device_config_t dev_config = { 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 .connection_timeout_ms = 5000, // 5 seconds, enough time to plug the device in or experiment with timeout
.out_buffer_size = 512, .out_buffer_size = 512,
.in_buffer_size = 512, .in_buffer_size = 512,
.event_cb = handle_event, .event_cb = SerialUSBHost::handle_event,
.data_cb = handle_rx, .data_cb = SerialUSBHost::handle_rx,
.user_arg = NULL, .user_arg = NULL,
}; };
@ -117,5 +68,6 @@ void loop() {
// We are done. Wait for device disconnection and start over // We are done. Wait for device disconnection and start over
ESP_LOGI(TAG, "Done. You can reconnect the VCP device to run again."); ESP_LOGI(TAG, "Done. You can reconnect the VCP device to run again.");
xSemaphoreTake(serialUSB->device_disconnected_sem, portMAX_DELAY);
SerialUSBHost::takeSem();
} }