@@ -37,14 +37,26 @@ static void modbus_serial_tx_on(struct modbus_context *ctx)
37
37
gpio_pin_set (cfg -> de -> port , cfg -> de -> pin , 1 );
38
38
}
39
39
40
- uart_irq_tx_enable (cfg -> dev );
40
+ if (IS_ENABLED (CONFIG_MODBUS_SERIAL_ASYNC_API )) {
41
+ int err ;
42
+
43
+ err = uart_tx (cfg -> dev , cfg -> uart_buf , cfg -> uart_buf_ctr , SYS_FOREVER_US );
44
+ if (err ) {
45
+ LOG_ERR ("uart_tx failed with %d" , err );
46
+ }
47
+ } else {
48
+ uart_irq_tx_enable (cfg -> dev );
49
+ }
41
50
}
42
51
43
52
static void modbus_serial_tx_off (struct modbus_context * ctx )
44
53
{
45
54
struct modbus_serial_config * cfg = ctx -> cfg ;
46
55
47
- uart_irq_tx_disable (cfg -> dev );
56
+ if (!IS_ENABLED (CONFIG_MODBUS_SERIAL_ASYNC_API )) {
57
+ uart_irq_tx_disable (cfg -> dev );
58
+ }
59
+
48
60
if (cfg -> de != NULL ) {
49
61
gpio_pin_set (cfg -> de -> port , cfg -> de -> pin , 0 );
50
62
}
@@ -70,14 +82,29 @@ static void modbus_serial_rx_on(struct modbus_context *ctx)
70
82
}
71
83
72
84
atomic_set_bit (& ctx -> state , MODBUS_STATE_RX_ENABLED );
73
- uart_irq_rx_enable (cfg -> dev );
85
+ if (IS_ENABLED (CONFIG_MODBUS_SERIAL_ASYNC_API )) {
86
+ int err ;
87
+
88
+ err = uart_rx_enable (cfg -> dev , cfg -> uart_buf ,
89
+ sizeof (cfg -> uart_buf ), cfg -> rtu_timeout );
90
+ if (err ) {
91
+ LOG_ERR ("uart_rx_enable failed with %d" , err );
92
+ }
93
+ } else {
94
+ uart_irq_rx_enable (cfg -> dev );
95
+ }
74
96
}
75
97
76
98
static void modbus_serial_rx_off (struct modbus_context * ctx )
77
99
{
78
100
struct modbus_serial_config * cfg = ctx -> cfg ;
79
101
80
- uart_irq_rx_disable (cfg -> dev );
102
+ if (IS_ENABLED (CONFIG_MODBUS_SERIAL_ASYNC_API )) {
103
+ uart_rx_disable (cfg -> dev );
104
+ } else {
105
+ uart_irq_rx_disable (cfg -> dev );
106
+ }
107
+
81
108
atomic_clear_bit (& ctx -> state , MODBUS_STATE_RX_ENABLED );
82
109
83
110
if (cfg -> re != NULL ) {
@@ -424,6 +451,44 @@ static void uart_cb_handler(const struct device *dev, void *app_data)
424
451
}
425
452
}
426
453
454
+ static void uart_cb_async_handler (const struct device * dev , struct uart_event * evt , void * app_data )
455
+ {
456
+ struct modbus_context * ctx = (struct modbus_context * )app_data ;
457
+ struct modbus_serial_config * cfg ;
458
+
459
+ if (ctx == NULL ) {
460
+ LOG_ERR ("Modbus hardware is not properly initialized" );
461
+ return ;
462
+ }
463
+
464
+ cfg = ctx -> cfg ;
465
+
466
+ switch (evt -> type ) {
467
+ case UART_TX_DONE :
468
+ cfg -> uart_buf_ctr = 0 ;
469
+ modbus_serial_tx_off (ctx );
470
+ modbus_serial_rx_on (ctx );
471
+ break ;
472
+ case UART_RX_RDY :
473
+ cfg -> uart_buf_ctr = evt -> data .rx .len ;
474
+ k_work_submit (& ctx -> server_work );
475
+ break ;
476
+ case UART_TX_ABORTED :
477
+ __fallthrough ;
478
+ case UART_RX_STOPPED :
479
+ __fallthrough ;
480
+ case UART_RX_BUF_REQUEST :
481
+ __fallthrough ;
482
+ case UART_RX_BUF_RELEASED :
483
+ __fallthrough ;
484
+ case UART_RX_DISABLED :
485
+ break ;
486
+ default :
487
+ LOG_WRN ("Unhandled UART event type: %d" , evt -> type );
488
+ break ;
489
+ }
490
+ }
491
+
427
492
/* This function is called when the RTU framing timer expires. */
428
493
static void rtu_tmr_handler (struct k_timer * t_id )
429
494
{
@@ -621,18 +686,20 @@ int modbus_serial_init(struct modbus_context *ctx,
621
686
cfg -> uart_buf_ctr = 0 ;
622
687
cfg -> uart_buf_ptr = & cfg -> uart_buf [0 ];
623
688
624
- err = uart_irq_callback_user_data_set (cfg -> dev , uart_cb_handler , ctx );
625
- if (err != 0 ) {
626
- return err ;
627
- };
628
-
629
- k_timer_init (& cfg -> rtu_timer , rtu_tmr_handler , NULL );
630
- k_timer_user_data_set (& cfg -> rtu_timer , ctx );
689
+ if (IS_ENABLED (CONFIG_MODBUS_SERIAL_ASYNC_API )) {
690
+ err = uart_callback_set (cfg -> dev , uart_cb_async_handler , ctx );
691
+ } else {
692
+ err = uart_irq_callback_user_data_set (cfg -> dev , uart_cb_handler , ctx );
693
+ if (!err ) {
694
+ k_timer_init (& cfg -> rtu_timer , rtu_tmr_handler , NULL );
695
+ k_timer_user_data_set (& cfg -> rtu_timer , ctx );
696
+ modbus_serial_rx_on (ctx );
697
+ }
698
+ }
631
699
632
- modbus_serial_rx_on (ctx );
633
700
LOG_INF ("RTU timeout %u us" , cfg -> rtu_timeout );
634
701
635
- return 0 ;
702
+ return err ;
636
703
}
637
704
638
705
void modbus_serial_disable (struct modbus_context * ctx )
0 commit comments