Repository navigation
fix(esp8266): stop shadowing vendor IO_MUX macros — GPIO12 hung addLeds() (#3735) - #3787
Conversation
…ds() (#3735) `pin_esp8266_native.hpp` `#undef`s the ESP8266 core's `GPF()` and installs its own: #define GPF(p) ESP8266_REG(0x800 + ((p & 0xF) * 4)) The IO_MUX block is ordered by PAD NAME, not by GPIO number, which is why the vendor indexes a lookup table instead. GPIO12's mux register is at offset 0x04 (MTDI); the linear formula sends it to 0x30 — SD_CMD, one of the pads wired to the external SPI flash. Configuring GPIO12 as a LED pin therefore took flash chip-select away from SPI0, the next instruction-cache miss could not be serviced, and the chip hung inside `addLeds()` until the watchdog fired. That is exactly the reporter's table: GPIO13/14/15 land on the harmless GPIO0/2/4 mux registers and work; every combination involving GPIO12 hangs. Note `GPC(p)` one line above uses the same linear formula and is CORRECT — the GPIO_PIN block really is indexed by GPIO number. Only IO_MUX is pad-ordered. That asymmetry is what the original shim missed. Two further defects of the same kind, in the same file: - `GPFFS_GPIO(p)` was defined as a constant 0. Which function number means "GPIO" is pad-dependent — 0 for GPIO0/2/4/5, 1 for GPIO16, 3 for the rest, because MTDI/MTCK/MTMS/MTDO default to JTAG on function 0. `pinMode` also wrote a literal 0 rather than the function select. Independently of the register offset, this would have left GPIO12-15 mute. - `GP16FPD` was defined as 6, but bit 6 of the GPIO16 function register is function-select bit 2; the pull-down is bit 3. Enabling the GPIO16 pull-down re-muxed the pad instead. `GP16FFS(f)` likewise dropped the split field layout, unnoticed only because every call site passes 0. Fix: stop redefining `GPF`, `GPFFS`, `GPFFS_GPIO`, `GP16FFS` and `GP16FPD`, and use the vendor definitions already in scope via `<esp8266_peri.h>` — per agents/docs/register-maps.md, use the vendor PAL rather than hand-rolled shims. `pinMode` now writes `GPFFS(GPFFS_GPIO(pin))`, matching the core's wiring_digital.cpp that this file is documented as being based on. `FL_STATIC_ASSERT`s pin the vendor semantics so a future shim cannot silently take over: all six fail against the pre-fix definitions. Regression window: introduced by 44cf559 (native pin implementations), which is in 3.10.4 and 3.10.5 but not 3.10.3 — matching the reporter's "works on 3.9.12, watchdog on 3.10.5". Before it, setOutput() called the core's pinMode, which uses the correct table. Also corrects the file's docstring: it claimed to be for non-Arduino builds only, but pin_esp8266.hpp uses it for both — which made this look unreachable from an Arduino-core sketch when it was not. Validation: no ESP8266 hardware here, and the esp8266 workflow has no pull_request trigger, so this could not be exercised end-to-end. What was done instead: compiled the affected translation unit against the real 3.1.2 core headers with the real xtensa toolchain (-Wall -Wextra, clean, no macro redefinition warnings), and confirmed the six static assertions FAIL on master and PASS with this change. Bench confirmation on an ESP8266 with GPIO12 is still worth doing before release. Co-Authored-By: Claude <[email protected]>
📝 WalkthroughWalkthroughThe ESP8266 native GPIO implementation now supports Arduino and non-Arduino builds with vendor-defined IO_MUX mappings and GPIO function values. ChangesESP8266 GPIO configuration
Estimated code review effort: 3 (Moderate) | ~20 minutes 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches 💡 1📝 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 |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 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 `@src/platforms/esp/8266/pin_esp8266_native.hpp`:
- Around line 158-165: Update the Input-mode comment near the GPC(pin)
configuration to accurately state that the GPCD bit enables open-drain, matching
the existing behavior and the InputPullup path; do not change the register
assignment.
🪄 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 Plus
Run ID: 5c095ee3-fc89-4ef7-9177-fb70db9491dc
📒 Files selected for processing (1)
src/platforms/esp/8266/pin_esp8266_native.hpp
Fixes #3735.
Root cause
pin_esp8266_native.hpp#undefs the ESP8266 core'sGPF()and installs its own:The IO_MUX block is ordered by pad name, not by GPIO number — which is why the vendor indexes a lookup table instead:
GPIO12's mux register is at offset
0x04(MTDI). The linear formula sends it to0x30— SD_CMD, one of the pads wired to the external SPI flash. Configuring GPIO12 as a LED pin took flash chip-select away from SPI0; the next instruction-cache miss couldn't be serviced, and the chip hung insideaddLeds()until the watchdog fired.That reproduces the reporter's table exactly:
0x30SD_CMD (flash)0x34GPIO0 mux0x38GPIO2 mux0x3CGPIO4 muxEvery reported failing combination involves GPIO12; every passing one doesn't.
The subtlety that caused this:
GPC(p)one line above uses the same linear formula and is correct — the GPIO_PIN block really is indexed by GPIO number. Only IO_MUX is pad-ordered.Two more defects of the same kind, same file
GPFFS_GPIO(p)was a constant0. Which function number means "GPIO" is pad-dependent — 0 for GPIO0/2/4/5, 1 for GPIO16, 3 for the rest, because MTDI/MTCK/MTMS/MTDO default to JTAG on function 0.pinModealso wrote a literal0rather than the function select. Independently of the register offset, this would have left GPIO12–15 mute.GP16FPDwas6, but bit 6 of the GPIO16 function register is function-select bit 2; the pull-down is bit 3. Enabling the GPIO16 pull-down re-muxed the pad instead.GP16FFS(f)likewise dropped the split field layout (bits 0–1 plus bit 6), unnoticed only because every call site passes 0.Fix
Stop redefining
GPF,GPFFS,GPFFS_GPIO,GP16FFS,GP16FPD— use the vendor definitions already in scope via<esp8266_peri.h>. This isagents/docs/register-maps.md's "use the vendor PAL, don't hand-roll shims" applied to the file that violated it.pinModenow writesGPFFS(GPFFS_GPIO(pin)), matching the core'swiring_digital.cppthat this file is documented as being based on.FL_STATIC_ASSERTs pin the vendor semantics so a future shim can't silently take over.Regression window
Introduced by
44cf5594a0("native pin implementations"), whichgit tag --containsplaces in 3.10.4 and 3.10.5 but not 3.10.3 — matching the report of "works on 3.9.12, watchdog on 3.10.5". Before it,setOutput()called the core'spinMode, which uses the correct table.Validation — please read
I could not exercise this end-to-end:
build_esp8266.ymlhas nopull_requesttrigger (onlypushto master), so this PR's CI will not compile it;bash compile esp8266fails onfs_sdcard_arduino.cpp.hpp: SPI.h: No such file— reproduced identically on unmodified master, so pre-existing and unrelated (same fbuild SPI-resolution class as teensy4: drop the Arduino SPI library dependency from SpiHw2/4MXRT1062 (local fork, like PJRC audio + SdFat) — real fix for #3775 #3777).What I did instead:
-Wall -Wextra— clean, exit 0, no macro-redefinition warnings.Verified
GPF,GPFFS_GPIO,GP16FPDagainst the actual vendor header rather than from memory.Bench confirmation on an ESP8266 with GPIO12 driving a clocked chipset is still worth doing before release. The reasoning is solid and the mechanism explains every data point in the report, but this is pad-mux code and I'd rather flag the gap than imply coverage I don't have.
Worth a separate look:
build_esp8266.ymlnot running on PRs means ESP8266 regressions can only be caught after they land on master.🤖 Generated with Claude Code
Summary by CodeRabbit