Skip to content

Install the link's UART from core 1, so its interrupt runs there - #48

Merged
Armitage64 merged 4 commits into
Armitage64:mainfrom
JC-000:idf6-link-latency
Nov 16, 2026
Merged

Armitage64 merged 4 commits into
Armitage64:mainfrom
JC-000:idf6-link-latency

Conversation

@JC-000

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

Copy link
Copy Markdown
Contributor

What

The link's UART interrupt now runs on core 1 (firmware/main/link.c).

uart_driver_install() allocates the UART's interrupt on the core it is called from, and link_open() is called from app_main, on core 0. The driver is now installed from a short task pinned to LINK_CORE (core 1), at the link's priority, and link_open() waits on a semaphore until it is done. The rest of the UART setup is unchanged.

This is the PR's only change. It does not depend on the ESP-IDF version: on both 5.5.5 and 6.0.3, uart_driver_install() calls esp_intr_alloc_intrstatus() in the caller's context, so the interrupt is bound to the calling core.

Why

Core 0 is Wi-Fi's (sdkconfig.defaults). With the interrupt there, each byte from the console was taken on Wi-Fi's core and then woke the link task across on core 1. Now the interrupt and the task it wakes are on the same core, the link's.

This comes from the ESP-IDF 6 Wi-Fi latency work on the 1541 Ultimate's U64 Wi-Fi module (GideonZ/1541ultimate#996, "Performance"), noted in #17. There it was one of six changes and was not measured on its own, so what it does here is not known yet (see Testing).

The other changes from that work are listed in the #17 comment. None of them are in this PR. Raising the UART FIFO thresholds would work against the link, which takes an interrupt for every byte. The Wi-Fi and lwIP IRAM options cost internal RAM and would need their own measurements.

