59
59
#define UART_IRQ_BREAK (1 << UART_BREAK)
60
60
#define MP_UART_ALLOWED_FLAGS (UART_IRQ_RX | UART_IRQ_RXIDLE | UART_IRQ_BREAK)
61
61
#define RXIDLE_TIMER_MIN (5000) // 500 us
62
+ #define UART_QUEUE_SIZE (3)
62
63
63
64
enum {
64
65
RXIDLE_INACTIVE ,
@@ -174,6 +175,13 @@ static void uart_event_task(void *self_in) {
174
175
}
175
176
}
176
177
178
+ static inline void uart_event_task_create (machine_uart_obj_t * self ) {
179
+ if (xTaskCreatePinnedToCore (uart_event_task , "uart_event_task" , 2048 , self ,
180
+ ESP_TASKD_EVENT_PRIO , (TaskHandle_t * )& self -> uart_event_task , MP_TASK_COREID ) != pdPASS ) {
181
+ mp_raise_msg (& mp_type_RuntimeError , MP_ERROR_TEXT ("failed to create UART event task" ));
182
+ }
183
+ }
184
+
177
185
static void mp_machine_uart_print (const mp_print_t * print , mp_obj_t self_in , mp_print_kind_t kind ) {
178
186
machine_uart_obj_t * self = MP_OBJ_TO_PTR (self_in );
179
187
uint32_t baudrate ;
@@ -250,7 +258,7 @@ static void mp_machine_uart_init_helper(machine_uart_obj_t *self, size_t n_args,
250
258
// wait for all data to be transmitted before changing settings
251
259
uart_wait_tx_done (self -> uart_num , pdMS_TO_TICKS (1000 ));
252
260
253
- if (args [ARG_txbuf ].u_int >= 0 || args [ARG_rxbuf ].u_int >= 0 ) {
261
+ if (( args [ARG_txbuf ].u_int >= 0 && args [ ARG_txbuf ]. u_int != self -> txbuf ) || ( args [ARG_rxbuf ].u_int >= 0 && args [ ARG_rxbuf ]. u_int != self -> rxbuf ) ) {
254
262
// must reinitialise driver to change the tx/rx buffer size
255
263
#if MICROPY_HW_ENABLE_UART_REPL
256
264
if (self -> uart_num == MICROPY_HW_UART_REPL ) {
@@ -275,9 +283,12 @@ static void mp_machine_uart_init_helper(machine_uart_obj_t *self, size_t n_args,
275
283
check_esp_err (uart_get_word_length (self -> uart_num , & uartcfg .data_bits ));
276
284
check_esp_err (uart_get_parity (self -> uart_num , & uartcfg .parity ));
277
285
check_esp_err (uart_get_stop_bits (self -> uart_num , & uartcfg .stop_bits ));
278
- check_esp_err ( uart_driver_delete ( self -> uart_num ) );
286
+ mp_machine_uart_deinit ( self );
279
287
check_esp_err (uart_param_config (self -> uart_num , & uartcfg ));
280
- check_esp_err (uart_driver_install (self -> uart_num , self -> rxbuf , self -> txbuf , 0 , NULL , 0 ));
288
+ check_esp_err (uart_driver_install (self -> uart_num , self -> rxbuf , self -> txbuf , UART_QUEUE_SIZE , & self -> uart_queue , 0 ));
289
+ if (self -> mp_irq_obj != NULL && self -> mp_irq_obj -> handler != mp_const_none ) {
290
+ uart_event_task_create (self );
291
+ }
281
292
}
282
293
283
294
// set baudrate
@@ -437,7 +448,8 @@ static mp_obj_t mp_machine_uart_make_new(const mp_obj_type_t *type, size_t n_arg
437
448
self -> timeout_char = 0 ;
438
449
self -> invert = 0 ;
439
450
self -> flowcontrol = 0 ;
440
- self -> uart_event_task = 0 ;
451
+ self -> uart_event_task = NULL ;
452
+ self -> uart_queue = NULL ;
441
453
self -> rxidle_state = RXIDLE_INACTIVE ;
442
454
443
455
switch (uart_num ) {
@@ -470,12 +482,13 @@ static mp_obj_t mp_machine_uart_make_new(const mp_obj_type_t *type, size_t n_arg
470
482
{
471
483
// Remove any existing configuration
472
484
check_esp_err (uart_driver_delete (self -> uart_num ));
485
+ self -> uart_queue = NULL ;
473
486
474
487
// init the peripheral
475
488
// Setup
476
489
check_esp_err (uart_param_config (self -> uart_num , & uartcfg ));
477
490
478
- check_esp_err (uart_driver_install (uart_num , self -> rxbuf , self -> txbuf , 3 , & self -> uart_queue , 0 ));
491
+ check_esp_err (uart_driver_install (uart_num , self -> rxbuf , self -> txbuf , UART_QUEUE_SIZE , & self -> uart_queue , 0 ));
479
492
}
480
493
481
494
mp_map_t kw_args ;
@@ -489,7 +502,12 @@ static mp_obj_t mp_machine_uart_make_new(const mp_obj_type_t *type, size_t n_arg
489
502
}
490
503
491
504
static void mp_machine_uart_deinit (machine_uart_obj_t * self ) {
505
+ if (self -> uart_event_task != NULL ) {
506
+ vTaskDelete (self -> uart_event_task );
507
+ self -> uart_event_task = NULL ;
508
+ }
492
509
check_esp_err (uart_driver_delete (self -> uart_num ));
510
+ self -> uart_queue = NULL ;
493
511
}
494
512
495
513
static mp_int_t mp_machine_uart_any (machine_uart_obj_t * self ) {
@@ -568,6 +586,12 @@ static const mp_irq_methods_t uart_irq_methods = {
568
586
};
569
587
570
588
static mp_irq_obj_t * mp_machine_uart_irq (machine_uart_obj_t * self , bool any_args , mp_arg_val_t * args ) {
589
+ #if MICROPY_HW_ENABLE_UART_REPL
590
+ if (self -> uart_num == MICROPY_HW_UART_REPL ) {
591
+ mp_raise_ValueError (MP_ERROR_TEXT ("UART does not support IRQs" ));
592
+ }
593
+ #endif
594
+
571
595
if (self -> mp_irq_obj == NULL ) {
572
596
self -> mp_irq_trigger = 0 ;
573
597
self -> mp_irq_obj = mp_irq_new (& uart_irq_methods , MP_OBJ_FROM_PTR (self ));
@@ -597,9 +621,8 @@ static mp_irq_obj_t *mp_machine_uart_irq(machine_uart_obj_t *self, bool any_args
597
621
uart_irq_configure_timer (self , trigger );
598
622
599
623
// Start a task for handling events
600
- if (handler != mp_const_none && self -> uart_event_task == NULL ) {
601
- xTaskCreatePinnedToCore (uart_event_task , "uart_event_task" , 2048 , self ,
602
- ESP_TASKD_EVENT_PRIO , (TaskHandle_t * )& self -> uart_event_task , MP_TASK_COREID );
624
+ if (handler != mp_const_none && self -> uart_event_task == NULL && self -> uart_queue != NULL ) {
625
+ uart_event_task_create (self );
603
626
} else if (handler == mp_const_none && self -> uart_event_task != NULL ) {
604
627
vTaskDelete (self -> uart_event_task );
605
628
self -> uart_event_task = NULL ;
0 commit comments