Skip to content

Commit 005f372

Browse files
committed
drivers: serial: stm32: Add persistent uart_config struct
Add static uart_config struct to init macro with corresponding pointer as uart_stm32_config struct member. This struct will store boot and runtime UART configuration for reinitialization upon exiting low-power modes, where register contents might be lost. Remove hw_flow_control, parity, and baud_rate from uart_stm32_config and uart_stm32_data structs, respectively, as the need for these is now obviated by the presence of the uart_config struct, which contains equivalent members. Add default baudrate, parity, stop, and data bits constants, used to initialize the uart_config struct, in case the corresponding UART DT properties are not set. Signed-off-by: Kenneth J. Miller <[email protected]>
1 parent c19ede6 commit 005f372

File tree

2 files changed

+35
-18
lines changed

2 files changed

+35
-18
lines changed

drivers/serial/uart_stm32.c

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,7 @@ static int uart_stm32_configure(const struct device *dev,
465465
{
466466
const struct uart_stm32_config *config = dev->config;
467467
struct uart_stm32_data *data = dev->data;
468+
struct uart_config *uart_cfg = data->uart_cfg;
468469
const uint32_t parity = uart_stm32_cfg2ll_parity(cfg->parity);
469470
const uint32_t stopbits = uart_stm32_cfg2ll_stopbits(config, cfg->stop_bits);
470471
const uint32_t databits = uart_stm32_cfg2ll_databits(cfg->data_bits,
@@ -536,9 +537,9 @@ static int uart_stm32_configure(const struct device *dev,
536537
}
537538
#endif
538539

539-
if (cfg->baudrate != data->baud_rate) {
540+
if (cfg->baudrate != uart_cfg->baudrate) {
540541
uart_stm32_set_baudrate(dev, cfg->baudrate);
541-
data->baud_rate = cfg->baudrate;
542+
uart_cfg->baudrate = cfg->baudrate;
542543
}
543544

544545
LL_USART_Enable(config->usart);
@@ -549,8 +550,9 @@ static int uart_stm32_config_get(const struct device *dev,
549550
struct uart_config *cfg)
550551
{
551552
struct uart_stm32_data *data = dev->data;
553+
struct uart_config *uart_cfg = data->uart_cfg;
552554

553-
cfg->baudrate = data->baud_rate;
555+
cfg->baudrate = uart_cfg->baudrate;
554556
cfg->parity = uart_stm32_ll2cfg_parity(uart_stm32_get_parity(dev));
555557
cfg->stop_bits = uart_stm32_ll2cfg_stopbits(
556558
uart_stm32_get_stopbits(dev));
@@ -1817,6 +1819,7 @@ static int uart_stm32_init(const struct device *dev)
18171819
{
18181820
const struct uart_stm32_config *config = dev->config;
18191821
struct uart_stm32_data *data = dev->data;
1822+
struct uart_config *uart_cfg = data->uart_cfg;
18201823
uint32_t ll_parity;
18211824
uint32_t ll_datawidth;
18221825
int err;
@@ -1868,18 +1871,18 @@ static int uart_stm32_init(const struct device *dev)
18681871
/* Determine the datawidth and parity. If we use other parity than
18691872
* 'none' we must use datawidth = 9 (to get 8 databit + 1 parity bit).
18701873
*/
1871-
if (config->parity == 2) {
1874+
if (uart_cfg->parity == 2) {
18721875
/* 8 databit, 1 parity bit, parity even */
18731876
ll_parity = LL_USART_PARITY_EVEN;
18741877
ll_datawidth = LL_USART_DATAWIDTH_9B;
1875-
} else if (config->parity == 1) {
1878+
} else if (uart_cfg->parity == 1) {
18761879
/* 8 databit, 1 parity bit, parity odd */
18771880
ll_parity = LL_USART_PARITY_ODD;
18781881
ll_datawidth = LL_USART_DATAWIDTH_9B;
18791882
} else { /* Default to 8N0, but show warning if invalid value */
1880-
if (config->parity != 0) {
1883+
if (uart_cfg->parity != 0) {
18811884
LOG_WRN("Invalid parity setting '%d'."
1882-
"Defaulting to 'none'.", config->parity);
1885+
"Defaulting to 'none'.", uart_cfg->parity);
18831886
}
18841887
/* 8 databit, parity none */
18851888
ll_parity = LL_USART_PARITY_NONE;
@@ -1892,12 +1895,12 @@ static int uart_stm32_init(const struct device *dev)
18921895
ll_parity,
18931896
LL_USART_STOPBITS_1);
18941897

1895-
if (config->hw_flow_control) {
1898+
if (uart_cfg->flow_ctrl) {
18961899
uart_stm32_set_hwctrl(dev, LL_USART_HWCONTROL_RTS_CTS);
18971900
}
18981901

18991902
/* Set the default baudrate */
1900-
uart_stm32_set_baudrate(dev, data->baud_rate);
1903+
uart_stm32_set_baudrate(dev, uart_cfg->baudrate);
19011904

19021905
/* Enable the single wire / half-duplex mode */
19031906
if (config->single_wire) {
@@ -2141,13 +2144,25 @@ PINCTRL_DT_INST_DEFINE(index); \
21412144
static const struct stm32_pclken pclken_##index[] = \
21422145
STM32_DT_INST_CLOCKS(index);\
21432146
\
2147+
static struct uart_config uart_cfg_##index = { \
2148+
.baudrate = DT_INST_PROP_OR(index, current_speed, \
2149+
STM32_UART_DEFAULT_BAUDRATE), \
2150+
.parity = DT_INST_ENUM_IDX_OR(index, parity, \
2151+
STM32_UART_DEFAULT_PARITY), \
2152+
.stop_bits = DT_INST_ENUM_IDX_OR(index, stop_bits, \
2153+
STM32_UART_DEFAULT_STOP_BITS), \
2154+
.data_bits = DT_INST_ENUM_IDX_OR(index, data_bits, \
2155+
STM32_UART_DEFAULT_DATA_BITS), \
2156+
.flow_ctrl = DT_INST_PROP(index, hw_flow_control) \
2157+
? UART_CFG_FLOW_CTRL_RTS_CTS \
2158+
: UART_CFG_FLOW_CTRL_NONE, \
2159+
}; \
2160+
\
21442161
static const struct uart_stm32_config uart_stm32_cfg_##index = { \
21452162
.usart = (USART_TypeDef *)DT_INST_REG_ADDR(index), \
21462163
.reset = RESET_DT_SPEC_GET(DT_DRV_INST(index)), \
21472164
.pclken = pclken_##index, \
21482165
.pclk_len = DT_INST_NUM_CLOCKS(index), \
2149-
.hw_flow_control = DT_INST_PROP(index, hw_flow_control), \
2150-
.parity = DT_INST_ENUM_IDX_OR(index, parity, UART_CFG_PARITY_NONE), \
21512166
.pcfg = PINCTRL_DT_INST_DEV_CONFIG_GET(index), \
21522167
.single_wire = DT_INST_PROP_OR(index, single_wire, false), \
21532168
.tx_rx_swap = DT_INST_PROP_OR(index, tx_rx_swap, false), \
@@ -2162,7 +2177,7 @@ static const struct uart_stm32_config uart_stm32_cfg_##index = { \
21622177
}; \
21632178
\
21642179
static struct uart_stm32_data uart_stm32_data_##index = { \
2165-
.baud_rate = DT_INST_PROP(index, current_speed), \
2180+
.uart_cfg = &uart_cfg_##index, \
21662181
UART_DMA_CHANNEL(index, rx, RX, PERIPHERAL, MEMORY) \
21672182
UART_DMA_CHANNEL(index, tx, TX, MEMORY, PERIPHERAL) \
21682183
}; \

drivers/serial/uart_stm32.h

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@
1818

1919
#include <stm32_ll_usart.h>
2020

21+
#define STM32_UART_DEFAULT_BAUDRATE 115200
22+
#define STM32_UART_DEFAULT_PARITY UART_CFG_PARITY_NONE
23+
#define STM32_UART_DEFAULT_STOP_BITS UART_CFG_STOP_BITS_1
24+
#define STM32_UART_DEFAULT_DATA_BITS UART_CFG_DATA_BITS_8
25+
2126
/* device config */
2227
struct uart_stm32_config {
2328
/* USART instance */
@@ -28,10 +33,6 @@ struct uart_stm32_config {
2833
const struct stm32_pclken *pclken;
2934
/* number of clock subsystems */
3035
size_t pclk_len;
31-
/* initial hardware flow control, 1 for RTS/CTS */
32-
bool hw_flow_control;
33-
/* initial parity, 0 for none, 1 for odd, 2 for even */
34-
int parity;
3536
/* switch to enable single wire / half duplex feature */
3637
bool single_wire;
3738
/* enable tx/rx pin swap */
@@ -48,6 +49,7 @@ struct uart_stm32_config {
4849
uint8_t de_deassert_time;
4950
/* enable de pin inversion */
5051
bool de_invert;
52+
/* pin muxing */
5153
const struct pinctrl_dev_config *pcfg;
5254
#if defined(CONFIG_UART_INTERRUPT_DRIVEN) || defined(CONFIG_UART_ASYNC_API) || \
5355
defined(CONFIG_PM)
@@ -82,10 +84,10 @@ struct uart_dma_stream {
8284

8385
/* driver data */
8486
struct uart_stm32_data {
85-
/* Baud rate */
86-
uint32_t baud_rate;
8787
/* clock device */
8888
const struct device *clock;
89+
/* uart config */
90+
struct uart_config *uart_cfg;
8991
#ifdef CONFIG_UART_INTERRUPT_DRIVEN
9092
uart_irq_callback_user_data_t user_cb;
9193
void *user_data;

0 commit comments

Comments
 (0)