Skip to content

Flash driver#73

Open
xarantolus wants to merge 9 commits into
mainfrom
feature/flash-driver
Open

Flash driver#73
xarantolus wants to merge 9 commits into
mainfrom
feature/flash-driver

Conversation

@xarantolus
Copy link
Copy Markdown
Contributor

@xarantolus xarantolus commented May 10, 2026

Adds a somewhat generic flash driver, based on device tree definitions.

  • C primitives: erase, program, unlock/lock, etc.
  • FlashAddress / FlashOffset / FlashPageStart newtypes: basically types that ensure that you don't use the flash the wrong way (if you have to pass a start address of a page, this makes it really clear compared to just an usize). Also differentiates between an offset and an actual flash address (0x8... vs 0x0....)
    • Conversions between these types
    • checked_add / checked_sub / distance_from + iter_pages iterator (makes it easy to do the correct thing on different page sizes)
  • ARM HAL exposes primitives using these types
  • Driver-layer Region with read/erase/program/write, partition-scoped offsets, configurable timeouts.
    • DT read-only partitions reject mutating ops before they reach the chip.
      • So basically a software-level definition we can use to protect the bootloader from being overwritten

Copilot AI review requested due to automatic review settings May 10, 2026 22:45
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Adds a DT-driven flash partition/region abstraction spanning device-tree codegen, ARM Cortex-M HAL primitives (STM32L4), and kernel-facing driver + uapi plumbing, with typed flash address newtypes to reduce misuse.

Changes:

  • Extend dtgen to emit a flash partition registry + lookup helpers (by_compatible, by_label, by_address) and FLASH_BASE/FLASH_TOTAL_SIZE constants.
  • Introduce hal_api::flash_addr newtypes (FlashAddress/FlashOffset/FlashPageStart) and integrate them into a new Cortex-M flash HAL (native + host stub).
  • Add a kernel driver-layer drivers::flash::Region (read/erase/program/write with timeouts + DT read-only enforcement) and expose it via uapi::flash.

Reviewed changes

Copilot reviewed 16 out of 17 changed files in this pull request and generated 11 comments.

Show a summary per file
File Description
xtasks/crates/dtgen/src/codegen.rs Generates flash partition registry/constants and query API from DT.
src/uapi/flash.rs New UAPI entry point for opening flash regions and re-exporting flash types.
src/uapi.rs Exposes the new uapi::flash module.
src/drivers/flash.rs Implements driver-layer Region read/erase/program/write over HAL flash primitives.
src/drivers.rs Exposes the new drivers::flash module.
machine/cortex-m/st/stm32l4/interface/flash.c Adds STM32L4 flash C primitives (busy/size/page ops/unlock/erase/program).
machine/cortex-m/st/stm32l4/interface/export.h Exports flash constants and function prototypes for bindgen.
machine/cortex-m/src/stub/flash.rs Host stub implementation for flash API/types.
machine/cortex-m/src/stub.rs Wires the stub flash module into the stub HAL.
machine/cortex-m/src/native/flash.rs Native Cortex-M flash HAL: bindings mapping + DT-backed Region.
machine/cortex-m/src/native.rs Wires the native flash module into the native HAL.
machine/cortex-m/Cargo.toml Adds build deps for bindgen macro filtering.
machine/cortex-m/build.rs Updates bindgen generation to allowlist macros via // bindgen-export: markers and adds SoC defines.
machine/api/src/lib.rs Exposes the new flash_addr module from hal-api.
machine/api/src/flash_addr.rs Adds the flash address/offset/page-start newtypes + utilities.
Cargo.lock Records added build dependencies.
boards/nucleo_l4r5zi.dts Updates board DT to define a flash partition under flash0.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread xtasks/crates/dtgen/src/codegen.rs Outdated
Comment thread xtasks/crates/dtgen/src/codegen.rs Outdated
Comment thread xtasks/crates/dtgen/src/codegen.rs Outdated
Comment thread xtasks/crates/dtgen/src/codegen.rs Outdated
Comment thread src/drivers/flash.rs Outdated
Comment thread machine/cortex-m/src/native/flash.rs Outdated
Comment thread machine/cortex-m/src/native/flash.rs
Comment thread machine/cortex-m/Cargo.toml Outdated
Comment thread machine/cortex-m/build.rs Outdated
Comment thread src/uapi/flash.rs
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.

2 participants