Skip to content

Refactor: Hybrid Compile sdkconfig handling#477

Merged
Jason2866 merged 6 commits into
developfrom
refac_psram
Apr 15, 2026
Merged

Refactor: Hybrid Compile sdkconfig handling#477
Jason2866 merged 6 commits into
developfrom
refac_psram

Conversation

@Jason2866
Copy link
Copy Markdown

@Jason2866 Jason2866 commented Apr 14, 2026

Checklist:

  • The pull request is done against the latest develop branch
  • Only relevant files were touched
  • Only one feature/fix was added per PR, more changes are allowed when changing boards.json
  • I accept the CLA

Summary by CodeRabbit

  • Bug Fixes

    • More robust board configuration detection and validation for SDK generation, including expanded and earlier PSRAM detection and safer ordering to avoid misconfiguration.
    • Added ESP32 bootloader SPI write-protect pin behavior when PSRAM is absent.
    • Changed MCU and flash-mode defaults to no preset, deferring to board or user settings.
  • Refactor

    • Removed duplicated detection logic and streamlined config generation sequencing.
  • Bug Fix / Chore

    • Fixed duplicate SDK config flag handling so later/user entries take precedence.

Refactor PSRAM detection logic and configure PSRAM frequency only if supported.
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Apr 14, 2026

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: e6bdf686-90bf-4ee8-89cf-6aa9ea1a3219

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
📝 Walkthrough

Walkthrough

The change refactors ESP-IDF sdkconfig generation to detect board-specific configurations more comprehensively, moves PSRAM detection earlier in the process with improved heuristics, makes PSRAM frequency flag generation conditional on PSRAM presence, and adds ESP32-specific bootloader WP pin handling.

Changes

Cohort / File(s) Summary
PSRAM Detection & Board Config
builder/frameworks/espidf.py
Expanded has_board_specific_config() with explicit checks for build.f_cpu, build.f_flash, flash_mode, and upload.flash_size. Refactored generate_board_specific_config() to detect PSRAM earlier using defensive string/list handling and additional heuristics (memory type, build.psram_type, extra flags). Made PSRAM frequency flag generation (CONFIG_SPIRAM_SPEED*) conditional on PSRAM presence. Removed duplicate PSRAM recomputation. Added ESP32-only bootloader WP pin override when PSRAM is absent.

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~50 minutes

Possibly related PRs

Poem

🐰 PSRAM hops earlier now, detection's so keen,
Bootloader pins aligned, the cleanest we've seen,
Flags flash conditional, no duplicates linger,
Board configs explicit—a builder's quick finger!

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 inconclusive)

Check name Status Explanation Resolution
Title check ❓ Inconclusive The title 'Refactor: Hybrid Compile sdkconfig handling' is vague and does not clearly summarize the main changes in the pull request. Consider a more specific title that reflects the primary change, such as 'Improve PSRAM detection and sdkconfig generation for ESP32' or 'Refactor PSRAM handling and add bootloader config for ESP32 without PSRAM'.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch refac_psram

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 and usage tips.

@Jason2866
Copy link
Copy Markdown
Author

@coderabbitai full review

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Apr 14, 2026

✅ Actions performed

Full review triggered.

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

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 the current code and only fix it if needed.

Inline comments:
In `@builder/frameworks/espidf.py`:
- Around line 257-258: The PSRAM detection wrongly assumes extra_flags is a
list; coerce board.get("build.extra_flags", []) to a list when it's a string
before iterating so has_psram is computed correctly. Update the code around
extra_flags / has_psram in builder/frameworks/espidf.py to mirror the handling
used in generate_board_specific_config(): if extra_flags is a str, convert it to
a single-element list (or split it the same way generate_board_specific_config
does) and then compute has_psram = any("-DBOARD_HAS_PSRAM" in flag for flag in
extra_flags).
🪄 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: defaults

Review profile: CHILL

Plan: Pro

