Skip to content

stage3: two boot-time performance improvements - #24

Open
BlairLeduc wants to merge 2 commits into
pelrun:mainfrom
BlairLeduc:stage3-faster-boot
Open

BlairLeduc wants to merge 2 commits into
pelrun:mainfrom
BlairLeduc:stage3-faster-boot

Conversation

@BlairLeduc

Copy link
Copy Markdown

This PR submits two small, independent changes to stage3. Neither commit changes the boot process. This is a performance improvement only.

Poll the SD card without sleeping (common/petit_fatfs/diskio.c).
wait_ready() and rcvr_datablock() sleep 100µs between polls. The card only makes progress while it is clocked, so every sleep stalls it, and a sector took about 3ms to read. They now poll continuously, with
the same 500ms and 100ms timeouts measured in time rather than in
iterations.

Read the command before waiting for a key (stage3/stage3.c).
read_bootmode() watches the keyboard for half a second on every boot, and
then any command from bl_stage3_command() replaces the key press, if any.
The command is now checked first, and the keyboard is watched only when
there isn't a command. A boot without a command (power-on, or an app's reboot)
behaves exactly as before.

Measured

I used a PicoCalc with a Pimoroni Pico Plus 2 W, pico2 build, SDK 2.3.1,
GCC 15.2, each stage3 written at offset 0 over SWD. A 118KB RAM app
logs a line on UART0, reboots itself with BOOT_RAM and its own path
1s later, and logs again at its next start; the time is the gap
between the two lines minus that 1s, five runs each:

2.5 Polling only Command first only Both
BOOT_RAM to the app 0.837 – 0.839s 0.747 – 0.748s 0.338 – 0.339s 0.244 – 0.249s

The two savings add up: about 0.09s from the polling and 0.50s from
the command. BOOT_SD (an app returning to the menu) takes the same
path through stage3 with LOADER as the file.

stage3 grows by 80B on pico2 (6,740 → 6,820B of 8KB) and 72B on
pico (6,636 → 6,708B); RAM is unchanged.

Not checked

  • The polling on cards other than the two I have (a 32GB SanDisk Extreme and the 32GB Lexar 800x Pro that shipped with the PicoCalc).
  • Any RP2040 board: the pico build compiles but I have not run it.
  • A boot without a command was not timed; its code is unchanged.
  • On a commanded boot stage3 no longer initialises the keyboard's I2C,
    as nothing reads it. The apps I ran set it up themselves.

🤖 Generated with Claude Code and edited by Blair Leduc.

BlairLeduc and others added 2 commits October 25, 2026 21:10
wait_ready() and rcvr_datablock() slept 100 us between polls. The card
only moves on while it is being clocked, so each sleep stalled it, and
reading a sector took about 3 ms. Poll continuously instead, with the
same 500 ms and 100 ms timeouts, now measured in time rather than in
iterations.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
read_bootmode() watched the keyboard for 0.5 s on every boot, then a
command from bl_stage3_command() replaced whatever it had read. Check
for the command first and watch the keyboard only when there is none.
A boot with no command behaves as before; a commanded one (a RAM app
from the menu, or an app returning to it) no longer waits 0.5 s.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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