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 /hw/bsp/stm32f3 | |
| parent | 9368e5d40f3d8e5ac323d288f969782a962d593e (diff) | |
stm32 add uart hwfifo if supported, otherwise usb tu_fifo_t for receive uart in irq
Diffstat (limited to 'hw/bsp/stm32f3')
| -rw-r--r-- | hw/bsp/stm32f3/family.c | 80 |
1 files changed, 76 insertions, 4 deletions
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 |