Run ID: d227663a-8b1d-460e-9ab4-0e77637e6449

📥 Commits

Reviewing files that changed from the base of the PR and between fb230b1 and ee79d1a.

📒 Files selected for processing (1)
  • builder/frameworks/espidf.py

Comment thread builder/frameworks/espidf.py Outdated
Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (1)
builder/frameworks/espidf.py (1)

453-459: Minor inconsistency: Default value differs from has_board_specific_config().

At line 454, the default is "" (empty string), while at line 257 in has_board_specific_config(), the default is [] (empty list). Although both work correctly due to the isinstance() checks, using a consistent default would improve maintainability.

♻️ Optional: Use consistent default value
         # Check for PSRAM support based on board flags (needed before frequency config)
-        extra_flags = board.get("build.extra_flags", "")
+        extra_flags = board.get("build.extra_flags", [])
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@builder/frameworks/espidf.py` around lines 453 - 459, The code checks
board.get("build.extra_flags", "") and treats it as str or list; to be
consistent with has_board_specific_config(), change the default from "" to [] so
both use an empty list default; update the call in the block that sets
extra_flags and the subsequent isinstance checks around has_psram to work with
the new default (keep the current logic that handles str vs list) and reference
the same "build.extra_flags" key used in has_board_specific_config().
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@builder/frameworks/espidf.py`:
- Around line 251-252: The current check uses flash_mode =
board.get("build.flash_mode", "dio") and then if flash_mode: return True which
is always true; instead detect whether flash_mode was explicitly set in the
board manifest by testing the presence of the key (e.g., use board.get("build")
and check "flash_mode" in that dict or call board.has_key / board.get_raw
depending on API) and only return True when the manifest contains an explicit
"build.flash_mode"; update the conditional in the function that uses flash_mode
(the block with "if flash_mode: return True" in builder/frameworks/espidf.py) so
the subsequent PSRAM and memory-type logic (lines handling psram, flash_type
checks) can run when flash_mode is not explicitly provided, or alternatively
remove the early return and adjust the docstring to reflect that all boards
trigger sdkconfig generation—use the symbols flash_mode and
board.get/build.flash_mode to locate the code to change.

---

Nitpick comments:
In `@builder/frameworks/espidf.py`:
- Around line 453-459: The code checks board.get("build.extra_flags", "") and
treats it as str or list; to be consistent with has_board_specific_config(),
change the default from "" to [] so both use an empty list default; update the
call in the block that sets extra_flags and the subsequent isinstance checks
around has_psram to work with the new default (keep the current logic that
handles str vs list) and reference the same "build.extra_flags" key used in
has_board_specific_config().
🪄 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: defaults

Review profile: CHILL

Plan: Pro

Run ID: 31a35503-c089-4e79-85c9-64ade5776bc6

📥 Commits

Reviewing files that changed from the base of the PR and between ee79d1a and ed22381.

📒 Files selected for processing (1)
  • builder/frameworks/espidf.py

Comment thread builder/frameworks/espidf.py
Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🧹 Nitpick comments (2)
builder/frameworks/espidf.py (2)

758-777: Deduplication is still partial for flags not present in template lines.

