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/stm32f1 | |
| parent | 9368e5d40f3d8e5ac323d288f969782a962d593e (diff) | |
stm32 add uart hwfifo if supported, otherwise usb tu_fifo_t for receive uart in irq
Diffstat (limited to 'hw/bsp/stm32f1')
| -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 |
3 files changed, 67 insertions, 15 deletions
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 += \ |
