From 37585a3f209e421ca5b9700d570c76d2e74d44af Mon Sep 17 00:00:00 2001 From: winfenggao <104723992+winfenggao@users.noreply.github.com> Date: Tue, 6 Jun 2023 12:18:32 +0800 Subject: [PATCH] =?UTF-8?q?[serial=5Fv1]=20=E5=A2=9E=E5=8A=A0=E5=8F=98?= =?UTF-8?q?=E9=87=8F=20DR=5Fmask=20=E6=88=90=E5=91=98=E5=8F=98=E9=87=8F?= =?UTF-8?q?=E6=8F=90=E9=AB=98=E6=95=88=E7=8E=87=20(#7597)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bsp/stm32/libraries/HAL_Drivers/drv_usart.c | 99 +++++++++++---------- bsp/stm32/libraries/HAL_Drivers/drv_usart.h | 1 + 2 files changed, 52 insertions(+), 48 deletions(-) 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