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 | |
| parent | 625244854e4b4eeba01ef959ebfe1aa7d62d47e4 (diff) | |
| parent | 3747355841f35cf5f1b2998c1806d8d19b167722 (diff) | |
Merge pull request #3584 from hathach/fix-hil
Fix CDC echo test issue for IMXRT and fast MCUs
146 files changed, 1783 insertions, 798 deletions
diff --git a/.claude/commands/hil.md b/.claude/commands/hil.md index 2ba35ec22..07e1a865b 100644 --- a/.claude/commands/hil.md +++ b/.claude/commands/hil.md @@ -7,24 +7,52 @@ Run Hardware-in-the-Loop (HIL) tests on physical boards. ## Instructions -1. Determine the HIL config file: +1. Parse $ARGUMENTS: + - If $ARGUMENTS contains `-b BOARD_NAME`, run for that specific board only. + - If $ARGUMENTS is empty or has no `-b`, run for all boards in the config. + - Pass through any other flags (e.g. `-v` for verbose, `-r N` for retry count) directly to the command. + +2. Determine whether to run **locally** or **remotely via SSH**: + - **Local**: boards are attached to this machine (default when `local.json` is used) + - **Remote (`ssh ci.lan`)**: boards are attached to the CI machine (when `tinyusb.json` is used) + +3. **Local execution** (boards attached to this machine): ```bash - HIL_CONFIG=$( (systemctl list-units --type=service --state=running 2>/dev/null; systemctl --user list-units --type=service --state=running 2>/dev/null) | grep -q 'actions\.runner' && echo tinyusb.json || echo local.json ) + python test/hil/hil_test.py -b BOARD_NAME -B examples $HIL_CONFIG $EXTRA_ARGS ``` - Default is `local.json` for local development. -2. Parse $ARGUMENTS: - - If $ARGUMENTS contains `-b BOARD_NAME`, run for that specific board only. - - If $ARGUMENTS is empty or has no `-b`, run for all boards in the config. - - Pass through any other flags (e.g. `-v` for verbose) directly to the command. +4. **Remote execution** (boards attached to `ci.lan`): + Only copy the minimal files needed (firmware binaries + test script + config), then run remotely. + + ```bash + REMOTE=ci.lan + REMOTE_DIR=/tmp/tinyusb-hil + + # Create remote working directory + ssh $REMOTE "rm -rf $REMOTE_DIR && mkdir -p $REMOTE_DIR/test/hil" + + # Copy HIL test script and its dependency + scp test/hil/hil_test.py test/hil/pymtp.py test/hil/tinyusb.json $REMOTE:$REMOTE_DIR/test/hil/ + + # Copy only the firmware binaries for the target board(s) + # For a specific board: + scp -r examples/cmake-build-$BOARD_NAME $REMOTE:$REMOTE_DIR/examples/ + + # Or for all boards that have been built: + # for dir in examples/cmake-build-*/; do scp -r "$dir" $REMOTE:$REMOTE_DIR/examples/; done + + # Run the test remotely + ssh $REMOTE "cd $REMOTE_DIR && python3 test/hil/hil_test.py -b $BOARD_NAME -B examples tinyusb.json $EXTRA_ARGS" + ``` -3. Run the HIL test from the repo root directory: - - Specific board: `python test/hil/hil_test.py -b BOARD_NAME -B examples $HIL_CONFIG $EXTRA_ARGS` - - All boards: `python test/hil/hil_test.py -B examples $HIL_CONFIG $EXTRA_ARGS` + Note: The remote machine (`ci.lan`) must have: + - Python 3 with `pyserial` installed (`pip install pyserial`) + - Flasher tools: `JLinkExe`, `openocd`, etc. as needed by the board + - USB access to the boards (udev rules configured) -4. Use a timeout of at least 20 minutes (600000ms). HIL tests take 2-5 minutes. NEVER cancel early. +5. Use a timeout of at least 20 minutes (600000ms). HIL tests take 2-5 minutes. NEVER cancel early. -5. After the test completes: +6. After the test completes: - Show the test output to the user. - Summarize pass/fail results per board. - If there are failures, suggest re-running with `-v` flag for verbose output to help debug. diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index b458c9ce3..7669290a8 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -26,9 +26,3 @@ add_custom_target(tinyusb_metrics COMMENT "Generating average code size metrics" VERBATIM ) - -#add_custom_command(TARGET tinyusb_metrics POST_BUILD -# COMMAND ${Python3_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/../tools/metrics.py compare ${TOP}/cmake-build/cmake-build-${BOARD}/metrics.json ${CMAKE_BINARY_DIR}/metrics.json -# COMMENT "Generating average code size metrics" -# VERBATIM -# ) 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" diff --git a/hw/bsp/at32f402_405/family.c b/hw/bsp/at32f402_405/family.c index b7dbcbd98..56d4a7bea 100644 --- a/hw/bsp/at32f402_405/family.c +++ b/hw/bsp/at32f402_405/family.c @@ -205,22 +205,18 @@ int board_uart_read(uint8_t *buf, int len) { int board_uart_write(void const *buf, int len) { #if CFG_TUSB_OS == OPT_OS_NONE - int txsize = len; - u16 timeout = 0xffff; - while (txsize--) - { - while(usart_flag_get(PRINT_UART, USART_TDBE_FLAG) == RESET) - { - timeout--; - if(timeout == 0) - { - return 0; - } + uint8_t const *p = (uint8_t const *) buf; + int count = 0; + while (count < len) { + if (usart_flag_get(PRINT_UART, USART_TDBE_FLAG) != RESET) { + PRINT_UART->dt = (*p & 0x01FF); + p++; + count++; + } else { + break; } - PRINT_UART->dt = (*((uint8_t const *)buf) & 0x01FF); - buf++; } - return len; + return count; #else (void) buf; (void) len; diff --git a/hw/bsp/at32f403a_407/family.c b/hw/bsp/at32f403a_407/family.c index d4a7e446d..942e15872 100644 --- a/hw/bsp/at32f403a_407/family.c +++ b/hw/bsp/at32f403a_407/family.c @@ -223,19 +223,18 @@ int board_uart_read(uint8_t *buf, int len) { int board_uart_write(void const *buf, int len) { #if CFG_TUSB_OS == OPT_OS_NONE - int txsize = len; - u16 timeout = 0xffff; - while (txsize--) { - while (usart_flag_get(PRINT_UART, USART_TDBE_FLAG) == RESET) { - timeout--; - if (timeout == 0) { - return 0; - } + uint8_t const *p = (uint8_t const *) buf; + int count = 0; + while (count < len) { + if (usart_flag_get(PRINT_UART, USART_TDBE_FLAG) != RESET) { + PRINT_UART->dt = (*p & 0x01FF); + p++; + count++; + } else { + break; } - PRINT_UART->dt = (*((uint8_t const *) buf) & 0x01FF); - buf++; } - return len; + return count; #else (void) buf; (void) len; diff --git a/hw/bsp/at32f413/family.c b/hw/bsp/at32f413/family.c index adf29e097..d9af0ae4d 100644 --- a/hw/bsp/at32f413/family.c +++ b/hw/bsp/at32f413/family.c @@ -223,19 +223,18 @@ int board_uart_read(uint8_t *buf, int len) { int board_uart_write(void const *buf, int len) { #if CFG_TUSB_OS == OPT_OS_NONE - int txsize = len; - u16 timeout = 0xffff; - while (txsize--) { - while (usart_flag_get(PRINT_UART, USART_TDBE_FLAG) == RESET) { - timeout--; - if (timeout == 0) { - return 0; - } + uint8_t const *p = (uint8_t const *) buf; + int count = 0; + while (count < len) { + if (usart_flag_get(PRINT_UART, USART_TDBE_FLAG) != RESET) { + PRINT_UART->dt = (*p & 0x01FF); + p++; + count++; + } else { + break; } - PRINT_UART->dt = (*((uint8_t const *) buf) & 0x01FF); - buf++; } - return len; + return count; #else (void) buf; (void) len; diff --git a/hw/bsp/at32f415/family.c b/hw/bsp/at32f415/family.c index b592bf6c5..ca205d480 100644 --- a/hw/bsp/at32f415/family.c +++ b/hw/bsp/at32f415/family.c @@ -197,19 +197,18 @@ int board_uart_read(uint8_t *buf, int len) { // Send characters to UART. Return number of sent bytes int board_uart_write(void const *buf, int len) { #if CFG_TUSB_OS == OPT_OS_NONE - int txsize = len; - u16 timeout = 0xffff; - while (txsize--) { - while (usart_flag_get(PRINT_UART, USART_TDBE_FLAG) == RESET) { - timeout--; - if (timeout == 0) { - return 0; - } + uint8_t const *p = (uint8_t const *) buf; + int count = 0; + while (count < len) { + if (usart_flag_get(PRINT_UART, USART_TDBE_FLAG) != RESET) { + PRINT_UART->dt = (*p & 0x01FF); + p++; + count++; + } else { + break; } - PRINT_UART->dt = (*((uint8_t const *) buf) & 0x01FF); - buf++; } - return len; + return count; #else (void) buf; (void) len; diff --git a/hw/bsp/at32f423/family.c b/hw/bsp/at32f423/family.c index 71cb559dc..9f13dba07 100644 --- a/hw/bsp/at32f423/family.c +++ b/hw/bsp/at32f423/family.c @@ -224,19 +224,18 @@ int board_uart_read(uint8_t *buf, int len) { int board_uart_write(void const *buf, int len) { #if CFG_TUSB_OS == OPT_OS_NONE - int txsize = len; - u16 timeout = 0xffff; - while (txsize--) { - while (usart_flag_get(PRINT_UART, USART_TDBE_FLAG) == RESET) { - timeout--; - if (timeout == 0) { - return 0; - } + uint8_t const *p = (uint8_t const *) buf; + int count = 0; + while (count < len) { + if (usart_flag_get(PRINT_UART, USART_TDBE_FLAG) != RESET) { + PRINT_UART->dt = (*p & 0x01FF); + p++; + count++; + } else { + break; } - PRINT_UART->dt = (*((uint8_t const *) buf) & 0x01FF); - buf++; } - return len; + return count; #else (void) buf; (void) len; diff --git a/hw/bsp/at32f425/family.c b/hw/bsp/at32f425/family.c index 7f443509e..1629ad7c0 100644 --- a/hw/bsp/at32f425/family.c +++ b/hw/bsp/at32f425/family.c @@ -205,19 +205,18 @@ int board_uart_read(uint8_t *buf, int len) { // Send characters to UART. Return number of sent bytes int board_uart_write(void const *buf, int len) { #if CFG_TUSB_OS == OPT_OS_NONE - int txsize = len; - u16 timeout = 0xffff; - while (txsize--) { - while (usart_flag_get(PRINT_UART, USART_TDBE_FLAG) == RESET) { - timeout--; - if (timeout == 0) { - return 0; - } + uint8_t const *p = (uint8_t const *) buf; + int count = 0; + while (count < len) { + if (usart_flag_get(PRINT_UART, USART_TDBE_FLAG) != RESET) { + PRINT_UART->dt = (*p & 0x01FF); + p++; + count++; + } else { + break; } - PRINT_UART->dt = (*((uint8_t const *) buf) & 0x01FF); - buf++; } - return len; + return count; #else (void) buf; (void) len; diff --git a/hw/bsp/at32f435_437/family.c b/hw/bsp/at32f435_437/family.c index 6c6bc4d72..59a4fe120 100644 --- a/hw/bsp/at32f435_437/family.c +++ b/hw/bsp/at32f435_437/family.c @@ -250,19 +250,18 @@ int board_uart_read(uint8_t *buf, int len) { // Send characters to UART. Return number of sent bytes int board_uart_write(void const *buf, int len) { #if CFG_TUSB_OS == OPT_OS_NONE - int txsize = len; - u16 timeout = 0xffff; - while (txsize--) { - while (usart_flag_get(PRINT_UART, USART_TDBE_FLAG) == RESET) { - timeout--; - if (timeout == 0) { - return 0; - } + uint8_t const *p = (uint8_t const *) buf; + int count = 0; + while (count < len) { + if (usart_flag_get(PRINT_UART, USART_TDBE_FLAG) != RESET) { + PRINT_UART->dt = (*p & 0x01FF); + p++; + count++; + } else { + break; } - PRINT_UART->dt = (*((uint8_t const *) buf) & 0x01FF); - buf++; } - return len; + return count; #else (void) buf; (void) len; diff --git a/hw/bsp/at32f45x/family.c b/hw/bsp/at32f45x/family.c index fa0c1139f..27eae861f 100644 --- a/hw/bsp/at32f45x/family.c +++ b/hw/bsp/at32f45x/family.c @@ -201,19 +201,18 @@ int board_uart_read(uint8_t *buf, int len) { int board_uart_write(void const *buf, int len) { #if CFG_TUSB_OS == OPT_OS_NONE - int txsize = len; - uint16_t timeout = 0xffff; - while (txsize--) { - while (usart_flag_get(PRINT_UART, USART_TDBE_FLAG) == RESET) { - timeout--; - if (timeout == 0) { - return 0; - } + uint8_t const *p = (uint8_t const *) buf; + int count = 0; + while (count < len) { + if (usart_flag_get(PRINT_UART, USART_TDBE_FLAG) != RESET) { + PRINT_UART->dt = (*p & 0x01FF); + p++; + count++; + } else { + break; } - PRINT_UART->dt = (*((uint8_t const *) buf) & 0x01FF); - buf++; } - return len; + return count; #else (void) buf; (void) len; diff --git a/hw/bsp/board.c b/hw/bsp/board.c index 71c209950..ae58bb5fc 100644 --- a/hw/bsp/board.c +++ b/hw/bsp/board.c @@ -97,9 +97,22 @@ int sys_read (int fhdl, char *buf, size_t count) { #else // Default logging with on-board UART +// board_uart_write() is non-blocking, retry until all bytes sent. +// Returns negative if UART is not available (stub), break immediately. int sys_write (int fhdl, const char *buf, size_t count) { (void) fhdl; - return board_uart_write(buf, (int) count); + size_t written = 0; + while (written < count) { + int wr = board_uart_write(buf + written, (int)(count - written)); + if (wr < 0) { + break; // UART not available + } + if (wr == 0) { + continue; // TX busy, keep trying + } + written += (size_t) wr; + } + return (int) written; } int sys_read (int fhdl, char *buf, size_t count) { @@ -157,10 +170,13 @@ int board_getchar(void) { return (sys_read(0, &c, 1) > 0) ? (int) c : (-1); } -void board_putchar(int c) { - (void) sys_write(0, (const char*)&c, 1); +int board_putchar(int c) { + if (board_uart_write((const char *)&c, 1)) { + return c; + } else { + return -1; + } } - //-------------------------------------------------------------------- // FreeRTOS hooks //-------------------------------------------------------------------- diff --git a/hw/bsp/board_api.h b/hw/bsp/board_api.h index 4487871eb..73c06078a 100644 --- a/hw/bsp/board_api.h +++ b/hw/bsp/board_api.h @@ -92,10 +92,10 @@ uint32_t board_button_read(void); // Get board unique ID for USB serial number. Return number of bytes. Note max_len is typically 16 size_t board_get_unique_id(uint8_t id[], size_t max_len); -// Get characters from UART. Return number of read bytes +// Get characters from UART (non-blocking). Return number of read bytes. int board_uart_read(uint8_t *buf, int len); -// Send characters to UART. Return number of sent bytes +// Send characters to UART (non-blocking). Return number of sent bytes int board_uart_write(void const *buf, int len); //--------------------------------------------------------------------+ @@ -153,7 +153,7 @@ static inline void board_delay(uint32_t ms) { // stdio getchar() is blocking, this is non-blocking version int board_getchar(void); -void board_putchar(int c); +int board_putchar(int c); #ifdef __cplusplus } diff --git a/hw/bsp/broadcom_32bit/family.c b/hw/bsp/broadcom_32bit/family.c index 399397bb4..1c7f6c7a5 100644 --- a/hw/bsp/broadcom_32bit/family.c +++ b/hw/bsp/broadcom_32bit/family.c @@ -125,16 +125,16 @@ int board_uart_read(uint8_t* buf, int len) { } int board_uart_write(void const* buf, int len) { - for (int i = 0; i < len; i++) { - const char* cbuf = buf; - while (!UART1->STAT_b.TX_READY) {} - if (cbuf[i] == '\n') { - UART1->IO = '\r'; - while (!UART1->STAT_b.TX_READY) {} + const uint8_t* p = (const uint8_t*) buf; + int count = 0; + while (count < len) { + if (!UART1->STAT_b.TX_READY) { + break; } - UART1->IO = cbuf[i]; + UART1->IO = p[count]; + count++; } - return len; + return count; } #if CFG_TUSB_OS == OPT_OS_NONE diff --git a/hw/bsp/broadcom_64bit/family.c b/hw/bsp/broadcom_64bit/family.c index 399397bb4..1c7f6c7a5 100644 --- a/hw/bsp/broadcom_64bit/family.c +++ b/hw/bsp/broadcom_64bit/family.c @@ -125,16 +125,16 @@ int board_uart_read(uint8_t* buf, int len) { } int board_uart_write(void const* buf, int len) { - for (int i = 0; i < len; i++) { - const char* cbuf = buf; - while (!UART1->STAT_b.TX_READY) {} - if (cbuf[i] == '\n') { - UART1->IO = '\r'; - while (!UART1->STAT_b.TX_READY) {} + const uint8_t* p = (const uint8_t*) buf; + int count = 0; + while (count < len) { + if (!UART1->STAT_b.TX_READY) { + break; } - UART1->IO = cbuf[i]; + UART1->IO = p[count]; + count++; } - return len; + return count; } #if CFG_TUSB_OS == OPT_OS_NONE diff --git a/hw/bsp/ch32f20x/family.c b/hw/bsp/ch32f20x/family.c index dd84b7c77..ab6a6007a 100644 --- a/hw/bsp/ch32f20x/family.c +++ b/hw/bsp/ch32f20x/family.c @@ -136,11 +136,9 @@ int board_uart_read(uint8_t *buf, int len) int board_uart_write(void const *buf, int len) { - int txsize = len; - while ( txsize-- ) - { - uart_write(*(uint8_t const*) buf); - buf++; + uint8_t const *p = (uint8_t const *) buf; + for (int i = 0; i < len; i++) { + uart_write(p[i]); } return len; } diff --git a/hw/bsp/ch32v10x/family.c b/hw/bsp/ch32v10x/family.c index 344dcaf0b..72dae7086 100644 --- a/hw/bsp/ch32v10x/family.c +++ b/hw/bsp/ch32v10x/family.c @@ -143,11 +143,15 @@ int board_uart_read(uint8_t *buf, int len) { } int board_uart_write(void const *buf, int len) { - const char *bufc = (const char *) buf; - for (int i = 0; i < len; i++) { - while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET); - USART_SendData(USART1, *bufc++); + uint8_t const *p = (uint8_t const *) buf; + int count = 0; + while (count < len) { + if (USART_GetFlagStatus(USART1, USART_FLAG_TC) != RESET) { + USART_SendData(USART1, p[count]); + count++; + } else { + break; + } } - - return len; + return count; } diff --git a/hw/bsp/ch32v20x/boards/ch32v203g_r0_1v0/board.mk b/hw/bsp/ch32v20x/boards/ch32v203g_r0_1v0/board.mk index f71f53478..601e8eccd 100644 --- a/hw/bsp/ch32v20x/boards/ch32v203g_r0_1v0/board.mk +++ b/hw/bsp/ch32v20x/boards/ch32v203g_r0_1v0/board.mk @@ -4,6 +4,7 @@ CFLAGS += \ -DSYSCLK_FREQ_144MHz_HSI=144000000 \ -DCH32_FLASH_ENHANCE_READ_MODE=1 \ -DCFG_EXAMPLE_MSC_DUAL_READONLY \ + -DCFG_EXAMPLE_MSC_FILE_EXPLORER_RW_BUFSIZE=1024 # 32KB zero-wait, 224KB total flash LDFLAGS += \ diff --git a/hw/bsp/ch32v20x/family.c b/hw/bsp/ch32v20x/family.c index 4c22450f9..221f62107 100644 --- a/hw/bsp/ch32v20x/family.c +++ b/hw/bsp/ch32v20x/family.c @@ -207,14 +207,19 @@ int board_uart_read(uint8_t *buf, int len) { int board_uart_write(void const *buf, int len) { #ifdef UART_DEV - const char *bufc = (const char *) buf; - for (int i = 0; i < len; i++) { - while (USART_GetFlagStatus(UART_DEV, USART_FLAG_TC) == RESET); - USART_SendData(UART_DEV, *bufc++); + uint8_t const *p = (uint8_t const *) buf; + int count = 0; + while (count < len) { + if (USART_GetFlagStatus(UART_DEV, USART_FLAG_TC) != RESET) { + USART_SendData(UART_DEV, p[count]); + count++; + } else { + break; + } } + return count; #else (void) buf; (void) len; + return 0; #endif - - return len; } diff --git a/hw/bsp/ch32v30x/family.c b/hw/bsp/ch32v30x/family.c index 6295f7723..aee4e7d4f 100644 --- a/hw/bsp/ch32v30x/family.c +++ b/hw/bsp/ch32v30x/family.c @@ -173,12 +173,10 @@ int board_uart_read(uint8_t* buf, int len) { } int board_uart_write(void const* buf, int len) { - int txsize = len; - const char* bufc = (const char*) buf; - while (txsize--) { - uart_write(*bufc++); + uint8_t const *p = (uint8_t const *) buf; + for (int i = 0; i < len; i++) { + uart_write(p[i]); } - uart_sync(); return len; } diff --git a/hw/bsp/da1469x/family.c b/hw/bsp/da1469x/family.c index a4f7f2e8d..5f7e2d42f 100644 --- a/hw/bsp/da1469x/family.c +++ b/hw/bsp/da1469x/family.c @@ -123,14 +123,14 @@ uint32_t board_button_read(void) { int board_uart_read(uint8_t* buf, int len) { (void) buf; (void) len; - return 0; + return -1; } int board_uart_write(void const* buf, int len) { (void) buf; (void) len; - return 0; + return -1; } #if CFG_TUSB_OS == OPT_OS_NONE diff --git a/hw/bsp/efm32/family.c b/hw/bsp/efm32/family.c index d318e20d6..7a5ea6a74 100644 --- a/hw/bsp/efm32/family.c +++ b/hw/bsp/efm32/family.c @@ -673,13 +673,13 @@ uint32_t board_button_read(void) int board_uart_read(uint8_t* buf, int len) { (void) buf; (void) len; - return 0; + return -1; } int board_uart_write(void const * buf, int len) { (void) buf; (void) len; - return 0; + return -1; } #if CFG_TUSB_OS == OPT_OS_NONE diff --git a/hw/bsp/espressif/boards/family.c b/hw/bsp/espressif/boards/family.c index 04d8a4001..48b1253b6 100644 --- a/hw/bsp/espressif/boards/family.c +++ b/hw/bsp/espressif/boards/family.c @@ -162,8 +162,8 @@ int board_getchar(void) { return getchar(); } -void board_putchar(int c) { - putchar(c); +int board_putchar(int c) { + return putchar(c); } void board_init_after_tusb(void) { diff --git a/hw/bsp/f1c100s/family.c b/hw/bsp/f1c100s/family.c index 1e71333d4..e221217ef 100644 --- a/hw/bsp/f1c100s/family.c +++ b/hw/bsp/f1c100s/family.c @@ -68,12 +68,20 @@ int board_uart_read(uint8_t* buf, int len) { } int board_uart_write(void const* buf, int len) { - int txsize = len; - while (txsize--) { - sys_uart_putc(*(uint8_t const*) buf); - buf++; + // UART0 base = 0x01c25000, USR register at +0x7c, bit 1 = TFNF (TX FIFO not full) + volatile uint32_t *uart_usr = (volatile uint32_t *) (0x01c25000 + 0x7c); + volatile uint32_t *uart_thr = (volatile uint32_t *) (0x01c25000 + 0x00); + const uint8_t *p = (const uint8_t *) buf; + int count = 0; + while (count < len) { + if (*uart_usr & (1 << 1)) { + *uart_thr = p[count]; + count++; + } else { + break; + } } - return len; + return count; } #if CFG_TUSB_OS == OPT_OS_NONE diff --git a/hw/bsp/fomu/family.c b/hw/bsp/fomu/family.c index cf04a1f6f..d3ad7a738 100644 --- a/hw/bsp/fomu/family.c +++ b/hw/bsp/fomu/family.c @@ -104,16 +104,18 @@ int board_uart_read(uint8_t* buf, int len) int board_uart_write(void const * buf, int len) { - int32_t offset = 0; uint8_t const* buf8 = (uint8_t const*) buf; - for (offset = 0; offset < len; offset++) + int count = 0; + while (count < len) { - if (!(messible_status_read() & CSR_MESSIBLE_STATUS_FULL_OFFSET)) + if (messible_status_read() & CSR_MESSIBLE_STATUS_FULL_OFFSET) { - messible_in_write(buf8[offset]); + break; } + messible_in_write(buf8[count]); + count++; } - return len; + return count; } #if CFG_TUSB_OS == OPT_OS_NONE diff --git a/hw/bsp/ft9xx/family.c b/hw/bsp/ft9xx/family.c index ff24cfe89..5ee134eb0 100644 --- a/hw/bsp/ft9xx/family.c +++ b/hw/bsp/ft9xx/family.c @@ -221,16 +221,21 @@ int board_uart_read(uint8_t *buf, int len) // Send characters to UART int board_uart_write(void const *buf, int len) { - int r = 0; - + int count = 0; #ifdef BOARD_UART -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wcast-qual" // uart_writen does not have const for buffer parameter. - r = uart_writen(BOARD_UART, (uint8_t *)((const void *)buf), len); -#pragma GCC diagnostic pop + uint8_t const *p = (uint8_t const *) buf; + while (count < len) { + if (BOARD_UART->LSR_ICR_XON2 & MASK_UART_LSR_THRE) { + BOARD_UART->RHR_THR_DLL = p[count]; + count++; + } else { + break; + } + } +#else + (void) buf; (void) len; #endif - - return r; + return count; } // Get current milliseconds diff --git a/hw/bsp/gd32vf103/family.c b/hw/bsp/gd32vf103/family.c index 4c1099317..c1dc82bda 100644 --- a/hw/bsp/gd32vf103/family.c +++ b/hw/bsp/gd32vf103/family.c @@ -160,12 +160,17 @@ int board_uart_read(uint8_t* buf, int len) { int board_uart_write(void const* buf, int len) { #if defined(UART_DEV) - int txsize = len; - while (txsize--) { - usart_write(UART_DEV, *(uint8_t const*)buf); - buf++; + uint8_t const *p = (uint8_t const *) buf; + int count = 0; + while (count < len) { + if (usart_flag_get(UART_DEV, USART_FLAG_TBE) != RESET) { + usart_data_transmit(UART_DEV, p[count]); + count++; + } else { + break; + } } - return len; + return count; #else (void)buf; (void)len; diff --git a/hw/bsp/imxrt/family.c b/hw/bsp/imxrt/family.c index c1ee34b1a..9f3297e9a 100644 --- a/hw/bsp/imxrt/family.c +++ b/hw/bsp/imxrt/family.c @@ -229,8 +229,17 @@ int board_uart_read(uint8_t *buf, int len) { } int board_uart_write(void const *buf, int len) { - LPUART_WriteBlocking(UART_PORT, (uint8_t const *) buf, len); - return len; + const uint8_t *p = (const uint8_t *)buf; + int count = 0; + while (count < len) { + if (LPUART_GetStatusFlags(UART_PORT) & kLPUART_TxDataRegEmptyFlag) { + LPUART_WriteByte(UART_PORT, p[count]); + count++; + } else { + break; + } + } + return count; } #if CFG_TUSB_OS == OPT_OS_NONE diff --git a/hw/bsp/kinetis_k/family.c b/hw/bsp/kinetis_k/family.c index 1505defe0..8efab2762 100644 --- a/hw/bsp/kinetis_k/family.c +++ b/hw/bsp/kinetis_k/family.c @@ -125,13 +125,21 @@ int board_uart_read(uint8_t *buf, int len) { } int board_uart_write(void const *buf, int len) { - (void) buf; - (void) len; - #ifdef UART_DEV - UART_WriteBlocking(UART_DEV, (uint8_t const*) buf, len); - return len; + const uint8_t *p = (const uint8_t *) buf; + int count = 0; + while (count < len) { + if (UART_DEV->S1 & UART_S1_TDRE_MASK) { + UART_DEV->D = p[count]; + count++; + } else { + break; + } + } + return count; #else + (void) buf; + (void) len; return 0; #endif } diff --git a/hw/bsp/kinetis_k32l/family.c b/hw/bsp/kinetis_k32l/family.c index ec8dc6ecf..a3bd92cec 100644 --- a/hw/bsp/kinetis_k32l/family.c +++ b/hw/bsp/kinetis_k32l/family.c @@ -117,28 +117,30 @@ uint32_t board_button_read(void) { } int board_uart_read(uint8_t* buf, int len) { -#if 0 /* - Use this version if want the LED to blink during BOARD=board_test, - without having to hit a key. - */ - if( 0U != (kLPUART_RxDataRegFullFlag & LPUART_GetStatusFlags( UART_PORT )) ) - { - LPUART_ReadBlocking(UART_PORT, buf, len); - return len; + int count = 0; + while (count < len) { + if (UART_PORT->STAT & LPUART_STAT_RDRF_MASK) { + buf[count] = (uint8_t) UART_PORT->DATA; + count++; + } else { + break; } - - return( 0 ); -#else /* Wait for 'len' characters to come in */ - - LPUART_ReadBlocking(UART_PORT, buf, len); - return len; - -#endif + } + return count; } int board_uart_write(void const* buf, int len) { - LPUART_WriteBlocking(UART_PORT, (uint8_t const*) buf, len); - return len; + const uint8_t *p = (const uint8_t *) buf; + int count = 0; + while (count < len) { + if (UART_PORT->STAT & LPUART_STAT_TDRE_MASK) { + UART_PORT->DATA = p[count]; + count++; + } else { + break; + } + } + return count; } #if CFG_TUSB_OS == OPT_OS_NONE diff --git a/hw/bsp/kinetis_kl/family.c b/hw/bsp/kinetis_kl/family.c index f89434d06..b378b6821 100644 --- a/hw/bsp/kinetis_kl/family.c +++ b/hw/bsp/kinetis_kl/family.c @@ -125,14 +125,31 @@ uint32_t board_button_read(void) int board_uart_read(uint8_t* buf, int len) { - LPSCI_ReadBlocking(UART_PORT, buf, len); - return len; + int count = 0; + while (count < len) { + if (UART_PORT->S1 & UART0_S1_RDRF_MASK) { + buf[count] = UART_PORT->D; + count++; + } else { + break; + } + } + return count; } int board_uart_write(void const * buf, int len) { - LPSCI_WriteBlocking(UART_PORT, (uint8_t const*) buf, len); - return len; + const uint8_t *p = (const uint8_t *) buf; + int count = 0; + while (count < len) { + if (UART_PORT->S1 & UART0_S1_TDRE_MASK) { + UART_PORT->D = p[count]; + count++; + } else { + break; + } + } + return count; } #if CFG_TUSB_OS == OPT_OS_NONE diff --git a/hw/bsp/lpc11/family.c b/hw/bsp/lpc11/family.c index 76c2bd17c..06b74bcd7 100644 --- a/hw/bsp/lpc11/family.c +++ b/hw/bsp/lpc11/family.c @@ -114,13 +114,13 @@ uint32_t board_button_read(void) { int board_uart_read(uint8_t* buf, int len) { (void) buf; (void) len; - return 0; + return -1; } int board_uart_write(void const* buf, int len) { (void) buf; (void) len; - return 0; + return -1; } #if CFG_TUSB_OS == OPT_OS_NONE diff --git a/hw/bsp/lpc13/family.c b/hw/bsp/lpc13/family.c index 8513f4df4..e25449689 100644 --- a/hw/bsp/lpc13/family.c +++ b/hw/bsp/lpc13/family.c @@ -101,11 +101,11 @@ uint32_t board_button_read(void) { int board_uart_read(uint8_t* buf, int len) { (void) buf; (void) len; - return 0; + return -1; } int board_uart_write(void const* buf, int len) { (void) buf; (void) len; - return 0; + return -1; } diff --git a/hw/bsp/lpc15/family.c b/hw/bsp/lpc15/family.c index 0d092d3a9..bbfee1b51 100644 --- a/hw/bsp/lpc15/family.c +++ b/hw/bsp/lpc15/family.c @@ -130,7 +130,17 @@ int board_uart_read(uint8_t* buf, int len) int board_uart_write(void const * buf, int len) { - return Chip_UART_SendBlocking(UART_PORT, buf, len); + uint8_t const *p = (uint8_t const *) buf; + int count = 0; + while (count < len) { + if (Chip_UART_GetStatus(UART_PORT) & UART_STAT_TXRDY) { + Chip_UART_SendByte(UART_PORT, p[count]); + count++; + } else { + break; + } + } + return count; } #if CFG_TUSB_OS == OPT_OS_NONE diff --git a/hw/bsp/lpc17/family.c b/hw/bsp/lpc17/family.c index f398f7e3c..6b94c1163 100644 --- a/hw/bsp/lpc17/family.c +++ b/hw/bsp/lpc17/family.c @@ -64,29 +64,13 @@ void board_init(void) { Chip_GPIO_SetPinDIROutput(LPC_GPIO, LED_PORT, LED_PIN); Chip_GPIO_SetPinDIRInput(LPC_GPIO, BUTTON_PORT, BUTTON_PIN); -#if 0 //------------- UART -------------// - PINSEL_CFG_Type PinCfg = - { - .Portnum = 0, - .Pinnum = 0, // TXD is P0.0 - .Funcnum = 2, - .OpenDrain = 0, - .Pinmode = 0 - }; - PINSEL_ConfigPin(&PinCfg); - - PinCfg.Portnum = 0; - PinCfg.Pinnum = 1; // RXD is P0.1 - PINSEL_ConfigPin(&PinCfg); - - UART_CFG_Type UARTConfigStruct; - UART_ConfigStructInit(&UARTConfigStruct); - UARTConfigStruct.Baud_rate = CFG_BOARD_UART_BAUDRATE; - - UART_Init(BOARD_UART_PORT, &UARTConfigStruct); - UART_TxCmd(BOARD_UART_PORT, ENABLE); // Enable UART Transmit -#endif + // Pin muxing for UART3 (TXD3=P0.0, RXD3=P0.1) is configured in board.h pinmuxing[] + Chip_UART_Init(BOARD_UART_PORT); + Chip_UART_SetBaud(BOARD_UART_PORT, CFG_BOARD_UART_BAUDRATE); + Chip_UART_ConfigData(BOARD_UART_PORT, (UART_LCR_WLEN8 | UART_LCR_SBS_1BIT | UART_LCR_PARITY_DIS)); + Chip_UART_SetupFIFOS(BOARD_UART_PORT, (UART_FCR_FIFO_EN | UART_FCR_TRG_LEV0)); + Chip_UART_TXEnable(BOARD_UART_PORT); //------------- USB -------------// Chip_IOCON_SetPinMuxing(LPC_IOCON, pin_usb_mux, sizeof(pin_usb_mux) / sizeof(PINMUX_GRP_T)); @@ -125,17 +109,30 @@ uint32_t board_button_read(void) { } int board_uart_read(uint8_t* buf, int len) { -// return UART_ReceiveByte(BOARD_UART_PORT); - (void) buf; - (void) len; - return 0; + int count = 0; + while (count < len) { + if (BOARD_UART_PORT->LSR & UART_LSR_RDR) { + buf[count] = (uint8_t) BOARD_UART_PORT->RBR; + count++; + } else { + break; + } + } + return count; } int board_uart_write(void const* buf, int len) { -// UART_Send(BOARD_UART_PORT, &c, 1, BLOCKING); - (void) buf; - (void) len; - return 0; + const uint8_t *p = (const uint8_t *) buf; + int count = 0; + while (count < len) { + if (BOARD_UART_PORT->LSR & UART_LSR_THRE) { + BOARD_UART_PORT->THR = p[count]; + count++; + } else { + break; + } + } + return count; } #if CFG_TUSB_OS == OPT_OS_NONE diff --git a/hw/bsp/lpc18/family.c b/hw/bsp/lpc18/family.c index 2043cef99..8a612d9d8 100644 --- a/hw/bsp/lpc18/family.c +++ b/hw/bsp/lpc18/family.c @@ -128,12 +128,16 @@ int board_uart_read(uint8_t *buf, int len) { int board_uart_write(void const *buf, int len) { uint8_t const *buf8 = (uint8_t const *) buf; - for (int i = 0; i < len; i++) { - while ((Chip_UART_ReadLineStatus(UART_DEV) & UART_LSR_THRE) == 0) {} - Chip_UART_SendByte(UART_DEV, buf8[i]); + int count = 0; + while (count < len) { + if (Chip_UART_ReadLineStatus(UART_DEV) & UART_LSR_THRE) { + Chip_UART_SendByte(UART_DEV, buf8[count]); + count++; + } else { + break; + } } - - return len; + return count; } #if CFG_TUSB_OS == OPT_OS_NONE diff --git a/hw/bsp/lpc40/family.c b/hw/bsp/lpc40/family.c index 237cd996b..d8a63576b 100644 --- a/hw/bsp/lpc40/family.c +++ b/hw/bsp/lpc40/family.c @@ -139,14 +139,14 @@ int board_uart_read(uint8_t *buf, int len) { //return UART_ReceiveByte(BOARD_UART_PORT); (void) buf; (void) len; - return 0; + return -1; } int board_uart_write(void const *buf, int len) { //UART_Send(BOARD_UART_PORT, &c, 1, BLOCKING); (void) buf; (void) len; - return 0; + return -1; } #if CFG_TUSB_OS == OPT_OS_NONE diff --git a/hw/bsp/lpc43/family.c b/hw/bsp/lpc43/family.c index 56834a1b0..5aff49704 100644 --- a/hw/bsp/lpc43/family.c +++ b/hw/bsp/lpc43/family.c @@ -244,12 +244,16 @@ int board_uart_read(uint8_t *buf, int len) { int board_uart_write(void const *buf, int len) { uint8_t const *buf8 = (uint8_t const *) buf; - for ( int i = 0; i < len; i++ ) { - while ( (Chip_UART_ReadLineStatus(UART_DEV) & UART_LSR_THRE) == 0 ) {} - Chip_UART_SendByte(UART_DEV, buf8[i]); + int count = 0; + while (count < len) { + if (Chip_UART_ReadLineStatus(UART_DEV) & UART_LSR_THRE) { + Chip_UART_SendByte(UART_DEV, buf8[count]); + count++; + } else { + break; + } } - - return len; + return count; } #if CFG_TUSB_OS == OPT_OS_NONE diff --git a/hw/bsp/lpc51/family.c b/hw/bsp/lpc51/family.c index 847972350..57c3e26dd 100644 --- a/hw/bsp/lpc51/family.c +++ b/hw/bsp/lpc51/family.c @@ -106,13 +106,13 @@ uint32_t board_button_read(void) { int board_uart_read(uint8_t* buf, int len) { (void) buf; (void) len; - return 0; + return -1; } int board_uart_write(void const* buf, int len) { (void) buf; (void) len; - return 0; + return -1; } #if CFG_TUSB_OS == OPT_OS_NONE diff --git a/hw/bsp/lpc54/family.c b/hw/bsp/lpc54/family.c index 806bd53dc..5a5087e56 100644 --- a/hw/bsp/lpc54/family.c +++ b/hw/bsp/lpc54/family.c @@ -224,12 +224,27 @@ uint32_t board_button_read(void) { int board_uart_read(uint8_t* buf, int len) { (void) buf; (void) len; - return 0; + return -1; } int board_uart_write(void const* buf, int len) { - USART_WriteBlocking(UART_DEV, (uint8_t const*) buf, len); - return 0; +#ifdef UART_DEV + const uint8_t *p = (const uint8_t *) buf; + int count = 0; + while (count < len) { + if (UART_DEV->FIFOSTAT & USART_FIFOSTAT_TXNOTFULL_MASK) { + UART_DEV->FIFOWR = p[count]; + count++; + } else { + break; + } + } + return count; +#else + (void) buf; + (void) len; + return -1; +#endif } #if CFG_TUSB_OS == OPT_OS_NONE diff --git a/hw/bsp/lpc55/family.c b/hw/bsp/lpc55/family.c index b5d097234..e021caf35 100644 --- a/hw/bsp/lpc55/family.c +++ b/hw/bsp/lpc55/family.c @@ -173,12 +173,27 @@ uint32_t board_button_read(void) { int board_uart_read(uint8_t* buf, int len) { (void) buf; (void) len; - return 0; + return -1; } int board_uart_write(void const* buf, int len) { - USART_WriteBlocking(UART_DEV, (uint8_t const*) buf, len); - return len; +#ifdef UART_DEV + const uint8_t *p = (const uint8_t *) buf; + int count = 0; + while (count < len) { + if (UART_DEV->FIFOSTAT & USART_FIFOSTAT_TXNOTFULL_MASK) { + UART_DEV->FIFOWR = p[count]; + count++; + } else { + break; + } + } + return count; +#else + (void) buf; + (void) len; + return -1; +#endif } #if CFG_TUSB_OS == OPT_OS_NONE diff --git a/hw/bsp/maxim/family.c b/hw/bsp/maxim/family.c index 6ef4c12c1..7f2a0eee6 100644 --- a/hw/bsp/maxim/family.c +++ b/hw/bsp/maxim/family.c @@ -186,13 +186,17 @@ int board_uart_read(uint8_t *buf, int len) { } int board_uart_write(void const *buf, int len) { - int act_len = 0; - const uint8_t *ch_ptr = (const uint8_t *) buf; - while (act_len < len) { - MXC_UART_WriteCharacter(ConsoleUart, *ch_ptr++); - act_len++; + const uint8_t *p = (const uint8_t *) buf; + int count = 0; + while (count < len) { + if (MXC_UART_GetTXFIFOAvailable(ConsoleUart) > 0) { + MXC_UART_WriteCharacterRaw(ConsoleUart, p[count]); + count++; + } else { + break; + } } - return len; + return count; } #if CFG_TUSB_OS == OPT_OS_NONE diff --git a/hw/bsp/mcx/family.c b/hw/bsp/mcx/family.c index a3969c217..2facfb1e0 100644 --- a/hw/bsp/mcx/family.c +++ b/hw/bsp/mcx/family.c @@ -199,16 +199,25 @@ uint32_t board_button_read(void) { int board_uart_read(uint8_t* buf, int len) { (void) buf; (void) len; - return 0; + return -1; } int board_uart_write(void const* buf, int len) { #ifdef UART_DEV - LPUART_WriteBlocking(UART_DEV, (uint8_t const*) buf, len); - return len; + const uint8_t *p = (const uint8_t *) buf; + int count = 0; + while (count < len) { + if (UART_DEV->STAT & LPUART_STAT_TDRE_MASK) { + UART_DEV->DATA = p[count]; + count++; + } else { + break; + } + } + return count; #else (void) buf; (void) len; - return 0; + return -1; #endif } diff --git a/hw/bsp/mm32/family.c b/hw/bsp/mm32/family.c index 330b01f6d..651c9496e 100644 --- a/hw/bsp/mm32/family.c +++ b/hw/bsp/mm32/family.c @@ -153,14 +153,17 @@ int board_uart_read(uint8_t* buf, int len) { int board_uart_write(void const* buf, int len) { #ifdef UART_DEV - const char* buff = buf; - while (len) { - while ((UART1->CSR & UART_IT_TXIEN) == 0); //The loop is sent until it is finished - UART1->TDR = (*buff & 0xFF); - buff++; - len--; + uint8_t const *p = (uint8_t const *) buf; + int count = 0; + while (count < len) { + if (UART1->CSR & UART_IT_TXIEN) { + UART1->TDR = (p[count] & 0xFF); + count++; + } else { + break; + } } - return len; + return count; #else (void) buf; (void) len; diff --git a/hw/bsp/msp430/family.c b/hw/bsp/msp430/family.c index 413ad7db6..0438f0abc 100644 --- a/hw/bsp/msp430/family.c +++ b/hw/bsp/msp430/family.c @@ -186,16 +186,18 @@ int board_uart_read(uint8_t * buf, int len) int board_uart_write(void const * buf, int len) { - const char * char_buf = (const char *) buf; - - for(int i = 0; i < len; i++) + uint8_t const *p = (uint8_t const *) buf; + int count = 0; + while (count < len) { - // Wait until TX buffer is empty (cleared by writing buffer). - while(!(UCA1IFG & UCTXIFG)); - UCA1TXBUF = char_buf[i]; + if (UCA1IFG & UCTXIFG) { + UCA1TXBUF = p[count]; + count++; + } else { + break; + } } - - return len; + return count; } #if CFG_TUSB_OS == OPT_OS_NONE diff --git a/hw/bsp/msp432e4/family.c b/hw/bsp/msp432e4/family.c index 90eb945f4..be93976a5 100644 --- a/hw/bsp/msp432e4/family.c +++ b/hw/bsp/msp432e4/family.c @@ -188,11 +188,16 @@ int board_uart_read(uint8_t * buf, int len) int board_uart_write(void const * buf, int len) { uint8_t const *p = (uint8_t const *)buf; - for (int i = 0; i < len; ++i) { - while (UART0->FR & UART_FR_TXFF) ; - UART0->DR = *p++; + int count = 0; + while (count < len) { + if (!(UART0->FR & UART_FR_TXFF)) { + UART0->DR = p[count]; + count++; + } else { + break; + } } - return len; + return count; } #if CFG_TUSB_OS == OPT_OS_NONE diff --git a/hw/bsp/nrf/family.c b/hw/bsp/nrf/family.c index 04bfbf320..f1e8b829c 100644 --- a/hw/bsp/nrf/family.c +++ b/hw/bsp/nrf/family.c @@ -274,7 +274,7 @@ size_t board_get_unique_id(uint8_t id[], size_t max_len) { int board_uart_read(uint8_t* buf, int len) { (void) buf; (void) len; - return 0; + return -1; // nrfx_err_t err = nrfx_uarte_rx(&_uart_id, buf, (size_t) len); // return NRFX_SUCCESS == err ? len : 0; } diff --git a/hw/bsp/nuc100_120/family.c b/hw/bsp/nuc100_120/family.c index cfe60121c..4ff24aae7 100644 --- a/hw/bsp/nuc100_120/family.c +++ b/hw/bsp/nuc100_120/family.c @@ -120,11 +120,11 @@ uint32_t board_button_read(void) int board_uart_read(uint8_t* buf, int len) { (void) buf; (void) len; - return 0; + return -1; } int board_uart_write(void const * buf, int len) { (void) buf; (void) len; - return 0; + return -1; } diff --git a/hw/bsp/nuc121_125/family.c b/hw/bsp/nuc121_125/family.c index dce5b4d62..9dd3c71fc 100644 --- a/hw/bsp/nuc121_125/family.c +++ b/hw/bsp/nuc121_125/family.c @@ -109,11 +109,11 @@ uint32_t board_button_read(void) int board_uart_read(uint8_t* buf, int len) { (void) buf; (void) len; - return 0; + return -1; } int board_uart_write(void const * buf, int len) { (void) buf; (void) len; - return 0; + return -1; } diff --git a/hw/bsp/nuc126/family.c b/hw/bsp/nuc126/family.c index 3343064e5..663bffbe7 100644 --- a/hw/bsp/nuc126/family.c +++ b/hw/bsp/nuc126/family.c @@ -134,11 +134,11 @@ uint32_t board_button_read(void) int board_uart_read(uint8_t* buf, int len) { (void) buf; (void) len; - return 0; + return -1; } int board_uart_write(void const * buf, int len) { (void) buf; (void) len; - return 0; + return -1; } diff --git a/hw/bsp/nuc505/family.c b/hw/bsp/nuc505/family.c index f1a77e4a5..41f6017c2 100644 --- a/hw/bsp/nuc505/family.c +++ b/hw/bsp/nuc505/family.c @@ -117,11 +117,11 @@ uint32_t board_button_read(void) int board_uart_read(uint8_t* buf, int len) { (void) buf; (void) len; - return 0; + return -1; } int board_uart_write(void const * buf, int len) { (void) buf; (void) len; - return 0; + return -1; } diff --git a/hw/bsp/pic32mz/family.c b/hw/bsp/pic32mz/family.c index 2bfc876e1..5805e653f 100644 --- a/hw/bsp/pic32mz/family.c +++ b/hw/bsp/pic32mz/family.c @@ -96,7 +96,7 @@ TU_ATTR_WEAK int board_uart_read(uint8_t * buf, int len) (void) buf; (void) len; - return 0; + return -1; } TU_ATTR_WEAK int board_uart_write(void const * buf, int len) diff --git a/hw/bsp/ra/family.c b/hw/bsp/ra/family.c index f371e694b..c8a4d33d9 100644 --- a/hw/bsp/ra/family.c +++ b/hw/bsp/ra/family.c @@ -153,13 +153,13 @@ size_t board_get_unique_id(uint8_t id[], size_t max_len) { int board_uart_read(uint8_t *buf, int len) { (void) buf; (void) len; - return 0; + return -1; } int board_uart_write(void const *buf, int len) { (void) buf; (void) len; - return 0; + return -1; } #if CFG_TUSB_OS == OPT_OS_NONE diff --git a/hw/bsp/rp2040/family.c b/hw/bsp/rp2040/family.c index c64025036..b5a1375a2 100644 --- a/hw/bsp/rp2040/family.c +++ b/hw/bsp/rp2040/family.c @@ -273,11 +273,13 @@ int board_uart_read(uint8_t *buf, int len) { int board_uart_write(void const *buf, int len) { #ifdef UART_DEV - char const *bufch = (char const *) buf; - for ( int i = 0; i < len; i++ ) { - uart_putc(uart_inst, bufch[i]); + const uint8_t *p = (const uint8_t *) buf; + int count = 0; + while (count < len && uart_is_writable(uart_inst)) { + uart_putc_raw(uart_inst, p[count]); + count++; } - return len; + return count; #else (void) buf; (void) len; return 0; @@ -288,8 +290,8 @@ int board_getchar(void) { return getchar_timeout_us(0); } -void board_putchar(int c) { - stdio_putchar(c); +int board_putchar(int c) { + return stdio_putchar(c); } void board_init_after_tusb(void) { diff --git a/hw/bsp/rw61x/family.c b/hw/bsp/rw61x/family.c index fcc7fb262..5d7bd4754 100644 --- a/hw/bsp/rw61x/family.c +++ b/hw/bsp/rw61x/family.c @@ -108,16 +108,25 @@ uint32_t board_button_read(void) { int board_uart_read(uint8_t* buf, int len) { (void) buf; (void) len; - return 0; + return -1; } int board_uart_write(void const* buf, int len) { #ifdef UART_DEV - USART_WriteBlocking(UART_DEV, (uint8_t const*) buf, len); - return len; + const uint8_t *p = (const uint8_t *) buf; + int count = 0; + while (count < len) { + if (UART_DEV->FIFOSTAT & USART_FIFOSTAT_TXNOTFULL_MASK) { + UART_DEV->FIFOWR = p[count]; + count++; + } else { + break; + } + } + return count; #else (void) buf; (void) len; - return 0; + return -1; #endif } diff --git a/hw/bsp/samd11/family.c b/hw/bsp/samd11/family.c index bccbac8ea..0c987b85a 100644 --- a/hw/bsp/samd11/family.c +++ b/hw/bsp/samd11/family.c @@ -137,13 +137,13 @@ uint32_t board_button_read(void) int board_uart_read(uint8_t* buf, int len) { (void) buf; (void) len; - return 0; + return -1; } int board_uart_write(void const * buf, int len) { (void) buf; (void) len; - return 0; + return -1; } #if CFG_TUSB_OS == OPT_OS_NONE diff --git a/hw/bsp/samd2x_l2x/family.c b/hw/bsp/samd2x_l2x/family.c index 737219b6c..11b6343cd 100644 --- a/hw/bsp/samd2x_l2x/family.c +++ b/hw/bsp/samd2x_l2x/family.c @@ -322,7 +322,7 @@ static inline void uart_send_str(const char* text) int board_uart_read(uint8_t* buf, int len) { (void) buf; (void) len; - return 0; + return -1; } int board_uart_write(void const * buf, int len) @@ -343,13 +343,13 @@ static void uart_init(void) { int board_uart_read(uint8_t* buf, int len) { (void) buf; (void) len; - return 0; + return -1; } int board_uart_write(void const* buf, int len) { (void) buf; (void) len; - return 0; + return -1; } #endif diff --git a/hw/bsp/samd5x_e5x/family.c b/hw/bsp/samd5x_e5x/family.c index c008b9719..71ef2d6ce 100644 --- a/hw/bsp/samd5x_e5x/family.c +++ b/hw/bsp/samd5x_e5x/family.c @@ -194,13 +194,13 @@ size_t board_get_unique_id(uint8_t id[], size_t max_len) { int board_uart_read(uint8_t* buf, int len) { (void) buf; (void) len; - return 0; + return -1; } int board_uart_write(void const* buf, int len) { (void) buf; (void) len; - return 0; + return -1; } #if CFG_TUSB_OS == OPT_OS_NONE diff --git a/hw/bsp/same7x/family.c b/hw/bsp/same7x/family.c index 61c1792d1..d02e6c5f1 100644 --- a/hw/bsp/same7x/family.c +++ b/hw/bsp/same7x/family.c @@ -133,7 +133,7 @@ uint32_t board_button_read(void) { int board_uart_read(uint8_t *buf, int len) { (void) buf; (void) len; - return 0; + return -1; } int board_uart_write(void const *buf, int len) { diff --git a/hw/bsp/samg/family.c b/hw/bsp/samg/family.c index b27134305..519068986 100644 --- a/hw/bsp/samg/family.c +++ b/hw/bsp/samg/family.c @@ -134,11 +134,16 @@ int board_uart_read(uint8_t* buf, int len) { int board_uart_write(void const* buf, int len) { uint8_t const* buf8 = (uint8_t const*) buf; - for (int i = 0; i < len; i++) { - while (!_usart_sync_is_ready_to_send(&edbg_com)) {} - _usart_sync_write_byte(&edbg_com, buf8[i]); + int count = 0; + while (count < len) { + if (_usart_sync_is_ready_to_send(&edbg_com)) { + _usart_sync_write_byte(&edbg_com, buf8[count]); + count++; + } else { + break; + } } - return len; + return count; } #if CFG_TUSB_OS == OPT_OS_NONE diff --git a/hw/bsp/stm32c0/boards/stm32c071nucleo/board.h b/hw/bsp/stm32c0/boards/stm32c071nucleo/board.h index 460b42a21..085d0d721 100644 --- a/hw/bsp/stm32c0/boards/stm32c071nucleo/board.h +++ b/hw/bsp/stm32c0/boards/stm32c071nucleo/board.h @@ -54,8 +54,7 @@ #define BUTTON_STATE_ACTIVE 0 // Enable UART serial communication with the ST-Link -#define UART_DEV USART2 -#define UART_CLK_EN __HAL_RCC_USART2_CLK_ENABLE +#define UART_ID 2 #define UART_GPIO_PORT GPIOA #define UART_GPIO_AF GPIO_AF1_USART2 #define UART_TX_PIN GPIO_PIN_2 diff --git a/hw/bsp/stm32c0/family.c b/hw/bsp/stm32c0/family.c index e20c0ee15..72af3ce7f 100644 --- a/hw/bsp/stm32c0/family.c +++ b/hw/bsp/stm32c0/family.c @@ -33,6 +33,16 @@ #include "bsp/board_api.h" #include "board.h" +#ifdef UART_ID + #if UART_ID == 1 + #define USARTn USART1 + #define UARTn_CLK_ENABLE __HAL_RCC_USART1_CLK_ENABLE + #elif UART_ID == 2 + #define USARTn USART2 + #define UARTn_CLK_ENABLE __HAL_RCC_USART2_CLK_ENABLE + #endif +#endif + //--------------------------------------------------------------------+ // Forward USB interrupt events to TinyUSB IRQ Handler //--------------------------------------------------------------------+ @@ -49,7 +59,9 @@ void USB_IRQHandler(void) { //--------------------------------------------------------------------+ // MACRO TYPEDEF CONSTANT ENUM //--------------------------------------------------------------------+ +#ifdef UART_ID UART_HandleTypeDef UartHandle; +#endif void board_init(void) { HAL_Init(); @@ -93,8 +105,8 @@ void board_init(void) { HAL_GPIO_Init(BUTTON_PORT, &gpio_init); } -#ifdef UART_DEV - UART_CLK_EN(); +#ifdef UART_ID + UARTn_CLK_ENABLE(); // UART { GPIO_InitTypeDef gpio_init = { 0 }; @@ -107,7 +119,7 @@ void board_init(void) { } UartHandle = (UART_HandleTypeDef){ - .Instance = UART_DEV, + .Instance = USARTn, .Init.BaudRate = CFG_BOARD_UART_BAUDRATE, .Init.WordLength = UART_WORDLENGTH_8B, .Init.StopBits = UART_STOPBITS_1, @@ -118,6 +130,7 @@ void board_init(void) { .AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT }; HAL_UART_Init(&UartHandle); + HAL_UARTEx_EnableFifoMode(&UartHandle); #endif } @@ -148,19 +161,38 @@ size_t board_get_unique_id(uint8_t id[], size_t max_len) { } int board_uart_read(uint8_t *buf, int len) { - (void) buf; - (void) len; +#ifdef UART_ID + int count = 0; + while (count < len) { + if (__HAL_UART_GET_FLAG(&UartHandle, UART_FLAG_RXNE)) { + buf[count] = (uint8_t) UartHandle.Instance->RDR; + count++; + } else { + break; + } + } + return count; +#else + (void) buf; (void) len; return 0; +#endif } int board_uart_write(void const *buf, int len) { -#ifdef UART_DEV - HAL_UART_Transmit(&UartHandle, (uint8_t*)(uintptr_t) buf, len, 0xffff); - return len; +#ifdef UART_ID + const uint8_t *p = (const uint8_t *) buf; + int count = 0; + while (count < len) { + if (__HAL_UART_GET_FLAG(&UartHandle, UART_FLAG_TXE)) { + UartHandle.Instance->TDR = p[count]; + count++; + } else { + break; + } + } + return count; #else - (void) buf; - (void) len; - (void) UartHandle; + (void) buf; (void) len; return 0; #endif } diff --git a/hw/bsp/stm32f0/boards/stm32f070rbnucleo/board.h b/hw/bsp/stm32f0/boards/stm32f070rbnucleo/board.h index 82ad309a3..5239e014f 100644 --- a/hw/bsp/stm32f0/boards/stm32f070rbnucleo/board.h +++ b/hw/bsp/stm32f0/boards/stm32f070rbnucleo/board.h @@ -47,8 +47,7 @@ #define BUTTON_STATE_ACTIVE 0 // UART -#define UART_DEV USART2 -#define UART_CLK_EN __HAL_RCC_USART2_CLK_ENABLE +#define UART_ID 2 #define UART_GPIO_PORT GPIOA #define UART_GPIO_AF GPIO_AF1_USART2 #define UART_TX_PIN GPIO_PIN_2 diff --git a/hw/bsp/stm32f0/boards/stm32f072disco/board.h b/hw/bsp/stm32f0/boards/stm32f072disco/board.h index 3ca1b3641..971d9bdc0 100644 --- a/hw/bsp/stm32f0/boards/stm32f072disco/board.h +++ b/hw/bsp/stm32f0/boards/stm32f072disco/board.h @@ -47,8 +47,7 @@ #define BUTTON_STATE_ACTIVE 1 // UART -#define UART_DEV USART1 -#define UART_CLK_EN __HAL_RCC_USART1_CLK_ENABLE +#define UART_ID 1 #define UART_GPIO_PORT GPIOA #define UART_GPIO_AF GPIO_AF1_USART1 #define UART_TX_PIN GPIO_PIN_9 diff --git a/hw/bsp/stm32f0/boards/stm32f072eval/board.h b/hw/bsp/stm32f0/boards/stm32f072eval/board.h index 2828000b9..32023f8ab 100644 --- a/hw/bsp/stm32f0/boards/stm32f072eval/board.h +++ b/hw/bsp/stm32f0/boards/stm32f072eval/board.h @@ -50,8 +50,7 @@ #define BUTTON_STATE_ACTIVE 1 // UART -#define UART_DEV USART2 -#define UART_CLK_EN __HAL_RCC_USART2_CLK_ENABLE +#define UART_ID 2 #define UART_GPIO_PORT GPIOD #define UART_GPIO_AF GPIO_AF0_USART2 #define UART_TX_PIN GPIO_PIN_5 diff --git a/hw/bsp/stm32f0/family.c b/hw/bsp/stm32f0/family.c index 5a35a3e50..f413163e5 100644 --- a/hw/bsp/stm32f0/family.c +++ b/hw/bsp/stm32f0/family.c @@ -30,8 +30,23 @@ #include "stm32f0xx_hal.h" #include "bsp/board_api.h" +#include "common/tusb_fifo.h" #include "board.h" +#ifdef UART_ID + #if UART_ID == 1 + #define USARTn USART1 + #define USARTn_IRQn USART1_IRQn + #define USARTn_IRQHandler USART1_IRQHandler + #define UARTn_CLK_ENABLE __HAL_RCC_USART1_CLK_ENABLE + #elif UART_ID == 2 + #define USARTn USART2 + #define USARTn_IRQn USART2_IRQn + #define USARTn_IRQHandler USART2_IRQHandler + #define UARTn_CLK_ENABLE __HAL_RCC_USART2_CLK_ENABLE + #endif +#endif + //--------------------------------------------------------------------+ // Forward USB interrupt events to TinyUSB IRQ Handler //--------------------------------------------------------------------+ @@ -42,7 +57,36 @@ void USB_IRQHandler(void) { //--------------------------------------------------------------------+ // MACRO TYPEDEF CONSTANT ENUM //--------------------------------------------------------------------+ -UART_HandleTypeDef UartHandle; +#ifdef UART_ID +static UART_HandleTypeDef UartHandle = { + .Instance = USARTn, + .Init = { + .BaudRate = CFG_BOARD_UART_BAUDRATE, + .WordLength = UART_WORDLENGTH_8B, + .StopBits = UART_STOPBITS_1, + .Parity = UART_PARITY_NONE, + .HwFlowCtl = UART_HWCONTROL_NONE, + .Mode = UART_MODE_TX_RX, + .OverSampling = UART_OVERSAMPLING_16, + } +}; + +// RX ring buffer via RXNE interrupt +static uint8_t uart_rx_ff_buf[32]; +static tu_fifo_t uart_rx_ff; + +// F0 uses new USART IP (ISR/RDR/TDR/ICR) — same as F7 +void USARTn_IRQHandler(void) { + uint32_t isr = USARTn->ISR; + if (isr & USART_ISR_RXNE) { + uint8_t byte = (uint8_t) USARTn->RDR; + tu_fifo_write(&uart_rx_ff, &byte); + } + if (isr & (USART_ISR_ORE | USART_ISR_FE | USART_ISR_NE | USART_ISR_PE)) { + USARTn->ICR = USART_ICR_ORECF | USART_ICR_FECF | USART_ICR_NCF | USART_ICR_PECF; + } +} +#endif void board_init(void) { board_stm32f0_clock_init(); @@ -54,9 +98,6 @@ void board_init(void) { __HAL_RCC_GPIOD_CLK_ENABLE(); __HAL_RCC_GPIOF_CLK_ENABLE(); - // Enable UART Clock - UART_CLK_EN(); - #if CFG_TUSB_OS == OPT_OS_NONE // 1ms tick timer SysTick_Config(SystemCoreClock / 1000); @@ -84,6 +125,10 @@ void board_init(void) { GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; HAL_GPIO_Init(BUTTON_PORT, &GPIO_InitStruct); +#ifdef UART_ID + // Enable UART Clock + UARTn_CLK_ENABLE(); + // Uart GPIO_InitStruct.Pin = UART_TX_PIN | UART_RX_PIN; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; @@ -92,15 +137,12 @@ void board_init(void) { GPIO_InitStruct.Alternate = UART_GPIO_AF; HAL_GPIO_Init(UART_GPIO_PORT, &GPIO_InitStruct); - UartHandle.Instance = UART_DEV; - UartHandle.Init.BaudRate = CFG_BOARD_UART_BAUDRATE; - UartHandle.Init.WordLength = UART_WORDLENGTH_8B; - UartHandle.Init.StopBits = UART_STOPBITS_1; - UartHandle.Init.Parity = UART_PARITY_NONE; - UartHandle.Init.HwFlowCtl = UART_HWCONTROL_NONE; - UartHandle.Init.Mode = UART_MODE_TX_RX; - UartHandle.Init.OverSampling = UART_OVERSAMPLING_16; HAL_UART_Init(&UartHandle); + tu_fifo_config(&uart_rx_ff, uart_rx_ff_buf, sizeof(uart_rx_ff_buf), false); + USARTn->CR1 |= USART_CR1_RXNEIE; + NVIC_SetPriority(USARTn_IRQn, (1 << __NVIC_PRIO_BITS) - 1); + NVIC_EnableIRQ(USARTn_IRQn); +#endif // USB Pins // Configure USB DM and DP pins. This is optional, and maintained only for user guidance. @@ -141,15 +183,31 @@ size_t board_get_unique_id(uint8_t id[], size_t max_len) { } int board_uart_read(uint8_t *buf, int len) { - (void) buf; - (void) len; +#ifdef UART_ID + return (int) tu_fifo_read_n(&uart_rx_ff, buf, (uint16_t) len); +#else + (void) buf; (void) len; return 0; +#endif } int board_uart_write(void const *buf, int len) { - HAL_UART_Transmit(&UartHandle, (uint8_t * )(uintptr_t) - buf, len, 0xffff); - return len; +#ifdef UART_ID + const uint8_t *p = (const uint8_t *) buf; + int count = 0; + while (count < len) { + if (__HAL_UART_GET_FLAG(&UartHandle, UART_FLAG_TXE)) { + UartHandle.Instance->TDR = p[count]; + count++; + } else { + break; + } + } + return count; +#else + (void) buf; (void) len; + return 0; +#endif } #if CFG_TUSB_OS == OPT_OS_NONE diff --git a/hw/bsp/stm32f1/boards/stm32f103_bluepill/board.h b/hw/bsp/stm32f1/boards/stm32f103_bluepill/board.h index c8a74337f..3549c1d79 100644 --- a/hw/bsp/stm32f1/boards/stm32f103_bluepill/board.h +++ b/hw/bsp/stm32f1/boards/stm32f103_bluepill/board.h @@ -47,8 +47,7 @@ #define BUTTON_STATE_ACTIVE 1 // UART -#define UART_DEV USART1 -#define UART_CLK_EN __HAL_RCC_USART1_CLK_ENABLE +#define UART_ID 1 #define UART_GPIO_PORT GPIOA //#define UART_GPIO_AF GPIO_AF1_USART1 #define UART_TX_PIN GPIO_PIN_9 diff --git a/hw/bsp/stm32f1/family.c b/hw/bsp/stm32f1/family.c index 78a425453..74e0f53f2 100644 --- a/hw/bsp/stm32f1/family.c +++ b/hw/bsp/stm32f1/family.c @@ -30,8 +30,28 @@ #include "stm32f1xx_hal.h" #include "bsp/board_api.h" +#include "common/tusb_fifo.h" #include "board.h" +#ifdef UART_ID + #if UART_ID == 1 + #define USARTn USART1 + #define USARTn_IRQn USART1_IRQn + #define USARTn_IRQHandler USART1_IRQHandler + #define UARTn_CLK_ENABLE __HAL_RCC_USART1_CLK_ENABLE + #elif UART_ID == 2 + #define USARTn USART2 + #define USARTn_IRQn USART2_IRQn + #define USARTn_IRQHandler USART2_IRQHandler + #define UARTn_CLK_ENABLE __HAL_RCC_USART2_CLK_ENABLE + #elif UART_ID == 3 + #define USARTn USART3 + #define USARTn_IRQn USART3_IRQn + #define USARTn_IRQHandler USART3_IRQHandler + #define UARTn_CLK_ENABLE __HAL_RCC_USART3_CLK_ENABLE + #endif +#endif + //--------------------------------------------------------------------+ // Forward USB interrupt events to TinyUSB IRQ Handler //--------------------------------------------------------------------+ @@ -50,7 +70,32 @@ void USBWakeUp_IRQHandler(void) { //--------------------------------------------------------------------+ // MACRO TYPEDEF CONSTANT ENUM //--------------------------------------------------------------------+ -UART_HandleTypeDef UartHandle; +#ifdef UART_ID +static UART_HandleTypeDef UartHandle = { + .Instance = USARTn, + .Init.BaudRate = CFG_BOARD_UART_BAUDRATE, + .Init.WordLength = UART_WORDLENGTH_8B, + .Init.StopBits = UART_STOPBITS_1, + .Init.Parity = UART_PARITY_NONE, + .Init.HwFlowCtl = UART_HWCONTROL_NONE, + .Init.Mode = UART_MODE_TX_RX, + .Init.OverSampling = UART_OVERSAMPLING_16 +}; + +// RX ring buffer via RXNE interrupt +static uint8_t uart_rx_ff_buf[32]; +static tu_fifo_t uart_rx_ff; + +// F1 uses old USART IP (SR/DR) +void USARTn_IRQHandler(void) { + uint32_t sr = USARTn->SR; + if (sr & USART_SR_RXNE) { + uint8_t byte = (uint8_t) USARTn->DR; + tu_fifo_write(&uart_rx_ff, &byte); + } + // Reading DR clears RXNE. OR is cleared by reading SR then DR (already done). +} +#endif void board_init(void) { board_stm32f1_clock_init(); @@ -101,9 +146,9 @@ void board_init(void) { GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; HAL_GPIO_Init(BUTTON_PORT, &GPIO_InitStruct); -#ifdef UART_DEV +#ifdef UART_ID // UART - UART_CLK_EN(); + UARTn_CLK_ENABLE(); GPIO_InitStruct.Pin = UART_TX_PIN | UART_RX_PIN; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; @@ -112,17 +157,11 @@ void board_init(void) { //GPIO_InitStruct.Alternate = UART_GPIO_AF; HAL_GPIO_Init(UART_GPIO_PORT, &GPIO_InitStruct); - UartHandle = (UART_HandleTypeDef) { - .Instance = UART_DEV, - .Init.BaudRate = CFG_BOARD_UART_BAUDRATE, - .Init.WordLength = UART_WORDLENGTH_8B, - .Init.StopBits = UART_STOPBITS_1, - .Init.Parity = UART_PARITY_NONE, - .Init.HwFlowCtl = UART_HWCONTROL_NONE, - .Init.Mode = UART_MODE_TX_RX, - .Init.OverSampling = UART_OVERSAMPLING_16 - }; HAL_UART_Init(&UartHandle); + tu_fifo_config(&uart_rx_ff, uart_rx_ff_buf, sizeof(uart_rx_ff_buf), false); + USARTn->CR1 |= USART_CR1_RXNEIE; + NVIC_SetPriority(USARTn_IRQn, (1 << __NVIC_PRIO_BITS) - 1); + NVIC_EnableIRQ(USARTn_IRQn); #endif #ifdef USB_CONNECT_PIN @@ -184,14 +223,31 @@ size_t board_get_unique_id(uint8_t id[], size_t max_len) { } int board_uart_read(uint8_t *buf, int len) { - (void) buf; - (void) len; +#ifdef UART_ID + return (int) tu_fifo_read_n(&uart_rx_ff, buf, (uint16_t) len); +#else + (void) buf; (void) len; return 0; +#endif } int board_uart_write(void const *buf, int len) { - HAL_UART_Transmit(&UartHandle, (uint8_t *) (uintptr_t) buf, len, 0xffff); - return len; +#ifdef UART_ID + const uint8_t *p = (const uint8_t *) buf; + int count = 0; + while (count < len) { + if (UartHandle.Instance->SR & USART_SR_TXE) { + UartHandle.Instance->DR = p[count]; + count++; + } else { + break; + } + } + return count; +#else + (void) buf; (void) len; + return 0; +#endif } #if CFG_TUSB_OS == OPT_OS_NONE diff --git a/hw/bsp/stm32f1/family.cmake b/hw/bsp/stm32f1/family.cmake index 9e94d86c6..2ee7ea06a 100644 --- a/hw/bsp/stm32f1/family.cmake +++ b/hw/bsp/stm32f1/family.cmake @@ -39,6 +39,7 @@ function(family_add_board BOARD_TARGET) ${ST_HAL_DRIVER}/Src/${ST_PREFIX}_hal_rcc.c ${ST_HAL_DRIVER}/Src/${ST_PREFIX}_hal_rcc_ex.c ${ST_HAL_DRIVER}/Src/${ST_PREFIX}_hal_gpio.c + ${ST_HAL_DRIVER}/Src/${ST_PREFIX}_hal_dma.c ${ST_HAL_DRIVER}/Src/${ST_PREFIX}_hal_uart.c ) target_include_directories(${BOARD_TARGET} PUBLIC diff --git a/hw/bsp/stm32f1/family.mk b/hw/bsp/stm32f1/family.mk index d4b6dfa6c..ca022c7ec 100644 --- a/hw/bsp/stm32f1/family.mk +++ b/hw/bsp/stm32f1/family.mk @@ -34,6 +34,7 @@ SRC_C += \ ${ST_HAL_DRIVER}/Src/stm32${ST_FAMILY}xx_hal_rcc.c \ ${ST_HAL_DRIVER}/Src/stm32${ST_FAMILY}xx_hal_rcc_ex.c \ ${ST_HAL_DRIVER}/Src/stm32${ST_FAMILY}xx_hal_gpio.c \ + ${ST_HAL_DRIVER}/Src/stm32${ST_FAMILY}xx_hal_dma.c \ ${ST_HAL_DRIVER}/Src/stm32${ST_FAMILY}xx_hal_uart.c INC += \ diff --git a/hw/bsp/stm32f2/family.c b/hw/bsp/stm32f2/family.c index f1fd4ddb2..051a026c5 100644 --- a/hw/bsp/stm32f2/family.c +++ b/hw/bsp/stm32f2/family.c @@ -30,7 +30,28 @@ #include "stm32f2xx_hal.h" #include "bsp/board_api.h" +#include "common/tusb_fifo.h" #include "board.h" + +#ifdef UART_ID + #if UART_ID == 1 + #define USARTn USART1 + #define USARTn_IRQn USART1_IRQn + #define USARTn_IRQHandler USART1_IRQHandler + #define UARTn_CLK_ENABLE __HAL_RCC_USART1_CLK_ENABLE + #elif UART_ID == 2 + #define USARTn USART2 + #define USARTn_IRQn USART2_IRQn + #define USARTn_IRQHandler USART2_IRQHandler + #define UARTn_CLK_ENABLE __HAL_RCC_USART2_CLK_ENABLE + #elif UART_ID == 3 + #define USARTn USART3 + #define USARTn_IRQn USART3_IRQn + #define USARTn_IRQHandler USART3_IRQHandler + #define UARTn_CLK_ENABLE __HAL_RCC_USART3_CLK_ENABLE + #endif +#endif + //--------------------------------------------------------------------+ // Forward USB interrupt events to TinyUSB IRQ Handler //--------------------------------------------------------------------+ @@ -41,6 +62,38 @@ void OTG_FS_IRQHandler(void) { //--------------------------------------------------------------------+ // MACRO TYPEDEF CONSTANT ENUM //--------------------------------------------------------------------+ +#ifdef UART_ID +static UART_HandleTypeDef UartHandle = { + .Instance = USARTn, + .Init = { + .BaudRate = CFG_BOARD_UART_BAUDRATE, + .WordLength = UART_WORDLENGTH_8B, + .StopBits = UART_STOPBITS_1, + .Parity = UART_PARITY_NONE, + .HwFlowCtl = UART_HWCONTROL_NONE, + .Mode = UART_MODE_TX_RX, + .OverSampling = UART_OVERSAMPLING_16, + } +}; + +// RX ring buffer via RXNE interrupt +static uint8_t uart_rx_ff_buf[32]; +static tu_fifo_t uart_rx_ff; + +// F2 uses old USART IP (SR/DR) +void USARTn_IRQHandler(void) { + uint32_t sr = USARTn->SR; + if (sr & USART_SR_RXNE) { + uint8_t byte = (uint8_t) USARTn->DR; + tu_fifo_write(&uart_rx_ff, &byte); + } + // Reading DR clears RXNE. OR is cleared by reading SR then DR (already done). +} +#endif + +//--------------------------------------------------------------------+ +// MACRO TYPEDEF CONSTANT ENUM +//--------------------------------------------------------------------+ // enable all LED, Button, Uart, USB clock static void all_rcc_clk_enable(void) { @@ -110,6 +163,14 @@ void board_init(void) { cfg.vbus_sensing = true; tud_configure(0, TUD_CFGID_DWC2, &cfg); #endif + +#ifdef UART_ID + HAL_UART_Init(&UartHandle); + tu_fifo_config(&uart_rx_ff, uart_rx_ff_buf, sizeof(uart_rx_ff_buf), false); + USARTn->CR1 |= USART_CR1_RXNEIE; + NVIC_SetPriority(USARTn_IRQn, (1 << __NVIC_PRIO_BITS) - 1); + NVIC_EnableIRQ(USARTn_IRQn); +#endif } //--------------------------------------------------------------------+ @@ -126,15 +187,31 @@ uint32_t board_button_read(void) { } int board_uart_read(uint8_t* buf, int len) { - (void) buf; - (void) len; +#ifdef UART_ID + return (int) tu_fifo_read_n(&uart_rx_ff, buf, (uint16_t) len); +#else + (void) buf; (void) len; return 0; +#endif } int board_uart_write(void const* buf, int len) { - (void) buf; - (void) len; +#ifdef UART_ID + const uint8_t *p = (const uint8_t *) buf; + int count = 0; + while (count < len) { + if (UartHandle.Instance->SR & USART_SR_TXE) { + UartHandle.Instance->DR = p[count]; + count++; + } else { + break; + } + } + return count; +#else + (void) buf; (void) len; return 0; +#endif } #if CFG_TUSB_OS == OPT_OS_NONE diff --git a/hw/bsp/stm32f3/family.c b/hw/bsp/stm32f3/family.c index fde1e9f6d..0864d0fad 100644 --- a/hw/bsp/stm32f3/family.c +++ b/hw/bsp/stm32f3/family.c @@ -30,8 +30,28 @@ #include "stm32f3xx_hal.h" #include "bsp/board_api.h" +#include "common/tusb_fifo.h" #include "board.h" +#ifdef UART_ID + #if UART_ID == 1 + #define USARTn USART1 + #define USARTn_IRQn USART1_IRQn + #define USARTn_IRQHandler USART1_IRQHandler + #define UARTn_CLK_ENABLE __HAL_RCC_USART1_CLK_ENABLE + #elif UART_ID == 2 + #define USARTn USART2 + #define USARTn_IRQn USART2_IRQn + #define USARTn_IRQHandler USART2_IRQHandler + #define UARTn_CLK_ENABLE __HAL_RCC_USART2_CLK_ENABLE + #elif UART_ID == 3 + #define USARTn USART3 + #define USARTn_IRQn USART3_IRQn + #define USARTn_IRQHandler USART3_IRQHandler + #define UARTn_CLK_ENABLE __HAL_RCC_USART3_CLK_ENABLE + #endif +#endif + //--------------------------------------------------------------------+ // Forward USB interrupt events to TinyUSB IRQ Handler //--------------------------------------------------------------------+ @@ -63,6 +83,40 @@ void USBWakeUp_RMP_IRQHandler(void) { //--------------------------------------------------------------------+ // MACRO TYPEDEF CONSTANT ENUM //--------------------------------------------------------------------+ +#ifdef UART_ID +static UART_HandleTypeDef UartHandle = { + .Instance = USARTn, + .Init = { + .BaudRate = CFG_BOARD_UART_BAUDRATE, + .WordLength = UART_WORDLENGTH_8B, + .StopBits = UART_STOPBITS_1, + .Parity = UART_PARITY_NONE, + .HwFlowCtl = UART_HWCONTROL_NONE, + .Mode = UART_MODE_TX_RX, + .OverSampling = UART_OVERSAMPLING_16, + } +}; + +// RX ring buffer via RXNE interrupt +static uint8_t uart_rx_ff_buf[32]; +static tu_fifo_t uart_rx_ff; + +// F3 uses new USART IP (ISR/RDR/TDR/ICR) — same as F7 +void USARTn_IRQHandler(void) { + uint32_t isr = USARTn->ISR; + if (isr & USART_ISR_RXNE) { + uint8_t byte = (uint8_t) USARTn->RDR; + tu_fifo_write(&uart_rx_ff, &byte); + } + if (isr & (USART_ISR_ORE | USART_ISR_FE | USART_ISR_NE | USART_ISR_PE)) { + USARTn->ICR = USART_ICR_ORECF | USART_ICR_FECF | USART_ICR_NCF | USART_ICR_PECF; + } +} +#endif + +//--------------------------------------------------------------------+ +// MACRO TYPEDEF CONSTANT ENUM +//--------------------------------------------------------------------+ void board_init(void) { @@ -108,6 +162,14 @@ void board_init(void) { // Enable USB clock __HAL_RCC_USB_CLK_ENABLE(); + +#ifdef UART_ID + HAL_UART_Init(&UartHandle); + tu_fifo_config(&uart_rx_ff, uart_rx_ff_buf, sizeof(uart_rx_ff_buf), false); + USARTn->CR1 |= USART_CR1_RXNEIE; + NVIC_SetPriority(USARTn_IRQn, (1 << __NVIC_PRIO_BITS) - 1); + NVIC_EnableIRQ(USARTn_IRQn); +#endif } //--------------------------------------------------------------------+ @@ -137,15 +199,31 @@ size_t board_get_unique_id(uint8_t id[], size_t max_len) { } int board_uart_read(uint8_t* buf, int len) { - (void) buf; - (void) len; +#ifdef UART_ID + return (int) tu_fifo_read_n(&uart_rx_ff, buf, (uint16_t) len); +#else + (void) buf; (void) len; return 0; +#endif } int board_uart_write(void const* buf, int len) { - (void) buf; - (void) len; +#ifdef UART_ID + const uint8_t *p = (const uint8_t *) buf; + int count = 0; + while (count < len) { + if (__HAL_UART_GET_FLAG(&UartHandle, UART_FLAG_TXE)) { + UartHandle.Instance->TDR = p[count]; + count++; + } else { + break; + } + } + return count; +#else + (void) buf; (void) len; return 0; +#endif } #if CFG_TUSB_OS == OPT_OS_NONE diff --git a/hw/bsp/stm32f4/boards/feather_stm32f405/board.h b/hw/bsp/stm32f4/boards/feather_stm32f405/board.h index 2db42b98a..fa3233b46 100644 --- a/hw/bsp/stm32f4/boards/feather_stm32f405/board.h +++ b/hw/bsp/stm32f4/boards/feather_stm32f405/board.h @@ -36,7 +36,7 @@ extern "C" { #endif -#define UART_DEV USART3 +#define UART_ID 3 #define PINID_LED 0 #define PINID_BUTTON 1 @@ -104,8 +104,6 @@ static inline void board_clock_init(void) RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2; HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5); - // Enable clocks for Uart - __HAL_RCC_USART3_CLK_ENABLE(); } static inline void board_vbus_set(uint8_t rhport, bool state) { diff --git a/hw/bsp/stm32f4/boards/pyboardv11/board.h b/hw/bsp/stm32f4/boards/pyboardv11/board.h index 319d2336a..0848fb27d 100644 --- a/hw/bsp/stm32f4/boards/pyboardv11/board.h +++ b/hw/bsp/stm32f4/boards/pyboardv11/board.h @@ -36,7 +36,7 @@ extern "C" { #endif -#define UART_DEV USART2 +#define UART_ID 2 #define PINID_LED 0 #define PINID_BUTTON 1 @@ -104,8 +104,6 @@ static inline void board_clock_init(void) RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2; HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5); - // Enable clocks for Uart - __HAL_RCC_USART2_CLK_ENABLE(); } static inline void board_vbus_set(uint8_t rhport, bool state) { diff --git a/hw/bsp/stm32f4/boards/stm32f401blackpill/board.h b/hw/bsp/stm32f4/boards/stm32f401blackpill/board.h index b69ebbeaf..931087c1a 100644 --- a/hw/bsp/stm32f4/boards/stm32f401blackpill/board.h +++ b/hw/bsp/stm32f4/boards/stm32f401blackpill/board.h @@ -37,7 +37,7 @@ #endif // Enable PA2 as the debug log UART -#define UART_DEV USART2 +#define UART_ID 2 #define PINID_LED 0 #define PINID_BUTTON 1 @@ -105,8 +105,6 @@ static inline void board_clock_init(void) RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2); - // Enable clocks for Uart - __HAL_RCC_USART2_CLK_ENABLE(); } static inline void board_vbus_set(uint8_t rhport, bool state) { diff --git a/hw/bsp/stm32f4/boards/stm32f407blackvet/board.h b/hw/bsp/stm32f4/boards/stm32f407blackvet/board.h index ebefeb988..f6d4a71d3 100644 --- a/hw/bsp/stm32f4/boards/stm32f407blackvet/board.h +++ b/hw/bsp/stm32f4/boards/stm32f407blackvet/board.h @@ -37,7 +37,7 @@ #endif // Enable PA2 as the debug log UART -#define UART_DEV USART2 +#define UART_ID 2 #define PINID_LED 0 #define PINID_BUTTON 1 @@ -104,8 +104,6 @@ static inline void board_clock_init(void) RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2; HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5); - // Enable clocks for LED, Button, Uart - __HAL_RCC_USART2_CLK_ENABLE(); } static inline void board_vbus_set(uint8_t rhport, bool state) { diff --git a/hw/bsp/stm32f4/boards/stm32f407disco/board.h b/hw/bsp/stm32f4/boards/stm32f407disco/board.h index bcfa6059a..8f9a8a18d 100644 --- a/hw/bsp/stm32f4/boards/stm32f407disco/board.h +++ b/hw/bsp/stm32f4/boards/stm32f407disco/board.h @@ -38,7 +38,7 @@ // Enable PA2 as the debug log UART // It is not routed to the ST/Link on the Discovery board. -#define UART_DEV USART2 +#define UART_ID 2 #define PINID_LED 0 #define PINID_BUTTON 1 @@ -112,8 +112,6 @@ static inline void board_clock_init(void) RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2; HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5); - // Enable clocks Uart - __HAL_RCC_USART2_CLK_ENABLE(); } static inline void board_vbus_set(uint8_t rhport, bool state) { diff --git a/hw/bsp/stm32f4/boards/stm32f411blackpill/board.h b/hw/bsp/stm32f4/boards/stm32f411blackpill/board.h index 0faf6fe11..a12f47938 100644 --- a/hw/bsp/stm32f4/boards/stm32f411blackpill/board.h +++ b/hw/bsp/stm32f4/boards/stm32f411blackpill/board.h @@ -36,7 +36,7 @@ extern "C" { #endif -#define UART_DEV USART2 +#define UART_ID 2 #define PINID_LED 0 #define PINID_BUTTON 1 @@ -104,8 +104,6 @@ static inline void board_clock_init(void) RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2); - // Enable clocks for Uart - __HAL_RCC_USART2_CLK_ENABLE(); } static inline void board_vbus_set(uint8_t rhport, bool state) { diff --git a/hw/bsp/stm32f4/boards/stm32f411disco/board.h b/hw/bsp/stm32f4/boards/stm32f411disco/board.h index 1a289dfb5..291589629 100644 --- a/hw/bsp/stm32f4/boards/stm32f411disco/board.h +++ b/hw/bsp/stm32f4/boards/stm32f411disco/board.h @@ -36,7 +36,7 @@ extern "C" { #endif -#define UART_DEV USART2 +#define UART_ID 2 #define PINID_LED 0 #define PINID_BUTTON 1 @@ -109,8 +109,6 @@ static inline void board_clock_init(void) { RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2); - // Enable clocks for UART - __HAL_RCC_USART2_CLK_ENABLE(); } static inline void board_vbus_set(uint8_t rhport, bool state) { diff --git a/hw/bsp/stm32f4/boards/stm32f412disco/board.h b/hw/bsp/stm32f4/boards/stm32f412disco/board.h index 0689dfe87..b7192b185 100644 --- a/hw/bsp/stm32f4/boards/stm32f412disco/board.h +++ b/hw/bsp/stm32f4/boards/stm32f412disco/board.h @@ -37,7 +37,7 @@ #endif // UART Enable PA2 as the debug log UART -#define UART_DEV USART2 +#define UART_ID 2 #define PINID_LED 0 #define PINID_BUTTON 1 @@ -125,8 +125,6 @@ static inline void board_clock_init(void) { RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_3); - // Enable clocks for Uart - __HAL_RCC_USART2_CLK_ENABLE(); } static inline void board_vbus_set(uint8_t rhport, bool state) { diff --git a/hw/bsp/stm32f4/boards/stm32f412nucleo/board.h b/hw/bsp/stm32f4/boards/stm32f412nucleo/board.h index be58f8ae7..77616d707 100644 --- a/hw/bsp/stm32f4/boards/stm32f412nucleo/board.h +++ b/hw/bsp/stm32f4/boards/stm32f412nucleo/board.h @@ -37,7 +37,7 @@ #endif // UART Enable for STLink VCOM -#define UART_DEV USART3 +#define UART_ID 3 #define PINID_LED 0 #define PINID_BUTTON 1 @@ -126,8 +126,6 @@ static inline void board_clock_init(void) RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_3); - // Enable clocks for Uart - __HAL_RCC_USART3_CLK_ENABLE(); } static inline void board_vbus_set(uint8_t rhport, bool state) { diff --git a/hw/bsp/stm32f4/boards/stm32f439nucleo/board.h b/hw/bsp/stm32f4/boards/stm32f439nucleo/board.h index b1633b395..973ab4b8a 100644 --- a/hw/bsp/stm32f4/boards/stm32f439nucleo/board.h +++ b/hw/bsp/stm32f4/boards/stm32f439nucleo/board.h @@ -38,7 +38,7 @@ // UART Enable for STLink VCOM -#define UART_DEV USART3 +#define UART_ID 3 #define PINID_LED 0 #define PINID_BUTTON 1 @@ -115,8 +115,6 @@ static inline void board_clock_init(void) RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2; HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5); - // Enable clocks Uart - __HAL_RCC_USART3_CLK_ENABLE(); } static inline void board_vbus_set(uint8_t rhport, bool state) { diff --git a/hw/bsp/stm32f4/family.c b/hw/bsp/stm32f4/family.c index 665ea114a..f4ef99150 100644 --- a/hw/bsp/stm32f4/family.c +++ b/hw/bsp/stm32f4/family.c @@ -30,6 +30,7 @@ #include "stm32f4xx_hal.h" #include "bsp/board_api.h" +#include "common/tusb_fifo.h" typedef struct { GPIO_TypeDef* port; @@ -39,6 +40,30 @@ typedef struct { #include "board.h" +#ifdef UART_ID + #if UART_ID == 1 + #define USARTn USART1 + #define USARTn_IRQn USART1_IRQn + #define USARTn_IRQHandler USART1_IRQHandler + #define UARTn_CLK_ENABLE __HAL_RCC_USART1_CLK_ENABLE + #elif UART_ID == 2 + #define USARTn USART2 + #define USARTn_IRQn USART2_IRQn + #define USARTn_IRQHandler USART2_IRQHandler + #define UARTn_CLK_ENABLE __HAL_RCC_USART2_CLK_ENABLE + #elif UART_ID == 3 + #define USARTn USART3 + #define USARTn_IRQn USART3_IRQn + #define USARTn_IRQHandler USART3_IRQHandler + #define UARTn_CLK_ENABLE __HAL_RCC_USART3_CLK_ENABLE + #elif UART_ID == 6 + #define USARTn USART6 + #define USARTn_IRQn USART6_IRQn + #define USARTn_IRQHandler USART6_IRQHandler + #define UARTn_CLK_ENABLE __HAL_RCC_USART6_CLK_ENABLE + #endif +#endif + //--------------------------------------------------------------------+ // Forward USB interrupt events to TinyUSB IRQ Handler //--------------------------------------------------------------------+ @@ -53,19 +78,33 @@ void OTG_HS_IRQHandler(void) { //--------------------------------------------------------------------+ // MACRO TYPEDEF CONSTANT ENUM //--------------------------------------------------------------------+ -#ifdef UART_DEV +#ifdef UART_ID static UART_HandleTypeDef UartHandle = { - .Instance = UART_DEV, + .Instance = USARTn, .Init = { - .BaudRate = CFG_BOARD_UART_BAUDRATE, - .WordLength = UART_WORDLENGTH_8B, - .StopBits = UART_STOPBITS_1, - .Parity = UART_PARITY_NONE, - .HwFlowCtl = UART_HWCONTROL_NONE, - .Mode = UART_MODE_TX_RX, + .BaudRate = CFG_BOARD_UART_BAUDRATE, + .WordLength = UART_WORDLENGTH_8B, + .StopBits = UART_STOPBITS_1, + .Parity = UART_PARITY_NONE, + .HwFlowCtl = UART_HWCONTROL_NONE, + .Mode = UART_MODE_TX_RX, .OverSampling = UART_OVERSAMPLING_16 } }; + +// RX ring buffer via RXNE interrupt +static uint8_t uart_rx_ff_buf[32]; +static tu_fifo_t uart_rx_ff; + +// F4 uses old USART IP (SR/DR) +void USARTn_IRQHandler(void) { + uint32_t sr = USARTn->SR; + if (sr & USART_SR_RXNE) { + uint8_t byte = (uint8_t) USARTn->DR; + tu_fifo_write(&uart_rx_ff, &byte); + } + // Reading DR clears RXNE. OR is cleared by reading SR then DR (already done). +} #endif void board_init(void) { @@ -111,8 +150,13 @@ void board_init(void) { board_led_write(false); -#ifdef UART_DEV +#ifdef UART_ID + UARTn_CLK_ENABLE(); HAL_UART_Init(&UartHandle); + tu_fifo_config(&uart_rx_ff, uart_rx_ff_buf, sizeof(uart_rx_ff_buf), false); + USARTn->CR1 |= USART_CR1_RXNEIE; + NVIC_SetPriority(USARTn_IRQn, (1 << __NVIC_PRIO_BITS) - 1); + NVIC_EnableIRQ(USARTn_IRQn); #endif //------------- USB FS -------------// @@ -228,15 +272,27 @@ size_t board_get_unique_id(uint8_t id[], size_t max_len) { } int board_uart_read(uint8_t *buf, int len) { - (void) buf; - (void) len; +#ifdef UART_ID + return (int) tu_fifo_read_n(&uart_rx_ff, buf, (uint16_t) len); +#else + (void) buf; (void) len; return 0; +#endif } int board_uart_write(void const *buf, int len) { -#ifdef UART_DEV - HAL_UART_Transmit(&UartHandle, (uint8_t *) (uintptr_t) buf, len, 0xffff); - return len; +#ifdef UART_ID + const uint8_t *p = (const uint8_t *) buf; + int count = 0; + while (count < len) { + if (UartHandle.Instance->SR & USART_SR_TXE) { + UartHandle.Instance->DR = p[count]; + count++; + } else { + break; + } + } + return count; #else (void) buf; (void) len; return 0; diff --git a/hw/bsp/stm32f7/boards/stlinkv3mini/board.h b/hw/bsp/stm32f7/boards/stlinkv3mini/board.h index 06adb79ad..88cef9f37 100644 --- a/hw/bsp/stm32f7/boards/stlinkv3mini/board.h +++ b/hw/bsp/stm32f7/boards/stlinkv3mini/board.h @@ -36,8 +36,7 @@ extern "C" { #endif -#define UART_DEV USART6 -#define UART_CLK_EN __HAL_RCC_USART6_CLK_ENABLE +#define UART_ID 6 // VBUS Sense detection #define OTG_FS_VBUS_SENSE 1 @@ -106,7 +105,6 @@ static inline void board_clock_init(void) HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_7); - UART_CLK_EN(); } static inline void board_vbus_set(uint8_t rhport, bool state) { diff --git a/hw/bsp/stm32f7/boards/stm32f723disco/board.h b/hw/bsp/stm32f7/boards/stm32f723disco/board.h index ca9641c68..698a1a230 100644 --- a/hw/bsp/stm32f7/boards/stm32f723disco/board.h +++ b/hw/bsp/stm32f7/boards/stm32f723disco/board.h @@ -36,8 +36,7 @@ extern "C" { #endif -#define UART_DEV USART6 -#define UART_CLK_EN __HAL_RCC_USART6_CLK_ENABLE +#define UART_ID 6 // VBUS Sense detection #define OTG_FS_VBUS_SENSE 1 @@ -121,7 +120,6 @@ static inline void board_clock_init(void) { HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_7); - UART_CLK_EN(); } static inline void board_vbus_set(uint8_t rhport, bool state) { diff --git a/hw/bsp/stm32f7/boards/stm32f746disco/board.h b/hw/bsp/stm32f7/boards/stm32f746disco/board.h index f57ffb317..cf70d8959 100644 --- a/hw/bsp/stm32f7/boards/stm32f746disco/board.h +++ b/hw/bsp/stm32f7/boards/stm32f746disco/board.h @@ -36,8 +36,7 @@ extern "C" { #endif -#define UART_DEV USART1 -#define UART_CLK_EN __HAL_RCC_USART1_CLK_ENABLE +#define UART_ID 1 // VBUS Sense detection #define OTG_FS_VBUS_SENSE 1 @@ -112,7 +111,6 @@ static inline void board_clock_init(void) HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_7); - UART_CLK_EN(); } static inline void board_vbus_set(uint8_t rhport, bool state) { diff --git a/hw/bsp/stm32f7/boards/stm32f746nucleo/board.h b/hw/bsp/stm32f7/boards/stm32f746nucleo/board.h index b039f5543..2d3a96dee 100644 --- a/hw/bsp/stm32f7/boards/stm32f746nucleo/board.h +++ b/hw/bsp/stm32f7/boards/stm32f746nucleo/board.h @@ -36,8 +36,7 @@ extern "C" { #endif -#define UART_DEV USART3 -#define UART_CLK_EN __HAL_RCC_USART3_CLK_ENABLE +#define UART_ID 3 // VBUS Sense detection #define OTG_FS_VBUS_SENSE 1 @@ -111,7 +110,6 @@ static inline void board_clock_init(void) HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_7); - UART_CLK_EN(); } static inline void board_vbus_set(uint8_t rhport, bool state) { diff --git a/hw/bsp/stm32f7/boards/stm32f767nucleo/board.h b/hw/bsp/stm32f7/boards/stm32f767nucleo/board.h index b5b3841f1..bd0963649 100644 --- a/hw/bsp/stm32f7/boards/stm32f767nucleo/board.h +++ b/hw/bsp/stm32f7/boards/stm32f767nucleo/board.h @@ -36,8 +36,7 @@ extern "C" { #endif -#define UART_DEV USART3 -#define UART_CLK_EN __HAL_RCC_USART3_CLK_ENABLE +#define UART_ID 3 // VBUS Sense detection #define OTG_FS_VBUS_SENSE 1 @@ -113,7 +112,6 @@ static inline void board_clock_init(void) HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_7); - UART_CLK_EN(); } static inline void board_vbus_set(uint8_t rhport, bool state) { diff --git a/hw/bsp/stm32f7/boards/stm32f769disco/board.h b/hw/bsp/stm32f7/boards/stm32f769disco/board.h index 8ac520619..ccf43a46b 100644 --- a/hw/bsp/stm32f7/boards/stm32f769disco/board.h +++ b/hw/bsp/stm32f7/boards/stm32f769disco/board.h @@ -36,8 +36,7 @@ extern "C" { #endif -#define UART_DEV USART1 -#define UART_CLK_EN __HAL_RCC_USART1_CLK_ENABLE +#define UART_ID 1 // VBUS Sense detection #define OTG_FS_VBUS_SENSE 1 @@ -113,7 +112,6 @@ static inline void board_clock_init(void) HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_7); - UART_CLK_EN(); } static inline void board_vbus_set(uint8_t rhport, bool state) { diff --git a/hw/bsp/stm32f7/family.c b/hw/bsp/stm32f7/family.c index f8145cd63..078e372d8 100644 --- a/hw/bsp/stm32f7/family.c +++ b/hw/bsp/stm32f7/family.c @@ -32,32 +32,70 @@ #include "stm32f7xx_hal.h" #include "bsp/board_api.h" +#include "common/tusb_fifo.h" typedef struct { - GPIO_TypeDef* port; + GPIO_TypeDef *port; GPIO_InitTypeDef pin_init; - uint8_t active_state; + uint8_t active_state; } board_pindef_t; #include "board.h" +#ifdef UART_ID + #if UART_ID == 1 + #define USARTn USART1 + #define USARTn_IRQn USART1_IRQn + #define USARTn_IRQHandler USART1_IRQHandler + #define UARTn_CLK_ENABLE __HAL_RCC_USART1_CLK_ENABLE + #elif UART_ID == 2 + #define USARTn USART2 + #define USARTn_IRQn USART2_IRQn + #define USARTn_IRQHandler USART2_IRQHandler + #define UARTn_CLK_ENABLE __HAL_RCC_USART2_CLK_ENABLE + #elif UART_ID == 3 + #define USARTn USART3 + #define USARTn_IRQn USART3_IRQn + #define USARTn_IRQHandler USART3_IRQHandler + #define UARTn_CLK_ENABLE __HAL_RCC_USART3_CLK_ENABLE + #elif UART_ID == 6 + #define USARTn USART6 + #define USARTn_IRQn USART6_IRQn + #define USARTn_IRQHandler USART6_IRQHandler + #define UARTn_CLK_ENABLE __HAL_RCC_USART6_CLK_ENABLE + #endif +#endif + //--------------------------------------------------------------------+ // MACRO TYPEDEF CONSTANT ENUM //--------------------------------------------------------------------+ -#ifdef UART_DEV -static UART_HandleTypeDef UartHandle = { - .Instance = UART_DEV, - .Init = { - .BaudRate = CFG_BOARD_UART_BAUDRATE, - .WordLength = UART_WORDLENGTH_8B, - .StopBits = UART_STOPBITS_1, - .Parity = UART_PARITY_NONE, - .HwFlowCtl = UART_HWCONTROL_NONE, - .Mode = UART_MODE_TX_RX, - .OverSampling = UART_OVERSAMPLING_16, +#ifdef UART_ID +static UART_HandleTypeDef UartHandle = {.Instance = USARTn, + .Init = { + .BaudRate = CFG_BOARD_UART_BAUDRATE, + .WordLength = UART_WORDLENGTH_8B, + .StopBits = UART_STOPBITS_1, + .Parity = UART_PARITY_NONE, + .HwFlowCtl = UART_HWCONTROL_NONE, + .Mode = UART_MODE_TX_RX, + .OverSampling = UART_OVERSAMPLING_16, + }}; + +// RX ring buffer via RXNE interrupt — no HAL IT functions used (avoid HAL state conflicts) +static uint8_t uart_rx_ff_buf[32]; +static tu_fifo_t uart_rx_ff; + +void USARTn_IRQHandler(void) { + uint32_t isr = USARTn->ISR; + if (isr & USART_ISR_RXNE) { + uint8_t byte = (uint8_t) USARTn->RDR; + tu_fifo_write(&uart_rx_ff, &byte); + } + if (isr & (USART_ISR_ORE | USART_ISR_FE | USART_ISR_NE | USART_ISR_PE)) { + USARTn->ICR = USART_ICR_ORECF | USART_ICR_FECF | USART_ICR_NCF | USART_ICR_PECF; } -}; +} #endif //--------------------------------------------------------------------+ @@ -90,8 +128,8 @@ void board_init(void) { __HAL_RCC_GPIOC_CLK_ENABLE(); __HAL_RCC_GPIOD_CLK_ENABLE(); __HAL_RCC_GPIOG_CLK_ENABLE(); - __HAL_RCC_GPIOH_CLK_ENABLE(); // ULPI NXT - __HAL_RCC_GPIOI_CLK_ENABLE(); // ULPI NXT + __HAL_RCC_GPIOH_CLK_ENABLE(); // ULPI NXT + __HAL_RCC_GPIOI_CLK_ENABLE(); // ULPI NXT #ifdef __HAL_RCC_GPIOJ_CLK_ENABLE __HAL_RCC_GPIOJ_CLK_ENABLE(); #endif @@ -109,48 +147,53 @@ void board_init(void) { SysTick->CTRL &= ~1U; // If freeRTOS is used, IRQ priority is limit by max syscall ( smaller is higher ) - NVIC_SetPriority(OTG_FS_IRQn, configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY ); - NVIC_SetPriority(OTG_HS_IRQn, configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY ); + NVIC_SetPriority(OTG_FS_IRQn, configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY); + NVIC_SetPriority(OTG_HS_IRQn, configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY); #endif -#ifdef UART_DEV +#ifdef UART_ID + UARTn_CLK_ENABLE(); HAL_UART_Init(&UartHandle); + tu_fifo_config(&uart_rx_ff, uart_rx_ff_buf, sizeof(uart_rx_ff_buf), false); + USARTn->CR1 |= USART_CR1_RXNEIE; + NVIC_SetPriority(USARTn_IRQn, (1 << __NVIC_PRIO_BITS) - 1); + NVIC_EnableIRQ(USARTn_IRQn); #endif GPIO_InitTypeDef GPIO_InitStruct; //------------- rhport0: OTG_FS -------------// /* Configure DM DP Pins */ - GPIO_InitStruct.Pin = (GPIO_PIN_11 | GPIO_PIN_12); - GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; - GPIO_InitStruct.Pull = GPIO_NOPULL; - GPIO_InitStruct.Speed = GPIO_SPEED_HIGH; + GPIO_InitStruct.Pin = (GPIO_PIN_11 | GPIO_PIN_12); + GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; + GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Speed = GPIO_SPEED_HIGH; GPIO_InitStruct.Alternate = GPIO_AF10_OTG_FS; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); /* Configure OTG-FS ID pin */ - GPIO_InitStruct.Pin = GPIO_PIN_10; - GPIO_InitStruct.Mode = GPIO_MODE_AF_OD; - GPIO_InitStruct.Pull = GPIO_PULLUP; + GPIO_InitStruct.Pin = GPIO_PIN_10; + GPIO_InitStruct.Mode = GPIO_MODE_AF_OD; + GPIO_InitStruct.Pull = GPIO_PULLUP; GPIO_InitStruct.Alternate = GPIO_AF10_OTG_FS; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); // Suppress warning caused by mcu driver #ifdef __GNUC__ -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wshadow" + #pragma GCC diagnostic push + #pragma GCC diagnostic ignored "-Wshadow" #endif /* Enable USB FS Clocks */ __HAL_RCC_USB_OTG_FS_CLK_ENABLE(); #ifdef __GNUC__ -#pragma GCC diagnostic pop + #pragma GCC diagnostic pop #endif #if OTG_FS_VBUS_SENSE /* Configure VBUS Pin */ - GPIO_InitStruct.Pin = GPIO_PIN_9; + GPIO_InitStruct.Pin = GPIO_PIN_9; GPIO_InitStruct.Mode = GPIO_MODE_INPUT; GPIO_InitStruct.Pull = GPIO_NOPULL; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); @@ -158,7 +201,7 @@ void board_init(void) { #if CFG_TUD_ENABLED && BOARD_TUD_RHPORT == 0 tud_configure_dwc2_t cfg = CFG_TUD_CONFIGURE_DWC2_DEFAULT; - cfg.vbus_sensing = OTG_FS_VBUS_SENSE; + cfg.vbus_sensing = OTG_FS_VBUS_SENSE; tud_configure(0, TUD_CFGID_DWC2, &cfg); #endif @@ -188,46 +231,46 @@ void board_init(void) { // MCU with external ULPI PHY /* ULPI CLK */ - GPIO_InitStruct.Pin = GPIO_PIN_5; - GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; - GPIO_InitStruct.Pull = GPIO_NOPULL; - GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; + GPIO_InitStruct.Pin = GPIO_PIN_5; + GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; + GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; GPIO_InitStruct.Alternate = GPIO_AF10_OTG_HS; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); /* ULPI D0 */ - GPIO_InitStruct.Pin = GPIO_PIN_3; - GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; - GPIO_InitStruct.Pull = GPIO_NOPULL; - GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; + GPIO_InitStruct.Pin = GPIO_PIN_3; + GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; + GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; GPIO_InitStruct.Alternate = GPIO_AF10_OTG_HS; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); /* ULPI D1 D2 D3 D4 D5 D6 D7 */ - GPIO_InitStruct.Pin = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_10 | GPIO_PIN_11 | GPIO_PIN_12 | GPIO_PIN_13 | GPIO_PIN_5; + GPIO_InitStruct.Pin = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_10 | GPIO_PIN_11 | GPIO_PIN_12 | GPIO_PIN_13 | GPIO_PIN_5; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Alternate = GPIO_AF10_OTG_HS; HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); /* ULPI STP */ - GPIO_InitStruct.Pin = GPIO_PIN_0 | GPIO_PIN_2; - GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; - GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Pin = GPIO_PIN_0 | GPIO_PIN_2; + GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; + GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Alternate = GPIO_AF10_OTG_HS; HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); /* NXT */ - GPIO_InitStruct.Pin = GPIO_PIN_4; - GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; - GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Pin = GPIO_PIN_4; + GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; + GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Alternate = GPIO_AF10_OTG_HS; HAL_GPIO_Init(GPIOH, &GPIO_InitStruct); /* ULPI DIR */ - GPIO_InitStruct.Pin = GPIO_PIN_11; - GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; - GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Pin = GPIO_PIN_11; + GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; + GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Alternate = GPIO_AF10_OTG_HS; HAL_GPIO_Init(GPIOI, &GPIO_InitStruct); #endif // USB_HS_PHYC @@ -238,7 +281,7 @@ void board_init(void) { #if CFG_TUD_ENABLED && BOARD_TUD_RHPORT == 1 tud_configure_dwc2_t cfg = CFG_TUD_CONFIGURE_DWC2_DEFAULT; - cfg.vbus_sensing = OTG_HS_VBUS_SENSE; + cfg.vbus_sensing = OTG_HS_VBUS_SENSE; tud_configure(1, TUD_CFGID_DWC2, &cfg); #endif @@ -258,17 +301,17 @@ void board_init(void) { void board_led_write(bool state) { #ifdef PINID_LED - board_pindef_t* pindef = &board_pindef[PINID_LED]; - GPIO_PinState pin_state = state == pindef->active_state ? GPIO_PIN_SET : GPIO_PIN_RESET; + board_pindef_t *pindef = &board_pindef[PINID_LED]; + GPIO_PinState pin_state = state == pindef->active_state ? GPIO_PIN_SET : GPIO_PIN_RESET; HAL_GPIO_WritePin(pindef->port, pindef->pin_init.Pin, pin_state); #else - (void) state; + (void)state; #endif } uint32_t board_button_read(void) { #ifdef PINID_BUTTON - board_pindef_t* pindef = &board_pindef[PINID_BUTTON]; + board_pindef_t *pindef = &board_pindef[PINID_BUTTON]; return pindef->active_state == HAL_GPIO_ReadPin(pindef->port, pindef->pin_init.Pin); #else return 0; @@ -276,10 +319,10 @@ uint32_t board_button_read(void) { } size_t board_get_unique_id(uint8_t id[], size_t max_len) { - (void) max_len; - volatile uint32_t * stm32_uuid = (volatile uint32_t *) UID_BASE; - uint32_t* id32 = (uint32_t*) (uintptr_t) id; - uint8_t const len = 12; + (void)max_len; + volatile uint32_t *stm32_uuid = (volatile uint32_t *)UID_BASE; + uint32_t *id32 = (uint32_t *)(uintptr_t)id; + const uint8_t len = 12; id32[0] = stm32_uuid[0]; id32[1] = stm32_uuid[1]; @@ -289,15 +332,22 @@ size_t board_get_unique_id(uint8_t id[], size_t max_len) { } int board_uart_read(uint8_t *buf, int len) { -#ifdef UART_DEV - int count = 0; - // clear overrun error if any - if (__HAL_UART_GET_FLAG(&UartHandle, UART_FLAG_ORE)) { - __HAL_UART_CLEAR_FLAG(&UartHandle, UART_CLEAR_OREF); - } - for (int i = 0; i < len; i++) { - if (__HAL_UART_GET_FLAG(&UartHandle, UART_FLAG_RXNE)) { - buf[i] = (uint8_t) UartHandle.Instance->RDR; +#ifdef UART_ID + return (int)tu_fifo_read_n(&uart_rx_ff, buf, (uint16_t)len); +#else + (void)buf; + (void)len; + return 0; +#endif +} + +int board_uart_write(const void *buf, int len) { +#ifdef UART_ID + const uint8_t *p = (const uint8_t *)buf; + int count = 0; + while (count < len) { + if (__HAL_UART_GET_FLAG(&UartHandle, UART_FLAG_TXE)) { + UartHandle.Instance->TDR = p[count]; count++; } else { break; @@ -305,22 +355,12 @@ int board_uart_read(uint8_t *buf, int len) { } return count; #else - (void) buf; (void) len; + (void)buf; + (void)len; return 0; #endif } -int board_uart_write(void const *buf, int len) { -#ifdef UART_DEV - HAL_UART_Transmit(&UartHandle, (uint8_t * )(uintptr_t) - buf, len, 0xffff); - return len; -#else - (void) buf; (void) len; - return -1; -#endif -} - #if CFG_TUSB_OS == OPT_OS_NONE volatile uint32_t system_ticks = 0; diff --git a/hw/bsp/stm32g0/boards/stm32g0b1nucleo/board.h b/hw/bsp/stm32g0/boards/stm32g0b1nucleo/board.h index 14d309da1..e02256034 100644 --- a/hw/bsp/stm32g0/boards/stm32g0b1nucleo/board.h +++ b/hw/bsp/stm32g0/boards/stm32g0b1nucleo/board.h @@ -52,8 +52,7 @@ #define BUTTON_STATE_ACTIVE 0 // UART Enable for STLink VCOM -#define UART_DEV USART2 -#define UART_CLK_EN __HAL_RCC_USART2_CLK_ENABLE +#define UART_ID 2 #define UART_GPIO_PORT GPIOA #define UART_GPIO_AF GPIO_AF1_USART2 #define UART_TX_PIN GPIO_PIN_2 diff --git a/hw/bsp/stm32g0/family.c b/hw/bsp/stm32g0/family.c index b25897264..d0ff8bac2 100644 --- a/hw/bsp/stm32g0/family.c +++ b/hw/bsp/stm32g0/family.c @@ -33,6 +33,22 @@ #include "bsp/board_api.h" #include "board.h" +#ifdef UART_ID + #if UART_ID == 1 + #define USARTn USART1 + #define UARTn_CLK_ENABLE __HAL_RCC_USART1_CLK_ENABLE + #elif UART_ID == 2 + #define USARTn USART2 + #define UARTn_CLK_ENABLE __HAL_RCC_USART2_CLK_ENABLE + #elif UART_ID == 3 + #define USARTn USART3 + #define UARTn_CLK_ENABLE __HAL_RCC_USART3_CLK_ENABLE + #elif UART_ID == 4 + #define USARTn USART4 + #define UARTn_CLK_ENABLE __HAL_RCC_USART4_CLK_ENABLE + #endif +#endif + //--------------------------------------------------------------------+ // Forward USB interrupt events to TinyUSB IRQ Handler //--------------------------------------------------------------------+ @@ -43,7 +59,7 @@ void USB_UCPD1_2_IRQHandler(void) { //--------------------------------------------------------------------+ // MACRO TYPEDEF CONSTANT ENUM //--------------------------------------------------------------------+ -#ifdef UART_DEV +#ifdef UART_ID UART_HandleTypeDef UartHandle; #endif @@ -90,8 +106,8 @@ void board_init(void) { GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; HAL_GPIO_Init(BUTTON_PORT, &GPIO_InitStruct); -#ifdef UART_DEV - UART_CLK_EN(); +#ifdef UART_ID + UARTn_CLK_ENABLE(); // UART GPIO_InitStruct.Pin = UART_TX_PIN | UART_RX_PIN; @@ -102,7 +118,7 @@ void board_init(void) { HAL_GPIO_Init(UART_GPIO_PORT, &GPIO_InitStruct); UartHandle = (UART_HandleTypeDef){ - .Instance = UART_DEV, + .Instance = USARTn, .Init.BaudRate = CFG_BOARD_UART_BAUDRATE, .Init.WordLength = UART_WORDLENGTH_8B, .Init.StopBits = UART_STOPBITS_1, @@ -113,6 +129,7 @@ void board_init(void) { .AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT }; HAL_UART_Init(&UartHandle); + HAL_UARTEx_EnableFifoMode(&UartHandle); #endif // USB Pins TODO double check USB clock and pin setup @@ -157,18 +174,38 @@ size_t board_get_unique_id(uint8_t id[], size_t max_len) { } int board_uart_read(uint8_t *buf, int len) { - (void) buf; - (void) len; +#ifdef UART_ID + int count = 0; + while (count < len) { + if (__HAL_UART_GET_FLAG(&UartHandle, UART_FLAG_RXNE)) { + buf[count] = (uint8_t) UartHandle.Instance->RDR; + count++; + } else { + break; + } + } + return count; +#else + (void) buf; (void) len; return 0; +#endif } int board_uart_write(void const *buf, int len) { -#ifdef UART_DEV - HAL_UART_Transmit(&UartHandle, (uint8_t*)(uintptr_t) buf, len, 0xffff); - return len; +#ifdef UART_ID + const uint8_t *p = (const uint8_t *) buf; + int count = 0; + while (count < len) { + if (__HAL_UART_GET_FLAG(&UartHandle, UART_FLAG_TXE)) { + UartHandle.Instance->TDR = p[count]; + count++; + } else { + break; + } + } + return count; #else - (void) buf; - (void) len; + (void) buf; (void) len; return 0; #endif } diff --git a/hw/bsp/stm32g4/boards/b_g474e_dpow1/board.h b/hw/bsp/stm32g4/boards/b_g474e_dpow1/board.h index d569783fc..1684454fa 100644 --- a/hw/bsp/stm32g4/boards/b_g474e_dpow1/board.h +++ b/hw/bsp/stm32g4/boards/b_g474e_dpow1/board.h @@ -51,8 +51,7 @@ #define BUTTON_STATE_ACTIVE 0 // UART Enable for STLink VCOM -#define UART_DEV USART3 -#define UART_CLK_EN __HAL_RCC_USART3_CLK_ENABLE +#define UART_ID 3 #define UART_GPIO_PORT GPIOC #define UART_GPIO_AF GPIO_AF7_USART3 #define UART_TX_PIN GPIO_PIN_10 diff --git a/hw/bsp/stm32g4/boards/stm32g474nucleo/board.h b/hw/bsp/stm32g4/boards/stm32g474nucleo/board.h index cfef1c09f..10d27f1dd 100644 --- a/hw/bsp/stm32g4/boards/stm32g474nucleo/board.h +++ b/hw/bsp/stm32g4/boards/stm32g474nucleo/board.h @@ -51,8 +51,7 @@ #define BUTTON_STATE_ACTIVE 1 // UART Enable for STLink VCOM -#define UART_DEV LPUART1 -#define UART_CLK_EN __HAL_RCC_LPUART1_CLK_ENABLE +#define UART_ID 11 #define UART_GPIO_PORT GPIOA #define UART_GPIO_AF GPIO_AF12_LPUART1 #define UART_TX_PIN GPIO_PIN_2 diff --git a/hw/bsp/stm32g4/boards/stm32g491nucleo/board.h b/hw/bsp/stm32g4/boards/stm32g491nucleo/board.h index be3d44645..f4c333621 100644 --- a/hw/bsp/stm32g4/boards/stm32g491nucleo/board.h +++ b/hw/bsp/stm32g4/boards/stm32g491nucleo/board.h @@ -51,8 +51,7 @@ #define BUTTON_STATE_ACTIVE 1 // UART Enable for STLink VCOM -#define UART_DEV LPUART1 -#define UART_CLK_EN __HAL_RCC_LPUART1_CLK_ENABLE +#define UART_ID 11 #define UART_GPIO_PORT GPIOA #define UART_GPIO_AF GPIO_AF12_LPUART1 #define UART_TX_PIN GPIO_PIN_2 diff --git a/hw/bsp/stm32g4/family.c b/hw/bsp/stm32g4/family.c index 2e13a1d4b..98739ffd5 100644 --- a/hw/bsp/stm32g4/family.c +++ b/hw/bsp/stm32g4/family.c @@ -34,6 +34,22 @@ #include "bsp/board_api.h" #include "board.h" +#ifdef UART_ID + #if UART_ID == 1 + #define USARTn USART1 + #define UARTn_CLK_ENABLE __HAL_RCC_USART1_CLK_ENABLE + #elif UART_ID == 2 + #define USARTn USART2 + #define UARTn_CLK_ENABLE __HAL_RCC_USART2_CLK_ENABLE + #elif UART_ID == 3 + #define USARTn USART3 + #define UARTn_CLK_ENABLE __HAL_RCC_USART3_CLK_ENABLE + #elif UART_ID == 11 + #define USARTn LPUART1 + #define UARTn_CLK_ENABLE __HAL_RCC_LPUART1_CLK_ENABLE + #endif +#endif + //--------------------------------------------------------------------+ // Forward USB interrupt events to TinyUSB IRQ Handler //--------------------------------------------------------------------+ @@ -57,7 +73,7 @@ void UCPD1_IRQHandler(void) { //--------------------------------------------------------------------+ // MACRO TYPEDEF CONSTANT ENUM //--------------------------------------------------------------------+ -#ifdef UART_DEV +#ifdef UART_ID UART_HandleTypeDef UartHandle; #endif @@ -106,8 +122,8 @@ void board_init(void) { GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; HAL_GPIO_Init(BUTTON_PORT, &GPIO_InitStruct); -#ifdef UART_DEV - UART_CLK_EN(); +#ifdef UART_ID + UARTn_CLK_ENABLE(); // UART memset(&GPIO_InitStruct, 0, sizeof(GPIO_InitStruct)); @@ -119,7 +135,7 @@ void board_init(void) { HAL_GPIO_Init(UART_GPIO_PORT, &GPIO_InitStruct); UartHandle = (UART_HandleTypeDef){ - .Instance = UART_DEV, + .Instance = USARTn, .Init.BaudRate = CFG_BOARD_UART_BAUDRATE, .Init.WordLength = UART_WORDLENGTH_8B, .Init.StopBits = UART_STOPBITS_1, @@ -129,6 +145,7 @@ void board_init(void) { .Init.OverSampling = UART_OVERSAMPLING_16 }; HAL_UART_Init(&UartHandle); + HAL_UARTEx_EnableFifoMode(&UartHandle); #endif // USB Pins TODO double check USB clock and pin setup @@ -187,18 +204,38 @@ size_t board_get_unique_id(uint8_t id[], size_t max_len) { } int board_uart_read(uint8_t *buf, int len) { - (void) buf; - (void) len; +#ifdef UART_ID + int count = 0; + while (count < len) { + if (__HAL_UART_GET_FLAG(&UartHandle, UART_FLAG_RXNE)) { + buf[count] = (uint8_t) UartHandle.Instance->RDR; + count++; + } else { + break; + } + } + return count; +#else + (void) buf; (void) len; return 0; +#endif } int board_uart_write(void const *buf, int len) { -#ifdef UART_DEV - HAL_UART_Transmit(&UartHandle, (uint8_t*)(uintptr_t) buf, len, 0xffff); - return len; +#ifdef UART_ID + const uint8_t *p = (const uint8_t *) buf; + int count = 0; + while (count < len) { + if (__HAL_UART_GET_FLAG(&UartHandle, UART_FLAG_TXE)) { + UartHandle.Instance->TDR = p[count]; + count++; + } else { + break; + } + } + return count; #else - (void) buf; - (void) len; + (void) buf; (void) len; return 0; #endif } diff --git a/hw/bsp/stm32h5/family.c b/hw/bsp/stm32h5/family.c index 1e8acd502..234a089eb 100644 --- a/hw/bsp/stm32h5/family.c +++ b/hw/bsp/stm32h5/family.c @@ -54,6 +54,19 @@ typedef struct { #include "board.h" +#ifdef UART_ID + #if UART_ID == 1 + #define USARTn USART1 + #define UARTn_CLK_ENABLE __HAL_RCC_USART1_CLK_ENABLE + #elif UART_ID == 2 + #define USARTn USART2 + #define UARTn_CLK_ENABLE __HAL_RCC_USART2_CLK_ENABLE + #elif UART_ID == 3 + #define USARTn USART3 + #define UARTn_CLK_ENABLE __HAL_RCC_USART3_CLK_ENABLE + #endif +#endif + //--------------------------------------------------------------------+ // Forward USB interrupt events to TinyUSB IRQ Handler //--------------------------------------------------------------------+ @@ -64,9 +77,9 @@ void USB_DRD_FS_IRQHandler(void) { //--------------------------------------------------------------------+ // MACRO TYPEDEF CONSTANT ENUM //--------------------------------------------------------------------+ -#ifdef UART_DEV +#ifdef UART_ID static UART_HandleTypeDef UartHandle = { - .Instance = UART_DEV, + .Instance = USARTn, .Init = { .BaudRate = CFG_BOARD_UART_BAUDRATE, .WordLength = UART_WORDLENGTH_8B, @@ -117,9 +130,10 @@ void board_init(void) { HAL_GPIO_Init(board_pindef[i].port, &board_pindef[i].pin_init); } - #ifdef UART_DEV - UART_CLK_EN(); + #ifdef UART_ID + UARTn_CLK_ENABLE(); HAL_UART_Init(&UartHandle); + HAL_UARTEx_EnableFifoMode(&UartHandle); #endif // USB Pins TODO double check USB clock and pin setup @@ -183,20 +197,40 @@ size_t board_get_unique_id(uint8_t id[], size_t max_len) { } int board_uart_read(uint8_t* buf, int len) { - (void) buf; - (void) len; +#ifdef UART_ID + int count = 0; + while (count < len) { + if (__HAL_UART_GET_FLAG(&UartHandle, UART_FLAG_RXNE)) { + buf[count] = (uint8_t) UartHandle.Instance->RDR; + count++; + } else { + break; + } + } + return count; +#else + (void) buf; (void) len; return 0; +#endif } int board_uart_write(void const* buf, int len) { - #ifdef UART_DEV - HAL_UART_Transmit(&UartHandle, (uint8_t*) (uintptr_t) buf, len, 0xffff); - return len; - #else - (void) buf; - (void) len; +#ifdef UART_ID + const uint8_t *p = (const uint8_t *) buf; + int count = 0; + while (count < len) { + if (__HAL_UART_GET_FLAG(&UartHandle, UART_FLAG_TXE)) { + UartHandle.Instance->TDR = p[count]; + count++; + } else { + break; + } + } + return count; +#else + (void) buf; (void) len; return 0; - #endif +#endif } #if CFG_TUSB_OS == OPT_OS_NONE diff --git a/hw/bsp/stm32h7/boards/daisyseed/board.h b/hw/bsp/stm32h7/boards/daisyseed/board.h index 300ecb8b2..d615dab6d 100644 --- a/hw/bsp/stm32h7/boards/daisyseed/board.h +++ b/hw/bsp/stm32h7/boards/daisyseed/board.h @@ -37,8 +37,7 @@ #endif // UART -#define UART_DEV USART3 -#define UART_CLK_EN __HAL_RCC_USART3_CLK_ENABLE +#define UART_ID 3 // VBUS Sense detection #define OTG_FS_VBUS_SENSE 1 diff --git a/hw/bsp/stm32h7/boards/stm32h723nucleo/board.h b/hw/bsp/stm32h7/boards/stm32h723nucleo/board.h index f623149bd..937ec5e9a 100644 --- a/hw/bsp/stm32h7/boards/stm32h723nucleo/board.h +++ b/hw/bsp/stm32h7/boards/stm32h723nucleo/board.h @@ -36,8 +36,7 @@ extern "C" { #endif -#define UART_DEV USART3 -#define UART_CLK_EN __HAL_RCC_USART3_CLK_ENABLE +#define UART_ID 3 // VBUS Sense detection #define OTG_FS_VBUS_SENSE 1 diff --git a/hw/bsp/stm32h7/boards/stm32h743_weact/board.h b/hw/bsp/stm32h7/boards/stm32h743_weact/board.h index e17ddb41a..1d9ced3f1 100644 --- a/hw/bsp/stm32h7/boards/stm32h743_weact/board.h +++ b/hw/bsp/stm32h7/boards/stm32h743_weact/board.h @@ -37,8 +37,7 @@ extern "C" { #endif // UART -#define UART_DEV USART3 -#define UART_CLK_EN __HAL_RCC_USART3_CLK_ENABLE +#define UART_ID 3 // VBUS Sense detection #define OTG_FS_VBUS_SENSE 0 diff --git a/hw/bsp/stm32h7/boards/stm32h743eval/board.h b/hw/bsp/stm32h7/boards/stm32h743eval/board.h index ea91976c8..cd1521911 100644 --- a/hw/bsp/stm32h7/boards/stm32h743eval/board.h +++ b/hw/bsp/stm32h7/boards/stm32h743eval/board.h @@ -39,8 +39,7 @@ #include "mfxstm32l152.h" // Need to change jumper setting J7 and J8 from RS-232 to STLink -#define UART_DEV USART1 -#define UART_CLK_EN __HAL_RCC_USART1_CLK_ENABLE +#define UART_ID 1 // VBUS Sense detection #define OTG_FS_VBUS_SENSE 1 diff --git a/hw/bsp/stm32h7/boards/stm32h743nucleo/board.h b/hw/bsp/stm32h7/boards/stm32h743nucleo/board.h index 0277d05c7..c6b209665 100644 --- a/hw/bsp/stm32h7/boards/stm32h743nucleo/board.h +++ b/hw/bsp/stm32h7/boards/stm32h743nucleo/board.h @@ -36,8 +36,7 @@ extern "C" { #endif -#define UART_DEV USART3 -#define UART_CLK_EN __HAL_RCC_USART3_CLK_ENABLE +#define UART_ID 3 // VBUS Sense detection #define OTG_FS_VBUS_SENSE 1 diff --git a/hw/bsp/stm32h7/boards/stm32h745disco/board.h b/hw/bsp/stm32h7/boards/stm32h745disco/board.h index ebdd5a17a..0536274b0 100644 --- a/hw/bsp/stm32h7/boards/stm32h745disco/board.h +++ b/hw/bsp/stm32h7/boards/stm32h745disco/board.h @@ -37,8 +37,7 @@ #endif // UART -#define UART_DEV USART3 -#define UART_CLK_EN __HAL_RCC_USART3_CLK_ENABLE +#define UART_ID 3 // VBUS Sense detection #define OTG_FS_VBUS_SENSE 1 diff --git a/hw/bsp/stm32h7/boards/stm32h747disco/board.h b/hw/bsp/stm32h7/boards/stm32h747disco/board.h index 1793338f8..204eb8bc4 100644 --- a/hw/bsp/stm32h7/boards/stm32h747disco/board.h +++ b/hw/bsp/stm32h7/boards/stm32h747disco/board.h @@ -37,8 +37,7 @@ #endif // UART -#define UART_DEV USART3 -#define UART_CLK_EN __HAL_RCC_USART3_CLK_ENABLE +#define UART_ID 3 // VBUS Sense detection #define OTG_FS_VBUS_SENSE 1 diff --git a/hw/bsp/stm32h7/boards/stm32h750_weact/board.h b/hw/bsp/stm32h7/boards/stm32h750_weact/board.h index e11a55103..dadcd6e86 100644 --- a/hw/bsp/stm32h7/boards/stm32h750_weact/board.h +++ b/hw/bsp/stm32h7/boards/stm32h750_weact/board.h @@ -37,8 +37,7 @@ #endif // UART -#define UART_DEV USART3 -#define UART_CLK_EN __HAL_RCC_USART3_CLK_ENABLE +#define UART_ID 3 // VBUS Sense detection #define OTG_FS_VBUS_SENSE 1 diff --git a/hw/bsp/stm32h7/boards/stm32h750bdk/board.h b/hw/bsp/stm32h7/boards/stm32h750bdk/board.h index ac417601b..6955592ee 100644 --- a/hw/bsp/stm32h7/boards/stm32h750bdk/board.h +++ b/hw/bsp/stm32h7/boards/stm32h750bdk/board.h @@ -37,8 +37,7 @@ #endif // UART -#define UART_DEV USART3 -#define UART_CLK_EN __HAL_RCC_USART3_CLK_ENABLE +#define UART_ID 3 // VBUS Sense detection #define OTG_FS_VBUS_SENSE 1 diff --git a/hw/bsp/stm32h7/boards/waveshare_openh743i/board.h b/hw/bsp/stm32h7/boards/waveshare_openh743i/board.h index bfaf42784..96f035919 100644 --- a/hw/bsp/stm32h7/boards/waveshare_openh743i/board.h +++ b/hw/bsp/stm32h7/boards/waveshare_openh743i/board.h @@ -76,8 +76,7 @@ #endif // Need to change jumper setting J7 and J8 from RS-232 to STLink -#define UART_DEV USART3 -#define UART_CLK_EN __HAL_RCC_USART3_CLK_ENABLE +#define UART_ID 3 // VBUS Sense detection #define OTG_FS_VBUS_SENSE 1 diff --git a/hw/bsp/stm32h7/family.c b/hw/bsp/stm32h7/family.c index a95674217..b32f73754 100644 --- a/hw/bsp/stm32h7/family.c +++ b/hw/bsp/stm32h7/family.c @@ -44,13 +44,29 @@ typedef struct { #include "board.h" +#ifdef UART_ID + #if UART_ID == 1 + #define USARTn USART1 + #define UARTn_CLK_ENABLE __HAL_RCC_USART1_CLK_ENABLE + #elif UART_ID == 2 + #define USARTn USART2 + #define UARTn_CLK_ENABLE __HAL_RCC_USART2_CLK_ENABLE + #elif UART_ID == 3 + #define USARTn USART3 + #define UARTn_CLK_ENABLE __HAL_RCC_USART3_CLK_ENABLE + #elif UART_ID == 6 + #define USARTn USART6 + #define UARTn_CLK_ENABLE __HAL_RCC_USART6_CLK_ENABLE + #endif +#endif + //--------------------------------------------------------------------+ // MACRO TYPEDEF CONSTANT ENUM //--------------------------------------------------------------------+ -#ifdef UART_DEV +#ifdef UART_ID static UART_HandleTypeDef UartHandle = { - .Instance = UART_DEV, + .Instance = USARTn, .Init = { .BaudRate = CFG_BOARD_UART_BAUDRATE, .WordLength = UART_WORDLENGTH_8B, @@ -147,9 +163,10 @@ void board_init(void) { GPIO_InitTypeDef GPIO_InitStruct; -#ifdef UART_DEV - UART_CLK_EN(); +#ifdef UART_ID + UARTn_CLK_ENABLE(); HAL_UART_Init(&UartHandle); + HAL_UARTEx_EnableFifoMode(&UartHandle); #endif //------------- USB FS -------------// @@ -275,7 +292,7 @@ size_t board_get_unique_id(uint8_t id[], size_t max_len) { } int board_uart_read(uint8_t *buf, int len) { -#ifdef UART_DEV +#ifdef UART_ID int count = 0; // clear overrun error if any if (__HAL_UART_GET_FLAG(&UartHandle, UART_FLAG_ORE)) { @@ -297,13 +314,21 @@ int board_uart_read(uint8_t *buf, int len) { } int board_uart_write(void const *buf, int len) { -#ifdef UART_DEV - HAL_UART_Transmit(&UartHandle, (uint8_t * )(uintptr_t) - buf, len, 0xffff); - return len; +#ifdef UART_ID + const uint8_t *p = (const uint8_t *) buf; + int count = 0; + while (count < len) { + if (__HAL_UART_GET_FLAG(&UartHandle, UART_FLAG_TXE)) { + UartHandle.Instance->TDR = p[count]; + count++; + } else { + break; + } + } + return count; #else (void) buf; (void) len; - return -1; + return 0; #endif } diff --git a/hw/bsp/stm32h7rs/boards/stm32h7s3nucleo/board.h b/hw/bsp/stm32h7rs/boards/stm32h7s3nucleo/board.h index 4b8564709..b1446414e 100644 --- a/hw/bsp/stm32h7rs/boards/stm32h7s3nucleo/board.h +++ b/hw/bsp/stm32h7rs/boards/stm32h7s3nucleo/board.h @@ -40,8 +40,7 @@ #include "stm32h7rsxx_ll_exti.h" #include "stm32h7rsxx_ll_system.h" -#define UART_DEV USART3 -#define UART_CLK_EN __HAL_RCC_USART3_CLK_ENABLE +#define UART_ID 3 // VBUS Sense detection #define OTG_FS_VBUS_SENSE 0 diff --git a/hw/bsp/stm32h7rs/family.c b/hw/bsp/stm32h7rs/family.c index 3bf75ba97..7ae9e5532 100644 --- a/hw/bsp/stm32h7rs/family.c +++ b/hw/bsp/stm32h7rs/family.c @@ -44,13 +44,26 @@ typedef struct { #include "board.h" +#ifdef UART_ID + #if UART_ID == 1 + #define USARTn USART1 + #define UARTn_CLK_ENABLE __HAL_RCC_USART1_CLK_ENABLE + #elif UART_ID == 2 + #define USARTn USART2 + #define UARTn_CLK_ENABLE __HAL_RCC_USART2_CLK_ENABLE + #elif UART_ID == 3 + #define USARTn USART3 + #define UARTn_CLK_ENABLE __HAL_RCC_USART3_CLK_ENABLE + #endif +#endif + //--------------------------------------------------------------------+ // MACRO TYPEDEF CONSTANT ENUM //--------------------------------------------------------------------+ -#ifdef UART_DEV +#ifdef UART_ID static UART_HandleTypeDef UartHandle = { - .Instance = UART_DEV, + .Instance = USARTn, .Init = { .BaudRate = CFG_BOARD_UART_BAUDRATE, .WordLength = UART_WORDLENGTH_8B, @@ -317,9 +330,10 @@ void board_init(void) { -#ifdef UART_DEV - UART_CLK_EN(); +#ifdef UART_ID + UARTn_CLK_ENABLE(); HAL_UART_Init(&UartHandle); + HAL_UARTEx_EnableFifoMode(&UartHandle); #endif //------------- USB FS -------------// @@ -444,19 +458,39 @@ size_t board_get_unique_id(uint8_t id[], size_t max_len) { } int board_uart_read(uint8_t *buf, int len) { - (void) buf; - (void) len; +#ifdef UART_ID + int count = 0; + while (count < len) { + if (__HAL_UART_GET_FLAG(&UartHandle, UART_FLAG_RXNE)) { + buf[count] = (uint8_t) UartHandle.Instance->RDR; + count++; + } else { + break; + } + } + return count; +#else + (void) buf; (void) len; return 0; +#endif } int board_uart_write(void const *buf, int len) { -#ifdef UART_DEV - HAL_UART_Transmit(&UartHandle, (uint8_t * )(uintptr_t) - buf, len, 0xffff); - return len; +#ifdef UART_ID + const uint8_t *p = (const uint8_t *) buf; + int count = 0; + while (count < len) { + if (__HAL_UART_GET_FLAG(&UartHandle, UART_FLAG_TXE)) { + UartHandle.Instance->TDR = p[count]; + count++; + } else { + break; + } + } + return count; #else (void) buf; (void) len; - return -1; + return 0; #endif } diff --git a/hw/bsp/stm32l0/boards/stm32l052dap52/board.h b/hw/bsp/stm32l0/boards/stm32l052dap52/board.h index 50bbafadb..162c62abb 100644 --- a/hw/bsp/stm32l0/boards/stm32l052dap52/board.h +++ b/hw/bsp/stm32l0/boards/stm32l052dap52/board.h @@ -47,8 +47,7 @@ #define BUTTON_STATE_ACTIVE 0 // UART -#define UART_DEV USART2 -#define UART_CLK_EN __HAL_RCC_USART2_CLK_ENABLE +#define UART_ID 2 #define UART_GPIO_PORT GPIOA #define UART_GPIO_AF GPIO_AF4_USART2 #define UART_TX_PIN GPIO_PIN_2 diff --git a/hw/bsp/stm32l0/family.c b/hw/bsp/stm32l0/family.c index 7fd076dbd..930fa2d66 100644 --- a/hw/bsp/stm32l0/family.c +++ b/hw/bsp/stm32l0/family.c @@ -32,6 +32,16 @@ #include "bsp/board_api.h" #include "board.h" +#ifdef UART_ID + #if UART_ID == 2 + #define USARTn USART2 + #define UARTn_CLK_ENABLE __HAL_RCC_USART2_CLK_ENABLE + #elif UART_ID == 4 + #define USARTn USART4 + #define UARTn_CLK_ENABLE __HAL_RCC_USART4_CLK_ENABLE + #endif +#endif + //--------------------------------------------------------------------+ // Forward USB interrupt events to TinyUSB IRQ Handler //--------------------------------------------------------------------+ @@ -42,7 +52,7 @@ void USB_IRQHandler(void) { //--------------------------------------------------------------------+ // MACRO TYPEDEF CONSTANT ENUM //--------------------------------------------------------------------+ -#ifdef UART_DEV +#ifdef UART_ID UART_HandleTypeDef UartHandle; #endif @@ -84,9 +94,9 @@ void board_init(void) { GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; HAL_GPIO_Init(BUTTON_PORT, &GPIO_InitStruct); -#ifdef UART_DEV +#ifdef UART_ID // Enable UART Clock - UART_CLK_EN(); + UARTn_CLK_ENABLE(); GPIO_InitStruct.Pin = UART_TX_PIN | UART_RX_PIN; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; @@ -95,7 +105,7 @@ void board_init(void) { GPIO_InitStruct.Alternate = UART_GPIO_AF; HAL_GPIO_Init(UART_GPIO_PORT, &GPIO_InitStruct); - UartHandle.Instance = UART_DEV; + UartHandle.Instance = USARTn; UartHandle.Init.BaudRate = CFG_BOARD_UART_BAUDRATE; UartHandle.Init.WordLength = UART_WORDLENGTH_8B; UartHandle.Init.StopBits = UART_STOPBITS_1; @@ -151,9 +161,18 @@ int board_uart_read(uint8_t* buf, int len) { } int board_uart_write(void const* buf, int len) { -#ifdef UART_DEV - HAL_UART_Transmit(&UartHandle, (uint8_t*)(uintptr_t) buf, len, 0xffff); - return len; +#ifdef UART_ID + const uint8_t *p = (const uint8_t *) buf; + int count = 0; + while (count < len) { + if (UartHandle.Instance->ISR & USART_ISR_TXE) { + UartHandle.Instance->TDR = p[count]; + count++; + } else { + break; + } + } + return count; #else (void) buf; (void) len; diff --git a/hw/bsp/stm32l4/boards/stm32l412nucleo/board.h b/hw/bsp/stm32l4/boards/stm32l412nucleo/board.h index 980e1e321..a5250eda9 100644 --- a/hw/bsp/stm32l4/boards/stm32l412nucleo/board.h +++ b/hw/bsp/stm32l4/boards/stm32l412nucleo/board.h @@ -45,8 +45,7 @@ #define BUTTON_PIN GPIO_PIN_0 #define BUTTON_STATE_ACTIVE 1 -#define UART_DEV LPUART1 -#define UART_CLK_EN __HAL_RCC_LPUART1_CLK_ENABLE +#define UART_ID 11 #define UART_GPIO_PORT GPIOA #define UART_GPIO_AF GPIO_AF8_LPUART1 #define UART_TX_PIN GPIO_PIN_2 diff --git a/hw/bsp/stm32l4/boards/stm32l476disco/board.h b/hw/bsp/stm32l4/boards/stm32l476disco/board.h index cf84d3e66..eb0c67915 100644 --- a/hw/bsp/stm32l4/boards/stm32l476disco/board.h +++ b/hw/bsp/stm32l4/boards/stm32l476disco/board.h @@ -44,8 +44,7 @@ #define BUTTON_PIN GPIO_PIN_0 #define BUTTON_STATE_ACTIVE 1 -#define UART_DEV USART2 -#define UART_CLK_EN __HAL_RCC_USART2_CLK_ENABLE +#define UART_ID 2 #define UART_GPIO_PORT GPIOD #define UART_GPIO_AF GPIO_AF7_USART2 #define UART_TX_PIN GPIO_PIN_5 diff --git a/hw/bsp/stm32l4/boards/stm32l496nucleo/board.h b/hw/bsp/stm32l4/boards/stm32l496nucleo/board.h index 3b031e00f..d22cc969c 100644 --- a/hw/bsp/stm32l4/boards/stm32l496nucleo/board.h +++ b/hw/bsp/stm32l4/boards/stm32l496nucleo/board.h @@ -45,8 +45,7 @@ #define BUTTON_PIN GPIO_PIN_13 #define BUTTON_STATE_ACTIVE 1 -#define UART_DEV LPUART1 -#define UART_CLK_EN __HAL_RCC_LPUART1_CLK_ENABLE +#define UART_ID 11 #define UART_GPIO_PORT GPIOG #define UART_GPIO_AF GPIO_AF8_LPUART1 #define UART_TX_PIN GPIO_PIN_7 diff --git a/hw/bsp/stm32l4/boards/stm32l4p5nucleo/board.h b/hw/bsp/stm32l4/boards/stm32l4p5nucleo/board.h index 94978638e..b0823b08b 100644 --- a/hw/bsp/stm32l4/boards/stm32l4p5nucleo/board.h +++ b/hw/bsp/stm32l4/boards/stm32l4p5nucleo/board.h @@ -44,8 +44,7 @@ #define BUTTON_PIN GPIO_PIN_13 #define BUTTON_STATE_ACTIVE 1 -#define UART_DEV LPUART1 -#define UART_CLK_EN __HAL_RCC_LPUART1_CLK_ENABLE +#define UART_ID 11 #define UART_GPIO_PORT GPIOG #define UART_GPIO_AF GPIO_AF8_LPUART1 #define UART_TX_PIN GPIO_PIN_7 diff --git a/hw/bsp/stm32l4/boards/stm32l4r5nucleo/board.h b/hw/bsp/stm32l4/boards/stm32l4r5nucleo/board.h index f603ae855..306ac8c4d 100644 --- a/hw/bsp/stm32l4/boards/stm32l4r5nucleo/board.h +++ b/hw/bsp/stm32l4/boards/stm32l4r5nucleo/board.h @@ -44,8 +44,7 @@ #define BUTTON_PIN GPIO_PIN_13 #define BUTTON_STATE_ACTIVE 1 -#define UART_DEV LPUART1 -#define UART_CLK_EN __HAL_RCC_LPUART1_CLK_ENABLE +#define UART_ID 11 #define UART_GPIO_PORT GPIOG #define UART_GPIO_AF GPIO_AF8_LPUART1 #define UART_TX_PIN GPIO_PIN_7 diff --git a/hw/bsp/stm32l4/family.c b/hw/bsp/stm32l4/family.c index c87b643b8..7a8acb3de 100644 --- a/hw/bsp/stm32l4/family.c +++ b/hw/bsp/stm32l4/family.c @@ -34,6 +34,16 @@ #include "bsp/board_api.h" #include "board.h" +#ifdef UART_ID + #if UART_ID == 2 + #define USARTn USART2 + #define UARTn_CLK_ENABLE __HAL_RCC_USART2_CLK_ENABLE + #elif UART_ID == 11 + #define USARTn LPUART1 + #define UARTn_CLK_ENABLE __HAL_RCC_LPUART1_CLK_ENABLE + #endif +#endif + //--------------------------------------------------------------------+ // Forward USB interrupt events to TinyUSB IRQ Handler //--------------------------------------------------------------------+ @@ -50,7 +60,9 @@ void USB_IRQHandler(void) // MACRO TYPEDEF CONSTANT ENUM //--------------------------------------------------------------------+ +#ifdef UART_ID UART_HandleTypeDef UartHandle; +#endif void board_init(void) { board_clock_init(); @@ -72,7 +84,9 @@ void board_init(void) { __HAL_RCC_GPIOG_CLK_ENABLE(); #endif __HAL_RCC_GPIOH_CLK_ENABLE(); - UART_CLK_EN(); +#ifdef UART_ID + UARTn_CLK_ENABLE(); +#endif #if CFG_TUSB_OS == OPT_OS_NONE // 1ms tick timer @@ -121,6 +135,7 @@ void board_init(void) { HAL_PWREx_EnableVddIO2(); #endif +#ifdef UART_ID // Uart GPIO_InitStruct.Pin = UART_TX_PIN | UART_RX_PIN; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; @@ -128,7 +143,7 @@ void board_init(void) { GPIO_InitStruct.Alternate = UART_GPIO_AF; HAL_GPIO_Init(UART_GPIO_PORT, &GPIO_InitStruct); - UartHandle.Instance = UART_DEV; + UartHandle.Instance = USARTn; UartHandle.Init.BaudRate = CFG_BOARD_UART_BAUDRATE; UartHandle.Init.WordLength = UART_WORDLENGTH_8B; UartHandle.Init.StopBits = UART_STOPBITS_1; @@ -141,6 +156,10 @@ void board_init(void) { UartHandle.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT; HAL_UART_Init(&UartHandle); + #if defined(USART_CR1_FIFOEN) + HAL_UARTEx_EnableFifoMode(&UartHandle); + #endif +#endif /* Configure USB FS GPIOs */ /* Configure DM DP Pins */ @@ -213,14 +232,40 @@ size_t board_get_unique_id(uint8_t id[], size_t max_len) { } int board_uart_read(uint8_t *buf, int len) { - (void) buf; - (void) len; +#ifdef UART_ID + int count = 0; + while (count < len) { + if (__HAL_UART_GET_FLAG(&UartHandle, UART_FLAG_RXNE)) { + buf[count] = (uint8_t) UartHandle.Instance->RDR; + count++; + } else { + break; + } + } + return count; +#else + (void) buf; (void) len; return 0; +#endif } int board_uart_write(void const *buf, int len) { - HAL_UART_Transmit(&UartHandle, (uint8_t *) (uintptr_t) buf, len, 0xffff); - return len; +#ifdef UART_ID + const uint8_t *p = (const uint8_t *) buf; + int count = 0; + while (count < len) { + if (__HAL_UART_GET_FLAG(&UartHandle, UART_FLAG_TXE)) { + UartHandle.Instance->TDR = p[count]; + count++; + } else { + break; + } + } + return count; +#else + (void) buf; (void) len; + return 0; +#endif } #if CFG_TUSB_OS == OPT_OS_NONE diff --git a/hw/bsp/stm32n6/boards/stm32n6570dk/board.h b/hw/bsp/stm32n6/boards/stm32n6570dk/board.h index 8c2ec66dc..4d162bbca 100644 --- a/hw/bsp/stm32n6/boards/stm32n6570dk/board.h +++ b/hw/bsp/stm32n6/boards/stm32n6570dk/board.h @@ -41,8 +41,7 @@ extern "C" { #include "stm32n6xx_ll_system.h" #include "tcpp0203.h" -#define UART_DEV USART1 -#define UART_CLK_EN __HAL_RCC_USART1_CLK_ENABLE +#define UART_ID 1 // VBUS Sense detection #define OTG_FS_VBUS_SENSE 0 diff --git a/hw/bsp/stm32n6/boards/stm32n657nucleo/board.h b/hw/bsp/stm32n6/boards/stm32n657nucleo/board.h index 5bdbaff3c..c26367af6 100644 --- a/hw/bsp/stm32n6/boards/stm32n657nucleo/board.h +++ b/hw/bsp/stm32n6/boards/stm32n657nucleo/board.h @@ -41,8 +41,7 @@ extern "C" { #include "stm32n6xx_ll_system.h" #include "tcpp0203.h" -#define UART_DEV USART1 -#define UART_CLK_EN __HAL_RCC_USART1_CLK_ENABLE +#define UART_ID 1 // VBUS Sense detection #define OTG_FS_VBUS_SENSE 0 diff --git a/hw/bsp/stm32n6/family.c b/hw/bsp/stm32n6/family.c index 4354616c3..95578af04 100644 --- a/hw/bsp/stm32n6/family.c +++ b/hw/bsp/stm32n6/family.c @@ -58,13 +58,26 @@ typedef struct { #include "board.h" +#ifdef UART_ID + #if UART_ID == 1 + #define USARTn USART1 + #define UARTn_CLK_ENABLE __HAL_RCC_USART1_CLK_ENABLE + #elif UART_ID == 2 + #define USARTn USART2 + #define UARTn_CLK_ENABLE __HAL_RCC_USART2_CLK_ENABLE + #elif UART_ID == 3 + #define USARTn USART3 + #define UARTn_CLK_ENABLE __HAL_RCC_USART3_CLK_ENABLE + #endif +#endif + //--------------------------------------------------------------------+ // MACRO TYPEDEF CONSTANT ENUM //--------------------------------------------------------------------+ -#ifdef UART_DEV +#ifdef UART_ID static UART_HandleTypeDef UartHandle = { - .Instance = UART_DEV, + .Instance = USARTn, .Init = { .BaudRate = CFG_BOARD_UART_BAUDRATE, .WordLength = UART_WORDLENGTH_8B, @@ -154,9 +167,10 @@ void board_init(void) { -#ifdef UART_DEV - UART_CLK_EN(); +#ifdef UART_ID + UARTn_CLK_ENABLE(); HAL_UART_Init(&UartHandle); + HAL_UARTEx_EnableFifoMode(&UartHandle); #endif #if (CFG_TUD_ENABLED && BOARD_TUD_RHPORT == 0) || (CFG_TUH_ENABLED && BOARD_TUH_RHPORT == 0) @@ -342,19 +356,39 @@ size_t board_get_unique_id(uint8_t id[], size_t max_len) { } int board_uart_read(uint8_t *buf, int len) { - (void) buf; - (void) len; +#ifdef UART_ID + int count = 0; + while (count < len) { + if (__HAL_UART_GET_FLAG(&UartHandle, UART_FLAG_RXNE)) { + buf[count] = (uint8_t) UartHandle.Instance->RDR; + count++; + } else { + break; + } + } + return count; +#else + (void) buf; (void) len; return 0; +#endif } int board_uart_write(void const *buf, int len) { -#ifdef UART_DEV - HAL_UART_Transmit(&UartHandle, (uint8_t * )(uintptr_t) - buf, len, 0xffff); - return len; +#ifdef UART_ID + const uint8_t *p = (const uint8_t *) buf; + int count = 0; + while (count < len) { + if (__HAL_UART_GET_FLAG(&UartHandle, UART_FLAG_TXE)) { + UartHandle.Instance->TDR = p[count]; + count++; + } else { + break; + } + } + return count; #else (void) buf; (void) len; - return -1; + return 0; #endif } diff --git a/hw/bsp/stm32u0/boards/stm32u083cdk/board.h b/hw/bsp/stm32u0/boards/stm32u083cdk/board.h index 278d9b695..28f9fa285 100644 --- a/hw/bsp/stm32u0/boards/stm32u083cdk/board.h +++ b/hw/bsp/stm32u0/boards/stm32u083cdk/board.h @@ -47,8 +47,7 @@ #define BUTTON_STATE_ACTIVE 0 // Active low (pressed = 0) // UART - using USART2 on PA2/PA3 (VCP TX/RX from CubeMX) -#define UART_DEV USART2 -#define UART_CLK_EN __HAL_RCC_USART2_CLK_ENABLE +#define UART_ID 2 #define UART_GPIO_PORT GPIOA #define UART_GPIO_AF GPIO_AF7_USART2 #define UART_TX_PIN GPIO_PIN_2 diff --git a/hw/bsp/stm32u0/boards/stm32u083nucleo/board.h b/hw/bsp/stm32u0/boards/stm32u083nucleo/board.h index 23fddbef9..e2177cbc8 100644 --- a/hw/bsp/stm32u0/boards/stm32u083nucleo/board.h +++ b/hw/bsp/stm32u0/boards/stm32u083nucleo/board.h @@ -47,8 +47,7 @@ #define BUTTON_STATE_ACTIVE 0 // UART: USART2 on PA2/PA3 (VCP via ST-Link) -#define UART_DEV USART2 -#define UART_CLK_EN __HAL_RCC_USART2_CLK_ENABLE +#define UART_ID 2 #define UART_GPIO_PORT GPIOA #define UART_GPIO_AF GPIO_AF7_USART2 #define UART_TX_PIN GPIO_PIN_2 diff --git a/hw/bsp/stm32u0/family.c b/hw/bsp/stm32u0/family.c index af39ae398..5cf6e1eb2 100644 --- a/hw/bsp/stm32u0/family.c +++ b/hw/bsp/stm32u0/family.c @@ -35,6 +35,19 @@ TU_ATTR_UNUSED static void Error_Handler(void) { } #include "board.h" +#ifdef UART_ID + #if UART_ID == 1 + #define USARTn USART1 + #define UARTn_CLK_ENABLE __HAL_RCC_USART1_CLK_ENABLE + #elif UART_ID == 2 + #define USARTn USART2 + #define UARTn_CLK_ENABLE __HAL_RCC_USART2_CLK_ENABLE + #elif UART_ID == 3 + #define USARTn USART3 + #define UARTn_CLK_ENABLE __HAL_RCC_USART3_CLK_ENABLE + #endif +#endif + //--------------------------------------------------------------------+ // Forward USB interrupt events to TinyUSB IRQ Handler //--------------------------------------------------------------------+ @@ -45,7 +58,7 @@ void USB_DRD_FS_IRQHandler(void) { //--------------------------------------------------------------------+ // MACRO TYPEDEF CONSTANT ENUM //--------------------------------------------------------------------+ -#ifdef UART_DEV +#ifdef UART_ID UART_HandleTypeDef UartHandle; #endif @@ -96,7 +109,7 @@ void board_init(void) { GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; HAL_GPIO_Init(BUTTON_PORT, &GPIO_InitStruct); -#ifdef UART_DEV +#ifdef UART_ID // UART GPIO_InitStruct.Pin = UART_TX_PIN | UART_RX_PIN; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; @@ -105,8 +118,8 @@ void board_init(void) { GPIO_InitStruct.Alternate = UART_GPIO_AF; HAL_GPIO_Init(UART_GPIO_PORT, &GPIO_InitStruct); - UART_CLK_EN(); - UartHandle.Instance = UART_DEV; + UARTn_CLK_ENABLE(); + UartHandle.Instance = USARTn; UartHandle.Init.BaudRate = CFG_BOARD_UART_BAUDRATE; UartHandle.Init.WordLength = UART_WORDLENGTH_8B; UartHandle.Init.StopBits = UART_STOPBITS_1; @@ -115,6 +128,7 @@ void board_init(void) { UartHandle.Init.Mode = UART_MODE_TX_RX; UartHandle.Init.OverSampling = UART_OVERSAMPLING_16; HAL_UART_Init(&UartHandle); + HAL_UARTEx_EnableFifoMode(&UartHandle); #endif #if CFG_TUSB_OS == OPT_OS_FREERTOS @@ -153,18 +167,36 @@ uint32_t board_button_read(void) { } int board_uart_read(uint8_t* buf, int len) { -#ifdef UART_DEV - (void) buf; (void) len; - return 0; +#ifdef UART_ID + int count = 0; + while (count < len) { + if (__HAL_UART_GET_FLAG(&UartHandle, UART_FLAG_RXNE)) { + buf[count] = (uint8_t) UartHandle.Instance->RDR; + count++; + } else { + break; + } + } + return count; #else + (void) buf; (void) len; return 0; #endif } -int board_uart_write(void const * buf, int len) { -#ifdef UART_DEV - HAL_UART_Transmit(&UartHandle, (uint8_t*)(uintptr_t) buf, len, 0xffff); - return len; +int board_uart_write(void const *buf, int len) { +#ifdef UART_ID + const uint8_t *p = (const uint8_t *) buf; + int count = 0; + while (count < len) { + if (__HAL_UART_GET_FLAG(&UartHandle, UART_FLAG_TXE)) { + UartHandle.Instance->TDR = p[count]; + count++; + } else { + break; + } + } + return count; #else (void) buf; (void) len; return 0; diff --git a/hw/bsp/stm32u0/family.mk b/hw/bsp/stm32u0/family.mk index 241323f62..9119f3652 100644 --- a/hw/bsp/stm32u0/family.mk +++ b/hw/bsp/stm32u0/family.mk @@ -35,7 +35,8 @@ SRC_C += \ $(ST_HAL_DRIVER)/Src/stm32$(ST_FAMILY)xx_hal_rcc_ex.c \ $(ST_HAL_DRIVER)/Src/stm32$(ST_FAMILY)xx_hal_gpio.c \ ${ST_HAL_DRIVER}/Src/stm32$(ST_FAMILY)xx_hal_pwr_ex.c \ - $(ST_HAL_DRIVER)/Src/stm32$(ST_FAMILY)xx_hal_uart.c + $(ST_HAL_DRIVER)/Src/stm32$(ST_FAMILY)xx_hal_uart.c \ + $(ST_HAL_DRIVER)/Src/stm32$(ST_FAMILY)xx_hal_uart_ex.c INC += \ $(TOP)/$(BOARD_PATH) \ diff --git a/hw/bsp/stm32u5/boards/b_u585i_iot2a/board.h b/hw/bsp/stm32u5/boards/b_u585i_iot2a/board.h index c99743738..f775f3669 100644 --- a/hw/bsp/stm32u5/boards/b_u585i_iot2a/board.h +++ b/hw/bsp/stm32u5/boards/b_u585i_iot2a/board.h @@ -48,8 +48,7 @@ extern "C" #define BUTTON_STATE_ACTIVE 1 // UART Enable for STLink VCOM -#define UART_DEV USART1 -#define UART_CLK_EN __HAL_RCC_USART1_CLK_ENABLE +#define UART_ID 1 #define UART_GPIO_PORT GPIOA #define UART_GPIO_AF GPIO_AF7_USART1 #define UART_TX_PIN GPIO_PIN_9 diff --git a/hw/bsp/stm32u5/boards/stm32u545nucleo/board.h b/hw/bsp/stm32u5/boards/stm32u545nucleo/board.h index eb2b63721..80e2e4b21 100644 --- a/hw/bsp/stm32u5/boards/stm32u545nucleo/board.h +++ b/hw/bsp/stm32u5/boards/stm32u545nucleo/board.h @@ -48,8 +48,7 @@ extern "C" #define BUTTON_STATE_ACTIVE 1 // UART Enable for STLink VCOM -#define UART_DEV LPUART1 -#define UART_CLK_EN __HAL_RCC_LPUART1_CLK_ENABLE +#define UART_ID 11 #define UART_GPIO_PORT GPIOA #define UART_GPIO_AF GPIO_AF8_LPUART1 #define UART_TX_PIN GPIO_PIN_2 diff --git a/hw/bsp/stm32u5/boards/stm32u575eval/board.h b/hw/bsp/stm32u5/boards/stm32u575eval/board.h index cce3e38b3..ec941a6c5 100644 --- a/hw/bsp/stm32u5/boards/stm32u575eval/board.h +++ b/hw/bsp/stm32u5/boards/stm32u575eval/board.h @@ -49,8 +49,7 @@ extern "C" #define BUTTON_STATE_ACTIVE 1 // UART Enable for STLink VCOM -#define UART_DEV USART1 -#define UART_CLK_EN __HAL_RCC_USART1_CLK_ENABLE +#define UART_ID 1 #define UART_GPIO_PORT GPIOA #define UART_GPIO_AF GPIO_AF7_USART1 #define UART_TX_PIN GPIO_PIN_9 diff --git a/hw/bsp/stm32u5/boards/stm32u575nucleo/board.h b/hw/bsp/stm32u5/boards/stm32u575nucleo/board.h index b6b60f021..6f2f7596b 100644 --- a/hw/bsp/stm32u5/boards/stm32u575nucleo/board.h +++ b/hw/bsp/stm32u5/boards/stm32u575nucleo/board.h @@ -50,8 +50,7 @@ extern "C" #define BUTTON_STATE_ACTIVE 1 // UART Enable for STLink VCOM -#define UART_DEV LPUART1 -#define UART_CLK_EN __HAL_RCC_LPUART1_CLK_ENABLE +#define UART_ID 11 #define UART_GPIO_PORT GPIOG #define UART_GPIO_AF GPIO_AF8_LPUART1 #define UART_TX_PIN GPIO_PIN_7 diff --git a/hw/bsp/stm32u5/boards/stm32u5a5nucleo/board.h b/hw/bsp/stm32u5/boards/stm32u5a5nucleo/board.h index 15106aee6..48a9ae9fb 100644 --- a/hw/bsp/stm32u5/boards/stm32u5a5nucleo/board.h +++ b/hw/bsp/stm32u5/boards/stm32u5a5nucleo/board.h @@ -50,8 +50,7 @@ extern "C" #define BUTTON_STATE_ACTIVE 1 // UART Enable for STLink VCOM -#define UART_DEV USART1 -#define UART_CLK_EN __HAL_RCC_USART1_CLK_ENABLE +#define UART_ID 1 #define UART_GPIO_PORT GPIOA #define UART_GPIO_AF GPIO_AF7_USART1 #define UART_TX_PIN GPIO_PIN_9 diff --git a/hw/bsp/stm32u5/family.c b/hw/bsp/stm32u5/family.c index 55ca25d58..41e354351 100644 --- a/hw/bsp/stm32u5/family.c +++ b/hw/bsp/stm32u5/family.c @@ -48,6 +48,22 @@ TU_ATTR_UNUSED static void Error_Handler(void) { #include "board.h" +#ifdef UART_ID + #if UART_ID == 1 + #define USARTn USART1 + #define UARTn_CLK_ENABLE __HAL_RCC_USART1_CLK_ENABLE + #elif UART_ID == 2 + #define USARTn USART2 + #define UARTn_CLK_ENABLE __HAL_RCC_USART2_CLK_ENABLE + #elif UART_ID == 3 + #define USARTn USART3 + #define UARTn_CLK_ENABLE __HAL_RCC_USART3_CLK_ENABLE + #elif UART_ID == 11 + #define USARTn LPUART1 + #define UARTn_CLK_ENABLE __HAL_RCC_LPUART1_CLK_ENABLE + #endif +#endif + //--------------------------------------------------------------------+ // Forward USB interrupt events to TinyUSB IRQ Handler //--------------------------------------------------------------------+ @@ -70,7 +86,9 @@ void OTG_HS_IRQHandler(void) { // MACRO TYPEDEF CONSTANT ENUM //--------------------------------------------------------------------+ +#ifdef UART_ID UART_HandleTypeDef UartHandle; +#endif void board_init(void) { // Init clock, implemented in board.h @@ -89,8 +107,6 @@ void board_init(void) { __HAL_RCC_GPIOG_CLK_ENABLE(); __HAL_RCC_GPIOH_CLK_ENABLE(); - UART_CLK_EN(); - /* Enable Instruction cache */ HAL_ICACHE_Enable(); @@ -119,6 +135,7 @@ void board_init(void) { // IOSV bit MUST be set to access GPIO port G[2:15] */ HAL_PWREx_EnableVddIO2(); +#ifdef UART_ID // Uart GPIO_InitStruct.Pin = UART_TX_PIN | UART_RX_PIN; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; @@ -127,7 +144,8 @@ void board_init(void) { GPIO_InitStruct.Alternate = UART_GPIO_AF; HAL_GPIO_Init(UART_GPIO_PORT, &GPIO_InitStruct); - UartHandle.Instance = UART_DEV; + UARTn_CLK_ENABLE(); + UartHandle.Instance = USARTn; UartHandle.Init.BaudRate = CFG_BOARD_UART_BAUDRATE; UartHandle.Init.WordLength = UART_WORDLENGTH_8B; UartHandle.Init.StopBits = UART_STOPBITS_1; @@ -139,6 +157,8 @@ void board_init(void) { UartHandle.Init.ClockPrescaler = UART_PRESCALER_DIV1; UartHandle.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT; HAL_UART_Init(&UartHandle); + HAL_UARTEx_EnableFifoMode(&UartHandle); +#endif /* Configure USB GPIOs */ /* Configure DM DP Pins */ @@ -252,14 +272,40 @@ size_t board_get_unique_id(uint8_t id[], size_t max_len) { } int board_uart_read(uint8_t *buf, int len) { - (void) buf; - (void) len; +#ifdef UART_ID + int count = 0; + while (count < len) { + if (__HAL_UART_GET_FLAG(&UartHandle, UART_FLAG_RXNE)) { + buf[count] = (uint8_t) UartHandle.Instance->RDR; + count++; + } else { + break; + } + } + return count; +#else + (void) buf; (void) len; return 0; +#endif } int board_uart_write(void const *buf, int len) { - HAL_UART_Transmit(&UartHandle, (uint8_t *) (uintptr_t) buf, len, 0xffff); - return len; +#ifdef UART_ID + const uint8_t *p = (const uint8_t *) buf; + int count = 0; + while (count < len) { + if (__HAL_UART_GET_FLAG(&UartHandle, UART_FLAG_TXE)) { + UartHandle.Instance->TDR = p[count]; + count++; + } else { + break; + } + } + return count; +#else + (void) buf; (void) len; + return 0; +#endif } #if CFG_TUSB_OS == OPT_OS_NONE diff --git a/hw/bsp/stm32u5/family.mk b/hw/bsp/stm32u5/family.mk index 3dab8c610..90796836b 100644 --- a/hw/bsp/stm32u5/family.mk +++ b/hw/bsp/stm32u5/family.mk @@ -37,6 +37,7 @@ SRC_C += \ $(ST_HAL_DRIVER)/Src/stm32$(ST_FAMILY)xx_hal_rcc.c \ $(ST_HAL_DRIVER)/Src/stm32$(ST_FAMILY)xx_hal_rcc_ex.c \ $(ST_HAL_DRIVER)/Src/stm32$(ST_FAMILY)xx_hal_uart.c \ + $(ST_HAL_DRIVER)/Src/stm32$(ST_FAMILY)xx_hal_uart_ex.c \ $(ST_HAL_DRIVER)/Src/stm32$(ST_FAMILY)xx_hal_adc.c \ $(ST_HAL_DRIVER)/Src/stm32$(ST_FAMILY)xx_hal_adc_ex.c \ $(ST_HAL_DRIVER)/Src/stm32$(ST_FAMILY)xx_hal_tim.c diff --git a/hw/bsp/stm32wb/boards/stm32wb55nucleo/board.h b/hw/bsp/stm32wb/boards/stm32wb55nucleo/board.h index 704592506..3829828e8 100644 --- a/hw/bsp/stm32wb/boards/stm32wb55nucleo/board.h +++ b/hw/bsp/stm32wb/boards/stm32wb55nucleo/board.h @@ -47,8 +47,7 @@ #define BUTTON_STATE_ACTIVE 0 // UART Enable for STLink VCOM -#define UART_DEV USART1 -#define UART_CLK_EN __HAL_RCC_USART1_CLK_ENABLE +#define UART_ID 1 #define UART_GPIO_PORT GPIOB #define UART_GPIO_AF GPIO_AF7_USART1 #define UART_TX_PIN GPIO_PIN_6 diff --git a/hw/bsp/stm32wb/family.c b/hw/bsp/stm32wb/family.c index de503c072..d97be2115 100644 --- a/hw/bsp/stm32wb/family.c +++ b/hw/bsp/stm32wb/family.c @@ -32,6 +32,18 @@ #include "bsp/board_api.h" #include "board.h" +#ifdef UART_ID + #if UART_ID == 1 + #define USARTn USART1 + #define UARTn_CLK_ENABLE __HAL_RCC_USART1_CLK_ENABLE + #elif UART_ID == 2 + #define USARTn USART2 + #define UARTn_CLK_ENABLE __HAL_RCC_USART2_CLK_ENABLE + #else + #error "UART_ID not supported" + #endif +#endif + //--------------------------------------------------------------------+ // Forward USB interrupt events to TinyUSB IRQ Handler //--------------------------------------------------------------------+ @@ -46,7 +58,7 @@ void USB_LP_IRQHandler(void) { //--------------------------------------------------------------------+ // MACRO TYPEDEF CONSTANT ENUM //--------------------------------------------------------------------+ -#ifdef UART_DEV +#ifdef UART_ID UART_HandleTypeDef UartHandle; #endif @@ -101,8 +113,8 @@ void board_init(void) { GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; HAL_GPIO_Init(BUTTON_PORT, &GPIO_InitStruct); -#ifdef UART_DEV - UART_CLK_EN(); +#ifdef UART_ID + UARTn_CLK_ENABLE(); // UART GPIO_InitStruct.Pin = UART_TX_PIN | UART_RX_PIN; @@ -113,7 +125,7 @@ void board_init(void) { HAL_GPIO_Init(UART_GPIO_PORT, &GPIO_InitStruct); UartHandle = (UART_HandleTypeDef) { - .Instance = UART_DEV, + .Instance = USARTn, .Init.BaudRate = CFG_BOARD_UART_BAUDRATE, .Init.WordLength = UART_WORDLENGTH_8B, .Init.StopBits = UART_STOPBITS_1, @@ -123,6 +135,7 @@ void board_init(void) { .Init.OverSampling = UART_OVERSAMPLING_16 }; HAL_UART_Init(&UartHandle); + HAL_UARTEx_EnableFifoMode(&UartHandle); #endif // USB Pins TODO double check USB clock and pin setup @@ -151,15 +164,36 @@ uint32_t board_button_read(void) { } int board_uart_read(uint8_t* buf, int len) { - (void) buf; - (void) len; +#ifdef UART_ID + int count = 0; + while (count < len) { + if (__HAL_UART_GET_FLAG(&UartHandle, UART_FLAG_RXNE)) { + buf[count] = (uint8_t) UartHandle.Instance->RDR; + count++; + } else { + break; + } + } + return count; +#else + (void) buf; (void) len; return 0; +#endif } int board_uart_write(void const* buf, int len) { -#ifdef UART_DEV - HAL_UART_Transmit(&UartHandle, (uint8_t*) (uintptr_t) buf, len, 0xffff); - return len; +#ifdef UART_ID + const uint8_t *p = (const uint8_t *) buf; + int count = 0; + while (count < len) { + if (__HAL_UART_GET_FLAG(&UartHandle, UART_FLAG_TXE)) { + UartHandle.Instance->TDR = p[count]; + count++; + } else { + break; + } + } + return count; #else (void) buf; (void) len; return 0; diff --git a/hw/bsp/stm32wb/family.mk b/hw/bsp/stm32wb/family.mk index 0b1a51cec..9397be62d 100644 --- a/hw/bsp/stm32wb/family.mk +++ b/hw/bsp/stm32wb/family.mk @@ -29,6 +29,7 @@ SRC_C += \ $(ST_HAL_DRIVER)/Src/${ST_PREFIX}_hal_rcc.c \ $(ST_HAL_DRIVER)/Src/${ST_PREFIX}_hal_rcc_ex.c \ $(ST_HAL_DRIVER)/Src/${ST_PREFIX}_hal_uart.c \ + $(ST_HAL_DRIVER)/Src/${ST_PREFIX}_hal_uart_ex.c \ $(ST_HAL_DRIVER)/Src/${ST_PREFIX}_hal_gpio.c INC += \ diff --git a/hw/bsp/stm32wba/family.c b/hw/bsp/stm32wba/family.c index 878355b48..615a71cf5 100644 --- a/hw/bsp/stm32wba/family.c +++ b/hw/bsp/stm32wba/family.c @@ -117,6 +117,7 @@ static void board_uart_configuration(void) { .Init.OverSampling = UART_OVERSAMPLING_16 }; HAL_UART_Init(&uart_handle); + HAL_UARTEx_EnableFifoMode(&uart_handle); } void board_init(void) { @@ -183,14 +184,30 @@ void board_led_write(bool state) { uint32_t board_button_read(void) { return HAL_GPIO_ReadPin(BUTTON_PORT, BUTTON_PIN) == BUTTON_STATE_ACTIVE; } int board_uart_read(uint8_t *buf, int len) { - (void) buf; - (void) len; - return 0; + int count = 0; + while (count < len) { + if (__HAL_UART_GET_FLAG(&uart_handle, UART_FLAG_RXNE)) { + buf[count] = (uint8_t) uart_handle.Instance->RDR; + count++; + } else { + break; + } + } + return count; } int board_uart_write(void const *buf, int len) { - (void) HAL_UART_Transmit(&uart_handle, (const uint8_t *) buf, len, USART_TIMEOUT_TICKS); - return len; + const uint8_t *p = (const uint8_t *) buf; + int count = 0; + while (count < len) { + if (__HAL_UART_GET_FLAG(&uart_handle, UART_FLAG_TXE)) { + uart_handle.Instance->TDR = p[count]; + count++; + } else { + break; + } + } + return count; } #if CFG_TUSB_OS == OPT_OS_NONE diff --git a/hw/bsp/stm32wba/family.cmake b/hw/bsp/stm32wba/family.cmake index 9628913cc..37f4f9c61 100644 --- a/hw/bsp/stm32wba/family.cmake +++ b/hw/bsp/stm32wba/family.cmake @@ -54,6 +54,7 @@ function(family_add_board BOARD_TARGET) ${ST_HAL_DRIVER}/Src/${ST_PREFIX}_hal_rcc.c ${ST_HAL_DRIVER}/Src/${ST_PREFIX}_hal_rcc_ex.c ${ST_HAL_DRIVER}/Src/${ST_PREFIX}_hal_uart.c + ${ST_HAL_DRIVER}/Src/${ST_PREFIX}_hal_uart_ex.c ${ST_HAL_DRIVER}/Src/${ST_PREFIX}_hal_gpio.c ${ST_HAL_DRIVER}/Src/${ST_PREFIX}_hal_pcd.c ${ST_HAL_DRIVER}/Src/${ST_PREFIX}_hal_pcd_ex.c diff --git a/hw/bsp/stm32wba/family.mk b/hw/bsp/stm32wba/family.mk index 9b319921f..0a325323b 100644 --- a/hw/bsp/stm32wba/family.mk +++ b/hw/bsp/stm32wba/family.mk @@ -32,6 +32,7 @@ SRC_C += \ $(ST_HAL_DRIVER)/Src/${ST_PREFIX}_hal_rcc.c \ $(ST_HAL_DRIVER)/Src/${ST_PREFIX}_hal_rcc_ex.c \ $(ST_HAL_DRIVER)/Src/${ST_PREFIX}_hal_uart.c \ + $(ST_HAL_DRIVER)/Src/${ST_PREFIX}_hal_uart_ex.c \ $(ST_HAL_DRIVER)/Src/${ST_PREFIX}_hal_gpio.c \ $(ST_HAL_DRIVER)/Src/${ST_PREFIX}_hal_pcd.c \ $(ST_HAL_DRIVER)/Src/${ST_PREFIX}_hal_pcd_ex.c \ diff --git a/hw/bsp/tm4c/family.c b/hw/bsp/tm4c/family.c index 503d0a8c9..6988a264e 100644 --- a/hw/bsp/tm4c/family.c +++ b/hw/bsp/tm4c/family.c @@ -189,13 +189,16 @@ size_t board_get_unique_id(uint8_t id[], size_t max_len) { int board_uart_write(void const* buf, int len) { uint8_t const* data = buf; - - for (int i = 0; i < len; i++) { - while ((UART0->FR & (1 << 5)) != 0) {} // Poll until previous data was shofted out - UART0->DR = data[i]; // Write UART0 DATA REGISTER + int count = 0; + while (count < len) { + if ((UART0->FR & (1 << 5)) == 0) { // TX FIFO not full + UART0->DR = data[count]; + count++; + } else { + break; + } } - - return len; + return count; } int board_uart_read(uint8_t* buf, int len) { diff --git a/hw/bsp/xmc4000/family.c b/hw/bsp/xmc4000/family.c index d0acd04cb..1325b784b 100644 --- a/hw/bsp/xmc4000/family.c +++ b/hw/bsp/xmc4000/family.c @@ -128,11 +128,25 @@ int board_uart_read(uint8_t* buf, int len) { int board_uart_write(void const* buf, int len) { #ifdef UART_DEV - char const* bufch = (char const*) buf; - for(int i=0;i<len;i++) { - XMC_UART_CH_Transmit(UART_DEV, bufch[i]); + uint8_t const *p = (uint8_t const *) buf; + int count = 0; + bool fifo_enabled = (UART_DEV->TBCTR & USIC_CH_TBCTR_SIZE_Msk) != 0UL; + while (count < len) { + if (fifo_enabled) { + if (XMC_USIC_CH_TXFIFO_IsFull(UART_DEV)) { + break; + } + UART_DEV->IN[0U] = p[count]; + } else { + if (XMC_USIC_CH_GetTransmitBufferStatus(UART_DEV) == XMC_USIC_CH_TBUF_STATUS_BUSY) { + break; + } + XMC_UART_CH_ClearStatusFlag(UART_DEV, (uint32_t)XMC_UART_CH_STATUS_FLAG_TRANSMIT_BUFFER_INDICATION); + UART_DEV->TBUF[0U] = p[count]; + } + count++; } - return len; + return count; #else (void) buf; (void) len; diff --git a/test/hil/hil_ci.sh b/test/hil/hil_ci.sh new file mode 100644 index 000000000..fa8bb0245 --- /dev/null +++ b/test/hil/hil_ci.sh @@ -0,0 +1,69 @@ +#!/bin/bash +# Run HIL test remotely on ci.lan +# Usage: test/hil/hil_ci.sh [-b BOARD] [-t TEST] [extra hil_test.py args...] +# Example: +# test/hil/hil_ci.sh -b stm32f723disco +# test/hil/hil_ci.sh -b stm32f723disco -t host/cdc_msc_hid -r 1 + +set -e + +REMOTE=ci.lan +REMOTE_DIR=/tmp/tinyusb-hil +SCRIPT_DIR="$(cd "$(dirname "$0")/../.." && pwd)" + +# Parse -b BOARD from arguments to know which build to copy +BOARD="" +ARGS=() +while [[ $# -gt 0 ]]; do + case "$1" in + -b) + BOARD="$2" + ARGS+=("$1" "$2") + shift 2 + ;; + *) + ARGS+=("$1") + shift + ;; + esac +done + +# Setup remote directory +echo "==> Setting up remote $REMOTE:$REMOTE_DIR" +ssh "$REMOTE" "rm -rf $REMOTE_DIR && mkdir -p $REMOTE_DIR/test/hil $REMOTE_DIR/examples" + +# Copy HIL test script and config +echo "==> Copying test scripts" +scp -q "$SCRIPT_DIR/test/hil/hil_test.py" \ + "$SCRIPT_DIR/test/hil/pymtp.py" \ + "$SCRIPT_DIR/test/hil/tinyusb.json" \ + "$REMOTE:$REMOTE_DIR/test/hil/" + +# Copy only firmware binaries (elf/bin/hex), preserving directory structure +copy_board_binaries() { + local src="$1" + local board_name + board_name=$(basename "$src") + rsync -a --include='*/' --include='*.elf' --include='*.bin' --include='*.hex' --exclude='*' \ + "$src" "$REMOTE:$REMOTE_DIR/examples/" +} + +if [ -n "$BOARD" ]; then + BUILD_DIR="$SCRIPT_DIR/examples/cmake-build-$BOARD" + if [ ! -d "$BUILD_DIR" ]; then + echo "Error: build directory not found: $BUILD_DIR" + echo "Build first with: cd examples && cmake -DBOARD=$BOARD -G Ninja -B cmake-build-$BOARD .. && cmake --build cmake-build-$BOARD" + exit 1 + fi + echo "==> Copying binaries for $BOARD" + copy_board_binaries "$BUILD_DIR" +else + echo "==> Copying all built binaries" + for dir in "$SCRIPT_DIR"/examples/cmake-build-*/; do + [ -d "$dir" ] && copy_board_binaries "$dir" + done +fi + +# Run test +echo "==> Running HIL test on $REMOTE" +ssh -t "$REMOTE" "cd $REMOTE_DIR && python3 -u test/hil/hil_test.py -B examples ${ARGS[*]} tinyusb.json" diff --git a/test/hil/hil_test.py b/test/hil/hil_test.py index cf3cff1a3..f23e2fc22 100755 --- a/test/hil/hil_test.py +++ b/test/hil/hil_test.py @@ -564,7 +564,7 @@ def test_host_cdc_msc_hid(board): ser.flush() # wait until this chunk is echoed back echo = b'' - t_end = time.monotonic() + 5.0 + t_end = time.monotonic() + 1.0 while time.monotonic() < t_end and len(echo) < chunk_size: rd = ser.read(chunk_size - len(echo)) if rd: @@ -1189,9 +1189,8 @@ def test_example(board, f1, example): print(f'Flashing {fw_name}.elf') # flash firmware. It may fail randomly, retry a few times - max_rety = 3 start_s = time.time() - for i in range(max_rety): + for i in range(max_retry): ret = globals()[f'flash_{board["flasher"]["name"].lower()}'](board, fw_name) if ret.returncode == 0: try: @@ -1202,14 +1201,14 @@ def test_example(board, f1, example): print(' OK', end='') break except Exception as e: - if i == max_rety - 1: + if i == max_retry - 1: err_count += 1 print(f'{STATUS_FAILED}: {e}') else: - print(f'\n Test failed: {e}, retry {i+2}/{max_rety}', end='') + print(f'\n Test failed: {e}, retry {i+2}/{max_retry}', end='') time.sleep(0.5) else: - print(f'\n Flash failed, retry {i+2}/{max_rety}', end='') + print(f'\n Flash failed, retry {i+2}/{max_retry}', end='') time.sleep(0.5) if ret.returncode != 0: @@ -1269,6 +1268,7 @@ def main(): global verbose global test_only global build_dir + global max_retry duration = time.time() @@ -1278,6 +1278,7 @@ def main(): parser.add_argument('-s', '--skip', action='append', default=[], help='Skip boards from test') parser.add_argument('-t', '--test-only', action='append', default=[], help='Tests to run, all if not specified') parser.add_argument('-B', '--build', default='cmake-build', help='Build folder name (default: cmake-build)') + parser.add_argument('-r', '--retry', type=int, default=3, help='Retry count for failed tests (default: 3)') parser.add_argument('-v', '--verbose', action='store_true', help='Verbose output') args = parser.parse_args() @@ -1287,6 +1288,7 @@ def main(): verbose = args.verbose test_only = args.test_only build_dir = args.build + max_retry = args.retry # if config file is not found, try to find it in the same directory as this script if not os.path.exists(config_file): diff --git a/test/hil/tinyusb.json b/test/hil/tinyusb.json index 86ac902ce..92b7b21b0 100644 --- a/test/hil/tinyusb.json +++ b/test/hil/tinyusb.json @@ -62,11 +62,15 @@ { "name": "metro_m4_express", "uid": "9995AD485337433231202020FF100A34", - "build" : { - "args": ["MAX3421_HOST=1"] + "build": { + "args": [ + "MAX3421_HOST=1" + ] }, "tests": { - "device": true, "host": false, "dual": true, + "device": true, + "host": false, + "dual": true, "dev_attached": [{"vid_pid": "067b_2303", "serial": "0", "is_cdc": true}], "comment": "pl23x" }, @@ -178,7 +182,21 @@ "uid": "560AE75E1C7152C9", "tests": { "device": false, "host": true, "dual": false, - "dev_attached": [{"vid_pid": "1a86_55d4", "serial": "52D2002694", "is_cdc": true}] + "dev_attached": [ + { + "vid_pid": "1a86_55d4", + "serial": "52D2002694", + "is_cdc": true + }, + { + "vid_pid": "0951_1603", + "serial": "820000000000000045B46338", + "is_msc": true, + "block_size": 512, + "block_count": 3987456, + "msc_inquiry": "Kingston DataTraveler 2.0 1.0" + } + ] }, "flasher": { "name": "openocd", |
