Skip to content

ESP32 WiFi modules: send queued frames into the TX FIFO directly, not one per TX interrupt - #1079

Open
JC-000 wants to merge 18 commits into
GideonZ:masterfrom
JC-000:fix/wifi-uart-tx-fill
Open

JC-000 wants to merge 18 commits into
GideonZ:masterfrom
JC-000:fix/wifi-uart-tx-fill

Conversation

@JC-000

@JC-000 JC-000 commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #1074

Problem

On a C64 Ultimate the module kept 11 replies queued and released one per frame it received. The TX path started a frame only from the TXFIFO_EMPTY interrupt, which did not come when it was enabled with the FIFO already below its threshold, and the handler's own enable raised nothing. Replies queued within one interrupt latency merged into one kick: one frame went out, the rest waited for the next kick, that is the next reply, a constant lag.

What changed and why

my_uart_tx_fill() sends as many queued frames as the FIFO has room for and says whether any is left. The TX interrupt and my_uart_transmit_packet both call it under the UART lock, and the interrupt is enabled only for what did not fit. Bounded to one FIFO; no printf, no allocation. Same my_uart.c in raw_u64, raw_c3 and u64ctrl.

This branch contains the RX-overflow PR (#1078), which contains the UART interrupt handler tests PR (#1077, the harness). They merge in that order.

Products. the ESP32 WiFi modules: raw_u64 (Ultimate 64 / Ultimate 64 Elite), raw_c3 (Ultimate-II+L) and u64ctrl (Ultimate 64 Elite II / C64 Ultimate); the same my_uart.c is in all three. Tested on the C64 Ultimate.

How I tested it

Host: macOS, with the device-free tests run in an Ubuntu 24.04 container (g++ 13.3). The hardware runs used the test harness from the same machine.

Host tests (device-free, run before and after the change):

  • UART handler tests with a TX FIFO model: make -C software/wifi/uart_tests isr; make -C software/wifi/uart_tests isr-red
    • with the change: 8 tests pass per module build (raw_u64, raw_c3, u64ctrl); a burst of 11 replies is sent whole
    • without it: on the parent: 10 replies stay queued; four old-handler models fail as required

Hardware.

Device Firmware Ultimate FPGA C64 core
Ultimate 64 Elite 7b15448a7 126 1.50
C64 Ultimate 7b15448a7 126 1.50

On the C64 Ultimate: on an image without the fix, a reply held behind a filled TX FIFO was never sent — the Ultimate saw no reply from the module. With the fix the queued reply is sent: the module answers each round, with no "no reply" and 0% ping loss.

Standard profile / soak: not reported here — this change is module-side (ESP32) firmware, which the host-driven ./run-tests standard profile and soak suites do not exercise. The module build is flashed and checked on its own (see above).

Untested

  • Ultimate-II cartridge (U2+ / U2+L): not tested. I do not have one. Not built either for the cartridges, because the build environment I use cannot build those targets; CI builds them.
  • The edge-like TXFIFO_EMPTY behaviour is what the C64 Ultimate showed; the IDF headers document a level behaviour. The host model is fitted to the evidence, not measured on the chip.
  • Module version number bumped in the same commit; the number is decided at merge.

Written with Claude Opus 5.5 in Claude Code.

JC-000 and others added 13 commits November 7, 2026 16:06
software/wifi/tests: the handler of my_uart.c (raw_u64, raw_c3, u64ctrl) is cut out
of each module as it is and run against a modelled UART and the real buffer queues of
software/io/uart/cmd_buffer.c. No hardware, no ESP-IDF; `make isr`. A fix adds its own
isr_<fix>_test.cpp and its models to the Makefile (`make isr-red`). First cases: a frame
is delivered whole, escapes are decoded, a frame in pieces is delivered once, a queued
frame goes out at the TX interrupt.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
GideonZ#1041 (wifi-fixes) adds its own software/wifi/tests; the two trees would collide on Makefile and stub/. Nothing else changes.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Two fixes edited the same lines of the Makefile; each now adds a file, and the Makefile includes isr_*.mk.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
The CI step "make host_tests" runs the other host suites; this one is added to it.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…per TX interrupt

On a C64U the module kept 11 replies queued and released one per frame it received. The TX
path started a frame only from the TXFIFO_EMPTY interrupt, which did not come when it was
enabled with the FIFO already below its threshold (the reset value 96: the 10 in
my_uart_init is applied only if the interrupt is in the enable mask, which it is not), and
the handler's own enable raised nothing. Replies queued within one interrupt latency
merged into one kick: one frame went out, the rest waited for the next kick, that is the
next reply, a constant lag. (The edge-like behaviour is what the C64U showed; the IDF
headers document a level behaviour. The host model is fitted to that evidence, not measured.)

my_uart_tx_fill() sends as many queued frames as the FIFO has room for and says whether any
is left. The TX interrupt and my_uart_transmit_packet both call it under the UART's lock,
and the interrupt is enabled only for what did not fit (it then comes on the drain across
the threshold). Bounded to one FIFO; no printf, no allocation. The same my_uart.c is in
raw_u64, raw_c3 and u64ctrl; all three change.

Host tests (software/wifi/tests, `make isr`): a 128-byte TX FIFO and an edge-like
TXFIFO_EMPTY model; a burst of 11 replies leaves 10 queued on the parent and all sent
here; four models of the code before the fix (`make isr-red`). The module identity is
bumped as a placeholder; the maintainers assign the number at merge.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
GideonZ#1041 (wifi-fixes) adds its own software/wifi/tests; the two trees would collide on Makefile and stub/. Nothing else changes.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Two fixes edited the same lines of the Makefile; each now adds a file, and the Makefile includes isr_*.mk.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…T tests in make host_tests

The module identity is bumped as a placeholder so the updater installs the new build; the maintainers assign the final number at merge. The UART interrupt handler tests are added to the make host_tests run.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…e next frame

An overflow of the UART RX FIFO loses bytes at an unknown place in the stream. The
handler had code for it (reset the FIFO) but RXFIFO_OVF was not in the enable mask of
my_uart_init, so it never ran, and it came after the receive code anyway: the frame in
progress carried on with whatever arrived next. It was delivered cut short, or glued
to the next frame when its closing 0xC0 was lost, or the rest of a frame was taken for a
frame, and the dispatcher ran a command nobody sent.

Enable RXFIFO_OVF and handle it first in the pass: the FIFO is emptied and not read, the
open frame is thrown away, and everything up to the next 0xC0 is skipped (it ends the
damaged frame or starts the next). With no frame open there is nothing to skip.

Seen on a C64U with the module's RTS flow control forced off for a hold under load: frames
that began in the middle of an echo payload were answered as commands, and the link was
dead afterwards. With flow control on, this needs a sender overrunning RTS (the margin
is 6 characters); the same my_uart.c is in raw_u64, raw_c3 and u64ctrl, all three change.

Host tests (software/wifi/tests, `make isr`): 5 cases, RED on this file's parent, plus
five models of the code before the fix (`make isr-red`). The module identity is bumped as a
placeholder; the maintainers assign the number at merge.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
GideonZ#1041 (wifi-fixes) adds its own software/wifi/tests; the two trees would collide on Makefile and stub/. Nothing else changes.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Two fixes edited the same lines of the Makefile; each now adds a file, and the Makefile includes isr_*.mk.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…er does (raw status in the RX branch)

The first version enabled the RXFIFO_OVF interrupt and handled it at the top of the pass. The
mechanism that ran on hardware (a C64U, an RX overflow forced by switching the module's RTS flow
control off for a hold under load: the module stayed alive, rx_fifo_ovf counted 1) keeps the
interrupt disabled and looks at the raw status inside the RX branch, at the interrupt the full FIFO
brings. This takes that mechanism, so the hardware result applies to what is merged: same detection
point, same actions (the FIFO emptied and not read, the open frame thrown away, skip to the next
0xC0). The enable mask is as on master; the dead overflow branch at the end of the pass is as on master.
Only the bench's counter is left out (master has no link counters).

Host harness: the stub offers the raw status (uart_hal_get_intraw_mask); the cases are the same and
still fail on master; the models of the old handler follow the new detection (isr_ovf.mk).

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
The CI step "make host_tests" runs the other host suites; this one is added to it.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@chrisgleissner chrisgleissner added bug Existing behavior is incorrect, broken, or regressed; includes supported documentation defects. network Networking and remote services: REST, Socket DMA, FTP, Telnet, and online access. labels Nov 10, 2026

@chrisgleissner chrisgleissner left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

@JC-000 thanks for tracking down the TX kick. The branch conflicts with master. Please merge or rebase master after #1077 and #1078 are in, and renumber the module identity as you describe.

JC-000 and others added 5 commits November 10, 2026 08:19
…eonZ#1077)

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…1078)

