Skip to content

Commit 07a58aa

Browse files
committed
drivers: serial: Improve BFLB UART driver: add runtime configuration
This adds the ability to do runtime configuration of the BFLB UART driver. Signed-off-by: Camille BAUD <[email protected]>
1 parent 7966a5f commit 07a58aa

File tree

1 file changed

+55
-19
lines changed

1 file changed

+55
-19
lines changed

drivers/serial/uart_bflb.c

Lines changed: 55 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,7 @@
3333

3434
struct bflb_config {
3535
const struct pinctrl_dev_config *pincfg;
36-
uint32_t baudrate;
37-
uint8_t direction;
38-
uint8_t data_bits;
39-
uint8_t stop_bits;
40-
uint8_t parity;
4136
uint8_t bit_order;
42-
uint8_t flow_ctrl;
4337
uint8_t tx_fifo_threshold;
4438
uint8_t rx_fifo_threshold;
4539
uint32_t base_reg;
@@ -49,6 +43,7 @@ struct bflb_config {
4943
};
5044

5145
struct bflb_data {
46+
struct uart_config uart_cfg;
5247
#ifdef CONFIG_UART_INTERRUPT_DRIVEN
5348
uart_irq_callback_user_data_t user_cb;
5449
void *user_data;
@@ -294,12 +289,13 @@ static void uart_bflb_isr(const struct device *dev)
294289
static int uart_bflb_configure(const struct device *dev)
295290
{
296291
const struct bflb_config *cfg = dev->config;
292+
struct bflb_data *const data = dev->data;
297293
uint32_t tx_cfg = 0;
298294
uint32_t rx_cfg = 0;
299295
uint32_t divider = 0;
300296
uint32_t tmp = 0;
301297

302-
divider = (uart_bflb_get_clock() * 10 / cfg->baudrate + 5) / 10;
298+
divider = (uart_bflb_get_clock() * 10 / data->uart_cfg.baudrate + 5) / 10;
303299
if (divider >= 0xFFFF) {
304300
divider = 0xFFFF - 1;
305301
}
@@ -314,7 +310,7 @@ static int uart_bflb_configure(const struct device *dev)
314310
tx_cfg = sys_read32(cfg->base_reg + UART_UTX_CONFIG_OFFSET);
315311
rx_cfg = sys_read32(cfg->base_reg + UART_URX_CONFIG_OFFSET);
316312

317-
switch (cfg->parity) {
313+
switch (data->uart_cfg.parity) {
318314
case UART_PARITY_NONE:
319315
tx_cfg &= ~UART_CR_UTX_PRT_EN;
320316
rx_cfg &= ~UART_CR_URX_PRT_EN;
@@ -337,16 +333,16 @@ static int uart_bflb_configure(const struct device *dev)
337333

338334
/* Configure data bits */
339335
tx_cfg &= ~UART_CR_UTX_BIT_CNT_D_MASK;
340-
tx_cfg |= (cfg->data_bits + 4) << UART_CR_UTX_BIT_CNT_D_SHIFT;
336+
tx_cfg |= (data->uart_cfg.data_bits + 4) << UART_CR_UTX_BIT_CNT_D_SHIFT;
341337
rx_cfg &= ~UART_CR_URX_BIT_CNT_D_MASK;
342-
rx_cfg |= (cfg->data_bits + 4) << UART_CR_URX_BIT_CNT_D_SHIFT;
338+
rx_cfg |= (data->uart_cfg.data_bits + 4) << UART_CR_URX_BIT_CNT_D_SHIFT;
343339

344340
/* Configure tx stop bits */
345341
tx_cfg &= ~UART_CR_UTX_BIT_CNT_P_MASK;
346-
tx_cfg |= cfg->stop_bits << UART_CR_UTX_BIT_CNT_P_SHIFT;
342+
tx_cfg |= data->uart_cfg.stop_bits << UART_CR_UTX_BIT_CNT_P_SHIFT;
347343

348344
/* Configure tx cts flow control function */
349-
if (cfg->flow_ctrl & UART_FLOWCTRL_CTS) {
345+
if (data->uart_cfg.flow_ctrl & UART_FLOWCTRL_CTS) {
350346
tx_cfg |= UART_CR_UTX_CTS_EN;
351347
} else {
352348
tx_cfg &= ~UART_CR_UTX_CTS_EN;
@@ -401,6 +397,38 @@ static int uart_bflb_configure(const struct device *dev)
401397
return 0;
402398
}
403399

400+
#ifdef CONFIG_UART_USE_RUNTIME_CONFIGURE
401+
402+
static int uart_bflb_runtime_configure(const struct device *dev,
403+
const struct uart_config *cfg)
404+
{
405+
struct bflb_data *data = dev->data;
406+
int ret;
407+
408+
data->uart_cfg = *cfg;
409+
410+
ret = uart_bflb_configure(dev);
411+
if (ret < 0) {
412+
return ret;
413+
}
414+
415+
uart_bflb_enabled(dev, 1);
416+
417+
return 0;
418+
}
419+
420+
static int uart_bflb_runtime_config_get(const struct device *dev,
421+
struct uart_config *cfg)
422+
{
423+
struct bflb_data *data = dev->data;
424+
425+
*cfg = data->uart_cfg;
426+
427+
return 0;
428+
}
429+
430+
#endif /* CONFIG_UART_USE_RUNTIME_CONFIGURE */
431+
404432
static int uart_bflb_init(const struct device *dev)
405433
{
406434
const struct bflb_config *cfg = dev->config;
@@ -519,6 +547,10 @@ static int uart_bflb_pm_control(const struct device *dev,
519547
static DEVICE_API(uart, uart_bflb_driver_api) = {
520548
.poll_in = uart_bflb_poll_in,
521549
.poll_out = uart_bflb_poll_out,
550+
#ifdef CONFIG_UART_USE_RUNTIME_CONFIGURE
551+
.configure = uart_bflb_runtime_configure,
552+
.config_get = uart_bflb_runtime_config_get,
553+
#endif
522554
#ifdef CONFIG_UART_INTERRUPT_DRIVEN
523555
.err_check = uart_bflb_err_check,
524556
.fifo_fill = uart_bflb_fifo_fill,
@@ -564,18 +596,22 @@ static DEVICE_API(uart, uart_bflb_driver_api) = {
564596
PINCTRL_DT_INST_DEFINE(instance); \
565597
PM_DEVICE_DT_INST_DEFINE(instance, uart_bflb_pm_control); \
566598
BFLB_UART_IRQ_HANDLER_DECL(instance) \
567-
static struct bflb_data uart##instance##_bflb_data; \
599+
static struct bflb_data uart##instance##_bflb_data = { \
600+
.uart_cfg = { \
601+
.baudrate = DT_INST_PROP(instance, current_speed), \
602+
.parity = UART_CFG_PARITY_NONE, \
603+
.stop_bits = UART_CFG_STOP_BITS_1, \
604+
.data_bits = UART_CFG_DATA_BITS_8, \
605+
.flow_ctrl = DT_INST_PROP(instance, hw_flow_control) \
606+
? UART_CFG_FLOW_CTRL_RTS_CTS \
607+
: UART_CFG_FLOW_CTRL_NONE, \
608+
}, \
609+
}; \
568610
static const struct bflb_config uart##instance##_bflb_config = { \
569611
.pincfg = PINCTRL_DT_INST_DEV_CONFIG_GET(instance), \
570612
.base_reg = DT_INST_REG_ADDR(instance), \
571613
\
572-
.baudrate = DT_INST_PROP(instance, current_speed), \
573-
.data_bits = UART_DATA_BITS_8, \
574-
.stop_bits = UART_STOP_BITS_1, \
575-
.parity = UART_PARITY_NONE, \
576614
.bit_order = UART_MSB_FIRST, \
577-
.flow_ctrl = UART_FLOWCTRL_NONE, \
578-
/* overflow interrupt threshold, size is 32 bytes*/ \
579615
.tx_fifo_threshold = 8, \
580616
.rx_fifo_threshold = 0, \
581617
BFLB_UART_IRQ_HANDLER_FUNC(instance) \

0 commit comments

Comments
 (0)