Skip to content

Commit 21cbddd

Browse files
committed
Fix serial port full-duplex bug
When a receive IT fires while the HAL handle is locked, the receive callback fails to re-enable the IT and the serial port stops receiving anymore. This can happen when transmit IT is enabled outside the IRQ in HardwareSerial::write(), following a buffer full condition that disables transmit IT in HAL_UART_TxCpltCallback(). Solution is to temporarily disable the IRQ while the interrupt enable flags are modified.
1 parent e382064 commit 21cbddd

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

Diff for: cores/arduino/stm32/uart.c

+12-9
Original file line numberDiff line numberDiff line change
@@ -694,12 +694,14 @@ void uart_attach_rx_callback(serial_t *obj, void (*callback)(serial_t*))
694694
rx_callback[obj->index] = callback;
695695
rx_callback_obj[obj->index] = obj;
696696

697+
/* Must disable interrupt to prevent handle lock contention */
698+
HAL_NVIC_DisableIRQ(obj->irq);
699+
700+
HAL_UART_Receive_IT(uart_handlers[obj->index], &(obj->recv), 1);
701+
702+
/* Enable interrupt */
697703
HAL_NVIC_SetPriority(obj->irq, 0, 1);
698704
HAL_NVIC_EnableIRQ(obj->irq);
699-
700-
if(HAL_UART_Receive_IT(uart_handlers[obj->index], &(obj->recv), 1) != HAL_OK) {
701-
return;
702-
}
703705
}
704706

705707
/**
@@ -718,14 +720,15 @@ void uart_attach_tx_callback(serial_t *obj, int (*callback)(serial_t*))
718720
tx_callback[obj->index] = callback;
719721
tx_callback_obj[obj->index] = obj;
720722

723+
/* Must disable interrupt to prevent handle lock contention */
724+
HAL_NVIC_DisableIRQ(obj->irq);
725+
726+
/* The following function will enable UART_IT_TXE and error interrupts */
727+
HAL_UART_Transmit_IT(uart_handlers[obj->index], &obj->tx_buff[obj->tx_tail], 1);
728+
721729
/* Enable interrupt */
722730
HAL_NVIC_SetPriority(obj->irq, 0, 2);
723731
HAL_NVIC_EnableIRQ(obj->irq);
724-
725-
/* The following function will enable UART_IT_TXE and error interrupts */
726-
if (HAL_UART_Transmit_IT(uart_handlers[obj->index], &obj->tx_buff[obj->tx_tail], 1) != HAL_OK) {
727-
return;
728-
}
729732
}
730733

731734
/**

0 commit comments

Comments
 (0)