Brings in GideonZ#1077 (updated onto master). Module IDENT renumbered after master: raw_u64 1.19, raw_c3 1.12, u64ctrl 1.30.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Brings in GideonZ#1078 and GideonZ#1077 (updated onto master). Module IDENT renumbered after master and GideonZ#1078: raw_u64 1.20, raw_c3 1.13, u64ctrl 1.31.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…u64 1.23, raw_c3 1.15, u64ctrl 1.33

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…dule identity raw_u64 1.24, raw_c3 1.16, u64ctrl 1.34

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@JC-000 JC-000 changed the title # ESP32 WiFi modules: send queued frames into the TX FIFO directly, not one per TX interrupt ESP32 WiFi modules: send queued frames into the TX FIFO directly, not one per TX interrupt Nov 10, 2026
@chrisgleissner

Copy link
Copy Markdown
Collaborator

@JC-000 thanks. I re-reviewed this PR at head 8e9182b. It goes in after #1078.

Code review. my_uart_tx_fill is the old TX block moved into a function, plus a loop that starts the next queued frame while there is room. It runs under the UART lock from the interrupt and from my_uart_transmit_packet. The FromISR queue calls are made inside that critical section, and woken is honoured with taskYIELD() after the lock is released. I found no defect.

The TX threshold claim checks out in source. In esp_driver_uart/src/uart.c (uart_intr_config, IDF 6 tree), the TX empty threshold is written only when UART_INTR_TXFIFO_EMPTY is in the enable mask. my_uart_init enables only RXFIFO_TOUT | RXFIFO_FULL, so its value of 10 (64 in #996) is never applied. That source check supports the reasoning. It does not show the stall on a chip.

Host red/green at head.

Hardware. The description calls the model "fitted to evidence", so I want a measurement on master + this PR, not only the model. On the Ultimate 64 Elite (flashed) and the U2+L (CI build) I plan to compare the reply backlog and throughput before and after. If you have the bench counter that showed 11 replies queued, please share it as a patch; it stays out of this PR. As on #1078, our C64 Ultimate is not available for module flashing.

@chrisgleissner

chrisgleissner commented Nov 12, 2026 •

Copy link
Copy Markdown
Collaborator

@JC-000 to save you some time, I'm happy to write the bench counter for the reply backlog and run the measurement on our devices, including the C64 Ultimate, which can now take module builds. I'll post the patch and the numbers here, and none of it will be added to this PR. This goes in after #1078. If you already have the counter on a branch, a pointer is welcome and I'll use yours.

@JC-000

JC-000 commented Nov 12, 2026

Copy link
Copy Markdown
Contributor Author

Thanks. We have the counter on a branch: a bench-only command that records the deepest transmit queue, the backlog left, the drain time and the frame count for a burst of N queued frames, so backlog and throughput can be compared before and after. It applies to master, to #1078 and to this PR's head, builds for all three modules and passes host tests. It has not run on hardware yet; that happens on our Elite today, and I'll post the branch link with the results afterwards. If you'd rather not wait, go ahead with your own.

Written with Claude Sonnet 5.5 and Claude Opus 5.5 in Claude Code.

@chrisgleissner

Copy link
Copy Markdown
Collaborator

@JC-000 thanks, I'll wait for your counter branch and numbers. If you'd like the same measurement on our C64 Ultimate or U2+L afterwards, just say.

@JC-000

JC-000 commented Nov 12, 2026

Copy link
Copy Markdown
Contributor Author

The counter branch is up: https://github.com/JC-000/1541ultimate/tree/d59f892a62cc334d75cc92005c9ca963a9ca9619/bench-repro/1079-reply-backlog (branch repro/1079-reply-backlog-counter, three commits on 085aad7). It adds module command 0x6C (all three modules) and UCI 03 E7 <arg> on the host, plus the driver txq.py. The README has the frame layout and the exact commands. Cherry-pick the range 085aad730..repro/1079-reply-backlog-counter onto #1078's head for "before" and onto this PR's head for "after". It applies cleanly to master, 49074a0 and 8e9182b, the u64 firmware builds on master plus the branch, and the three modules build. The module version has to be bumped for the updater to take it; the README says how.

Measured on our Ultimate 64 Elite with a module that already has the TX fill, so these are "after" numbers only:

Burst sent q_max left drain
11 × 4 B 11 1 0 0.28 ms
11 × 1000 B 11 10 0 16.5 ms (about 667 KB/s)
11 × 1500 B 11 10 0 24.7 ms (about 667 KB/s)

667 KB/s is the 6.67 Mbaud UART limit. We have no module without the TX fill on the bench, so the "before" run on master + #1078 is the one we haven't done. If you have the time, running it on your C64 Ultimate or U2+L would give the first before/after pair.

Written with Claude Sonnet 5.5 and Claude Opus 5.5 in Claude Code.

@chrisgleissner

Copy link
Copy Markdown
Collaborator

@JC-000 thanks for the counter branch. I'm running the before/after on our C64 Ultimate now: master + #1078 + your counter commits for before, and master + #1078 + #1079 + the same commits for after, with the u64ctrl module flashed in both cases. I'll post the numbers here when they are in.

@chrisgleissner

Copy link
Copy Markdown
Collaborator

@JC-000 here is the C64 Ultimate before/after with your counter branch.

Setup. C64 Ultimate (XC7A50T), wired, WiFi connected. Both builds were flashed through the updater, and the module was installed in both cases:

The bench modules report version 1.0 at run time, so that a normal updater replaces them afterwards. Nothing else differs from your branch. Each cell is one run of txq.py -H c64u --burst 11 [--size N].

Burst Before: left / drain_us After: left / drain_us
11 × 4 B, run 1 0 / 254 0 / 188
11 × 4 B, run 2 0 / 275 0 / 202
11 × 4 B, run 3 0 / 245 0 / 186
11 × 1000 B, runs 1-3 0 / 22083, 22083, 22085 0 / 22092, 22079, 22073
11 × 1500 B, runs 1-2 (not run) 0 / 33096, 33091

sent was 11 in every burst. q_max was 1 for the 4-byte bursts and 10 for the 1000 and 1500 byte bursts, on both builds. A read one second after each 4-byte burst showed left 0. The host console printed the Ev 6F lines of each burst at once.

Result. On this C64 Ultimate, master + #1078 does not show the stall: left is 0 and the 4-byte burst drains in about 250 µs. So the edge-like behaviour does not occur on this module and chip. With #1079 the 4-byte burst drains about 60 µs sooner (about 190 µs). The 1000 and 1500 byte bursts take the same time on both builds, about 498 KB/s, so #1079 does not reduce throughput.

The board is back on its normal firmware and module.

@JC-000

JC-000 commented Nov 13, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the C64 Ultimate run. On our Ultimate 64 Elite the idle bursts match yours: no stall before or after, with #1079 draining 11 × 4 B about 80 µs sooner (about 320 µs before, 240 µs after) and the large bursts at the UART limit on both builds.

The difference shows under load. With six 1472-byte ICMP streams running:

master + #1078 + #1079
Echoes answered, per 700 sent about 592–623 693–697
Bursts left stuck in the queue 1 (a 1500-byte burst still queued after 200 ms) 0
Longest drain 200 ms 74 ms

Both builds run out of the 11 TX buffers under that load. What #1079 changes is that queued frames keep moving. Same counter branch as yours; both builds were flashed through the updater.

Written with Claude Sonnet 5.5 and Claude Opus 5.5 in Claude Code.

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

Labels

bug Existing behavior is incorrect, broken, or regressed; includes supported documentation defects. network Networking and remote services: REST, Socket DMA, FTP, Telnet, and online access.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

WiFi module UART: an RX FIFO overflow splices frames into junk commands, and queued replies go out one per received frame

2 participants