33
33
34
34
struct bflb_config {
35
35
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 ;
41
36
uint8_t bit_order ;
42
- uint8_t flow_ctrl ;
43
37
uint8_t tx_fifo_threshold ;
44
38
uint8_t rx_fifo_threshold ;
45
39
uint32_t base_reg ;
@@ -49,6 +43,7 @@ struct bflb_config {
49
43
};
50
44
51
45
struct bflb_data {
46
+ struct uart_config uart_cfg ;
52
47
#ifdef CONFIG_UART_INTERRUPT_DRIVEN
53
48
uart_irq_callback_user_data_t user_cb ;
54
49
void * user_data ;
@@ -294,12 +289,13 @@ static void uart_bflb_isr(const struct device *dev)
294
289
static int uart_bflb_configure (const struct device * dev )
295
290
{
296
291
const struct bflb_config * cfg = dev -> config ;
292
+ struct bflb_data * const data = dev -> data ;
297
293
uint32_t tx_cfg = 0 ;
298
294
uint32_t rx_cfg = 0 ;
299
295
uint32_t divider = 0 ;
300
296
uint32_t tmp = 0 ;
301
297
302
- divider = (uart_bflb_get_clock () * 10 / cfg -> baudrate + 5 ) / 10 ;
298
+ divider = (uart_bflb_get_clock () * 10 / data -> uart_cfg . baudrate + 5 ) / 10 ;
303
299
if (divider >= 0xFFFF ) {
304
300
divider = 0xFFFF - 1 ;
305
301
}
@@ -314,7 +310,7 @@ static int uart_bflb_configure(const struct device *dev)
314
310
tx_cfg = sys_read32 (cfg -> base_reg + UART_UTX_CONFIG_OFFSET );
315
311
rx_cfg = sys_read32 (cfg -> base_reg + UART_URX_CONFIG_OFFSET );
316
312
317
- switch (cfg -> parity ) {
313
+ switch (data -> uart_cfg . parity ) {
318
314
case UART_PARITY_NONE :
319
315
tx_cfg &= ~UART_CR_UTX_PRT_EN ;
320
316
rx_cfg &= ~UART_CR_URX_PRT_EN ;
@@ -337,16 +333,16 @@ static int uart_bflb_configure(const struct device *dev)
337
333
338
334
/* Configure data bits */
339
335
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 ;
341
337
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 ;
343
339
344
340
/* Configure tx stop bits */
345
341
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 ;
347
343
348
344
/* Configure tx cts flow control function */
349
- if (cfg -> flow_ctrl & UART_FLOWCTRL_CTS ) {
345
+ if (data -> uart_cfg . flow_ctrl & UART_FLOWCTRL_CTS ) {
350
346
tx_cfg |= UART_CR_UTX_CTS_EN ;
351
347
} else {
352
348
tx_cfg &= ~UART_CR_UTX_CTS_EN ;
@@ -401,6 +397,38 @@ static int uart_bflb_configure(const struct device *dev)
401
397
return 0 ;
402
398
}
403
399
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
+
404
432
static int uart_bflb_init (const struct device * dev )
405
433
{
406
434
const struct bflb_config * cfg = dev -> config ;
@@ -519,6 +547,10 @@ static int uart_bflb_pm_control(const struct device *dev,
519
547
static DEVICE_API (uart , uart_bflb_driver_api ) = {
520
548
.poll_in = uart_bflb_poll_in ,
521
549
.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
522
554
#ifdef CONFIG_UART_INTERRUPT_DRIVEN
523
555
.err_check = uart_bflb_err_check ,
524
556
.fifo_fill = uart_bflb_fifo_fill ,
@@ -564,18 +596,22 @@ static DEVICE_API(uart, uart_bflb_driver_api) = {
564
596
PINCTRL_DT_INST_DEFINE(instance); \
565
597
PM_DEVICE_DT_INST_DEFINE(instance, uart_bflb_pm_control); \
566
598
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
+ }; \
568
610
static const struct bflb_config uart##instance##_bflb_config = { \
569
611
.pincfg = PINCTRL_DT_INST_DEV_CONFIG_GET(instance), \
570
612
.base_reg = DT_INST_REG_ADDR(instance), \
571
613
\
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, \
576
614
.bit_order = UART_MSB_FIRST, \
577
- .flow_ctrl = UART_FLOWCTRL_NONE, \
578
- /* overflow interrupt threshold, size is 32 bytes*/ \
579
615
.tx_fifo_threshold = 8, \
580
616
.rx_fifo_threshold = 0, \
581
617
BFLB_UART_IRQ_HANDLER_FUNC(instance) \
0 commit comments