Repository navigation
fix(esp32-s3): LCD_CLOCKLESS chunk-ISR IRAM/ISR-safety + equal-chunk (#2647 WIP) - #2648
Conversation
LCD_CLOCKLESS uses an ISR-driven 3-buffer ring (see issue #2258) where on_color_trans_done re-encodes and re-arms the next DMA chunk inside the interrupt callback. The original implementation had two latent bugs that would deadlock the chip under flash-cache pressure: 1. ISR path lived in flash. isrChunkDone, encodeChunk, and LcdSpiPeripheralEsp::transmit (plus the EspWrapper thunk) were all compiled into the default .text section. When any concurrent flash operation suspended the cache (NVS commit, esp_flash_erase_region, etc.), the ISR could not fetch its own code, interrupts stayed masked past the 300 ms IWDT window, and the device panicked with "Interrupt wdt timeout on CPU1" in ISR context with PC inside spi_flash_chip_generic_erase_block. Fix: mark every function on the ISR re-arm path FL_IRAM (matches the PARLIO worker-ISR convention). 2. transmit() called the regular xSemaphoreTake from ISR context. lcd_spi_flush_ready already did xSemaphoreGiveFromISR before invoking the user callback so the take was guaranteed non-blocking, but xSemaphoreTake (not the FromISR variant) asserts when invoked from an interrupt — undefined behavior in release builds, panic in debug. Fix: route through xSemaphoreTakeFromISR when xPortInIsrContext() returns true; keep the existing 2 s blocking take on the task path. Test infrastructure: bump runSingleTest laneSizes upper bound from 8 to 16 so autoresearch can exercise LCD_CAM's full 16-bit parallel bus (matching the wave8Transpose_16 path the driver already supports). Repro before the fix: bash autoresearch esp32s3 --lcd --lanes 16 --strip-sizes 256 → Guru Meditation Error: Interrupt wdt timeout on CPU1 → PC inside spi_flash_chip_generic_erase_block, ISR context Co-Authored-By: Claude Opus 4.7 <[email protected]>
LcdSpiPeripheralEsp::transmit() tears down and recreates the ESP-IDF panel_io handle whenever the transaction size changes — its GDMA descriptor chain is sized per transaction. The teardown calls esp_lcd_panel_io_del, which lives in flash; performing it from isrChunkDone (ISR context) deadlocks the chip with "Interrupt wdt timeout on CPU1" if any concurrent flash op has suspended the cache. Round numChunks up from ceil(maxSize / requested) until it evenly divides maxSize so every ISR-driven transmit uses the same DMA byte count and the panel_io recreate path is never taken from ISR. For typical RGB LED counts (n × 3 bytes per pixel) this terminates in a couple of iterations: e.g. 16 lanes × 256 LEDs (maxSize=768) with requested=90 → numChunks walks 9→10→11→12, settles on 12 × 64 input bytes (8192 DMA bytes per chunk). Co-Authored-By: Claude Opus 4.7 <[email protected]>
📝 WalkthroughWalkthroughThe PR hardens the ESP32 LCD driver stack against ISR and flash-cache suspension failures by adding IRAM safety annotations to critical DMA and completion handlers, implementing ISR-aware semaphore logic in the transmit path, increasing the default chunk size to 1024 bytes, and refactoring chunk calculation to ensure exact divisor boundaries. A separate change extends AutoResearch lane validation to 16 lanes. ChangesAutoResearch Lane Count Extension
ESP32 LCD Driver ISR/IRAM Hardening
Sequence DiagramsequenceDiagram
participant Caller
participant LcdSpiPeripheralEsp
participant RTOS as FreeRTOS
Caller->>LcdSpiPeripheralEsp: transmit(buffer, size)
LcdSpiPeripheralEsp->>RTOS: xPortInIsrContext()
alt ISR Context
RTOS-->>LcdSpiPeripheralEsp: true
LcdSpiPeripheralEsp->>RTOS: xSemaphoreTakeFromISR()
RTOS->>RTOS: portYIELD_FROM_ISR()
else Task Context
RTOS-->>LcdSpiPeripheralEsp: false
LcdSpiPeripheralEsp->>RTOS: xSemaphoreTake(2000ms)
end
alt Semaphore Available
RTOS-->>LcdSpiPeripheralEsp: acquired
LcdSpiPeripheralEsp-->>Caller: transmit DMA
else Timeout/Fail
LcdSpiPeripheralEsp->>LcdSpiPeripheralEsp: clear mBusy
LcdSpiPeripheralEsp-->>Caller: false
end
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related issues
Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
ChannelDriverLcdClockless::isrChunkDone calls back into esp_lcd_panel_io_tx_color() from the on_color_trans_done callback to re-arm the next chunk in the ring. With trans_queue_depth=1 the currently-running transaction has not yet been retired from the I80 queue when the user callback fires, so the re-arm blocks waiting for a free queue slot — from ISR context — and trips the 300 ms interrupt watchdog. Bumping the depth to 2 leaves room for the ISR-driven re-arm and eliminates the "Guru Meditation Error: Interrupt wdt timeout on CPU1" panic at every multi-chunk LCD_CLOCKLESS frame size (verified on 16 lanes × 256 LEDs WS2812B-V5 on XIAO ESP32-S3). Caveat: with this fix the device stays alive but ISR-driven chunks 2..N still emit garbled data on the wire (87.5–91.8 % byte corruption in RMT-RX loopback validation). The first chunk — encoded and submitted from task context in beginTransmission() — is correct. Tracking the correctness issue as a follow-up on #2647; this commit just gets the chip past the IWDT panic so the correctness work can proceed. Co-Authored-By: Claude Opus 4.7 <[email protected]>
…2647 corruption) Bump kDefaultChunkInputBytes 90 → 1024 so a typical RGB frame fits in one DMA chunk and the ISR re-arm path is not exercised. Background. The ISR-driven re-arm path in ChannelDriverLcdClockless::isrChunkDone currently emits garbled data for chunks 2..N — only the first chunk (encoded + submitted from beginTransmission() in task context) lands correctly on the wire. The defense-in-depth fixes earlier in this PR (FL_IRAM on the ISR path, xSemaphoreTakeFromISR, trans_queue_depth=2, equal-chunk sizing) all prevent the IWDT panic that previously occurred, but the multi-chunk output is still 87–92 % corrupted in RX-loopback validation. The correctness fix for multi-chunk streaming is tracked separately. This change makes 16 lanes × 256 LEDs WS2812B-V5 actually work on hardware today by selecting a chunk size that fits a 768-byte/lane frame in a single transaction. Worst-case PSRAM for 16-lane wave8 with 1024-byte chunks: 1024 × 128 × 3 ring buffers = 384 KB (XIAO ESP32-S3 has 8 MB PSRAM, plenty). Verified on hardware (Seeed XIAO ESP32-S3, GPIO 1 → GPIO 2 jumper): bash autoresearch esp32s3 --lcd --upload-port COM13 \ --lanes 16 --strip-sizes 256 --skip-lint TEST LCD_CLOCKLESS lanes=16 leds=4096 PASS 1694ms Tests: 4/4 passed Co-Authored-By: Claude Opus 4.7 <[email protected]>
There was a problem hiding this comment.
Actionable comments posted: 2
🧹 Nitpick comments (1)
src/platforms/esp/32/drivers/lcd_spi/channel_driver_lcd_clockless.h (1)
98-101: ⚡ Quick winUse text-only warning prefixes in these comments.
⚠️is now present in C++ source comments. Please switch those to a text prefix likeWARNING:to match repo style.As per coding guidelines, "Avoid emoticons and emoji characters in C++ source files - use text-based prefixes instead"
Also applies to: 113-118
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@src/platforms/esp/32/drivers/lcd_spi/channel_driver_lcd_clockless.h` around lines 98 - 101, Replace the emoji warning prefixes in the hot-path comment blocks with a text-only prefix: change the leading "⚠️" to "WARNING:" in the ISR/IRAM comment that mentions IRAM, FL_IRAM and the interrupt watchdog (the comment immediately above the hot-path/ISR note) and do the same for the similar comment later (the block that spans the second occurrence around the FL_IRAM/ISR explanation). Keep the rest of the wording intact (retain references to IRAM, FL_IRAM, ISR context, flash cache suspension, and interrupt watchdog) but use "WARNING:" as the prefix to comply with coding guidelines.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@examples/AutoResearch/AutoResearchRemote.cpp`:
- Around line 162-165: The remote's laneSizes upper bound change to 16 is
inconsistent with firmware validation (src/fl/channels/validation.cpp.hpp which
enforces max 8), unit tests (tests/fl/channels/validation.cpp), docs
(AutoResearchTest.h), and CI (ci/autoresearch/phases.py); either revert the
change in AutoResearchRemote.cpp (reset the laneSizes upper bound to 8 in the
validation block that sets "InvalidLaneCount") or update the firmware validation
function in validation.cpp.hpp, the unit test expectations in
tests/fl/channels/validation.cpp, the documentation in AutoResearchTest.h, and
the CI phase limits in ci/autoresearch/phases.py to accept up to 16 lanes so the
contract matches end-to-end.
In `@src/platforms/esp/32/drivers/lcd_spi/channel_driver_lcd_clockless.cpp.hpp`:
- Around line 369-385: The current loop increases numChunks until maxSize %
numChunks == 0 which can shrink chunkInputBytes unnecessarily; instead compute
chunkInputBytes by rounding up so we don't force many small chunks. Replace the
divisor-seeking loop logic around numChunks/chunkInputBytes: keep the initial
numChunks = ceil(maxSize / requestedChunkInputBytes), then set chunkInputBytes =
ceil_div(maxSize, numChunks) (i.e. (maxSize + numChunks - 1) / numChunks) and do
not increment numChunks to force an exact divisor; fall back to chunkInputBytes
= maxSize only if that computed chunkInputBytes is 0. Reference variables:
numChunks, chunkInputBytes, maxSize, requestedChunkInputBytes.
---
Nitpick comments:
In `@src/platforms/esp/32/drivers/lcd_spi/channel_driver_lcd_clockless.h`:
- Around line 98-101: Replace the emoji warning prefixes in the hot-path comment
blocks with a text-only prefix: change the leading "⚠️" to "WARNING:" in the
ISR/IRAM comment that mentions IRAM, FL_IRAM and the interrupt watchdog (the
comment immediately above the hot-path/ISR note) and do the same for the similar
comment later (the block that spans the second occurrence around the FL_IRAM/ISR
explanation). Keep the rest of the wording intact (retain references to IRAM,
FL_IRAM, ISR context, flash cache suspension, and interrupt watchdog) but use
"WARNING:" as the prefix to comply with coding guidelines.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: ad9af0bc-e53f-41f7-b422-4c05d60e4a79
📒 Files selected for processing (5)
examples/AutoResearch/AutoResearchRemote.cppsrc/platforms/esp/32/drivers/lcd_spi/channel_driver_lcd_clockless.cpp.hppsrc/platforms/esp/32/drivers/lcd_spi/channel_driver_lcd_clockless.hsrc/platforms/esp/32/drivers/lcd_spi/lcd_spi_peripheral_esp.cpp.hppsrc/platforms/esp/32/drivers/lcd_spi/lcd_spi_peripheral_esp.h
| if (lane_sizes_json.size() == 0 || lane_sizes_json.size() > 16) { | ||
| response.set("success", false); | ||
| response.set("error", "InvalidLaneCount"); | ||
| response.set("message", "laneSizes must have 1-8 elements"); | ||
| response.set("message", "laneSizes must have 1-16 elements"); |
There was a problem hiding this comment.
Contract mismatch: remote accepts 1–16 lanes, but firmware rejects > 8.
The remote now validates laneSizes with an upper bound of 16, but the firmware-side validation in src/fl/channels/validation.cpp.hpp still rejects lane_sizes.size() > 8 with error "Lane count cannot exceed 8" (context snippet 1). This creates a broken contract: RPC calls with 9–16 lanes will pass remote validation but fail at the firmware layer with a confusing error message.
Additional mismatches:
- Unit test in
tests/fl/channels/validation.cppexplicitly expects failure at 9 lanes (context snippet 2). - Documentation in
AutoResearchTest.hstill describes lane count as1-8(context snippet 3). - CI script
ci/autoresearch/phases.pyhard-limits--lane-countsto 1-8 and cannot generate 9–16 lane test cases (context snippet 4).
Either revert this change until the full stack (firmware validation, tests, docs, CI) supports 16 lanes, or ensure those layers are updated in the same PR to maintain contract consistency.
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@examples/AutoResearch/AutoResearchRemote.cpp` around lines 162 - 165, The
remote's laneSizes upper bound change to 16 is inconsistent with firmware
validation (src/fl/channels/validation.cpp.hpp which enforces max 8), unit tests
(tests/fl/channels/validation.cpp), docs (AutoResearchTest.h), and CI
(ci/autoresearch/phases.py); either revert the change in AutoResearchRemote.cpp
(reset the laneSizes upper bound to 8 in the validation block that sets
"InvalidLaneCount") or update the firmware validation function in
validation.cpp.hpp, the unit test expectations in
tests/fl/channels/validation.cpp, the documentation in AutoResearchTest.h, and
the CI phase limits in ci/autoresearch/phases.py to accept up to 16 lanes so the
contract matches end-to-end.
| // Pick numChunks so chunkInputBytes = maxSize / numChunks is exact (the | ||
| // remainder must be 0). Walk numChunks upward from | ||
| // ceil(maxSize / requested) until we hit a divisor; in the worst case we | ||
| // fall back to numChunks = maxSize (single-byte chunks). For typical LED | ||
| // counts (powers of two × 3 bytes per RGB pixel) the loop terminates in | ||
| // a couple of iterations. | ||
| size_t numChunks = | ||
| (maxSize + requestedChunkInputBytes - 1) / requestedChunkInputBytes; | ||
| if (numChunks == 0) { | ||
| numChunks = 1; | ||
| } | ||
| while (numChunks < maxSize && (maxSize % numChunks) != 0) { | ||
| ++numChunks; | ||
| } | ||
| size_t chunkInputBytes = maxSize / numChunks; | ||
| if (chunkInputBytes == 0) { | ||
| chunkInputBytes = maxSize; |
There was a problem hiding this comment.
Round chunkInputBytes up instead of shrinking it via numChunks.
This loop can collapse a near-1024-byte request into many small ISR-driven chunks. For example, maxSize = 1025 yields 5 chunks of 205 bytes, even though a single 1025-byte chunk would satisfy the equal-size requirement. That defeats the fallback meant to avoid the ISR re-arm path and raises interrupt load on awkward frame sizes.
Possible fix
- size_t numChunks =
- (maxSize + requestedChunkInputBytes - 1) / requestedChunkInputBytes;
- if (numChunks == 0) {
- numChunks = 1;
- }
- while (numChunks < maxSize && (maxSize % numChunks) != 0) {
- ++numChunks;
- }
- size_t chunkInputBytes = maxSize / numChunks;
- if (chunkInputBytes == 0) {
- chunkInputBytes = maxSize;
- }
+ size_t chunkInputBytes = requestedChunkInputBytes;
+ while (chunkInputBytes < maxSize &&
+ (maxSize % chunkInputBytes) != 0) {
+ ++chunkInputBytes;
+ }🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@src/platforms/esp/32/drivers/lcd_spi/channel_driver_lcd_clockless.cpp.hpp`
around lines 369 - 385, The current loop increases numChunks until maxSize %
numChunks == 0 which can shrink chunkInputBytes unnecessarily; instead compute
chunkInputBytes by rounding up so we don't force many small chunks. Replace the
divisor-seeking loop logic around numChunks/chunkInputBytes: keep the initial
numChunks = ceil(maxSize / requestedChunkInputBytes), then set chunkInputBytes =
ceil_div(maxSize, numChunks) (i.e. (maxSize + numChunks - 1) / numChunks) and do
not increment numChunks to force an exact divisor; fall back to chunkInputBytes
= maxSize only if that computed chunkInputBytes is 0. Reference variables:
numChunks, chunkInputBytes, maxSize, requestedChunkInputBytes.
Summary
Defense-in-depth fixes plus a chunk-sizing workaround for the LCD_CLOCKLESS ISR-driven chunked DMA path discussed in #2647. With this PR the typical-frame case (RGB frames that fit in a single DMA chunk) now passes the autoresearch RX-loopback matrix on a XIAO ESP32-S3; the multi-chunk corruption (chunks 2..N of an ISR-driven stream) is tracked separately as a #2647 follow-up.
Commits
7bfb1bb53f—fix(esp32-s3): make LCD_CLOCKLESS chunk-ISR path IRAM/ISR-safeisrChunkDone,encodeChunk,LcdSpiPeripheralEsp::transmit, and theEspWrapper::transmitthunkFL_IRAM(matches the PARLIOworkerIsrCallbackconvention).LcdSpiPeripheralEsp::transmit's completion-semaphore take throughxSemaphoreTakeFromISRwhenxPortInIsrContext(), keeping the existing 2 s blocking take on the task path.runSingleTest'slaneSizesupper bound 8 → 16 so autoresearch can exercise the full LCD_CAM 16-bit bus.821fd8919a—fix(esp32-s3): force LCD_CLOCKLESS chunks to equal DMA sizenumChunkssomaxSize / numChunksis exact, preventing the variable-size last chunk that forces an ISR-timeesp_lcd_panel_io_del+esp_lcd_new_panel_io_i80round-trip.eb59d1b491—fix(esp32-s3): bump LCD I80 trans_queue_depth 1 → 2 (#2647 IWDT fix)trans_queue_depth=1the ISR-driven re-arm inisrChunkDoneblocked waiting for a free queue slot — from ISR context — and tripped the 300 ms watchdog. Depth = 2 leaves room for the re-arm.8d791ad125—fix(esp32-s3): default LCD_CLOCKLESS chunk to 1024 B (workaround for #2647 corruption)kDefaultChunkInputBytes90 → 1024 so a typical RGB frame (≤ 341 LEDs/lane) fits in one DMA chunk and the still-broken ISR re-arm path is never exercised. Worst-case ring-buffer PSRAM for 16-lane wave8: 1024 × 128 × 3 = 384 KB (XIAO ESP32-S3 has 8 MB PSRAM).Status
esp32s3; CI green for all configured platforms.Test plan
bash autoresearch esp32s3 --lcd --upload-port COM<x> --lanes 16 --strip-sizes 256 --skip-lintcompletes withTEST_COMPLETED_EXIT_OK— verified.tests/platforms/esp/32/drivers/lcd_spi/channel_driver_lcd_clockless.cppmocks keep passing (CI: unit tests linux ✓).--lanes 1..8 --strip-sizes 100,300(smaller, previously-tested matrix) — recommended sanity check; not run in this PR but the kDefaultChunkInputBytes bump to 1024 only enlarges the single-chunk regime, so smaller frames remain in single-chunk mode and should be unaffected.🤖 Generated with Claude Code