diff options
| author | hathach <[email protected]> | 2026-04-03 12:51:06 +0700 |
|---|---|---|
| committer | hathach <[email protected]> | 2026-04-03 16:33:03 +0700 |
| commit | e82df22375ac22ffddf86234a818bb7f4d66af7c (patch) | |
| tree | e25bb44569b3700bb4bbdb0a21f62bd971298c86 | |
| parent | 9368e5d40f3d8e5ac323d288f969782a962d593e (diff) | |
stm32 add uart hwfifo if supported, otherwise usb tu_fifo_t for receive uart in irq
| -rw-r--r-- | hw/bsp/ch32v20x/boards/ch32v203g_r0_1v0/board.mk | 1 | ||||
| -rw-r--r-- | hw/bsp/stm32c0/family.c | 34 | ||||
| -rw-r--r-- | hw/bsp/stm32f0/family.c | 86 | ||||
| -rw-r--r-- | hw/bsp/stm32f1/family.c | 80 | ||||
| -rw-r--r-- | hw/bsp/stm32f1/family.cmake | 1 | ||||
| -rw-r--r-- | hw/bsp/stm32f1/family.mk | 1 | ||||
| -rw-r--r-- | hw/bsp/stm32f2/family.c | 79 | ||||
| -rw-r--r-- | hw/bsp/stm32f3/family.c | 80 | ||||
| -rw-r--r-- | hw/bsp/stm32f4/family.c | 64 | ||||
| -rw-r--r-- | hw/bsp/stm32f7/family.c | 189 | ||||
| -rw-r--r-- | hw/bsp/stm32g0/family.c | 33 | ||||
| -rw-r--r-- | hw/bsp/stm32g4/family.c | 33 | ||||
| -rw-r--r-- | hw/bsp/stm32h5/family.c | 39 | ||||
| -rw-r--r-- | hw/bsp/stm32h7/family.c | 17 | ||||
| -rw-r--r-- | hw/bsp/stm32h7rs/family.c | 33 | ||||
| -rw-r--r-- | hw/bsp/stm32l4/family.c | 37 | ||||
| -rw-r--r-- | hw/bsp/stm32n6/family.c | 33 | ||||
| -rw-r--r-- | hw/bsp/stm32u0/family.c | 29 | ||||
| -rw-r--r-- | hw/bsp/stm32u0/family.mk | 3 | ||||
| -rw-r--r-- | hw/bsp/stm32u5/family.c | 35 | ||||
| -rw-r--r-- | hw/bsp/stm32u5/family.mk | 1 | ||||
| -rw-r--r-- | hw/bsp/stm32wb/family.c | 30 | ||||
| -rw-r--r-- | hw/bsp/stm32wb/family.mk | 1 | ||||
| -rw-r--r-- | hw/bsp/stm32wba/family.c | 27 | ||||
| -rw-r--r-- | hw/bsp/stm32wba/family.cmake | 1 | ||||
| -rw-r--r-- | hw/bsp/stm32wba/family.mk | 1 |
26 files changed, 775 insertions, 193 deletions
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/stm32c0/family.c b/hw/bsp/stm32c0/family.c index e20c0ee15..d99720a64 100644 --- a/hw/bsp/stm32c0/family.c +++ b/hw/bsp/stm32c0/family.c @@ -118,6 +118,7 @@ void board_init(void) { .AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT }; HAL_UART_Init(&UartHandle); + HAL_UARTEx_EnableFifoMode(&UartHandle); #endif } @@ -148,19 +149,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_DEV + 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; + 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/family.c b/hw/bsp/stm32f0/family.c index 5a35a3e50..a6ed8629f 100644 --- a/hw/bsp/stm32f0/family.c +++ b/hw/bsp/stm32f0/family.c @@ -30,6 +30,7 @@ #include "stm32f0xx_hal.h" #include "bsp/board_api.h" +#include "common/tusb_fifo.h" #include "board.h" //--------------------------------------------------------------------+ @@ -42,7 +43,43 @@ void USB_IRQHandler(void) { //--------------------------------------------------------------------+ // MACRO TYPEDEF CONSTANT ENUM //--------------------------------------------------------------------+ -UART_HandleTypeDef UartHandle; +#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, + } +}; + +// 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 +static void uart_rx_isr(void) { + uint32_t isr = UART_DEV->ISR; + if (isr & USART_ISR_RXNE) { + uint8_t byte = (uint8_t) UART_DEV->RDR; + tu_fifo_write(&uart_rx_ff, &byte); + } + if (isr & (USART_ISR_ORE | USART_ISR_FE | USART_ISR_NE | USART_ISR_PE)) { + UART_DEV->ICR = USART_ICR_ORECF | USART_ICR_FECF | USART_ICR_NCF | USART_ICR_PECF; + } +} + +void USART1_IRQHandler(void) { + uart_rx_isr(); +} +void USART2_IRQHandler(void) { + uart_rx_isr(); +} +#endif void board_init(void) { board_stm32f0_clock_init(); @@ -54,9 +91,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 +118,10 @@ void board_init(void) { GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; HAL_GPIO_Init(BUTTON_PORT, &GPIO_InitStruct); +#ifdef UART_DEV + // Enable UART Clock + UART_CLK_EN(); + // Uart GPIO_InitStruct.Pin = UART_TX_PIN | UART_RX_PIN; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; @@ -92,15 +130,13 @@ 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); + UART_DEV->CR1 |= USART_CR1_RXNEIE; + const IRQn_Type uart_irqn = (UART_DEV == USART1) ? USART1_IRQn : USART2_IRQn; + NVIC_SetPriority(uart_irqn, (1 << __NVIC_PRIO_BITS) - 1); + NVIC_EnableIRQ(uart_irqn); +#endif // USB Pins // Configure USB DM and DP pins. This is optional, and maintained only for user guidance. @@ -141,15 +177,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_DEV + 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_DEV + 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/family.c b/hw/bsp/stm32f1/family.c index 78a425453..fa2d3b268 100644 --- a/hw/bsp/stm32f1/family.c +++ b/hw/bsp/stm32f1/family.c @@ -30,6 +30,7 @@ #include "stm32f1xx_hal.h" #include "bsp/board_api.h" +#include "common/tusb_fifo.h" #include "board.h" //--------------------------------------------------------------------+ @@ -50,7 +51,42 @@ void USBWakeUp_IRQHandler(void) { //--------------------------------------------------------------------+ // MACRO TYPEDEF CONSTANT ENUM //--------------------------------------------------------------------+ -UART_HandleTypeDef UartHandle; +#ifdef UART_DEV +static UART_HandleTypeDef UartHandle = { + .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 +}; + +// 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) +static void uart_rx_isr(void) { + uint32_t sr = UART_DEV->SR; + if (sr & USART_SR_RXNE) { + uint8_t byte = (uint8_t) UART_DEV->DR; + tu_fifo_write(&uart_rx_ff, &byte); + } + // Reading DR clears RXNE. OR is cleared by reading SR then DR (already done). +} + +void USART1_IRQHandler(void) { + uart_rx_isr(); +} +void USART2_IRQHandler(void) { + uart_rx_isr(); +} +void USART3_IRQHandler(void) { + uart_rx_isr(); +} +#endif void board_init(void) { board_stm32f1_clock_init(); @@ -112,17 +148,14 @@ 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); + UART_DEV->CR1 |= USART_CR1_RXNEIE; + const IRQn_Type uart_irqn = (UART_DEV == USART1) ? USART1_IRQn + : (UART_DEV == USART2) ? USART2_IRQn + : USART3_IRQn; + NVIC_SetPriority(uart_irqn, (1 << __NVIC_PRIO_BITS) - 1); + NVIC_EnableIRQ(uart_irqn); #endif #ifdef USB_CONNECT_PIN @@ -184,14 +217,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_DEV + 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_DEV + 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..3106fc9e3 100644 --- a/hw/bsp/stm32f2/family.c +++ b/hw/bsp/stm32f2/family.c @@ -30,7 +30,9 @@ #include "stm32f2xx_hal.h" #include "bsp/board_api.h" +#include "common/tusb_fifo.h" #include "board.h" + //--------------------------------------------------------------------+ // Forward USB interrupt events to TinyUSB IRQ Handler //--------------------------------------------------------------------+ @@ -41,6 +43,48 @@ void OTG_FS_IRQHandler(void) { //--------------------------------------------------------------------+ // 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, + } +}; + +// 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) +static void uart_rx_isr(void) { + uint32_t sr = UART_DEV->SR; + if (sr & USART_SR_RXNE) { + uint8_t byte = (uint8_t) UART_DEV->DR; + tu_fifo_write(&uart_rx_ff, &byte); + } + // Reading DR clears RXNE. OR is cleared by reading SR then DR (already done). +} + +void USART1_IRQHandler(void) { + uart_rx_isr(); +} +void USART2_IRQHandler(void) { + uart_rx_isr(); +} +void USART3_IRQHandler(void) { + uart_rx_isr(); +} +#endif + +//--------------------------------------------------------------------+ +// MACRO TYPEDEF CONSTANT ENUM +//--------------------------------------------------------------------+ // enable all LED, Button, Uart, USB clock static void all_rcc_clk_enable(void) { @@ -110,6 +154,17 @@ void board_init(void) { cfg.vbus_sensing = true; tud_configure(0, TUD_CFGID_DWC2, &cfg); #endif + +#ifdef UART_DEV + HAL_UART_Init(&UartHandle); + tu_fifo_config(&uart_rx_ff, uart_rx_ff_buf, sizeof(uart_rx_ff_buf), false); + UART_DEV->CR1 |= USART_CR1_RXNEIE; + const IRQn_Type uart_irqn = (UART_DEV == USART1) ? USART1_IRQn + : (UART_DEV == USART2) ? USART2_IRQn + : USART3_IRQn; + NVIC_SetPriority(uart_irqn, (1 << __NVIC_PRIO_BITS) - 1); + NVIC_EnableIRQ(uart_irqn); +#endif } //--------------------------------------------------------------------+ @@ -126,15 +181,31 @@ uint32_t board_button_read(void) { } int board_uart_read(uint8_t* buf, int len) { - (void) buf; - (void) len; +#ifdef UART_DEV + 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_DEV + 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..8cee5f8c0 100644 --- a/hw/bsp/stm32f3/family.c +++ b/hw/bsp/stm32f3/family.c @@ -30,6 +30,7 @@ #include "stm32f3xx_hal.h" #include "bsp/board_api.h" +#include "common/tusb_fifo.h" #include "board.h" //--------------------------------------------------------------------+ @@ -63,6 +64,50 @@ void USBWakeUp_RMP_IRQHandler(void) { //--------------------------------------------------------------------+ // 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, + } +}; + +// 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 +static void uart_rx_isr(void) { + uint32_t isr = UART_DEV->ISR; + if (isr & USART_ISR_RXNE) { + uint8_t byte = (uint8_t) UART_DEV->RDR; + tu_fifo_write(&uart_rx_ff, &byte); + } + if (isr & (USART_ISR_ORE | USART_ISR_FE | USART_ISR_NE | USART_ISR_PE)) { + UART_DEV->ICR = USART_ICR_ORECF | USART_ICR_FECF | USART_ICR_NCF | USART_ICR_PECF; + } +} + +void USART1_IRQHandler(void) { + uart_rx_isr(); +} +void USART2_IRQHandler(void) { + uart_rx_isr(); +} +void USART3_IRQHandler(void) { + uart_rx_isr(); +} +#endif + +//--------------------------------------------------------------------+ +// MACRO TYPEDEF CONSTANT ENUM +//--------------------------------------------------------------------+ void board_init(void) { @@ -108,6 +153,17 @@ void board_init(void) { // Enable USB clock __HAL_RCC_USB_CLK_ENABLE(); + +#ifdef UART_DEV + HAL_UART_Init(&UartHandle); + tu_fifo_config(&uart_rx_ff, uart_rx_ff_buf, sizeof(uart_rx_ff_buf), false); + UART_DEV->CR1 |= USART_CR1_RXNEIE; + const IRQn_Type uart_irqn = (UART_DEV == USART1) ? USART1_IRQn + : (UART_DEV == USART2) ? USART2_IRQn + : USART3_IRQn; + NVIC_SetPriority(uart_irqn, (1 << __NVIC_PRIO_BITS) - 1); + NVIC_EnableIRQ(uart_irqn); +#endif } //--------------------------------------------------------------------+ @@ -137,15 +193,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_DEV + 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_DEV + 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/family.c b/hw/bsp/stm32f4/family.c index 665ea114a..b8784d03c 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; @@ -57,15 +58,39 @@ void OTG_HS_IRQHandler(void) { 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, + .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) +static void uart_rx_isr(void) { + uint32_t sr = UART_DEV->SR; + if (sr & USART_SR_RXNE) { + uint8_t byte = (uint8_t) UART_DEV->DR; + tu_fifo_write(&uart_rx_ff, &byte); + } + // Reading DR clears RXNE. OR is cleared by reading SR then DR (already done). +} + +void USART1_IRQHandler(void) { + uart_rx_isr(); +} +void USART2_IRQHandler(void) { + uart_rx_isr(); +} +void USART3_IRQHandler(void) { + uart_rx_isr(); +} #endif void board_init(void) { @@ -113,6 +138,13 @@ void board_init(void) { #ifdef UART_DEV HAL_UART_Init(&UartHandle); + tu_fifo_config(&uart_rx_ff, uart_rx_ff_buf, sizeof(uart_rx_ff_buf), false); + UART_DEV->CR1 |= USART_CR1_RXNEIE; + const IRQn_Type uart_irqn = (UART_DEV == USART1) ? USART1_IRQn + : (UART_DEV == USART2) ? USART2_IRQn + : USART3_IRQn; + NVIC_SetPriority(uart_irqn, (1 << __NVIC_PRIO_BITS) - 1); + NVIC_EnableIRQ(uart_irqn); #endif //------------- USB FS -------------// @@ -228,15 +260,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_DEV + 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; + 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/family.c b/hw/bsp/stm32f7/family.c index f8145cd63..e29d3d5cd 100644 --- a/hw/bsp/stm32f7/family.c +++ b/hw/bsp/stm32f7/family.c @@ -32,11 +32,12 @@ #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" @@ -46,18 +47,47 @@ typedef struct { //--------------------------------------------------------------------+ #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, +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, + }}; + +// 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; + +// Minimal UART RX ISR: direct register access, no HAL overhead +static void uart_rx_isr(void) { + uint32_t isr = UART_DEV->ISR; + // Read data if available + if (isr & USART_ISR_RXNE) { + uint8_t byte = (uint8_t)UART_DEV->RDR; + tu_fifo_write(&uart_rx_ff, &byte); } -}; + // Clear error flags (OR, FE, NE, PE) via ICR + if (isr & (USART_ISR_ORE | USART_ISR_FE | USART_ISR_NE | USART_ISR_PE)) { + UART_DEV->ICR = USART_ICR_ORECF | USART_ICR_FECF | USART_ICR_NCF | USART_ICR_PECF; + } +} + +void USART1_IRQHandler(void) { + uart_rx_isr(); +} +void USART2_IRQHandler(void) { + uart_rx_isr(); +} +void USART3_IRQHandler(void) { + uart_rx_isr(); +} +void USART6_IRQHandler(void) { + uart_rx_isr(); +} #endif //--------------------------------------------------------------------+ @@ -90,8 +120,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 +139,58 @@ 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 HAL_UART_Init(&UartHandle); + tu_fifo_config(&uart_rx_ff, uart_rx_ff_buf, sizeof(uart_rx_ff_buf), false); + // Enable RXNE interrupt via direct register (not HAL_UART_Receive_IT) + UART_DEV->CR1 |= USART_CR1_RXNEIE; + const IRQn_Type uart_irqn = (UART_DEV == USART1) ? USART1_IRQn + : (UART_DEV == USART2) ? USART2_IRQn + : (UART_DEV == USART3) ? USART3_IRQn + : USART6_IRQn; + // Lowest priority: UART RX ISR only writes to tu_fifo, no FreeRTOS API calls + NVIC_SetPriority(uart_irqn, (1 << __NVIC_PRIO_BITS) - 1); + NVIC_EnableIRQ(uart_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 +198,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 +228,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 +278,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 +298,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 +316,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]; @@ -290,14 +330,21 @@ 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; + 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_DEV + 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 +352,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/family.c b/hw/bsp/stm32g0/family.c index b25897264..6cdea3653 100644 --- a/hw/bsp/stm32g0/family.c +++ b/hw/bsp/stm32g0/family.c @@ -113,6 +113,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 +158,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_DEV + 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; + 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/family.c b/hw/bsp/stm32g4/family.c index 2e13a1d4b..b6968b8bd 100644 --- a/hw/bsp/stm32g4/family.c +++ b/hw/bsp/stm32g4/family.c @@ -129,6 +129,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 +188,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_DEV + 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; + 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..6ee9a7e2b 100644 --- a/hw/bsp/stm32h5/family.c +++ b/hw/bsp/stm32h5/family.c @@ -120,6 +120,7 @@ void board_init(void) { #ifdef UART_DEV UART_CLK_EN(); HAL_UART_Init(&UartHandle); + HAL_UARTEx_EnableFifoMode(&UartHandle); #endif // USB Pins TODO double check USB clock and pin setup @@ -183,20 +184,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_DEV + 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_DEV + 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/family.c b/hw/bsp/stm32h7/family.c index a95674217..c60706b99 100644 --- a/hw/bsp/stm32h7/family.c +++ b/hw/bsp/stm32h7/family.c @@ -150,6 +150,7 @@ void board_init(void) { #ifdef UART_DEV UART_CLK_EN(); HAL_UART_Init(&UartHandle); + HAL_UARTEx_EnableFifoMode(&UartHandle); #endif //------------- USB FS -------------// @@ -298,12 +299,20 @@ 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; + 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/family.c b/hw/bsp/stm32h7rs/family.c index 3bf75ba97..d84c9d5f8 100644 --- a/hw/bsp/stm32h7rs/family.c +++ b/hw/bsp/stm32h7rs/family.c @@ -320,6 +320,7 @@ void board_init(void) { #ifdef UART_DEV UART_CLK_EN(); HAL_UART_Init(&UartHandle); + HAL_UARTEx_EnableFifoMode(&UartHandle); #endif //------------- USB FS -------------// @@ -444,19 +445,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_DEV + 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; + 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/stm32l4/family.c b/hw/bsp/stm32l4/family.c index c87b643b8..1a5c59fdc 100644 --- a/hw/bsp/stm32l4/family.c +++ b/hw/bsp/stm32l4/family.c @@ -141,6 +141,9 @@ 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 /* Configure USB FS GPIOs */ /* Configure DM DP Pins */ @@ -213,14 +216,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_DEV + 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_DEV + 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/family.c b/hw/bsp/stm32n6/family.c index 4354616c3..267da746e 100644 --- a/hw/bsp/stm32n6/family.c +++ b/hw/bsp/stm32n6/family.c @@ -157,6 +157,7 @@ void board_init(void) { #ifdef UART_DEV UART_CLK_EN(); 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 +343,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_DEV + 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; + 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/family.c b/hw/bsp/stm32u0/family.c index af39ae398..4acbb3437 100644 --- a/hw/bsp/stm32u0/family.c +++ b/hw/bsp/stm32u0/family.c @@ -115,6 +115,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 @@ -154,17 +155,35 @@ uint32_t board_button_read(void) { int board_uart_read(uint8_t* buf, int len) { #ifdef UART_DEV - (void) buf; (void) len; - return 0; + 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) { +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; + 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/family.c b/hw/bsp/stm32u5/family.c index 55ca25d58..27e84beb0 100644 --- a/hw/bsp/stm32u5/family.c +++ b/hw/bsp/stm32u5/family.c @@ -139,6 +139,7 @@ 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); /* Configure USB GPIOs */ /* Configure DM DP Pins */ @@ -252,14 +253,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_DEV + 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_DEV + 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/family.c b/hw/bsp/stm32wb/family.c index de503c072..0fdc5ca7d 100644 --- a/hw/bsp/stm32wb/family.c +++ b/hw/bsp/stm32wb/family.c @@ -123,6 +123,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 +152,36 @@ uint32_t board_button_read(void) { } int board_uart_read(uint8_t* buf, int len) { - (void) buf; - (void) len; +#ifdef UART_DEV + 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; + 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 \ |
