Skip to content

Commit a2c70f2

Browse files
committed
[Nuvoton] Fix time to init/deinit stdio_uart
With support for checking H/W UART initialized or not, we can simplify stdio management: 1. When serial_init(&stdio_uart) calls in, just set the 'stdio_uart_inited' flag. 2. When serial_free(&stdio_uart) calls in, just clear the 'stdio_uart_inited' flag. Except above, we needn't make special handling with 'stdio_uart'.
1 parent a937a62 commit a2c70f2

File tree

5 files changed

+49
-29
lines changed

5 files changed

+49
-29
lines changed

targets/TARGET_NUVOTON/TARGET_M2351/serial_api.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -250,10 +250,12 @@ void serial_init(serial_t *obj, PinName tx, PinName rx)
250250
obj->serial.dma_chn_id_rx = DMA_ERROR_OUT_OF_CHANNELS;
251251
#endif
252252

253-
// For stdio management
254-
if (obj->serial.uart == STDIO_UART) {
253+
/* With support for checking H/W UART initialized or not, we allow serial_init(&stdio_uart)
254+
* calls in even though H/W UART 'STDIO_UART' has initialized. When serial_init(&stdio_uart)
255+
* calls in, we only need to set the 'stdio_uart_inited' flag. */
256+
if (((uintptr_t) obj) == ((uintptr_t) &stdio_uart)) {
257+
MBED_ASSERT(obj->serial.uart == STDIO_UART);
255258
stdio_uart_inited = 1;
256-
memcpy(&stdio_uart, obj, sizeof(serial_t));
257259
}
258260

259261
if (var->ref_cnt) {
@@ -302,7 +304,9 @@ void serial_free(serial_t *obj)
302304
var->obj = NULL;
303305
}
304306

305-
if (obj->serial.uart == STDIO_UART) {
307+
/* Clear the 'stdio_uart_inited' flag when serial_free(&stdio_uart) calls in. */
308+
if (((uintptr_t) obj) == ((uintptr_t) &stdio_uart)) {
309+
MBED_ASSERT(obj->serial.uart == STDIO_UART);
306310
stdio_uart_inited = 0;
307311
}
308312

targets/TARGET_NUVOTON/TARGET_M451/serial_api.c

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -210,12 +210,14 @@ void serial_init(serial_t *obj, PinName tx, PinName rx)
210210
obj->serial.dma_chn_id_rx = DMA_ERROR_OUT_OF_CHANNELS;
211211
#endif
212212

213-
// For stdio management
214-
if (obj->serial.uart == STDIO_UART) {
213+
/* With support for checking H/W UART initialized or not, we allow serial_init(&stdio_uart)
214+
* calls in even though H/W UART 'STDIO_UART' has initialized. When serial_init(&stdio_uart)
215+
* calls in, we only need to set the 'stdio_uart_inited' flag. */
216+
if (((uintptr_t) obj) == ((uintptr_t) &stdio_uart)) {
217+
MBED_ASSERT(obj->serial.uart == STDIO_UART);
215218
stdio_uart_inited = 1;
216-
memcpy(&stdio_uart, obj, sizeof(serial_t));
217219
}
218-
220+
219221
if (var->ref_cnt) {
220222
// Mark this module to be inited.
221223
int i = modinit - uart_modinit_tab;
@@ -256,11 +258,13 @@ void serial_free(serial_t *obj)
256258
if (var->obj == obj) {
257259
var->obj = NULL;
258260
}
259-
260-
if (obj->serial.uart == STDIO_UART) {
261+
262+
/* Clear the 'stdio_uart_inited' flag when serial_free(&stdio_uart) calls in. */
263+
if (((uintptr_t) obj) == ((uintptr_t) &stdio_uart)) {
264+
MBED_ASSERT(obj->serial.uart == STDIO_UART);
261265
stdio_uart_inited = 0;
262266
}
263-
267+
264268
if (! var->ref_cnt) {
265269
// Mark this module to be deinited.
266270
int i = modinit - uart_modinit_tab;

targets/TARGET_NUVOTON/TARGET_M480/serial_api.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -240,10 +240,12 @@ void serial_init(serial_t *obj, PinName tx, PinName rx)
240240
obj->serial.dma_chn_id_rx = DMA_ERROR_OUT_OF_CHANNELS;
241241
#endif
242242

243-
// For stdio management
244-
if (obj->serial.uart == STDIO_UART) {
243+
/* With support for checking H/W UART initialized or not, we allow serial_init(&stdio_uart)
244+
* calls in even though H/W UART 'STDIO_UART' has initialized. When serial_init(&stdio_uart)
245+
* calls in, we only need to set the 'stdio_uart_inited' flag. */
246+
if (((uintptr_t) obj) == ((uintptr_t) &stdio_uart)) {
247+
MBED_ASSERT(obj->serial.uart == STDIO_UART);
245248
stdio_uart_inited = 1;
246-
memcpy(&stdio_uart, obj, sizeof(serial_t));
247249
}
248250

249251
if (var->ref_cnt) {
@@ -289,7 +291,9 @@ void serial_free(serial_t *obj)
289291
var->obj = NULL;
290292
}
291293

292-
if (obj->serial.uart == STDIO_UART) {
294+
/* Clear the 'stdio_uart_inited' flag when serial_free(&stdio_uart) calls in. */
295+
if (((uintptr_t) obj) == ((uintptr_t) &stdio_uart)) {
296+
MBED_ASSERT(obj->serial.uart == STDIO_UART);
293297
stdio_uart_inited = 0;
294298
}
295299

targets/TARGET_NUVOTON/TARGET_NANO100/serial_api.c

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -175,12 +175,14 @@ void serial_init(serial_t *obj, PinName tx, PinName rx)
175175
obj->serial.dma_chn_id_rx = DMA_ERROR_OUT_OF_CHANNELS;
176176
#endif
177177

178-
// For stdio management
179-
if (obj->serial.uart == STDIO_UART) {
178+
/* With support for checking H/W UART initialized or not, we allow serial_init(&stdio_uart)
179+
* calls in even though H/W UART 'STDIO_UART' has initialized. When serial_init(&stdio_uart)
180+
* calls in, we only need to set the 'stdio_uart_inited' flag. */
181+
if (((uintptr_t) obj) == ((uintptr_t) &stdio_uart)) {
182+
MBED_ASSERT(obj->serial.uart == STDIO_UART);
180183
stdio_uart_inited = 1;
181-
memcpy(&stdio_uart, obj, sizeof(serial_t));
182184
}
183-
185+
184186
if (var->ref_cnt) {
185187
// Mark this module to be inited.
186188
int i = modinit - uart_modinit_tab;
@@ -221,11 +223,13 @@ void serial_free(serial_t *obj)
221223
if (var->obj == obj) {
222224
var->obj = NULL;
223225
}
224-
225-
if (obj->serial.uart == STDIO_UART) {
226+
227+
/* Clear the 'stdio_uart_inited' flag when serial_free(&stdio_uart) calls in. */
228+
if (((uintptr_t) obj) == ((uintptr_t) &stdio_uart)) {
229+
MBED_ASSERT(obj->serial.uart == STDIO_UART);
226230
stdio_uart_inited = 0;
227231
}
228-
232+
229233
if (! var->ref_cnt) {
230234
// Mark this module to be deinited.
231235
int i = modinit - uart_modinit_tab;

targets/TARGET_NUVOTON/TARGET_NUC472/serial_api.c

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -240,12 +240,14 @@ void serial_init(serial_t *obj, PinName tx, PinName rx)
240240
obj->serial.dma_chn_id_rx = DMA_ERROR_OUT_OF_CHANNELS;
241241
#endif
242242

243-
// For stdio management
244-
if (obj->serial.uart == STDIO_UART) {
243+
/* With support for checking H/W UART initialized or not, we allow serial_init(&stdio_uart)
244+
* calls in even though H/W UART 'STDIO_UART' has initialized. When serial_init(&stdio_uart)
245+
* calls in, we only need to set the 'stdio_uart_inited' flag. */
246+
if (((uintptr_t) obj) == ((uintptr_t) &stdio_uart)) {
247+
MBED_ASSERT(obj->serial.uart == STDIO_UART);
245248
stdio_uart_inited = 1;
246-
memcpy(&stdio_uart, obj, sizeof(serial_t));
247249
}
248-
250+
249251
if (var->ref_cnt) {
250252
// Mark this module to be inited.
251253
int i = modinit - uart_modinit_tab;
@@ -286,11 +288,13 @@ void serial_free(serial_t *obj)
286288
if (var->obj == obj) {
287289
var->obj = NULL;
288290
}
289-
290-
if (obj->serial.uart == STDIO_UART) {
291+
292+
/* Clear the 'stdio_uart_inited' flag when serial_free(&stdio_uart) calls in. */
293+
if (((uintptr_t) obj) == ((uintptr_t) &stdio_uart)) {
294+
MBED_ASSERT(obj->serial.uart == STDIO_UART);
291295
stdio_uart_inited = 0;
292296
}
293-
297+
294298
if (! var->ref_cnt) {
295299
// Mark this module to be deinited.
296300
int i = modinit - uart_modinit_tab;

0 commit comments

Comments
 (0)