Skip to content

fix(firmware): stop settings writes from wearing out the flag page - #16

Merged
FilipNone merged 1 commit into
mainfrom
fix/settings-flash-wear
Nov 26, 2026
Merged

FilipNone merged 1 commit into
mainfrom
fix/settings-flash-wear

Conversation

@FilipNone

@FilipNone FilipNone commented Nov 26, 2026 •

Copy link
Copy Markdown
Owner

Problem

Every saved command (24 21 to 24 23, 24 30 to 24 35, the mode button, the H/SEQ switch) erased the settings page at 0x0800F000 right away, with no limit. A program that keeps changing a saved setting, for example a rev-light script using 24 30, got 25 to 40 erases a second. At the STM32F103's rating of 10,000 erases, the page would be worn out in under ten minutes.

The same page holds the bootloader flag. Every way into update mode writes it (24 00, both buttons, fault, stall, USB recovery), and write errors were ignored. A worn page could leave a unit that can't be put into update mode over USB.

Approach

  • Delayed writes (sh_saves_* in shifter.c, used by main.c): a change is written once nothing has changed for 1 s. Up to 16 writes can follow each other, then one more per minute. Later changes wait in memory and are still in use (LEDs, reports). Only writes that erased the page count against the limit.
  • 24 00 writes a waiting change at once, ignoring the wait and the limit.
  • A failed settings write is retried once.
  • Flag write: enter_bootloader() retries once. If that also fails, the unit detaches from USB and blinks all LEDs red, instead of resetting into the application or looping through fault, erase, reset.
  • The timing logic is in the portable core, so test_shifter.py drives it with a fake clock, including the clock wrapping after 49 days.
  • docs/protocol.md says saved commands write flash and that continuous colour changes must use 24 20.
  • The wiki's Troubleshooting page has a row for the red blinking.

Cost: 320 bytes of code, 12 bytes of RAM.

Automated validation

  • make -C firmware builds without warnings
  • make -C firmware test passes: 81 tests, differential tests ran (stock image, SHA-256 96da5e6b…), nothing skipped
  • CI passes on the latest commit
  • Code review of the diff done and findings resolved

New tests:

  • test_shifter.SettingsWrites (6): quiet time, urgent write, the 16-write burst, refill once a minute, no stored-up time, clock wrap.
  • test_emulated.OpenShifter:
    • 40 colour changes cost one erase
    • the 17th write waits
    • 24 00 writes a waiting change before the reset
    • a worn page (new emulator option flash_worn) gives two flag attempts, a USB detach, and red/off LED frames with no reset

Against the current firmware (0.5.0), three of the four new emulator tests fail as expected. The 24 00 one passes there too, since 0.5.0 wrote everything at once.

Existing emulator tests now wait 1.1 s before they read the settings page or power-cycle. That includes the differential ones, for both images.

Hardware testing

  • Tested on a real shifter (2026-11-26, Linux, unit TBRS004-20241224), built as 0.5.0+pr16 (SHA-256 5ac9798b…):
    • Installed over the 0.5.0 release with openshifter_flash.py. The unit restarted as OpenShifter 0.5.0+pr16.
    • 40 24 30 colour changes in 0.6 s, then 16 changes 1.15 s apart, then a replug. The unit came back with change 15 of the second set, so the 40 cost one write and the 16th waited.
    • The new firmware's own 24 00: a second install from 0.5.0+pr16 entered update mode and finished.
    • All 10 calibration targets, then a replug: the calibration read back identical.
    • Not tried: the red stop. It needs a settings page that no longer erases.

Limitations and follow-up

  • A change made less than 1 s before the shifter loses power is lost. A fault or stall within that second also skips the waiting change.
  • A program that deliberately changes a setting every minute can still wear the page out, over about a week of nonstop use. Preventing that completely would need wear levelling.
  • The AIR32F103's flash endurance is not published. The 10,000 figure is the STM32F103's.

Every saved command erased the settings page right away, with no limit,
so a program changing the colour with 24 30 could wear the page out in
minutes. The same page holds the bootloader flag that every recovery path
writes.

Settings now reach flash once nothing has changed for 1 s. Up to 16
writes can follow each other, then one more per minute; later changes
wait in memory. 24 00 still writes at once. The timing lives in
shifter.c (sh_saves_*) so the host tests can drive it with a fake clock.

If the flag write fails twice, the unit now leaves USB and blinks red
instead of resetting into the application or looping through a fault.
A failed settings write is retried once.
@FilipNone
FilipNone merged commit 92aa9d4 into main Nov 26, 2026
8 checks passed
@FilipNone
FilipNone deleted the fix/settings-flash-wear branch November 26, 2026 00:39
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