From a828d74894b7ef3bbb6a7a446093500216277bcf Mon Sep 17 00:00:00 2001 From: YuXifeng <2860280851@qq.com> Date: Mon, 31 Mar 2025 10:24:08 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E8=99=9A=E6=8B=9F=E4=B8=B2?= =?UTF-8?q?=E5=8F=A3=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main/CMakeLists.txt | 2 +- main/main.c | 1 + main/public.h | 1 + main/usb_serial_task.c | 77 ++++++++++++++++++++++++++++++++++++++++-- 4 files changed, 78 insertions(+), 3 deletions(-) diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt index cb13329..ec5648c 100755 --- a/main/CMakeLists.txt +++ b/main/CMakeLists.txt @@ -5,7 +5,7 @@ file(GLOB FILE_SYS_SRCS "file_system/spiffs.c" "file_system/file_encryptor.c" ) if(CONFIG_IDF_TARGET_ESP32S2) file(GLOB PORT_GUI "GUI/lvgl_task.c" ) -elseif(CONFIG_IDF_TARGET_ESP32C3) +# elseif(CONFIG_IDF_TARGET_ESP32C3) file(GLOB PORT_USB "usb_serial_task.c" ) endif() diff --git a/main/main.c b/main/main.c index dbd43f7..d271691 100755 --- a/main/main.c +++ b/main/main.c @@ -154,6 +154,7 @@ void app_main(void) #endif xTaskCreate(&Gui_Task, "Gui_Task", 1024*20, NULL, 12, NULL); xTaskCreate(&Read_CMD_task, "Read_CMD_task", 1024*20, NULL, 14, NULL); + xTaskCreate(&usb_serial_task,"usb_serial_task",1024*10,NULL,22,NULL); // xTaskCreate(&Flasher_tool_task, "Flasher_tool_task", 1024*20, NULL, 10, NULL); #if 0 home_html.html_data = load_html2Ram(home_html); diff --git a/main/public.h b/main/public.h index f38a377..c316934 100644 --- a/main/public.h +++ b/main/public.h @@ -55,5 +55,6 @@ extern void usb_serial2Uart(void *pvParameters); extern void Read_CMD_task(void *pvParameter); extern void Flasher_tool_task(void *pvParameter); extern void Gui_Task(void *pvParameter); +extern void usb_serial_task(void *pvParameters); #endif \ No newline at end of file diff --git a/main/usb_serial_task.c b/main/usb_serial_task.c index 942c1ca..7c70fcb 100644 --- a/main/usb_serial_task.c +++ b/main/usb_serial_task.c @@ -1,5 +1,7 @@ #include "public.h" #include "stream.h" +#include "tinyusb.h" +#include "tusb_cdc_acm.h" #define EX_UART_NUM UART_NUM_1 #define PATTERN_CHR_NUM (3) /*!< Set the number of consecutive and identical characters received by receiver which defines a UART pattern*/ @@ -11,7 +13,37 @@ static QueueHandle_t uart0_queue; static const char *TAG = "usb_serial"; +static uint8_t buf[CONFIG_TINYUSB_CDC_RX_BUFSIZE + 1]; +#if CONFIG_IDF_TARGET_ESP32S2 + +void tinyusb_cdc_rx_callback(int itf, cdcacm_event_t *event) +{ + /* initialization */ + size_t rx_size = 0; + + /* read */ + esp_err_t ret = tinyusb_cdcacm_read(itf, buf, CONFIG_TINYUSB_CDC_RX_BUFSIZE, &rx_size); + if (ret == ESP_OK) { + ESP_LOGI(TAG, "Data from channel %d:", itf); + ESP_LOG_BUFFER_HEXDUMP(TAG, buf, rx_size, ESP_LOG_INFO); + } else { + ESP_LOGE(TAG, "Read error"); + } + + /* write back */ + tinyusb_cdcacm_write_queue(itf, buf, rx_size); + tinyusb_cdcacm_write_flush(itf, 0); +} + +void tinyusb_cdc_line_state_changed_callback(int itf, cdcacm_event_t *event) +{ + int dtr = event->line_state_changed_data.dtr; + int rts = event->line_state_changed_data.rts; + ESP_LOGI(TAG, "Line state changed on channel %d: DTR:%d, RTS:%d", itf, dtr, rts); +} + +#elif CONFIG_IDF_TARGET_ESP32C3 static void echo_task(void *arg) { // Configure USB SERIAL JTAG @@ -63,6 +95,8 @@ static void echo_task(void *arg) } } +#endif + static void uart_event_task(void *pvParameters) { uart_event_t event; @@ -155,6 +189,45 @@ static void uart_event_task(void *pvParameters) stream_data_t usb_interface_data; + + +#if CONFIG_IDF_TARGET_ESP32S2 +void usb_serial_task(void *pvParameters) +{ + ESP_LOGI(TAG, "USB initialization"); + const tinyusb_config_t tusb_cfg = { + .device_descriptor = NULL, + .string_descriptor = NULL, + .external_phy = false, + .configuration_descriptor = NULL, + }; + + ESP_ERROR_CHECK(tinyusb_driver_install(&tusb_cfg)); + + tinyusb_config_cdcacm_t acm_cfg = { + .usb_dev = TINYUSB_USBDEV_0, + .cdc_port = TINYUSB_CDC_ACM_0, + .rx_unread_buf_sz = 64, + .callback_rx = &tinyusb_cdc_rx_callback, // the first way to register a callback + .callback_rx_wanted_char = NULL, + .callback_line_state_changed = NULL, + .callback_line_coding_changed = NULL + }; + + ESP_ERROR_CHECK(tusb_cdc_acm_init(&acm_cfg)); + /* the second way to register a callback */ + ESP_ERROR_CHECK(tinyusb_cdcacm_register_callback( + TINYUSB_CDC_ACM_0, + CDC_EVENT_LINE_STATE_CHANGED, + &tinyusb_cdc_line_state_changed_callback)); + ESP_LOGI(TAG, "USB initialization DONE"); + + vTaskDelete(NULL); +} + +#endif + +#if CONFIG_IDF_TARGET_ESP32C3 void usb_serial2Uart(void *pvParameters) { // stream_init(&usb_interface_data,1024*20); @@ -178,5 +251,5 @@ void usb_serial2Uart(void *pvParameters) xTaskCreate(uart_event_task, "uart_event_task", 2048*10, NULL, 12, NULL); #endif xTaskCreate(echo_task, "USB SERIAL JTAG_echo_task", ECHO_TASK_STACK_SIZE, NULL, 20, NULL); - -} \ No newline at end of file +} +#endif \ No newline at end of file