Current logic deduplicates only when a source sdkconfig line is matched. Duplicate custom flags that don’t exist in the template can still be appended multiple times at Line 783 onward. Pre-dedup idf_config_flags by flag name (keep last) before the template loop.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@builder/frameworks/espidf.py` around lines 758 - 777, The code currently only
removes duplicates when a template line matches; before iterating the template
you should pre-deduplicate idf_config_flags by flag name so only the last
occurrence is kept: build a map keyed by
extract_flag_name(idf_config_flags[i].replace("'", "")) and retain the last
index/value, then reconstruct idf_config_flags from that map (preserving the
chosen last entries) so later logic that pops and appends (references:
idf_config_flags, extract_flag_name, last_match_idx, flag_replaced, dst.write)
cannot append duplicate custom flags that never matched a template line.

657-661: Avoid duplicating WP defaults already provided by board sdkconfig.

boards/esp32-solo1.json already defines these two flags; unconditional append here can produce duplicate entries. Consider skipping auto-add when board espidf.custom_sdkconfig already sets either WP flag.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@builder/frameworks/espidf.py` around lines 657 - 661, The code
unconditionally appends WP defaults when mcu == "esp32" which can duplicate
flags already defined by the board's espidf.custom_sdkconfig; modify the mcu ==
"esp32" block to first inspect the board's espidf.custom_sdkconfig (and/or
existing board_config_flags) for the two entries ("#
CONFIG_BOOTLOADER_SPI_CUSTOM_WP_PIN is not set" and
"CONFIG_BOOTLOADER_SPI_WP_PIN=7") and only extend board_config_flags with each
entry if it is not already present in custom_sdkconfig or board_config_flags;
reference the mcu check and the board_config_flags list in your change.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@builder/frameworks/espidf.py`:
- Around line 79-86: The code calls mcu.lower() without ensuring mcu is set,
which can raise AttributeError; update the block around mcu and idf_variant so
you validate mcu (from board.get("build.mcu", None)) before calling .lower(): if
mcu is missing or falsy, raise a clear ValueError (or RuntimeError) with a
concise message about the missing build.mcu in the board manifest instead of
letting .lower() fail, and only assign idf_variant = mcu.lower() when mcu is a
non-empty string.
- Around line 453-474: The PSRAM detection is overbroad because it checks "opi"
anywhere in the raw memory_type string; update the logic in the block that
computes has_psram (using variables memory_type and extra_flags) to first ensure
memory_type is a string, split it on "_" and inspect the psram_memory_type part
(e.g., second segment) rather than the whole string, and only set has_psram=True
when that parsed psram_memory_type equals or contains "opi" or "psram"; keep the
other checks (build.psram_type and PSRAM in extra_flags) intact.

---

Nitpick comments:
In `@builder/frameworks/espidf.py`:
- Around line 758-777: The code currently only removes duplicates when a
template line matches; before iterating the template you should pre-deduplicate
idf_config_flags by flag name so only the last occurrence is kept: build a map
keyed by extract_flag_name(idf_config_flags[i].replace("'", "")) and retain the
last index/value, then reconstruct idf_config_flags from that map (preserving
the chosen last entries) so later logic that pops and appends (references:
idf_config_flags, extract_flag_name, last_match_idx, flag_replaced, dst.write)
cannot append duplicate custom flags that never matched a template line.
- Around line 657-661: The code unconditionally appends WP defaults when mcu ==
"esp32" which can duplicate flags already defined by the board's
espidf.custom_sdkconfig; modify the mcu == "esp32" block to first inspect the
board's espidf.custom_sdkconfig (and/or existing board_config_flags) for the two
entries ("# CONFIG_BOOTLOADER_SPI_CUSTOM_WP_PIN is not set" and
"CONFIG_BOOTLOADER_SPI_WP_PIN=7") and only extend board_config_flags with each
entry if it is not already present in custom_sdkconfig or board_config_flags;
reference the mcu check and the board_config_flags list in your change.
🪄 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: defaults

Review profile: CHILL

Plan: Pro

Run ID: 5578e1ec-9202-4af4-8a46-f913896ee96a

📥 Commits

Reviewing files that changed from the base of the PR and between ee79d1a and 4e824d6.

📒 Files selected for processing (1)
  • builder/frameworks/espidf.py

Comment thread builder/frameworks/espidf.py
Comment thread builder/frameworks/espidf.py
Added error handling for missing 'build.mcu' field in board manifest.
Add linker script for ESP32 when PSRAM is not configured.
@Jason2866
Copy link
Copy Markdown
Author

@coderabbitai full review

@Jason2866 Jason2866 merged commit c1113ed into develop Apr 15, 2026
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.

1 participant