增加虚拟串口接口

This commit is contained in:
YuXifeng 2025-03-31 10:24:08 +08:00
parent 0309ec6c35
commit a828d74894
4 changed files with 78 additions and 3 deletions

View File

@ -5,7 +5,7 @@ file(GLOB FILE_SYS_SRCS "file_system/spiffs.c" "file_system/file_encryptor.c" )
if(CONFIG_IDF_TARGET_ESP32S2) if(CONFIG_IDF_TARGET_ESP32S2)
file(GLOB PORT_GUI "GUI/lvgl_task.c" ) 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" ) file(GLOB PORT_USB "usb_serial_task.c" )
endif() endif()

View File

@ -154,6 +154,7 @@ void app_main(void)
#endif #endif
xTaskCreate(&Gui_Task, "Gui_Task", 1024*20, NULL, 12, NULL); xTaskCreate(&Gui_Task, "Gui_Task", 1024*20, NULL, 12, NULL);
xTaskCreate(&Read_CMD_task, "Read_CMD_task", 1024*20, NULL, 14, 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); // xTaskCreate(&Flasher_tool_task, "Flasher_tool_task", 1024*20, NULL, 10, NULL);
#if 0 #if 0
home_html.html_data = load_html2Ram(home_html); home_html.html_data = load_html2Ram(home_html);

View File

@ -55,5 +55,6 @@ extern void usb_serial2Uart(void *pvParameters);
extern void Read_CMD_task(void *pvParameter); extern void Read_CMD_task(void *pvParameter);
extern void Flasher_tool_task(void *pvParameter); extern void Flasher_tool_task(void *pvParameter);
extern void Gui_Task(void *pvParameter); extern void Gui_Task(void *pvParameter);
extern void usb_serial_task(void *pvParameters);
#endif #endif

View File

@ -1,5 +1,7 @@
#include "public.h" #include "public.h"
#include "stream.h" #include "stream.h"
#include "tinyusb.h"
#include "tusb_cdc_acm.h"
#define EX_UART_NUM UART_NUM_1 #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*/ #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 QueueHandle_t uart0_queue;
static const char *TAG = "usb_serial"; 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) static void echo_task(void *arg)
{ {
// Configure USB SERIAL JTAG // Configure USB SERIAL JTAG
@ -63,6 +95,8 @@ static void echo_task(void *arg)
} }
} }
#endif
static void uart_event_task(void *pvParameters) static void uart_event_task(void *pvParameters)
{ {
uart_event_t event; uart_event_t event;
@ -155,6 +189,45 @@ static void uart_event_task(void *pvParameters)
stream_data_t usb_interface_data; 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) void usb_serial2Uart(void *pvParameters)
{ {
// stream_init(&usb_interface_data,1024*20); // 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); xTaskCreate(uart_event_task, "uart_event_task", 2048*10, NULL, 12, NULL);
#endif #endif
xTaskCreate(echo_task, "USB SERIAL JTAG_echo_task", ECHO_TASK_STACK_SIZE, NULL, 20, NULL); xTaskCreate(echo_task, "USB SERIAL JTAG_echo_task", ECHO_TASK_STACK_SIZE, NULL, 20, NULL);
} }
#endif