summaryrefslogtreecommitdiff
path: root/hw/bsp/stm32f4
diff options
context:
space:
mode:
authorhathach <[email protected]>2026-04-03 19:38:07 +0700
committerhathach <[email protected]>2026-04-03 19:38:07 +0700
commit81efb78d1655dcab31da5e03fd7e22e91491d186 (patch)
tree0ea8eafc8730146e3a29f3e0349bfa3d02fa635a /hw/bsp/stm32f4
parent3ed9d6517485611b040ecdc4e5d327b6e86f3dee (diff)
update stm32 to use better uart id
Diffstat (limited to 'hw/bsp/stm32f4')
-rw-r--r--hw/bsp/stm32f4/boards/feather_stm32f405/board.h4
-rw-r--r--hw/bsp/stm32f4/boards/pyboardv11/board.h4
-rw-r--r--hw/bsp/stm32f4/boards/stm32f401blackpill/board.h4
-rw-r--r--hw/bsp/stm32f4/boards/stm32f407blackvet/board.h4
-rw-r--r--hw/bsp/stm32f4/boards/stm32f407disco/board.h4
-rw-r--r--hw/bsp/stm32f4/boards/stm32f411blackpill/board.h4
-rw-r--r--hw/bsp/stm32f4/boards/stm32f411disco/board.h4
-rw-r--r--hw/bsp/stm32f4/boards/stm32f412disco/board.h4
-rw-r--r--hw/bsp/stm32f4/boards/stm32f412nucleo/board.h4
-rw-r--r--hw/bsp/stm32f4/boards/stm32f439nucleo/board.h4
-rw-r--r--hw/bsp/stm32f4/family.c60
11 files changed, 46 insertions, 54 deletions
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 b8784d03c..f4ef99150 100644
--- a/hw/bsp/stm32f4/family.c
+++ b/hw/bsp/stm32f4/family.c
@@ -40,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
//--------------------------------------------------------------------+
@@ -54,9 +78,9 @@ 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,
@@ -73,24 +97,14 @@ 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;
+void USARTn_IRQHandler(void) {
+ uint32_t sr = USARTn->SR;
if (sr & USART_SR_RXNE) {
- uint8_t byte = (uint8_t) UART_DEV->DR;
+ 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).
}
-
-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) {
@@ -136,15 +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);
- 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);
+ USARTn->CR1 |= USART_CR1_RXNEIE;
+ NVIC_SetPriority(USARTn_IRQn, (1 << __NVIC_PRIO_BITS) - 1);
+ NVIC_EnableIRQ(USARTn_IRQn);
#endif
//------------- USB FS -------------//
@@ -260,7 +272,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
return (int) tu_fifo_read_n(&uart_rx_ff, buf, (uint16_t) len);
#else
(void) buf; (void) len;
@@ -269,7 +281,7 @@ int board_uart_read(uint8_t *buf, int len) {
}
int board_uart_write(void const *buf, int len) {
-#ifdef UART_DEV
+#ifdef UART_ID
const uint8_t *p = (const uint8_t *) buf;
int count = 0;
while (count < len) {