summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHiFiPhile <[email protected]>2026-05-14 14:08:57 +0200
committerHiFiPhile <[email protected]>2026-05-14 14:08:57 +0200
commit3da2464a30b5903619acd76d3daff5c848ce2fe9 (patch)
tree01a54e7f085d5b0540ee2e7181986f36ef8c5689
parent39f92bdd916d64b4dc4a7e0b2b18eea3dfd44be0 (diff)
rework bsp
Signed-off-by: HiFiPhile <[email protected]>
-rw-r--r--hw/bsp/stm32c5/boards/stm32c542nucleo/board.h2
-rw-r--r--hw/bsp/stm32c5/family.c75
2 files changed, 45 insertions, 32 deletions
diff --git a/hw/bsp/stm32c5/boards/stm32c542nucleo/board.h b/hw/bsp/stm32c5/boards/stm32c542nucleo/board.h
index d7e5e7e13..b64fad243 100644
--- a/hw/bsp/stm32c5/boards/stm32c542nucleo/board.h
+++ b/hw/bsp/stm32c5/boards/stm32c542nucleo/board.h
@@ -39,7 +39,7 @@
// Button
#define BUTTON_PORT HAL_GPIOC
#define BUTTON_PIN HAL_GPIO_PIN_13
-#define BUTTON_STATE_ACTIVE 0
+#define BUTTON_STATE_ACTIVE 1
// Enable UART serial communication with the ST-Link
#define UART_ID 2
diff --git a/hw/bsp/stm32c5/family.c b/hw/bsp/stm32c5/family.c
index a40a2a4ee..40b86fc55 100644
--- a/hw/bsp/stm32c5/family.c
+++ b/hw/bsp/stm32c5/family.c
@@ -35,18 +35,14 @@
#ifdef UART_ID
#if UART_ID == 1
- #define UARTn HAL_UART1
+ #define UARTn USART1
#define UARTn_CLK_ENABLE HAL_RCC_USART1_EnableClock
#elif UART_ID == 2
- #define UARTn HAL_UART2
+ #define UARTn USART2
#define UARTn_CLK_ENABLE HAL_RCC_USART2_EnableClock
#endif
#endif
-#ifndef UART_GET_INSTANCE
- #define UART_GET_INSTANCE(handle) ((USART_TypeDef *)((uint32_t)(handle)->instance))
-#endif
-
//--------------------------------------------------------------------+
// Forward USB interrupt events to TinyUSB IRQ Handler
//--------------------------------------------------------------------+
@@ -57,11 +53,12 @@ void USB_DRD_FS_IRQHandler(void) {
//--------------------------------------------------------------------+
// MACRO TYPEDEF CONSTANT ENUM
//--------------------------------------------------------------------+
-#ifdef UART_ID
-static hal_uart_handle_t hUSART;
-#endif
+static void mpu_init(void);
void board_init(void) {
+ mpu_init();
+ LL_ICACHE_Enable(ICACHE);
+
HAL_Init();
board_clock_init();
@@ -117,27 +114,43 @@ void board_init(void) {
HAL_GPIO_Init(UART_GPIO_PORT, UART_TX_PIN | UART_RX_PIN, &gpio_config);
}
- hal_uart_config_t uart_config;
- HAL_UART_Init(&hUSART, UARTn);
- uart_config.baud_rate = 115200;
- uart_config.clock_prescaler = HAL_UART_PRESCALER_DIV1;
- uart_config.word_length = HAL_UART_WORD_LENGTH_8_BIT;
- uart_config.stop_bits = HAL_UART_STOP_BIT_1;
- uart_config.parity = HAL_UART_PARITY_NONE;
- uart_config.direction = HAL_UART_DIRECTION_TX_RX;
- uart_config.hw_flow_ctl = HAL_UART_HW_CONTROL_NONE;
- uart_config.oversampling = HAL_UART_OVERSAMPLING_16;
- uart_config.one_bit_sampling = HAL_UART_ONE_BIT_SAMPLE_DISABLE;
+ LL_USART_ConfigAsyncMode(UARTn);
+ uint32_t reg_temp = (LL_USART_DATAWIDTH_8_BIT | LL_USART_PARITY_NONE
+ | LL_USART_DIRECTION_TX_RX | LL_USART_OVERSAMPLING_16);
+ LL_USART_ConfigXfer(UARTn, reg_temp, LL_USART_STOP_BIT_1);
+ LL_USART_SetBaudRate(UARTn, SystemCoreClock, LL_USART_PRESCALER_DIV1, LL_USART_OVERSAMPLING_16,
+ 115200);
+ LL_USART_EnableFIFO(UARTn);
+ LL_USART_Enable(UARTn);
+#endif
+}
- HAL_UART_SetConfig(&hUSART, &uart_config);
+static void mpu_init(void)
+{
+ /* Disables the MPU */
+ HAL_CORTEX_MPU_Disable();
- /* Fifo configuration */
- HAL_UART_SetTxFifoThreshold(&hUSART, HAL_UART_FIFO_THRESHOLD_1_8);
- HAL_UART_SetRxFifoThreshold(&hUSART, HAL_UART_FIFO_THRESHOLD_1_8);
- HAL_UART_EnableFifoMode(&hUSART);
+ /*
+ Initializes and configures the MPU attributes
+ */
+ HAL_CORTEX_MPU_SetCacheMemAttr(HAL_CORTEX_MPU_MEM_ATTR_0, HAL_CORTEX_MPU_NORMAL_MEM_NCACHEABLE);
- LL_USART_Enable(UART_GET_INSTANCE(&hUSART));
-#endif
+ /*
+ Initializes and configures the MPU Region
+ */
+ hal_cortex_mpu_region_config_t p_region_config = {0};
+
+ p_region_config.base_addr = 0x8FFE000;
+ p_region_config.limit_addr = 0x8FFFFFF;
+ p_region_config.access_attr = HAL_CORTEX_MPU_REGION_ALL_RO;
+ p_region_config.exec_attr = HAL_CORTEX_MPU_EXECUTION_ATTR_DISABLE;
+ p_region_config.attr_idx = HAL_CORTEX_MPU_MEM_ATTR_0;
+ HAL_CORTEX_MPU_SetConfigRegion(HAL_CORTEX_MPU_REGION_0, &p_region_config);
+
+ HAL_CORTEX_MPU_EnableRegion(HAL_CORTEX_MPU_REGION_0);
+
+ /* Enables the MPU */
+ HAL_CORTEX_MPU_Enable(HAL_CORTEX_MPU_HARDFAULT_NMI_DISABLE, HAL_CORTEX_MPU_ACCESS_FAULT_ONLY_PRIV);
}
//--------------------------------------------------------------------+
@@ -170,8 +183,8 @@ int board_uart_read(uint8_t *buf, int len) {
#ifdef UART_ID
int count = 0;
while (count < len) {
- if (LL_USART_IsActiveFlag_RXNE_RXFNE(UART_GET_INSTANCE(&hUSART))) {
- buf[count] = (uint8_t) UART_GET_INSTANCE(&hUSART)->RDR;
+ if (LL_USART_IsActiveFlag_RXNE_RXFNE(UARTn)) {
+ buf[count] = LL_USART_ReceiveData8(UARTn);
count++;
} else {
break;
@@ -189,8 +202,8 @@ int board_uart_write(void const *buf, int len) {
const uint8_t *p = (const uint8_t *) buf;
int count = 0;
while (count < len) {
- if (LL_USART_IsActiveFlag_TXE_TXFNF(UART_GET_INSTANCE(&hUSART))) {
- UART_GET_INSTANCE(&hUSART)->TDR = p[count];
+ if (LL_USART_IsActiveFlag_TXE_TXFNF(UARTn)) {
+ LL_USART_TransmitData8(UARTn, p[count]);
count++;
} else {
break;