Skip to content

Commit

Permalink
mimxrt: Fix the UART.deinit() and uart_deinit_all() behaviour.
Browse files Browse the repository at this point in the history
The code did not check at deinit whether a UART was initialized.
That did not matter for all MCU's except MIMXRT1176, which crashes
at the second soft reset in a row.

But since it is a general problem to use UART methods of a UART
which has been deinitialized, checks were added to all methods for a
clear response instead of e.g. a crash.

Signed-off-by: robert-hh <robert@hammelrath.com>
  • Loading branch information
robert-hh committed Aug 16, 2023
1 parent a18d62e commit 50572b0
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions ports/mimxrt/machine_uart.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ static const iomux_table_t iomux_table_uart[] = {
static const iomux_table_t iomux_table_uart_cts_rts[] = {
IOMUX_TABLE_UART_CTS_RTS
};
static bool uart_active[MICROPY_HW_UART_NUM];

STATIC const char *_parity_name[] = {"None", "", "0", "1"}; // Is defined as 0, 2, 3
STATIC const char *_invert_name[] = {"None", "INV_TX", "INV_RX", "INV_TX|INV_RX"};
Expand Down Expand Up @@ -306,6 +307,7 @@ STATIC mp_obj_t machine_uart_init_helper(machine_uart_obj_t *self, size_t n_args
self->lpuart->STAT |= 1 << LPUART_STAT_BRK13_SHIFT;
LPUART_EnableTx(self->lpuart, true);
}
uart_active[self->id] = true;

return MP_OBJ_FROM_PTR(self);
}
Expand Down Expand Up @@ -351,7 +353,10 @@ MP_DEFINE_CONST_FUN_OBJ_KW(machine_uart_init_obj, 1, machine_uart_init);
// uart.deinit()
STATIC mp_obj_t machine_uart_deinit(mp_obj_t self_in) {
machine_uart_obj_t *self = MP_OBJ_TO_PTR(self_in);
LPUART_Deinit(self->lpuart);
if (uart_active[self->id]) {
LPUART_Deinit(self->lpuart);
}
uart_active[self->id] = false;
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_uart_deinit_obj, machine_uart_deinit);
Expand Down Expand Up @@ -384,9 +389,10 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_uart_txdone_obj, machine_uart_txdone);

// Deinitialize all defined UARTs
void machine_uart_deinit_all(void) {
for (int i = 0; i < sizeof(uart_index_table); i++) {
if (uart_index_table[i] != 0) {
for (int i = 0; i < MICROPY_HW_UART_NUM; i++) {
if (uart_index_table[i] != 0 && uart_active[i] == true) {
LPUART_Deinit(uart_base_ptr_table[uart_index_table[i]]);
uart_active[i] = false;
}
}
}
Expand Down

0 comments on commit 50572b0

Please sign in to comment.