Skip to content

Feat: support P4 Rev 3.2 & H4 (stub), update stub JSONs - #376

Merged
Jason2866 merged 4 commits into
Jason2866:developmentfrom
laride:feat/support-p4-rev3_2
Sep 2, 2026
Merged

Feat: support P4 Rev 3.2 & H4 (stub), update stub JSONs#376
Jason2866 merged 4 commits into
Jason2866:developmentfrom
laride:feat/support-p4-rev3_2

Conversation

@laride

@laride laride commented Jul 9, 2026

Copy link
Copy Markdown

This PR adds support for ESP32-P4 Rev 3.2 and ESP32-H4 (stub), upgrades all chip stubs to esp-flasher-stub v1.0.0, and fixes several issues related to flash operations and stub loading.

Changes

Stub Upgrade to esp-flasher-stub v1.0.0

All chip stub binaries have been updated from the legacy flasher stub to esp-flasher-stub v1.0.0.

Due to behavioral differences between the new stub and the legacy stub, readFlash parameters have been fine-tuned accordingly:

  • Web Serial (desktop): switched to FLASH_SECTOR_SIZE (4096) + maxInFlightPackets = 64, matching esptool's default READ_FLASH protocol
  • WebUSB (Android): fixed adaptive multiplier application; aligned variable naming with protocol semantics (blockSizepacketSize, maxInFlightmaxInFlightPackets)

ESP32-P4 Rev 3.2 (302) Support

  • Extended flash power-on sequence in powerOnFlash() to handle both Rev 3.1 (301) and Rev 3.2 (302):
    • Rev 3.2: Added efuse check — if the ROM has already asserted XPD in download mode, only the PMU force-on state is cleared and the full power-on sequence is skipped
    • Rev 3.1: Existing full power-on sequence is unchanged

ESP32-H4 Stub Support

  • Added esp32h4.json stub file
  • Added chip revision detection for H4

Bug Fixes / Improvements

  • Flash erase timeout: Changed from IS_STUB ? DEFAULT_TIMEOUT : timeoutPerMb(...) to timeoutPerMb(...)
  • flash_begin encrypted flag: Replaced the IS_STUB shortcut with an explicit chip allowlist, aligning more precisely with esptool's behavior
  • Stub loading reliability:
    • Wrapped memFinish in a try-catch to tolerate native USB chips that do not send a ROM MEM_END response after the stub takes over
    • Extended readPacket timeout from 500 ms to 2500 ms
    • Reset _commandLock after stub is loaded to prevent potential command queue deadlocks
  • stubs/index.ts: Added stubcode?.default ?? stubcode to correctly handle ES module default exports from JSON imports

Testing

Validated on the following hardware:

  • ESP32-S3
  • ESP32-P4 Rev 1.x
  • ESP32-P4 Rev 3.2
  • ESP32-H4

Summary by CodeRabbit

  • New Features

    • Added support for additional ESP32 variants and updated bundled device data for multiple chips.
    • Expanded handling for ESP32-P4 revisions and added support for ESP32-H4 stubs.
  • Bug Fixes

    • Improved flash power-up behavior on ESP32-P4 devices.
    • Refined stub loading and flash-read behavior for more reliable communication.
    • Updated chip revision detection and read-flash acknowledgements for better stability.

Copilot AI review requested due to automatic review settings July 9, 2026 09:17
@coderabbitai

coderabbitai Bot commented Jul 9, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

This PR adds ESP32-P4 revision 302 and ESP32H4 efuse-based power-on/revision handling in esp_loader.ts, refactors flashBegin/runStub/readFlash protocol logic, updates stub selection in index.ts to support new chip/revision mappings, regenerates embedded firmware stub JS modules with new hashed filenames, and updates stub JSON payloads including new ESP32S3 plugin data.

Changes

ESP32-P4/H4 stub and loader update

