diff --git a/targets/TARGET_NUVOTON/TARGET_M2351/serial_api.c b/targets/TARGET_NUVOTON/TARGET_M2351/serial_api.c index 348be9572d..d6ad1afdda 100644 --- a/targets/TARGET_NUVOTON/TARGET_M2351/serial_api.c +++ b/targets/TARGET_NUVOTON/TARGET_M2351/serial_api.c @@ -250,10 +250,12 @@ void serial_init(serial_t *obj, PinName tx, PinName rx) obj->serial.dma_chn_id_rx = DMA_ERROR_OUT_OF_CHANNELS; #endif - // For stdio management - if (obj->serial.uart == STDIO_UART) { + /* With support for checking H/W UART initialized or not, we allow serial_init(&stdio_uart) + * calls in even though H/W UART 'STDIO_UART' has initialized. When serial_init(&stdio_uart) + * calls in, we only need to set the 'stdio_uart_inited' flag. */ + if (((uintptr_t) obj) == ((uintptr_t) &stdio_uart)) { + MBED_ASSERT(obj->serial.uart == STDIO_UART); stdio_uart_inited = 1; - memcpy(&stdio_uart, obj, sizeof(serial_t)); } if (var->ref_cnt) { @@ -302,7 +304,9 @@ void serial_free(serial_t *obj) var->obj = NULL; } - if (obj->serial.uart == STDIO_UART) { + /* Clear the 'stdio_uart_inited' flag when serial_free(&stdio_uart) calls in. */ + if (((uintptr_t) obj) == ((uintptr_t) &stdio_uart)) { + MBED_ASSERT(obj->serial.uart == STDIO_UART); stdio_uart_inited = 0; } diff --git a/targets/TARGET_NUVOTON/TARGET_M451/serial_api.c b/targets/TARGET_NUVOTON/TARGET_M451/serial_api.c index 6c50657ae0..ec557a43e6 100644 --- a/targets/TARGET_NUVOTON/TARGET_M451/serial_api.c +++ b/targets/TARGET_NUVOTON/TARGET_M451/serial_api.c @@ -210,12 +210,14 @@ void serial_init(serial_t *obj, PinName tx, PinName rx) obj->serial.dma_chn_id_rx = DMA_ERROR_OUT_OF_CHANNELS; #endif - // For stdio management - if (obj->serial.uart == STDIO_UART) { + /* With support for checking H/W UART initialized or not, we allow serial_init(&stdio_uart) + * calls in even though H/W UART 'STDIO_UART' has initialized. When serial_init(&stdio_uart) + * calls in, we only need to set the 'stdio_uart_inited' flag. */ + if (((uintptr_t) obj) == ((uintptr_t) &stdio_uart)) { + MBED_ASSERT(obj->serial.uart == STDIO_UART); stdio_uart_inited = 1; - memcpy(&stdio_uart, obj, sizeof(serial_t)); } - + if (var->ref_cnt) { // Mark this module to be inited. int i = modinit - uart_modinit_tab; @@ -256,11 +258,13 @@ void serial_free(serial_t *obj) if (var->obj == obj) { var->obj = NULL; } - - if (obj->serial.uart == STDIO_UART) { + + /* Clear the 'stdio_uart_inited' flag when serial_free(&stdio_uart) calls in. */ + if (((uintptr_t) obj) == ((uintptr_t) &stdio_uart)) { + MBED_ASSERT(obj->serial.uart == STDIO_UART); stdio_uart_inited = 0; } - + if (! var->ref_cnt) { // Mark this module to be deinited. int i = modinit - uart_modinit_tab; diff --git a/targets/TARGET_NUVOTON/TARGET_M480/serial_api.c b/targets/TARGET_NUVOTON/TARGET_M480/serial_api.c index 8663484f40..356ebcb599 100644 --- a/targets/TARGET_NUVOTON/TARGET_M480/serial_api.c +++ b/targets/TARGET_NUVOTON/TARGET_M480/serial_api.c @@ -240,10 +240,12 @@ void serial_init(serial_t *obj, PinName tx, PinName rx) obj->serial.dma_chn_id_rx = DMA_ERROR_OUT_OF_CHANNELS; #endif - // For stdio management - if (obj->serial.uart == STDIO_UART) { + /* With support for checking H/W UART initialized or not, we allow serial_init(&stdio_uart) + * calls in even though H/W UART 'STDIO_UART' has initialized. When serial_init(&stdio_uart) + * calls in, we only need to set the 'stdio_uart_inited' flag. */ + if (((uintptr_t) obj) == ((uintptr_t) &stdio_uart)) { + MBED_ASSERT(obj->serial.uart == STDIO_UART); stdio_uart_inited = 1; - memcpy(&stdio_uart, obj, sizeof(serial_t)); } if (var->ref_cnt) { @@ -289,7 +291,9 @@ void serial_free(serial_t *obj) var->obj = NULL; } - if (obj->serial.uart == STDIO_UART) { + /* Clear the 'stdio_uart_inited' flag when serial_free(&stdio_uart) calls in. */ + if (((uintptr_t) obj) == ((uintptr_t) &stdio_uart)) { + MBED_ASSERT(obj->serial.uart == STDIO_UART); stdio_uart_inited = 0; } diff --git a/targets/TARGET_NUVOTON/TARGET_NANO100/serial_api.c b/targets/TARGET_NUVOTON/TARGET_NANO100/serial_api.c index 1fb741e15e..447d8fb5d8 100644 --- a/targets/TARGET_NUVOTON/TARGET_NANO100/serial_api.c +++ b/targets/TARGET_NUVOTON/TARGET_NANO100/serial_api.c @@ -175,12 +175,14 @@ void serial_init(serial_t *obj, PinName tx, PinName rx) obj->serial.dma_chn_id_rx = DMA_ERROR_OUT_OF_CHANNELS; #endif - // For stdio management - if (obj->serial.uart == STDIO_UART) { + /* With support for checking H/W UART initialized or not, we allow serial_init(&stdio_uart) + * calls in even though H/W UART 'STDIO_UART' has initialized. When serial_init(&stdio_uart) + * calls in, we only need to set the 'stdio_uart_inited' flag. */ + if (((uintptr_t) obj) == ((uintptr_t) &stdio_uart)) { + MBED_ASSERT(obj->serial.uart == STDIO_UART); stdio_uart_inited = 1; - memcpy(&stdio_uart, obj, sizeof(serial_t)); } - + if (var->ref_cnt) { // Mark this module to be inited. int i = modinit - uart_modinit_tab; @@ -221,11 +223,13 @@ void serial_free(serial_t *obj) if (var->obj == obj) { var->obj = NULL; } - - if (obj->serial.uart == STDIO_UART) { + + /* Clear the 'stdio_uart_inited' flag when serial_free(&stdio_uart) calls in. */ + if (((uintptr_t) obj) == ((uintptr_t) &stdio_uart)) { + MBED_ASSERT(obj->serial.uart == STDIO_UART); stdio_uart_inited = 0; } - + if (! var->ref_cnt) { // Mark this module to be deinited. int i = modinit - uart_modinit_tab; diff --git a/targets/TARGET_NUVOTON/TARGET_NUC472/serial_api.c b/targets/TARGET_NUVOTON/TARGET_NUC472/serial_api.c index eeeca799ae..002f23ef48 100644 --- a/targets/TARGET_NUVOTON/TARGET_NUC472/serial_api.c +++ b/targets/TARGET_NUVOTON/TARGET_NUC472/serial_api.c @@ -240,12 +240,14 @@ void serial_init(serial_t *obj, PinName tx, PinName rx) obj->serial.dma_chn_id_rx = DMA_ERROR_OUT_OF_CHANNELS; #endif - // For stdio management - if (obj->serial.uart == STDIO_UART) { + /* With support for checking H/W UART initialized or not, we allow serial_init(&stdio_uart) + * calls in even though H/W UART 'STDIO_UART' has initialized. When serial_init(&stdio_uart) + * calls in, we only need to set the 'stdio_uart_inited' flag. */ + if (((uintptr_t) obj) == ((uintptr_t) &stdio_uart)) { + MBED_ASSERT(obj->serial.uart == STDIO_UART); stdio_uart_inited = 1; - memcpy(&stdio_uart, obj, sizeof(serial_t)); } - + if (var->ref_cnt) { // Mark this module to be inited. int i = modinit - uart_modinit_tab; @@ -286,11 +288,13 @@ void serial_free(serial_t *obj) if (var->obj == obj) { var->obj = NULL; } - - if (obj->serial.uart == STDIO_UART) { + + /* Clear the 'stdio_uart_inited' flag when serial_free(&stdio_uart) calls in. */ + if (((uintptr_t) obj) == ((uintptr_t) &stdio_uart)) { + MBED_ASSERT(obj->serial.uart == STDIO_UART); stdio_uart_inited = 0; } - + if (! var->ref_cnt) { // Mark this module to be deinited. int i = modinit - uart_modinit_tab;