@@ -59,14 +59,14 @@ typedef struct _task_params_t {
59
59
static const char * _parity_name [] = {"None" , "Odd" , "Even" };
60
60
static const char * _stopbits_name [] = {"1" , "1.5" , "2" };
61
61
62
- volatile uart_t * const uart [3 ] =
62
+ volatile uart_t * const uart [UART_NUM_MAX ] =
63
63
{
64
64
(volatile uart_t * )UART1_BASE_ADDR ,
65
65
(volatile uart_t * )UART2_BASE_ADDR ,
66
66
(volatile uart_t * )UART3_BASE_ADDR
67
67
};
68
68
69
- static const uint32_t uart_instances [3 ] = { 0 , 1 , 2 };
69
+ static const uint32_t uart_instances [UART_NUM_MAX ] = { 0 , 1 , 2 };
70
70
71
71
static const char * TAG = "[UART]" ;
72
72
@@ -348,7 +348,7 @@ int uart_putc(uint32_t uart_num, char c)
348
348
//while ((uart[uart_num]->LSR & (1u << 5)))
349
349
// ;
350
350
while (!(uart [uart_num ]-> LSR & (1u << 6 )))
351
- ;
351
+ vTaskDelay ( 1 ) ;
352
352
uart [uart_num ]-> THR = c ;
353
353
return 0 ;
354
354
}
@@ -361,7 +361,7 @@ int uart_write(uint32_t uart_num, const uint8_t *buff, size_t len)
361
361
//while ((uart[uart_num]->LSR & (1u << 5)))
362
362
// ;
363
363
while (!(uart [uart_num ]-> LSR & (1u << 6 )))
364
- ;
364
+ vTaskDelay ( 1 ) ;
365
365
uart [uart_num ]-> THR = * buff ++ ;
366
366
write ++ ;
367
367
}
@@ -469,13 +469,18 @@ int uart_hard_init(uint32_t uart_num, uint8_t tx, int8_t rx, gpio_pin_func_t fun
469
469
/* If tx == rx, special handling is used
470
470
* tx pin is initialized as INPUT and switched to OUTPUT only when sending
471
471
*/
472
- if (mp_used_pins [tx ].func != GPIO_FUNC_NONE ) {
473
- LOGD (TAG , "Tx %s" , gpiohs_funcs_in_use [mp_used_pins [tx ].func ]);
474
- return -1 ;
475
- }
476
- if (mp_used_pins [rx ].func != GPIO_FUNC_NONE ) {
477
- LOGD (TAG , "Rx %s" , gpiohs_funcs_in_use [mp_used_pins [rx ].func ]);
478
- return -2 ;
472
+ // TS. fix the 2nd time .init calling
473
+ static int first_time = 1 ;
474
+ if (first_time ) {
475
+ if (mp_used_pins [tx ].func != GPIO_FUNC_NONE ) {
476
+ LOGD (TAG , "Tx %s" , gpiohs_funcs_in_use [mp_used_pins [tx ].func ]);
477
+ return -1 ;
478
+ }
479
+ if (mp_used_pins [rx ].func != GPIO_FUNC_NONE ) {
480
+ LOGD (TAG , "Rx %s" , gpiohs_funcs_in_use [mp_used_pins [rx ].func ]);
481
+ return -2 ;
482
+ }
483
+ first_time = 0 ;
479
484
}
480
485
481
486
if (mpy_uarts [uart_num ].uart_buf == NULL ) {
@@ -549,6 +554,8 @@ bool uart_deinit(uint32_t uart_num, uint8_t *end_task, uint8_t tx, uint8_t rx)
549
554
tmo -- ;
550
555
}
551
556
if (mpy_uarts [uart_num ].task_id != NULL ) {
557
+ LOGE (TAG , "UART deinit timeout" );
558
+ vTaskDelay (100 / portTICK_PERIOD_MS );
552
559
return false;
553
560
}
554
561
}
@@ -659,10 +666,16 @@ static void uart_event_task(void *pvParameters)
659
666
}
660
667
661
668
// Terminate task
669
+ if (xSemaphoreTake (mpy_uarts [self -> uart_num ].uart_mutex , UART_MUTEX_TIMEOUT )) {
670
+ mpy_uarts [self -> uart_num ].task_id = NULL ;
671
+ xSemaphoreGive (mpy_uarts [self -> uart_num ].uart_mutex );
672
+ }
673
+ else {
674
+ mpy_uarts [self -> uart_num ].task_id = NULL ;
675
+ LOGE (TAG , "Can not take a semaphore!" );
676
+ }
662
677
LOGD (TAG , "UART task terminated" );
663
- xSemaphoreTake (mpy_uarts [self -> uart_num ].uart_mutex , UART_MUTEX_TIMEOUT );
664
- mpy_uarts [self -> uart_num ].task_id = NULL ;
665
- xSemaphoreGive (mpy_uarts [self -> uart_num ].uart_mutex );
678
+ vTaskDelay (100 / portTICK_PERIOD_MS );
666
679
vTaskDelete (NULL );
667
680
}
668
681
@@ -912,8 +925,8 @@ STATIC void machine_uart_init_helper(machine_uart_obj_t *self, size_t n_args, co
912
925
913
926
if ((self -> tx != args [ARG_tx ].u_int ) || (self -> rx != args [ARG_rx ].u_int )) {
914
927
// Tx or Rx changed
915
- self -> tx = args [ARG_tx ].u_int ;
916
- self -> rx = args [ARG_rx ].u_int ;
928
+ if ( args [ ARG_tx ]. u_int != UART_PIN_NO_CHANGE ) self -> tx = args [ARG_tx ].u_int ;
929
+ if ( args [ ARG_rx ]. u_int != UART_PIN_NO_CHANGE ) self -> rx = args [ARG_rx ].u_int ;
917
930
/*
918
931
if (mp_used_pins[self->tx].func != GPIO_FUNC_NONE) {
919
932
mp_printf(&mp_plat_print, "Tx pin\r\n");
@@ -1027,6 +1040,7 @@ STATIC mp_obj_t machine_uart_make_new(const mp_obj_type_t *type, size_t n_args,
1027
1040
if (res != pdPASS ) {
1028
1041
LOGE ("UART" , "Event task not started" );
1029
1042
}
1043
+ vTaskDelay (10 / portTICK_PERIOD_MS ); // TS added
1030
1044
}
1031
1045
return MP_OBJ_FROM_PTR (self );
1032
1046
}
0 commit comments