From eb030d52651cb8d04d9cc1528e3b06802a7f419e Mon Sep 17 00:00:00 2001 From: Frederic Pillon Date: Mon, 30 Nov 2026 15:48:19 +0100 Subject: [PATCH 1/2] fix(uart): make Uart::write() TX start atomic Uart::write() updated tx_head and then checked whether a HAL transfer was ongoing, without protection. If write() was preempted between the two steps for longer than the new data takes to send, the TX complete IRQ sent the data and left the UART idle with tx_tail == tx_head. write() then started a transfer from the empty buffer, and at its end tx_tail went past tx_head, so the last SERIAL_TX_BUFFER_SIZE bytes were sent a second time. Update tx_head and start the transfer in a critical section, restoring PRIMASK afterwards as write() may be called with interrupts disabled. Compute the transfer size from tx_head and tx_tail inside the critical section, and only start a transfer if data are pending. Fixes: #3125 Assisted-by: GitHub Copilot: claude-sonnet-5.5 Signed-off-by: Frederic Pillon --- cores/arduino/Serial.cpp | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/cores/arduino/Serial.cpp b/cores/arduino/Serial.cpp index 6ee2e1a42a..eb16c52861 100644 --- a/cores/arduino/Serial.cpp +++ b/cores/arduino/Serial.cpp @@ -628,7 +628,6 @@ size_t Uart::write(const uint8_t *buffer, size_t size) // Copy data to buffer. Take into account rollover if necessary. if (_serial.tx_head + size <= SERIAL_TX_BUFFER_SIZE) { memcpy(&_serial.tx_buff[_serial.tx_head], buffer, size); - size_intermediate = size; } else { // memcpy till end of buffer then continue memcpy from beginning of buffer size_intermediate = SERIAL_TX_BUFFER_SIZE - _serial.tx_head; @@ -637,18 +636,31 @@ size_t Uart::write(const uint8_t *buffer, size_t size) size - size_intermediate); } + // Moving the head and starting the transfer must be atomic: if the TX + // complete IRQ drained the buffer in between, a transfer would be started + // from an empty buffer and stale data would be sent again. + // PRIMASK is restored (not unconditionally enabled) as write() may be + // called with interrupts already disabled. + uint32_t primask = __get_PRIMASK(); + __disable_irq(); + // Data are copied to buffer, move head pointer accordingly _serial.tx_head = (_serial.tx_head + size) % SERIAL_TX_BUFFER_SIZE; // Transfer data with HAL only is there is no TX data transfer ongoing // otherwise, data transfer will be done asynchronously from callback - if (!serial_tx_active(&_serial)) { + if (!serial_tx_active(&_serial) && (_serial.tx_head != _serial.tx_tail)) { + size_t pending = (SERIAL_TX_BUFFER_SIZE + _serial.tx_head - _serial.tx_tail) + % SERIAL_TX_BUFFER_SIZE; // note: tx_size correspond to size of HAL data transfer, // not the total amount of data in the buffer. - // To compute size of data in buffer compare head and tail - _serial.tx_size = size_intermediate; - uart_attach_tx_callback(&_serial, _tx_complete_irq, size_intermediate); + // Limit the transmission to the buffer end because HAL is not able + // to manage rollover, the remaining is sent from the callback. + _serial.tx_size = min(pending, (size_t)(SERIAL_TX_BUFFER_SIZE - _serial.tx_tail)); + uart_attach_tx_callback(&_serial, _tx_complete_irq, _serial.tx_size); } + + __set_PRIMASK(primask); } /* Return 0 if not ready else requested transfer size */ return ret; From 25d356b10725b57107406062be5d6711dba8046c Mon Sep 17 00:00:00 2001 From: Frederic Pillon Date: Mon, 30 Nov 2026 17:02:26 +0100 Subject: [PATCH 2/2] refactor(uart): refresh free space after Uart::write() waits When the TX buffer was full, Uart::write() waited for the TX interrupt to free space but kept the free space value read before the wait, so it was 0. The split loop then recursed with a zero-length write before the real one. Read the free space again in the wait loop so the data are queued directly. Tested on NUCLEO_C562RE at 921600 baud with an echo using single byte and 200 bytes write() calls, with and without interrupt load. Assisted-by: GitHub Copilot: claude-sonnet-5.5 Signed-off-by: Frederic Pillon --- cores/arduino/Serial.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cores/arduino/Serial.cpp b/cores/arduino/Serial.cpp index eb16c52861..3f43bdd2fa 100644 --- a/cores/arduino/Serial.cpp +++ b/cores/arduino/Serial.cpp @@ -597,7 +597,7 @@ size_t Uart::write(const uint8_t *buffer, size_t size) ret = 0; } else { size_t size_intermediate; - size_t available = availableForWrite(); + size_t available; size_t available_till_buffer_end = SERIAL_TX_BUFFER_SIZE - _serial.tx_head; _written = true; @@ -610,7 +610,7 @@ size_t Uart::write(const uint8_t *buffer, size_t size) // If the output buffer is full, there's nothing for it other than to // wait for the interrupt handler to free space - while (!availableForWrite()) { + while ((available = availableForWrite()) == 0) { // nop, the interrupt handler will free up space for us }