From 12b1e74f1c64ccdc33480700103b4114cab70e7b Mon Sep 17 00:00:00 2001 From: mazhiyuan Date: Thu, 14 Oct 2021 10:15:55 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8DSTM32=E4=B8=B2=E5=8F=A3mask?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bsp/stm32/libraries/HAL_Drivers/drv_usart.c | 51 ++++++++++++++++++- .../libraries/HAL_Drivers/drv_usart_v2.c | 51 ++++++++++++++++++- .../libraries/HAL_Drivers/drv_usart_v2.h | 4 +- 3 files changed, 100 insertions(+), 6 deletions(-) diff --git a/bsp/stm32/libraries/HAL_Drivers/drv_usart.c b/bsp/stm32/libraries/HAL_Drivers/drv_usart.c index 06c1d81dfd..d8d8549627 100644 --- a/bsp/stm32/libraries/HAL_Drivers/drv_usart.c +++ b/bsp/stm32/libraries/HAL_Drivers/drv_usart.c @@ -251,6 +251,53 @@ 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) { struct stm32_uart *uart; @@ -282,9 +329,9 @@ static int stm32_getc(struct rt_serial_device *serial) #if defined(SOC_SERIES_STM32L4) || defined(SOC_SERIES_STM32WL) || defined(SOC_SERIES_STM32F7) || defined(SOC_SERIES_STM32F0) \ || defined(SOC_SERIES_STM32L0) || defined(SOC_SERIES_STM32G0) || defined(SOC_SERIES_STM32H7) \ || defined(SOC_SERIES_STM32G4) || defined(SOC_SERIES_STM32MP1) || defined(SOC_SERIES_STM32WB)|| defined(SOC_SERIES_STM32F3) - ch = uart->handle.Instance->RDR & 0xff; + ch = uart->handle.Instance->RDR & stm32_uart_get_mask(uart->handle.Init.WordLength, uart->handle.Init.Parity); #else - ch = uart->handle.Instance->DR & 0xff; + ch = uart->handle.Instance->DR & stm32_uart_get_mask(uart->handle.Init.WordLength, uart->handle.Init.Parity); #endif } return ch; diff --git a/bsp/stm32/libraries/HAL_Drivers/drv_usart_v2.c b/bsp/stm32/libraries/HAL_Drivers/drv_usart_v2.c index a2b6dc31bd..038129beef 100644 --- a/bsp/stm32/libraries/HAL_Drivers/drv_usart_v2.c +++ b/bsp/stm32/libraries/HAL_Drivers/drv_usart_v2.c @@ -302,6 +302,53 @@ static int stm32_putc(struct rt_serial_device *serial, char c) return 1; } +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_getc(struct rt_serial_device *serial) { int ch; @@ -311,7 +358,7 @@ static int stm32_getc(struct rt_serial_device *serial) ch = -1; if (__HAL_UART_GET_FLAG(&(uart->handle), UART_FLAG_RXNE) != RESET) - ch = UART_GET_RDR(&uart->handle); + ch = UART_GET_RDR(&uart->handle, stm32_uart_get_mask(uart->handle.Init.WordLength, uart->handle.Init.Parity)); return ch; } @@ -403,7 +450,7 @@ static void uart_isr(struct rt_serial_device *serial) rx_fifo = (struct rt_serial_rx_fifo *) serial->serial_rx; RT_ASSERT(rx_fifo != RT_NULL); - rt_ringbuffer_putchar(&(rx_fifo->rb), UART_GET_RDR(&uart->handle)); + rt_ringbuffer_putchar(&(rx_fifo->rb), UART_GET_RDR(&uart->handle, stm32_uart_get_mask(uart->handle.Init.WordLength, uart->handle.Init.Parity))); rt_hw_serial_isr(serial, RT_SERIAL_EVENT_RX_IND); } diff --git a/bsp/stm32/libraries/HAL_Drivers/drv_usart_v2.h b/bsp/stm32/libraries/HAL_Drivers/drv_usart_v2.h index 4b2091b446..defa740c53 100644 --- a/bsp/stm32/libraries/HAL_Drivers/drv_usart_v2.h +++ b/bsp/stm32/libraries/HAL_Drivers/drv_usart_v2.h @@ -24,11 +24,11 @@ int rt_hw_usart_init(void); || defined(SOC_SERIES_STM32L0) || defined(SOC_SERIES_STM32G0) || defined(SOC_SERIES_STM32H7) \ || defined(SOC_SERIES_STM32G4) #define UART_SET_TDR(__HANDLE__, __DATA__) ((__HANDLE__)->Instance->TDR = (__DATA__)) -#define UART_GET_RDR(__HANDLE__) ((__HANDLE__)->Instance->RDR & 0xFF) +#define UART_GET_RDR(__HANDLE__, MASK) ((__HANDLE__)->Instance->RDR & MASK) #else #define UART_SET_TDR(__HANDLE__, __DATA__) ((__HANDLE__)->Instance->DR = (__DATA__)) -#define UART_GET_RDR(__HANDLE__) ((__HANDLE__)->Instance->DR & 0xFF) +#define UART_GET_RDR(__HANDLE__, MASK) ((__HANDLE__)->Instance->DR & MASK) #endif