Skip to content

Commit e3d3990

Browse files
committed
Fix UART bugs
1 parent ce97a2a commit e3d3990

File tree

1 file changed

+30
-16
lines changed

1 file changed

+30
-16
lines changed

k210-freertos/mpy_support/standard_lib/machine/machine_uart.c

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,14 @@ typedef struct _task_params_t {
5959
static const char *_parity_name[] = {"None", "Odd", "Even"};
6060
static const char *_stopbits_name[] = {"1", "1.5", "2"};
6161

62-
volatile uart_t* const uart[3] =
62+
volatile uart_t* const uart[UART_NUM_MAX] =
6363
{
6464
(volatile uart_t*)UART1_BASE_ADDR,
6565
(volatile uart_t*)UART2_BASE_ADDR,
6666
(volatile uart_t*)UART3_BASE_ADDR
6767
};
6868

69-
static const uint32_t uart_instances[3] = { 0, 1, 2 };
69+
static const uint32_t uart_instances[UART_NUM_MAX] = { 0, 1, 2 };
7070

7171
static const char *TAG = "[UART]";
7272

@@ -348,7 +348,7 @@ int uart_putc(uint32_t uart_num, char c)
348348
//while ((uart[uart_num]->LSR & (1u << 5)))
349349
// ;
350350
while (!(uart[uart_num]->LSR & (1u << 6)))
351-
;
351+
vTaskDelay(1);
352352
uart[uart_num]->THR = c;
353353
return 0;
354354
}
@@ -361,7 +361,7 @@ int uart_write(uint32_t uart_num, const uint8_t *buff, size_t len)
361361
//while ((uart[uart_num]->LSR & (1u << 5)))
362362
// ;
363363
while (!(uart[uart_num]->LSR & (1u << 6)))
364-
;
364+
vTaskDelay(1);
365365
uart[uart_num]->THR = *buff++;
366366
write++;
367367
}
@@ -469,13 +469,18 @@ int uart_hard_init(uint32_t uart_num, uint8_t tx, int8_t rx, gpio_pin_func_t fun
469469
/* If tx == rx, special handling is used
470470
* tx pin is initialized as INPUT and switched to OUTPUT only when sending
471471
*/
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;
479484
}
480485

481486
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)
549554
tmo--;
550555
}
551556
if (mpy_uarts[uart_num].task_id != NULL) {
557+
LOGE(TAG, "UART deinit timeout");
558+
vTaskDelay(100 / portTICK_PERIOD_MS);
552559
return false;
553560
}
554561
}
@@ -659,10 +666,16 @@ static void uart_event_task(void *pvParameters)
659666
}
660667

661668
// 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+
}
662677
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);
666679
vTaskDelete(NULL);
667680
}
668681

@@ -912,8 +925,8 @@ STATIC void machine_uart_init_helper(machine_uart_obj_t *self, size_t n_args, co
912925

913926
if ((self->tx != args[ARG_tx].u_int) || (self->rx != args[ARG_rx].u_int)) {
914927
// 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;
917930
/*
918931
if (mp_used_pins[self->tx].func != GPIO_FUNC_NONE) {
919932
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,
10271040
if (res != pdPASS) {
10281041
LOGE("UART", "Event task not started");
10291042
}
1043+
vTaskDelay(10 / portTICK_PERIOD_MS); // TS added
10301044
}
10311045
return MP_OBJ_FROM_PTR(self);
10321046
}

0 commit comments

Comments
 (0)