Skip to content

fix(esp8266): stop shadowing vendor IO_MUX macros — GPIO12 hung addLeds() (#3735) - #3787

Merged
zackees merged 1 commit into
masterfrom
fix/3735-esp8266-gpf-iomux
Jul 31, 2026
Merged

zackees merged 1 commit into
masterfrom
fix/3735-esp8266-gpf-iomux

Conversation

@zackees

@zackees zackees commented Jul 31, 2026 •

Copy link
Copy Markdown
Member

Fixes #3735.

Root cause

pin_esp8266_native.hpp #undefs 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:

#define GPF(p) (*esp8266_gpioToFn[(p & 0xF)])

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 took flash chip-select away from SPI0; the next instruction-cache miss couldn't be serviced, and the chip hung inside addLeds() until the watchdog fired.

That reproduces the reporter's table exactly:

pins buggy offset lands on result
GPIO12 0x30 SD_CMD (flash) hang
GPIO13 0x34 GPIO0 mux harmless
GPIO14 0x38 GPIO2 mux harmless
GPIO15 0x3C GPIO4 mux harmless

Every 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 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 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 (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 is agents/docs/register-maps.md's "use the vendor PAL, don't hand-roll shims" applied to the file that violated it. 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_ASSERTs pin the vendor semantics so a future shim can't silently take over.

Regression window

Introduced by 44cf5594a0 ("native pin implementations"), which git tag --contains places 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's pinMode, which uses the correct table.

Validation — please read

I could not exercise this end-to-end:

What I did instead:

  1. Compiled the affected TU against the real 3.1.2 core headers with the real xtensa toolchain, -Wall -Wextra — clean, exit 0, no macro-redefinition warnings.
  2. Wrote six static assertions of the vendor semantics and confirmed they fail on master and pass with this change — so the fix demonstrably alters behaviour rather than merely compiling. Those assertions are now committed in the header.

Verified GPF, GPFFS_GPIO, GP16FPD against 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.yml not running on PRs means ESP8266 regressions can only be caught after they land on master.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • Improved ESP8266 GPIO pin configuration across Arduino and non-Arduino builds.
    • Ensured pull-up mode retains the correct pin function selection.
    • Improved handling for GPIO pins 0–15 and GPIO16.

…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]>
@coderabbitai

coderabbitai Bot commented Jul 31, 2026 •

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

The ESP8266 native GPIO implementation now supports Arduino and non-Arduino builds with vendor-defined IO_MUX mappings and GPIO function values. pinMode uses these values for pins 0–15, including pull-up configuration, with compile-time checks for GPIO semantics.

Changes

ESP8266 GPIO configuration

Layer / File(s) Summary
GPIO mapping and pin mode configuration
src/platforms/esp/8266/pin_esp8266_native.hpp
The implementation retains the linear GPIO control-register mapping and uses vendor IO_MUX mappings. Compile-time assertions validate GPIO function fields and GPIO16 semantics. pinMode uses vendor-derived function values and preserves them when enabling pull-ups.

Estimated code review effort: 3 (Moderate) | ~20 minutes

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly identifies the main fix: removing shadowed ESP8266 IO_MUX macros that caused GPIO12 to hang in addLeds().
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches 💡 1
📝 Generate docstrings 💡
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/3735-esp8266-gpf-iomux

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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

📥 Commits

Reviewing files that changed from the base of the PR and between acd4d84 and 7cde5e9.

📒 Files selected for processing (1)
  • src/platforms/esp/8266/pin_esp8266_native.hpp

@zackees
zackees merged commit c3564c4 into master Jul 31, 2026
11 of 13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

ESP8266: FastLED 3.10.5 watchdog reset in addLeds() when GPIO12 (D6) is used with P9813

1 participant