From 30003ffb86e755a2577c21d27ebbbe5e0ccc81f3 Mon Sep 17 00:00:00 2001 From: Pauli Salmenrinne Date: Wed, 18 Mar 2020 13:40:21 +0200 Subject: [PATCH] drivers: serial: stm32: Support for parity in DTS for STM32 uart. Add support for devicetree property 'parity' for stm32 serial devices. Signed-off-by: Pauli Salmenrinne --- drivers/serial/uart_stm32.c | 32 +++++++++++++++++++++--- drivers/serial/uart_stm32.h | 2 ++ dts/bindings/serial/st,stm32-lpuart.yaml | 6 +++++ dts/bindings/serial/st,stm32-uart.yaml | 6 +++++ dts/bindings/serial/st,stm32-usart.yaml | 6 +++++ 5 files changed, 48 insertions(+), 4 deletions(-) diff --git a/drivers/serial/uart_stm32.c b/drivers/serial/uart_stm32.c index cf0a6ec777d..32d7adc294a 100644 --- a/drivers/serial/uart_stm32.c +++ b/drivers/serial/uart_stm32.c @@ -661,6 +661,8 @@ static int uart_stm32_init(struct device *dev) const struct uart_stm32_config *config = DEV_CFG(dev); struct uart_stm32_data *data = DEV_DATA(dev); USART_TypeDef *UartInstance = UART_STRUCT(dev); + u32_t ll_parity; + u32_t ll_datawidth; __uart_stm32_get_clock(dev); /* enable clock */ @@ -675,10 +677,31 @@ static int uart_stm32_init(struct device *dev) LL_USART_SetTransferDirection(UartInstance, LL_USART_DIRECTION_TX_RX); - /* 8 data bit, 1 start bit, 1 stop bit, no parity */ + /* Determine the datawidth and parity. If we use other parity than + * 'none' we must use datawidth = 9 (to get 8 databit + 1 parity bit). + */ + if (config->parity == 2) { + /* 8 databit, 1 parity bit, parity even */ + ll_parity = LL_USART_PARITY_EVEN; + ll_datawidth = LL_USART_DATAWIDTH_9B; + } else if (config->parity == 1) { + /* 8 databit, 1 parity bit, parity odd */ + ll_parity = LL_USART_PARITY_ODD; + ll_datawidth = LL_USART_DATAWIDTH_9B; + } else { /* Default to 8N0, but show warning if invalid value */ + if (config->parity != 0) { + LOG_WRN("Invalid parity setting '%d'." + "Defaulting to 'none'.", config->parity); + } + /* 8 databit, parity none */ + ll_parity = LL_USART_PARITY_NONE; + ll_datawidth = LL_USART_DATAWIDTH_8B; + } + + /* Set datawidth and parity, 1 start bit, 1 stop bit */ LL_USART_ConfigCharacter(UartInstance, - LL_USART_DATAWIDTH_8B, - LL_USART_PARITY_NONE, + ll_datawidth, + ll_parity, LL_USART_STOPBITS_1); if (config->hw_flow_control) { @@ -740,7 +763,8 @@ static const struct uart_stm32_config uart_stm32_cfg_##index = { \ .pclken = { .bus = DT_INST_CLOCKS_CELL(index, bus), \ .enr = DT_INST_CLOCKS_CELL(index, bits) \ }, \ - .hw_flow_control = DT_INST_PROP(index, hw_flow_control)\ + .hw_flow_control = DT_INST_PROP(index, hw_flow_control),\ + .parity = DT_INST_PROP(index, parity)\ }; \ \ static struct uart_stm32_data uart_stm32_data_##index = { \ diff --git a/drivers/serial/uart_stm32.h b/drivers/serial/uart_stm32.h index 4928842fb13..f0d35125cd0 100644 --- a/drivers/serial/uart_stm32.h +++ b/drivers/serial/uart_stm32.h @@ -19,6 +19,8 @@ struct uart_stm32_config { struct stm32_pclken pclken; /* initial hardware flow control, 1 for RTS/CTS */ bool hw_flow_control; + /* initial parity, 0 for none, 1 for odd, 2 for even */ + int parity; }; /* driver data */ diff --git a/dts/bindings/serial/st,stm32-lpuart.yaml b/dts/bindings/serial/st,stm32-lpuart.yaml index cfe277d3bf4..a5c89483551 100644 --- a/dts/bindings/serial/st,stm32-lpuart.yaml +++ b/dts/bindings/serial/st,stm32-lpuart.yaml @@ -13,3 +13,9 @@ properties: clocks: required: true + + parity: + required: false + type: int + description: Configures the parity of the adapter. Value 0 for none, 1 for odd and 2 for even parity. + default: 0 diff --git a/dts/bindings/serial/st,stm32-uart.yaml b/dts/bindings/serial/st,stm32-uart.yaml index 48722812556..d391c5da5b5 100644 --- a/dts/bindings/serial/st,stm32-uart.yaml +++ b/dts/bindings/serial/st,stm32-uart.yaml @@ -10,3 +10,9 @@ properties: interrupts: required: true + + parity: + required: false + type: int + description: Configures the parity of the adapter. Value 0 for none, 1 for odd and 2 for even parity. + default: 0 diff --git a/dts/bindings/serial/st,stm32-usart.yaml b/dts/bindings/serial/st,stm32-usart.yaml index 8d1337f93e5..fa5dd306728 100644 --- a/dts/bindings/serial/st,stm32-usart.yaml +++ b/dts/bindings/serial/st,stm32-usart.yaml @@ -10,3 +10,9 @@ properties: interrupts: required: true + + parity: + required: false + type: int + description: Configures the parity of the adapter. Value 0 for none, 1 for odd and 2 for even parity. + default: 0