diff options
| author | Ha Thach <[email protected]> | 2026-04-05 11:56:15 +0700 |
|---|---|---|
| committer | GitHub <[email protected]> | 2026-04-05 11:56:15 +0700 |
| commit | 333a38d664c748d82bceedcf81a37fa580347991 (patch) | |
| tree | 2565315548fd25e8ef556c348e2166997b35477a /examples/host | |
| parent | 625244854e4b4eeba01ef959ebfe1aa7d62d47e4 (diff) | |
| parent | 3747355841f35cf5f1b2998c1806d8d19b167722 (diff) | |
Merge pull request #3584 from hathach/fix-hil
Fix CDC echo test issue for IMXRT and fast MCUs
Diffstat (limited to 'examples/host')
| -rw-r--r-- | examples/host/cdc_msc_hid/src/cdc_app.c | 72 | ||||
| -rw-r--r-- | examples/host/cdc_msc_hid/src/tusb_config.h | 5 | ||||
| -rw-r--r-- | examples/host/cdc_msc_hid_freertos/src/cdc_app.c | 4 | ||||
| -rw-r--r-- | examples/host/msc_file_explorer/src/main.c | 33 |
4 files changed, 45 insertions, 69 deletions
diff --git a/examples/host/cdc_msc_hid/src/cdc_app.c b/examples/host/cdc_msc_hid/src/cdc_app.c index 4c2c5e807..20033981e 100644 --- a/examples/host/cdc_msc_hid/src/cdc_app.c +++ b/examples/host/cdc_msc_hid/src/cdc_app.c @@ -28,11 +28,13 @@ #include "bsp/board_api.h" #include "app.h" -static size_t get_console_inputs(uint8_t* buf, size_t bufsize) { +static size_t console_read(uint8_t *buf, size_t bufsize) { size_t count = 0; while (count < bufsize) { - int ch = board_getchar(); - if (ch <= 0) { break; } + const int ch = board_getchar(); + if (ch < 0) { + break; + } buf[count] = (uint8_t) ch; count++; } @@ -40,46 +42,50 @@ static size_t get_console_inputs(uint8_t* buf, size_t bufsize) { return count; } +static size_t console_write(const uint8_t *buf, size_t bufsize) { + // Use board_uart_write directly for non-blocking behavior. + // board_putchar -> sys_write has a blocking retry loop that causes UART RX overrun. + int wr = board_uart_write(buf, (int) bufsize); + return (wr > 0) ? (size_t) wr : 0; +} + +// forward from console to usbh +static void console_to_usbh(uint8_t idx) { + uint8_t buf[64]; + size_t count = console_read(buf, sizeof(buf)); + if (count > 0) { + tuh_cdc_write(idx, buf, count); + } +} + void cdc_app_task(void) { - uint8_t buf[64 + 1]; // +1 for extra null character - uint32_t const bufsize = sizeof(buf) - 1; + const uint8_t idx = 0; + + // Bidirectional forwarding: console <-> host cdc interfaces + if (!tuh_cdc_mounted(idx)) { + return; + } - uint32_t count = get_console_inputs(buf, bufsize); - buf[count] = 0; + // usbh -> uart + uint8_t buf[64]; + uint32_t count = tuh_cdc_read(idx, buf, sizeof(buf)); + uint32_t wr = 0; - // loop over all mounted interfaces - for (uint8_t idx = 0; idx < CFG_TUH_CDC; idx++) { - if (tuh_cdc_mounted(idx)) { - // console --> cdc interfaces - if (count > 0) { - tuh_cdc_write(idx, buf, count); - tuh_cdc_write_flush(idx); - } + do { + // uart write is slow, while waiting forward uart -> usbh else uart rx can be overflow + if (count) { + wr += console_write(buf + wr, count - wr); } - } + console_to_usbh(idx); + } while (wr < count); + + tuh_cdc_write_flush(idx); } //--------------------------------------------------------------------+ // TinyUSB callbacks //--------------------------------------------------------------------+ -// Invoked when received new data -void tuh_cdc_rx_cb(uint8_t idx) { - uint8_t buf[64 + 1]; // +1 for extra null character - uint32_t const bufsize = sizeof(buf) - 1; - - // forward cdc interfaces -> console - const uint32_t count = tuh_cdc_read(idx, buf, bufsize); - if (count) { - buf[count] = 0; - printf("%s", (char*) buf); - - #ifndef __ICCARM__ // TODO IAR doesn't support stream control ? - fflush(stdout);// flush right away, else nanolib will wait for newline - #endif - } -} - // Invoked when a device with CDC interface is mounted // idx is index of cdc interface in the internal pool. void tuh_cdc_mount_cb(uint8_t idx) { diff --git a/examples/host/cdc_msc_hid/src/tusb_config.h b/examples/host/cdc_msc_hid/src/tusb_config.h index 75de3511c..26fcdd1cb 100644 --- a/examples/host/cdc_msc_hid/src/tusb_config.h +++ b/examples/host/cdc_msc_hid/src/tusb_config.h @@ -102,8 +102,11 @@ // Size of buffer to hold descriptors and other data used for enumeration #define CFG_TUH_ENUMERATION_BUFSIZE 256 +// Increase task event queue to handle rapid bulk transfer completions +#define CFG_TUH_TASK_QUEUE_SZ 64 + #define CFG_TUH_HUB 1 // number of supported hubs -#define CFG_TUH_CDC 2 // number of supported CDC devices. also activates CDC ACM +#define CFG_TUH_CDC 1 // number of supported CDC devices. also activates CDC ACM #define CFG_TUH_CDC_FTDI 1 // FTDI Serial. FTDI is not part of CDC class, only to re-use CDC driver API #define CFG_TUH_CDC_CP210X 1 // CP210x Serial. CP210X is not part of CDC class, only to re-use CDC driver API #define CFG_TUH_CDC_CH34X 1 // CH340 or CH341 Serial. CH34X is not part of CDC class, only to re-use CDC driver API diff --git a/examples/host/cdc_msc_hid_freertos/src/cdc_app.c b/examples/host/cdc_msc_hid_freertos/src/cdc_app.c index 0e0105980..30baacaac 100644 --- a/examples/host/cdc_msc_hid_freertos/src/cdc_app.c +++ b/examples/host/cdc_msc_hid_freertos/src/cdc_app.c @@ -53,7 +53,7 @@ void cdc_app_init(void) { } // helper -static size_t get_console_inputs(uint8_t *buf, size_t bufsize) { +static size_t console_read(uint8_t *buf, size_t bufsize) { size_t count = 0; while (count < bufsize) { int ch = board_getchar(); @@ -75,7 +75,7 @@ static void cdc_app_task(void* param) { uint32_t const bufsize = sizeof(buf) - 1; while (1) { - uint32_t count = get_console_inputs(buf, bufsize); + uint32_t count = console_read(buf, bufsize); buf[count] = 0; if (count) { diff --git a/examples/host/msc_file_explorer/src/main.c b/examples/host/msc_file_explorer/src/main.c index f6bf9a60a..07515e626 100644 --- a/examples/host/msc_file_explorer/src/main.c +++ b/examples/host/msc_file_explorer/src/main.c @@ -23,39 +23,6 @@ * */ -/* Example to show how to navigate mass storage device with built-in command line. - * Type help for list of supported commands and syntax (mostly linux commands) - - > help - * help - Print list of commands - * cat - Usage: cat [FILE]... - Concatenate FILE(s) to standard output.. - * cd - Usage: cd [DIR]... - Change the current directory to DIR. - * cp - Usage: cp SOURCE DEST - Copy SOURCE to DEST. - * ls - Usage: ls [DIR]... - List information about the FILEs (the current directory by default). - * pwd - Usage: pwd - Print the name of the current working directory. - * mkdir - Usage: mkdir DIR... - Create the DIRECTORY(ies), if they do not already exist.. - * mv - Usage: mv SOURCE DEST... - Rename SOURCE to DEST. - * rm - Usage: rm [FILE]... - Remove (unlink) the FILE(s). - */ - -#include <stdlib.h> #include <string.h> #include "bsp/board_api.h" |