Layer / File(s) Summary
ESP32-P4/H4 efuse constants and chip revision handling
src/const.ts, src/esp_loader.ts
Adds P4 efuse register/mask constants and H4 block address, expands P4 power-on gating to revisions 301/302, updates H4 revision decoding, and short-circuits power-on for rev 302 based on efuse bit state.
flashBegin/runStub/readFlash protocol refactor
src/esp_loader.ts
Adjusts erase timeout and encrypted-word payload logic in flashBegin, tolerates missing MEM_END in runStub with a longer OHAI timeout, and refactors readFlash packet sizing and per-packet ACK behavior.
Stub selection and normalization logic
src/stubs/index.ts
Extends supported chip exclusion, adds ESP32H4 stub import, maps ESP32P4 revisions to distinct stub files, and normalizes loaded stub modules via a nested default fallback.
Stub JSON payload updates
src/stubs/*.json
Replaces entry/text/data payloads and offsets across chip stub files, adds new esp32h4.json and esp32p4-rev1.json, and adds plugin/nand handler metadata to esp32s3.json.
Regenerated embedded stub JS modules
js/modules/*.js
Replaces generated firmware segment modules with new hashed filenames and export shapes across all supported chip targets.

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

Possibly related PRs

Poem

A rabbit hops through stubs anew,
ESP32-P4 gets efuse clues too,
H4 revisions now decoded right,
ACKs sent packet by packet, light,
New plugins nested deep in JSON's burrow —
🐇 hop hop, ship it, no more sorrow!

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
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.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes the main change: P4 Rev 3.2 and H4 stub support plus stub JSON updates.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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/stubs/esp32s3.json`:
- Around line 8-28: The stub loader currently ignores the new NAND plugin
metadata, so the NAND blob never gets uploaded or dispatched. Update the stub
model in src/stubs/index.ts to include plugin_first_opcode, plugin_table_offset,
plugin_table_entries, and plugins.nand, then extend the upload logic in
src/esp_loader.ts to send the plugin section alongside text/data and preserve
the plugin dispatch table for the stub entry path. Use the existing stub shape
and esp_loader upload flow to locate the changes.
🪄 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: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 8085b657-5225-44c1-9eac-5b70e04d11b1

📥 Commits

Reviewing files that changed from the base of the PR and between 3ad3529 and 231a85c.

📒 Files selected for processing (46)
  • js/modules/esp32-BRKoi17y.js
  • js/modules/esp32-DW9TTzBw.js
  • js/modules/esp32c2-B2rZuByC.js
  • js/modules/esp32c2-Btgr_lwh.js
  • js/modules/esp32c3-CHKfoI8W.js
  • js/modules/esp32c3-DvICuzl3.js
  • js/modules/esp32c5-BDW4KtLo.js
  • js/modules/esp32c5-Dz0QiUXa.js
  • js/modules/esp32c6-Bl9Nuzrv.js
  • js/modules/esp32c6-il8tTxAG.js
  • js/modules/esp32c61-DFH7YXsW.js
  • js/modules/esp32c61-thKzxBGf.js
  • js/modules/esp32h2-BK2Oz0x8.js
  • js/modules/esp32h2-CxoUHv_P.js
  • js/modules/esp32h4-BOgv2e75.js
  • js/modules/esp32p4-CmXpP18K.js
  • js/modules/esp32p4-D3jLP-jY.js
  • js/modules/esp32p4-rev1-JwKc7e7M.js
  • js/modules/esp32p4r3-CqI71ojR.js
  • js/modules/esp32s2-9bABqiJs.js
  • js/modules/esp32s2-iX3WoDbg.js
  • js/modules/esp32s3-C9Iakqrc.js
  • js/modules/esp32s3-DGwDVIgz.js
  • js/modules/esp32s31-Cu0bI6XV.js
  • js/modules/esp32s31-cyImlcoa.js
  • js/modules/esp8266-CUwxJpGa.js
  • js/modules/esp8266-DI9O9acR.js
  • js/modules/esptool.js
  • src/const.ts
  • src/esp_loader.ts
  • src/stubs/esp32.json
  • src/stubs/esp32c2.json
  • src/stubs/esp32c3.json
  • src/stubs/esp32c5.json
  • src/stubs/esp32c6.json
  • src/stubs/esp32c61.json
  • src/stubs/esp32h2.json
  • src/stubs/esp32h4.json
  • src/stubs/esp32p4-rev1.json
  • src/stubs/esp32p4.json
  • src/stubs/esp32p4r3.json
  • src/stubs/esp32s2.json
  • src/stubs/esp32s3.json
  • src/stubs/esp32s31.json
  • src/stubs/esp8266.json
  • src/stubs/index.ts
💤 Files with no reviewable changes (14)
  • js/modules/esp32c3-CHKfoI8W.js
  • src/stubs/esp32p4r3.json
  • js/modules/esp32p4-D3jLP-jY.js
  • js/modules/esp32s3-DGwDVIgz.js
  • js/modules/esp32c5-BDW4KtLo.js
  • js/modules/esp32p4r3-CqI71ojR.js
  • js/modules/esp8266-CUwxJpGa.js
  • js/modules/esp32c61-thKzxBGf.js
  • js/modules/esp32c6-il8tTxAG.js
  • js/modules/esp32c2-Btgr_lwh.js
  • js/modules/esp32s31-cyImlcoa.js
  • js/modules/esp32-BRKoi17y.js
  • js/modules/esp32h2-CxoUHv_P.js
  • js/modules/esp32s2-iX3WoDbg.js

Comment thread src/stubs/esp32s3.json

Copilot AI 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.

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

This PR updates stub handling and loader behavior to add ESP32-H4 stub support, improve ESP32-P4 revision-specific flash power handling, and align flash read/erase protocols with expected ROM/stub semantics.

Changes:

  • Add ESP32-H4 and ESP32-P4 Rev1 stub JSONs; update stub selection logic (including JSON default-export normalization).
  • Extend ESPLoader with ESP32-H4 revision detection and broaden ESP32-P4 flash power-on handling to Rev 3.1/3.2.
  • Adjust stub startup robustness and refine erase/read-flash protocol behavior (timeouts, parameter semantics, ACK strategy).

Reviewed changes

Copilot reviewed 31 out of 46 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
src/stubs/index.ts Adds JSON default normalization and updates chip-family stub mapping (ESP32-H4, ESP32-P4 rev split).
src/stubs/esp32p4r3.json Removes the legacy ESP32-P4 Rev3 stub JSON.
src/stubs/esp32p4-rev1.json Adds a dedicated ESP32-P4 Rev1 stub JSON.
src/stubs/esp32h4.json Adds ESP32-H4 stub JSON.
src/stubs/esp32c2.json Updates embedded stub payload values.
src/stubs/esp32.json Updates embedded stub payload values.
src/esp_loader.ts Adds H4 revision decoding, expands P4 flash power-on logic, and updates erase/read-flash/stub-start flows.
src/const.ts Adds ESP32-P4 efuse constants used for Rev 3.2 handling.
js/modules/esp8266-CUwxJpGa.js Removes a bundled stub module artifact.
js/modules/esp32s31-cyImlcoa.js Removes a bundled stub module artifact.
js/modules/esp32s3-DGwDVIgz.js Removes a bundled stub module artifact.
js/modules/esp32s2-iX3WoDbg.js Removes a bundled stub module artifact.
js/modules/esp32p4r3-CqI71ojR.js Removes a bundled stub module artifact.
js/modules/esp32p4-D3jLP-jY.js Removes a bundled stub module artifact.
js/modules/esp32h2-CxoUHv_P.js Removes a bundled stub module artifact.
js/modules/esp32c61-thKzxBGf.js Removes a bundled stub module artifact.
js/modules/esp32c6-il8tTxAG.js Removes a bundled stub module artifact.
js/modules/esp32c5-BDW4KtLo.js Removes a bundled stub module artifact.
js/modules/esp32c3-CHKfoI8W.js Removes a bundled stub module artifact.
js/modules/esp32c2-Btgr_lwh.js Removes a bundled stub module artifact.
js/modules/esp32-BRKoi17y.js Removes a bundled stub module artifact.
Files not reviewed (14)
  • js/modules/esp32-DW9TTzBw.js: Generated file
  • js/modules/esp32c2-B2rZuByC.js: Generated file
  • js/modules/esp32c3-DvICuzl3.js: Generated file
  • js/modules/esp32c5-Dz0QiUXa.js: Generated file
  • js/modules/esp32c6-Bl9Nuzrv.js: Generated file
  • js/modules/esp32c61-DFH7YXsW.js: Generated file
  • js/modules/esp32h2-BK2Oz0x8.js: Generated file
  • js/modules/esp32h4-BOgv2e75.js: Generated file
  • js/modules/esp32p4-CmXpP18K.js: Generated file
  • js/modules/esp32p4-rev1-JwKc7e7M.js: Generated file
  • js/modules/esp32s2-9bABqiJs.js: Generated file
  • js/modules/esp32s3-C9Iakqrc.js: Generated file
  • js/modules/esp32s31-Cu0bI6XV.js: Generated file
  • js/modules/esp8266-DI9O9acR.js: Generated file
Comments suppressed due to low confidence (1)

src/stubs/index.ts:1

  • Adding default?: LoadedStub to the stub payload type conflates the JSON data shape with the dynamic-import module wrapper shape. This makes the type recursive and also makes it easy to accidentally carry a default property through the returned stub object. Prefer modeling imports as a union (e.g., LoadedStub | { default: LoadedStub }) or a separate LoadedStubModule type, and keep LoadedStub describing only the JSON payload fields.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/esp_loader.ts Outdated
Comment thread src/esp_loader.ts Outdated
@Jason2866

Copy link
Copy Markdown
Owner

Thx!

@Jason2866
Jason2866 merged commit 16624ad into Jason2866:development Sep 2, 2026
1 check passed
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