Skip to content

feat: add SPI NOR flash command layer + spin-based kernel delay - #471

Open
jinwjinl wants to merge 2 commits into
vivoblueos:mainfrom
jinwjinl:nor-flash-wait-busy
Open

jinwjinl wants to merge 2 commits into
vivoblueos:mainfrom
jinwjinl:nor-flash-wait-busy

Conversation

@jinwjinl

@jinwjinl jinwjinl commented Sep 9, 2026

Copy link
Copy Markdown
Contributor

Description

This branch fixes a timing/starvation problem in the SPI NOR Flash command layer. The previous wait_busy polled the status register in a tight loop with no timeout and no yielding, which blocked the SPI bus for the full duration of erase operations (~110 ms per 4 KiB sector, up to ~400 ms for block erase). On a single-core cooperative scheduler this starved any task sharing the bus (LCD, net-stack) and could stall OTA flows. The fix adds a bounded, configurable timeout scheme and splits wait behavior into a yielding variant (for long erase operations) and a spin variant (for short page programs), while keeping all Flash logic userspace-unchanged.

Main changes

Timeout configuration

  • Adds SpiFlashTimeouts, a per-device timeout profile covering:
    • page program,
    • sector erase,
    • 32 KiB block erase,
    • 64 KiB block erase,
    • chip erase.
  • Adds configured_poll_interval(), reading CONFIG_SPI_FLASH_STATUS_POLL_INTERVAL_US to space status-register polls.
  • Adds new_with_timeouts / new_with_poll_interval constructors so a caller can supply an explicit profile; Default resolves to Kconfig values.

The timeouts are defined through Kconfig:

  • CONFIG_SPI_FLASH_PAGE_PROGRAM_TIMEOUT_MS
  • CONFIG_SPI_FLASH_SECTOR_ERASE_TIMEOUT_MS
  • CONFIG_SPI_FLASH_BLOCK_ERASE_32K_TIMEOUT_MS
  • CONFIG_SPI_FLASH_BLOCK_ERASE_64K_TIMEOUT_MS
  • CONFIG_SPI_FLASH_CHIP_ERASE_TIMEOUT_MS
  • CONFIG_SPI_FLASH_STATUS_POLL_INTERVAL_US

Wait-busy strategies

  • wait_busy_yield — used for erase operations. Polls the status register between suspend_me_for(Tick(1)) yields so the scheduler can drain other bus clients (LCD / net-stack) during the erase window. Aborts with a timeout error once the per-operation deadline passes.
  • wait_busy_spin — used for page program. Tight spin on the status register, no yield, since page programs are short and yielding would only add latency.

Mapping of operations to strategy:

Operation Wait strategy
page program wait_busy_spin
sector erase wait_busy_yield
32 KiB block erase wait_busy_yield
64 KiB block erase wait_busy_yield
chip erase wait_busy_yield

Shared helper

  • Adds wait_busy_with as the common deadline + poll loop body, parameterized by a yield hook so the yield/spin variants stay in lockstep and the deadline logic is not duplicated.

Tests

  • Adds test_configured_poll_interval asserting the Kconfig-derived interval resolves to Duration::from_micros(CONFIG_SPI_FLASH_STATUS_POLL_INTERVAL_US).
  • The existing test_wait_busy_timeout is kept.

- spi_flash_cmd.rs: JEDEC 25-series SPI NOR flash command layer
  (sector/block/chip erase, page program, read, wait_busy polling)
- delay.rs: switch to CPU_HZ spin (is_schedule_ready gate) instead of
  Tick::after/is_elapsed, avoiding boot-early deadloop when systimer
  is not yet running

Co-Authored-By: Claude <noreply@anthropic.com>
@jinwjinl

jinwjinl commented Sep 9, 2026

Copy link
Copy Markdown
Contributor Author

build_prs

@github-actions

github-actions Bot commented Sep 9, 2026

Copy link
Copy Markdown

@github-actions

github-actions Bot commented Sep 9, 2026

Copy link
Copy Markdown

❌ Job failed. Failed jobs: build_and_check_boards (failure), see https://github.com/vivoblueos/kernel/actions/runs/34340522633.

@jinwjinl

Copy link
Copy Markdown
Contributor Author

build_prs

@github-actions

Copy link
Copy Markdown

@github-actions

Copy link
Copy Markdown

✅ All jobs completed successfully, see https://github.com/vivoblueos/kernel/actions/runs/35575781993.

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