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,34 @@ 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
+ memcpy (& data -> uart_cfg , cfg , sizeof (data -> uart_cfg ));
409
+ ret = uart_bflb_configure (dev );
410
+ if (ret < 0 ) {
411
+ return ret ;
412
+ }
413
+ uart_bflb_enabled (dev , 1 );
414
+ return 0 ;
415
+ }
416
+
417
+ static int uart_bflb_runtime_config_get (const struct device * dev ,
418
+ struct uart_config * cfg )
419
+ {
420
+ struct bflb_data * data = dev -> data ;
421
+
422
+ * cfg = data -> uart_cfg ;
423
+ return 0 ;
424
+ }
425
+
426
+ #endif /* CONFIG_UART_USE_RUNTIME_CONFIGURE */
427
+
404
428
static int uart_bflb_init (const struct device * dev )
405
429
{
406
430
const struct bflb_config * cfg = dev -> config ;
@@ -516,6 +540,10 @@ static int uart_bflb_pm_control(const struct device *dev,
516
540
static DEVICE_API (uart , uart_bflb_driver_api ) = {
517
541
.poll_in = uart_bflb_poll_in ,
518
542
.poll_out = uart_bflb_poll_out ,
543
+ #ifdef CONFIG_UART_USE_RUNTIME_CONFIGURE
544
+ .configure = uart_bflb_runtime_configure ,
545
+ .config_get = uart_bflb_runtime_config_get ,
546
+ #endif
519
547
#ifdef CONFIG_UART_INTERRUPT_DRIVEN
520
548
.err_check = uart_bflb_err_check ,
521
549
.fifo_fill = uart_bflb_fifo_fill ,
@@ -561,19 +589,23 @@ static DEVICE_API(uart, uart_bflb_driver_api) = {
561
589
PINCTRL_DT_INST_DEFINE(instance); \
562
590
PM_DEVICE_DT_INST_DEFINE(instance, uart_bflb_pm_control); \
563
591
BFLB_UART_IRQ_HANDLER_DECL(instance) \
564
- static struct bflb_data uart##instance##_bflb_data; \
592
+ static struct bflb_data uart##instance##_bflb_data= { \
593
+ .uart_cfg = { \
594
+ .baudrate = DT_INST_PROP(instance, current_speed), \
595
+ .parity = UART_CFG_PARITY_NONE, \
596
+ .stop_bits = UART_CFG_STOP_BITS_1, \
597
+ .data_bits = UART_CFG_DATA_BITS_8, \
598
+ .flow_ctrl = DT_INST_PROP(instance, hw_flow_control) \
599
+ ? UART_CFG_FLOW_CTRL_RTS_CTS \
600
+ : UART_CFG_FLOW_CTRL_NONE, \
601
+ }, \
602
+ }; \
565
603
static const struct bflb_config uart##instance##_bflb_config = { \
566
604
.pincfg = PINCTRL_DT_INST_DEV_CONFIG_GET(instance), \
567
605
.base_reg = DT_INST_REG_ADDR(instance), \
568
606
\
569
- .baudrate = DT_INST_PROP(instance, current_speed), \
570
- .data_bits = UART_DATA_BITS_8, \
571
- .stop_bits = UART_STOP_BITS_1, \
572
- .parity = UART_PARITY_NONE, \
573
607
.bit_order = UART_MSB_FIRST, \
574
- .flow_ctrl = UART_FLOWCTRL_NONE, \
575
- /* overflow interrupt threshold, size is 32 bytes*/ \
576
- .tx_fifo_threshold = 8, \
608
+ .tx_fifo_threshold = 16, \
577
609
.rx_fifo_threshold = 0, \
578
610
BFLB_UART_IRQ_HANDLER_FUNC(instance) \
579
611
}; \
0 commit comments