diff --git a/bsp/stm32/libraries/HAL_Drivers/drv_usart.c b/bsp/stm32/libraries/HAL_Drivers/drv_usart.c index 13001b4bc0..e666df3733 100644 --- a/bsp/stm32/libraries/HAL_Drivers/drv_usart.c +++ b/bsp/stm32/libraries/HAL_Drivers/drv_usart.c @@ -98,6 +98,53 @@ static struct stm32_uart_config uart_config[] = static struct stm32_uart uart_obj[sizeof(uart_config) / sizeof(uart_config[0])] = {0}; +rt_uint32_t stm32_uart_get_mask(rt_uint32_t word_length, rt_uint32_t parity) +{ + rt_uint32_t mask = 0x00FFU; + if (word_length == UART_WORDLENGTH_8B) + { + if (parity == UART_PARITY_NONE) + { + mask = 0x00FFU ; + } + else + { + mask = 0x007FU ; + } + } +#ifdef UART_WORDLENGTH_9B + else if (word_length == UART_WORDLENGTH_9B) + { + if (parity == UART_PARITY_NONE) + { + mask = 0x01FFU ; + } + else + { + mask = 0x00FFU ; + } + } +#endif +#ifdef UART_WORDLENGTH_7B + else if (word_length == UART_WORDLENGTH_7B) + { + if (parity == UART_PARITY_NONE) + { + mask = 0x007FU ; + } + else + { + mask = 0x003FU ; + } + } + else + { + mask = 0x0000U; + } +#endif + return mask; +} + static rt_err_t stm32_configure(struct rt_serial_device *serial, struct serial_configure *cfg) { struct stm32_uart *uart; @@ -182,6 +229,7 @@ static rt_err_t stm32_configure(struct rt_serial_device *serial, struct serial_c { return -RT_ERROR; } + uart->DR_mask = stm32_uart_get_mask(uart->handle.Init.WordLength, uart->handle.Init.Parity); return RT_EOK; } @@ -257,52 +305,7 @@ static rt_err_t stm32_control(struct rt_serial_device *serial, int cmd, void *ar return RT_EOK; } -rt_uint32_t stm32_uart_get_mask(rt_uint32_t word_length, rt_uint32_t parity) -{ - rt_uint32_t mask; - if (word_length == UART_WORDLENGTH_8B) - { - if (parity == UART_PARITY_NONE) - { - mask = 0x00FFU ; - } - else - { - mask = 0x007FU ; - } - } -#ifdef UART_WORDLENGTH_9B - else if (word_length == UART_WORDLENGTH_9B) - { - if (parity == UART_PARITY_NONE) - { - mask = 0x01FFU ; - } - else - { - mask = 0x00FFU ; - } - } -#endif -#ifdef UART_WORDLENGTH_7B - else if (word_length == UART_WORDLENGTH_7B) - { - if (parity == UART_PARITY_NONE) - { - mask = 0x007FU ; - } - else - { - mask = 0x003FU ; - } - } - else - { - mask = 0x0000U; - } -#endif - return mask; -} + static int stm32_putc(struct rt_serial_device *serial, char c) { @@ -337,9 +340,9 @@ static int stm32_getc(struct rt_serial_device *serial) || defined(SOC_SERIES_STM32L0) || defined(SOC_SERIES_STM32G0) || defined(SOC_SERIES_STM32H7) || defined(SOC_SERIES_STM32L5) \ || defined(SOC_SERIES_STM32G4) || defined(SOC_SERIES_STM32MP1) || defined(SOC_SERIES_STM32WB)|| defined(SOC_SERIES_STM32F3) \ || defined(SOC_SERIES_STM32U5) - ch = uart->handle.Instance->RDR & stm32_uart_get_mask(uart->handle.Init.WordLength, uart->handle.Init.Parity); + ch = uart->handle.Instance->RDR & uart->DR_mask; #else - ch = uart->handle.Instance->DR & stm32_uart_get_mask(uart->handle.Init.WordLength, uart->handle.Init.Parity); + ch = uart->handle.Instance->DR & uart->DR_mask; #endif } return ch; diff --git a/bsp/stm32/libraries/HAL_Drivers/drv_usart.h b/bsp/stm32/libraries/HAL_Drivers/drv_usart.h index 80d06b5030..cc6aa66b8a 100644 --- a/bsp/stm32/libraries/HAL_Drivers/drv_usart.h +++ b/bsp/stm32/libraries/HAL_Drivers/drv_usart.h @@ -57,6 +57,7 @@ struct stm32_uart { UART_HandleTypeDef handle; struct stm32_uart_config *config; + rt_uint32_t DR_mask; #ifdef RT_SERIAL_USING_DMA struct