Changes since the review

  • The sdkconfig.defaults commit is reverted by a new commit, as preferred in the review. It set CONFIG_FREERTOS_IN_IRAM and CONFIG_RINGBUF_IN_IRAM, which only do anything on ESP-IDF 6. On 5.5.5, FreeRTOS is in IRAM already through a hidden option that defaults to y, the ring buffer is in IRAM because RINGBUF_PLACE_FUNCTIONS_INTO_FLASH defaults to n, and RINGBUF_IN_IRAM does not exist, so the build warned about it. The two options go with the move to ESP-IDF 6 (Move the firmware to ESP-IDF 6 before Mbed TLS 3.6 ends (March 2027) #17), where they can be measured against 6.x's defaults. I don't see an ESP-IDF 6 branch on GitHub yet; when there is one, I'll open a PR against it with both options, as a fresh commit with a corrected comment.
  • main is merged in to clear the conflict in link.c. main's PAL work changed link_open(void) to link_open(uint8_t region). The merge keeps that signature and calls install_on_link_core() where the direct uart_driver_install() call was. Nothing else changed.
  • No force push. The hardware-tested commit (5791a5b) is in the history unchanged.
  • The title no longer says IDF 6, and the turnaround_us item is gone from the test plan (see below).

Testing

Builds, by Armitage64, on the two original commits: ESP-IDF 5.5.5 and 6.0.3, esp32 and esp32s3. All four passed. (Their IRAM figures included the two options that are now reverted, so they don't apply to this head.)

Builds of this head:

The three configurations release.yml builds, with its idf.py commands, in the espressif/idf Docker images. Each is compared with a build of main (16696aa) on the same ESP-IDF version.

ESP-IDF Target Result Memory against main
5.5.5 esp32 builds IRAM the same (118,111 B, 12,961 free); DRAM +8 B
5.5.5 esp32s3 builds DIRAM +8 B (207,343 B, 134,417 free)
5.5.5 esp32s3 generic builds same as esp32s3
6.0.3 esp32 builds IRAM the same (108,083 B, 22,989 free); DRAM the same
6.0.3 esp32s3 builds DIRAM +16 B (198,945 B, 142,815 free)
6.0.3 esp32s3 generic builds same as esp32s3

Flash code grows by about 170 to 200 bytes. On 5.5.5 there is no longer an unknown kconfig symbol warning, and nothing in link.c warns on either version.

The 6.0.3 builds, of this head and of main, needed esp_driver_uart added to REQUIRES in firmware/main/CMakeLists.txt, as found in the #17 trial: without it main doesn't build on 6.0.3 either. I added that line locally for those builds only. It is not in this PR.

Hardware, by Armitage64: StickS3 with a real NES, ESP-IDF 5.5.5, commit 5791a5b (before the merge with main):

Test Result
Boot "Clean, single start, link opened. Allocating the interrupt on core 1 works."
Setup ROM 1,106 requests, 0 again; reply 94–214 µs, slowest 299 µs
Throughput app Its usual top rates
TMNT II netplay, a few minutes 8,513 frames, 4 late, 0 again
Link task stack 11,260 bytes left

These results show the change does no harm. They don't show a gain. As Armitage64 pointed out, turnaround_us starts in link_task after xQueueReceive returns, so the UART interrupt and the wake to core 1 happen before its clock starts, and it can't see the part this change moves. A gain would have to be measured on the wire, from a request's last stop bit to the reply's first start bit, with Wi-Fi busy. That measurement has not been done.

Not tested: the merged head on hardware (the hardware results above are for 5791a5b), a PAL console with this change, the StickC Plus2 (classic ESP32), and ESP-IDF 6.0.3 on hardware.

🤖 Generated with Claude Code

uart_driver_install() allocates the UART's interrupt on the core it is
called from, and link_open() is called from app_main, on core 0, which
is Wi-Fi's (sdkconfig.defaults). Every byte from the console was taken
by an interrupt that waited behind Wi-Fi's own and its task's, and then
woke the link task across on core 1.

The driver is now installed from a short task pinned to LINK_CORE, at
the link's priority, and link_open() waits for it; the rest of the
UART's setup is unchanged. The interrupt and the task it wakes now
share the core that is the link's alone.

This holds on ESP-IDF 5.5 as on 6. It is fix 3 of the ESP-IDF 6
latency work for the 1541 Ultimate's WiFi modules
(JC-000/1541ultimate, 6ee42b3), noted in Armitage64#17.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EHfcHBGKaU1123LfntqHtg
ESP-IDF 6 moved FreeRTOS's queue and task functions, and the ring
buffer's, to flash by default (CONFIG_FREERTOS_IN_IRAM and
CONFIG_RINGBUF_IN_IRAM are off); ESP-IDF 5 has them in RAM. Each of the
console's requests goes through them: the link task is woken by the
UART's event queue, reads the request from the driver's receive ring
buffer and writes the reply to its transmit one, and a call to any of
them can wait on a flash cache miss. Set both, for about 10 KB of RAM.

On the 1541 Ultimate's U64 WiFi bridge (classic ESP32, ESP-IDF 6.0.3)
these two put the request latency back in the range of the ESP-IDF 5
build: p50/p90/p99 35.8/82.5/155.8 ms, against 40.8/102.2/202.7 ms
without them (JC-000/1541ultimate, 4b3f7aa). Fixes 1 and 2 of the list
in Armitage64#17.

ESP-IDF 5.5 has no such options and passes over them.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EHfcHBGKaU1123LfntqHtg
@Armitage64

Copy link
Copy Markdown
Owner

Thanks for this. I built both commits on ESP-IDF 5.5.5 and 6.0.3, for esp32 and esp32s3. All four builds pass. With both options on IDF 6, the Plus2 uses 119,359 bytes of IRAM (91.1%, 11.7 KB free), and the S3 uses 11.5 KB more DIRAM. Both fit.

One change is needed on the sdkconfig.defaults commit. The comment says ESP-IDF 5 "has no such options and passes over them", which isn't quite right for 5.5.5:

  • CONFIG_FREERTOS_IN_IRAM does exist in 5.5.5. It's a hidden option that defaults to y (components/freertos/Kconfig), so setting it there has no effect.
  • CONFIG_RINGBUF_IN_IRAM doesn't exist in 5.5.5. Every 5.5.5 build now prints, twice:
    warning: unknown kconfig symbol 'RINGBUF_IN_IRAM' assigned to 'y' in .../firmware/sdkconfig.defaults
    

Either of these works:

  1. Move this commit to the ESP-IDF 6 branch (preferred). It only does anything on 6.x, it won't warn there, and on that branch it can be measured against 6.x's defaults.
  2. Keep it here and fix the comment so it says what 5.5.5 actually does: FreeRTOS is in IRAM through a hidden option, the ring buffer is in IRAM because RINGBUF_PLACE_FUNCTIONS_INTO_FLASH defaults to n, and the build warns about RINGBUF_IN_IRAM.

The link.c commit will be smoke-tested on a StickS3 on 5.5.5 before merging.

@Armitage64

Copy link
Copy Markdown
Owner

Smoke test of the link.c commit (5791a5b) on a StickS3 with a real NES, ESP-IDF 5.5.5:

Test Result
Boot Clean, single start, link opened (link: StickS3: TX G8, RX G1, 85227 baud). Allocating the interrupt on core 1 works.
Setup ROM Fine. 1,106 requests, 0 again; reply 94–214 µs, slowest 299 µs
Throughput app Reached its usual top rates, so the extra interrupts on core 1 during RAW runs cost nothing visible
TMNT II netplay, a few minutes No stalls, lost inputs or drops to the title. 8,513 frames, 4 late, 0 again; delay 1 tick throughout; network 5–7.5 ms there and back, peak 28.7 ms
Link task stack 11,260 bytes left

This commit is good to merge.

These tests show it does no harm. They don't show it helps, because turnaround_us can't see the part this commit changes. arrived is stamped in link_task after xQueueReceive returns (link.c:150), so the UART interrupt and the wake to core 1 happen before the clock starts. A gain would have to be measured on the wire, from a request's last stop bit to the reply's first start bit, with Wi-Fi busy. The PR's test plan should drop the turnaround_us item for that reason.

The sdkconfig.defaults commit is still waiting on the change in the comment above.

JC-000 and others added 2 commits November 15, 2026 21:04
This reverts commit a6dc84b.

It comes out of this branch at the maintainer's request and goes with
the move to ESP-IDF 6 (Armitage64#17) instead. On ESP-IDF 5.5.5,
which the releases are built with, it does nothing:
CONFIG_FREERTOS_IN_IRAM is a hidden option that is already y, and
CONFIG_RINGBUF_IN_IRAM does not exist, so the build warns of an unknown
kconfig symbol. This leaves the branch with only the change to
link.c.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
One conflict, in firmware/main/link.c: main's PAL work changed
link_open(void) to link_open(uint8_t region), next to this branch's
install_on_link_core(). The helper (its semaphore, install_task and
install_on_link_core) stays just before link_open(), which keeps
main's signature and body, with main's uart_driver_install() call in
that body replaced by install_on_link_core() as before.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@JC-000 JC-000 changed the title Idf6 link latency Install the link's UART from core 1, so its interrupt runs there Nov 16, 2026
@JC-000

JC-000 commented Nov 16, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for building both commits and for the StickS3 run.

  • sdkconfig.defaults: A new commit reverts it, so this PR is now only the link.c change. That is the first half of option 1: I don't see an ESP-IDF 6 branch on GitHub yet, and when there is one I'll open a PR against it with both options (a fresh commit, with the comment corrected), where they can be measured against 6.x's defaults.
  • Conflict: I merged main into the branch. The merge keeps link_open(uint8_t region) from the PAL work and calls install_on_link_core() where uart_driver_install() was. Nothing else changed. There was no force push, so 5791a5b, the commit you tested, is in the history as it was. The merged head builds (see the description); I haven't run it on hardware.
  • turnaround_us: dropped from the test plan. I agree: arrived is stamped after xQueueReceive returns, so the interrupt and the wake to core 1 are outside what it measures. The description now says the tests show no harm, not a gain, and names the wire measurement as the way to show one. I haven't done that measurement.
  • Title and description: updated. The title no longer says IDF 6, since the change doesn't depend on it.

Written with Claude Opus 5.5 in Claude Code.

@Armitage64

Copy link
Copy Markdown
Owner

Smoke test of the merged head (6793340) on a StickS3 with a real NES, ESP-IDF 5.5.5:

Test Result
Build Clean, no warnings
Boot Clean, single start, no panic or abort. link: StickS3: TX G8, RX G1, 85227 baud (NTSC), inverted. Wi-Fi joined.
Setup ROM link test Over 4,500 requests on screen, all GOOD, 0 no reply, 0 wrong. Stick's log: 5,480 requests, 0 again; reply 98–148 µs, slowest 305 µs
Link task stack 10,844 bytes left

Against the run of 5791a5b: the slowest reply is 305 µs, up from 299 µs, and both are well under 500 µs. The link task has 416 bytes less stack left, about what the PAL work merged from main would add. So the merge broke nothing. As before, this shows the change does no harm, not that it helps.

Not tested: netplay, a PAL console, the Plus2, ESP-IDF 6.

The work taken out of this PR is tracked in #17, for the move to ESP-IDF 6 (this comment):

  • The two IRAM options, and measuring them against 6.x's defaults.
  • esp_driver_uart in REQUIRES.
  • The on-the-wire latency measurement.

So the IDF 6 PR offered above can be opened against that work when the branch exists.

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.

3 participants