Skip to content

STM32 USARTv1/v2/v3: add SerialConfig.external_rx_buffer - #112

Open
bugobliterator wants to merge 1 commit into
ArduPilot:masterfrom
bugobliterator:pr-usart-external-rx-buffer
Open

bugobliterator wants to merge 1 commit into
ArduPilot:masterfrom
bugobliterator:pr-usart-external-rx-buffer

Conversation

@bugobliterator

@bugobliterator bugobliterator commented Sep 11, 2026

Copy link
Copy Markdown
Member

Problem

ArduPilot receives on USART ports with DMA: it clears RXNEIE after sdStart() and keeps IDLEIE on so a partially filled DMA buffer is flushed when the line goes idle. The serial driver's interrupt handler still runs on every IDLE (and TX) interrupt, and its receive loop reads RDR whenever RXNE/RXFNE is set, with no check that the receive interrupt is actually enabled. A byte that has landed in the RX FIFO but has not yet been fetched by the DMA at that instant is pulled into the SerialDriver input queue, which the DMA receive path never reads. Such a byte would be lost from the stream with no error flag and no DMA drop.

The window is the time between the handler sampling ISR and the DMA serving the request, a few hundred nanoseconds, so it can only matter at multi-megabaud rates with short inter-chunk gaps. The race was found by inspection while chasing a byte-loss bug that turned out to be a different issue altogether. A test that provokes this race deliberately is being written.

Fix

New SerialConfig.external_rx_buffer (USARTv1, v2, v3). When set:

  • sd_lld_start() does not enable RXNEIE;
  • the interrupt handler never reads the data register to take data (USARTv1 reads DR only to clear an error flag and leaves a pending byte alone).

Default false; behaviour for existing users is unchanged. The default_config initialisers gain the field.

Compile-checked on F4 (USARTv1), F7 (USARTv2) and H7 (USARTv3) targets, and run on an STM32H757 with ArduPilot setting the flag whenever RX DMA is enabled.

The issue was found and the fix developed with the assistance of Claude (Anthropic).

@bugobliterator
bugobliterator force-pushed the pr-usart-external-rx-buffer branch from 1fb39e4 to 7ab848f Compare September 11, 2026 03:12
When set, the driver leaves RXNEIE clear and its interrupt handler never
reads the data register to take data (USARTv1 reads it only to clear an
error flag). Without this, an IDLE or TX interrupt that finds a byte the
external receive path has not taken yet diverts it into the unused input
queue, losing it from the stream. Found by inspection while chasing a
byte-loss bug that turned out to be a different issue.

Issue found and fix developed with the assistance of Claude (Anthropic).
@bugobliterator
bugobliterator force-pushed the pr-usart-external-rx-buffer branch from 7ab848f to 7a3576b Compare September 11, 2026 03:12

@andyp1per andyp1per left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks ok to me. Please try and upstream this for maintenance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants