Skip to content

fix(watchdog/apollo3): drop non-existent RSTGEN::CLRSTAT + correct STAT bits (Closes #2787) - #2788

Merged
zackees merged 1 commit into
masterfrom
fix/apollo3-watchdog-rstgen-bits
Jun 5, 2026
Merged

zackees merged 1 commit into
masterfrom
fix/apollo3-watchdog-rstgen-bits

Conversation

@zackees

@zackees zackees commented Jun 5, 2026 •

Copy link
Copy Markdown
Member

Summary

Fixes both Apollo3 workflows (`build_apollo3_red.yml` and `build_apollo3_thing_explorable.yml`) that were red on master with:

```
src/platforms/apollo3/watchdog_apollo3.impl.hpp:96:17: error:
'struct RSTGEN_Type' has no member named 'CLRSTAT'
```

Closes #2787. Continues the README badge sweep started in #2779 / #2780 (AVR) / #2785 (STM32 GIGA).

What's broken

Two bugs in `src/platforms/apollo3/watchdog_apollo3.impl.hpp` against the SparkFun Arduino_Apollo3 v2.2.1 CMSIS layout of `RSTGEN_Type` (defined in `apollo3.h`):

  1. Compile error — line 96 wrote `RSTGEN->CLRSTAT = 1u;`, but `RSTGEN_Type` has no `CLRSTAT` member. The struct contains `CFG`, `SWPOI`, `SWPOR`, `TPIURST`, `INTEN`/`INTSTAT`/`INTCLR`/`INTSET` (BODH brown-out interrupt only), and `STAT` — and there is no clear register for `STAT`. Every `STAT_b` field is `(SBL)`-tagged: the Secondary Boot Loader latches it at boot and software cannot clear it.

  2. Runtime bug (latent) — the bit-position table in the comment and the `if/else` chain had bits 6 and 7 wrong:

Bit Code said Actual hardware (`STAT_b` in CMSIS)
6 `POISTAT` POR-init `WDRSTAT` watchdog
7 `WDRSTAT` watchdog `BOUSTAT` unregulated brown-out

So `if (stat & (1u << 7)) cause = WATCHDOG` was actually reading the unregulated-brownout bit, and `else if (stat & (1u << 6)) cause = POWER_ON` was reading the watchdog bit. Had this ever compiled, `lastResetWasWatchdog()` would never have returned true after a real watchdog reset, and would have falsely reported watchdog after an unregulated brown-out. The CLRSTAT compile failure inadvertently protected callers from shipping broken behavior.

What this PR changes

`src/platforms/apollo3/watchdog_apollo3.impl.hpp`:

  • Remove the impossible `RSTGEN->CLRSTAT = 1u;` write. Re-read protection is already provided by the `cause_cached` flag in `Apollo3WatchdogState`, so the watchdog crash counter cannot inflate from stale bits.
  • Re-derive the bit-position table from `RSTGEN_Type::STAT_b` in `apollo3.h` and update both the comment and the `if/else` conditions.
  • Map all Apollo3 brown-out variants (general `BORSTAT` bit 2, unregulated `BOUSTAT` bit 7, core-regulator `BOCSTAT` bit 8, memory-regulator `BOFSTAT` bit 9, BLE/burst `BOBSTAT` bit 10) to `ResetCause::BROWNOUT` so every flavor of brown-out reports consistently.

Test plan

Local verification with `bash compile` was blocked by a separate Windows-only fbuild toolchain cache issue (arm-none-eabi-gcc 9.2.1 cache for the SparkFun Apollo3 build missing `as.exe`). The failure fires inside the Arduino_Apollo3 core's `itoa.c` before any FastLED translation unit is compiled, and other arm-none-eabi-gcc boards in the same workspace (`stm32f103c8`, `nucleo_f429zi`, etc.) compile cleanly — i.e. unrelated to this change.

This PR's CI on apollo3_red / apollo3_thing_explorable is the authoritative verification. The compile error being fixed is unambiguous (`RSTGEN_Type` has no `CLRSTAT` member), and this PR removes the exact line that produced it; the bit-position corrections are verified by inspection against the CMSIS header.

`bash lint` clean.

Scope

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • Enhanced reset-cause detection with improved bit-mapping logic to accurately identify watchdog resets, brown-out detection events, power-on resets, external resets, debugger resets, and software-triggered resets. Now provides more precise diagnostic information for system troubleshooting and debugging.
    • Optimized reset-status handling by removing unnecessary sticky-bit clearing operations, resulting in reduced complexity and improved system efficiency.

…AT bits

`platforms/apollo3/watchdog_apollo3.impl.hpp` had two bugs against the
SparkFun Arduino_Apollo3 v2.2.1 CMSIS layout of `RSTGEN_Type`:

1. **Compile error** — `RSTGEN->CLRSTAT = 1u;` at line 96 referenced a
   register member that does not exist. `RSTGEN_Type` contains
   `CFG`/`SWPOI`/`SWPOR`/`TPIURST`/`INTEN`/`INTSTAT`/`INTCLR`/`INTSET`
   (last group is the BODH interrupt only) and `STAT`. No clear register
   exists for `STAT`; every `STAT` bit is documented as `(SBL)`, meaning
   the Secondary Boot Loader latches it at boot and software cannot clear
   it. This broke `bash compile apollo3_red` /
   `bash compile apollo3_thing_explorable` and the matching GitHub
   Actions workflows.

2. **Runtime bug (latent)** — the `STAT` bit-position table in the
   comment and the `if/else` chain were off by one for the upper bits:

   | Bit | Code said | Hardware actually says (`STAT_b`) |
   |---|---|---|
   | 6   | `POISTAT` POR-init   | `WDRSTAT`  watchdog |
   | 7   | `WDRSTAT` watchdog   | `BOUSTAT`  unregulated brown-out |

   `if (stat & (1u << 7)) cause = WATCHDOG` was reading the
   unregulated-brownout bit, and `else if (stat & (1u << 6))
   cause = POWER_ON` was reading the watchdog bit. Had this ever
   compiled, `lastResetWasWatchdog()` would never report true after a
   real watchdog reset and would falsely report it after an unregulated
   brown-out. The CLRSTAT compile failure inadvertently protected
   callers from shipping this.

Fix:
  - Remove the impossible `RSTGEN->CLRSTAT = 1u;` write. Re-read
    protection is already provided by the `cause_cached` flag in
    `Apollo3WatchdogState`.
  - Re-derive the bit positions from `RSTGEN_Type::STAT_b` in
    `apollo3.h` and update both the comment table and the conditions.
  - Map the additional Apollo3 brown-out variants (`BOUSTAT`/`BOCSTAT`/
    `BOFSTAT`/`BOBSTAT`, bits 7/8/9/10) to `ResetCause::BROWNOUT`
    alongside the general `BORSTAT` (bit 2), so every flavor of
    brown-out reports consistently.

Local verification was blocked by a separate Windows-only fbuild
toolchain cache issue (arm-none-eabi-gcc 9.2.1 cache for the SparkFun
Apollo3 build missing `as.exe`); the failure fires inside the
Arduino_Apollo3 core before any FastLED translation unit is compiled,
and other arm-none-eabi-gcc boards in the same workspace compile
cleanly. PR CI on apollo3_red / apollo3_thing_explorable provides
authoritative verification of the fix.

Closes #2787

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@coderabbitai

coderabbitai Bot commented Jun 5, 2026 •

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 9ca86310-d93e-4581-882b-e20f307ed10b

📥 Commits

Reviewing files that changed from the base of the PR and between 5d27167 and 280184c.

📒 Files selected for processing (1)
  • src/platforms/apollo3/watchdog_apollo3.impl.hpp

📝 Walkthrough

Walkthrough

The reset-cause detection in Apollo3's Watchdog::lastResetCause() was corrected to use accurate bit-to-status mappings when reading the hardware register, with expanded brown-out bit coverage, and removal of an invalid register clear operation.

Changes

Apollo3 Watchdog Reset-Cause Correction

Layer / File(s) Summary
STAT register bit-to-cause mapping update
src/platforms/apollo3/watchdog_apollo3.impl.hpp
Watchdog::lastResetCause() reads RSTGEN->STAT with corrected bit-priority logic: watchdog mapped to updated STAT bit, brown-out detection spans multiple BO-related bits, and POWER_ON/EXTERNAL/DEBUGGER/SOFTWARE mappings adjusted. Removed the invalid RSTGEN->CLRSTAT sticky-bit clearing write. Crash counter and cause caching remain.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related issues

Possibly related PRs

  • FastLED/FastLED#2756: Introduces ResetInfo and lastResetInfo() API whose correctness depends on accurate reset-cause classification provided by this updated Watchdog::lastResetCause() function.

Poem

🐰 The watchdog watches the register dance,
Reading bits with a critical glance,
Brown-out's now wider, the mapping's more clear,
One invalid write we bid adieu here,
Reset causes now flow with precision and grace!

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.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 and specifically describes the main changes: fixing two bugs (removing non-existent CLRSTAT write and correcting STAT bit positions) in the Apollo3 watchdog implementation, directly matching the core objectives.
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.

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

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/apollo3-watchdog-rstgen-bits

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.

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.

broken builds: apollo3_red + apollo3_thing_explorable — RSTGEN::CLRSTAT doesn't exist (+ wrong reset-cause bit positions)

1 participant