Skip to content

Commit 0c2dd3b

Browse files
committed
[UART] Search only once the debug uart
Signed-off-by: Frederic Pillon <[email protected]>
1 parent f9f4340 commit 0c2dd3b

File tree

1 file changed

+14
-11
lines changed

1 file changed

+14
-11
lines changed

cores/arduino/stm32/uart.c

+14-11
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,11 @@ void uart_deinit(serial_t *obj)
468468
}
469469

470470
HAL_UART_DeInit(uart_handlers[obj->index]);
471+
472+
/* Release uart debug to ensure init */
473+
if (serial_debug.index == obj->index) {
474+
serial_debug.index = UART_NUM;
475+
}
471476
}
472477

473478
#if defined(HAL_PWR_MODULE_ENABLED) && defined(UART_IT_WUF)
@@ -581,33 +586,31 @@ void uart_debug_init(void)
581586
*/
582587
size_t uart_debug_write(uint8_t *data, uint32_t size)
583588
{
584-
uint8_t index = 0;
585589
uint32_t tickstart = HAL_GetTick();
586590

587591
if (DEBUG_UART == NP) {
588592
return 0;
589593
}
590-
/* Search if DEBUG_UART already initialized */
591-
for (index = 0; index < UART_NUM; index++) {
592-
if (uart_handlers[index] != NULL) {
593-
if (DEBUG_UART == uart_handlers[index]->Instance) {
594-
break;
594+
if (serial_debug.index >= UART_NUM) {
595+
/* Search if DEBUG_UART already initialized */
596+
for (serial_debug.index = 0; serial_debug.index < UART_NUM; serial_debug.index++) {
597+
if (uart_handlers[serial_debug.index] != NULL) {
598+
if (DEBUG_UART == uart_handlers[serial_debug.index]->Instance) {
599+
break;
600+
}
595601
}
596602
}
597-
}
598603

599-
if (index >= UART_NUM) {
600-
/* DEBUG_UART not initialized */
601604
if (serial_debug.index >= UART_NUM) {
605+
/* DEBUG_UART not initialized */
602606
uart_debug_init();
603607
if (serial_debug.index >= UART_NUM) {
604608
return 0;
605609
}
606610
}
607-
index = serial_debug.index;
608611
}
609612

610-
while (HAL_UART_Transmit(uart_handlers[index], data, size, TX_TIMEOUT) != HAL_OK) {
613+
while (HAL_UART_Transmit(uart_handlers[serial_debug.index], data, size, TX_TIMEOUT) != HAL_OK) {
611614
if ((HAL_GetTick() - tickstart) >= TX_TIMEOUT) {
612615
return 0;
613616
}

0 commit comments

Comments
 (0)