Skip to content

Add Python script to generate flash_map C code from devicetree#4

Closed
Copilot wants to merge 5 commits intomainfrom
copilot/add-flash-map-generator-script
Closed

Add Python script to generate flash_map C code from devicetree#4
Copilot wants to merge 5 commits intomainfrom
copilot/add-flash-map-generator-script

Conversation

Copy link
Copy Markdown

Copilot AI commented Feb 6, 2026

The Flash Map subsystem currently generates partition map and flash_area objects using complex nested DT foreach macros. This PR replaces macro-based generation with a Python script that generates explicit C code at compile time.

Changes

New script: scripts/dts/gen_flash_map.py

  • Reads EDT pickle to extract fixed-partition and fixed-subpartition nodes
  • Generates default_flash_map[] array and individual partition objects (global_fixed_partition_ORD_<N>)
  • Uses DT macros in generated code (not computed values) to ensure proper compilation:
    {
        .fa_id = DT_FIXED_PARTITION_ID(DT_N_...),
        .fa_off = FIXED_PARTITION_NODE_OFFSET(DT_N_...),
        .fa_size = DT_REG_SIZE(DT_N_...),
        .fa_dev = DEVICE_DT_GET(DT_MTD_FROM_FIXED_PARTITION(DT_N_...)),
    }
  • Supports CONFIG_FLASH_MAP_LABELS via --labels flag

Build integration: subsys/storage/flash_map/CMakeLists.txt

  • Invokes gen_flash_map.py during build to generate flash_map_generated.c
  • Replaces flash_map_default.c with generated file (unless CONFIG_FLASH_MAP_CUSTOM)
  • Regenerates when EDT pickle or script changes

Compatibility

All FIXED_PARTITION_* macros in flash_map.h continue to work unchanged. Generated structures are identical to the macro-based version:

  • Sequential partition ID assignment matches gen_defines.py
  • Address translation for subpartitions via FIXED_PARTITION_NODE_OFFSET
  • Custom flash maps (CONFIG_FLASH_MAP_CUSTOM) unaffected

Benefits

  • Replaces nested preprocessor macros with straightforward Python logic
  • Generated C code is inspectable for debugging
  • Build-time generation with proper dependency tracking
Original prompt

There is Flash Map subsystem that uses partition map generated with use of DT macros and flash_area type objects also generated with use of DT macros.
Propose a script, python, for generating directly C code, replacing the use of DT macros.
The script should be integrated into build system so that the file with flash map and flash area objects gets generated at compile time and compiled in to build.
The change should not break how FIXED_PARTITION_ macros work.


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI and others added 4 commits February 6, 2026 10:51
Co-authored-by: de-nordic <56024351+de-nordic@users.noreply.github.com>
Co-authored-by: de-nordic <56024351+de-nordic@users.noreply.github.com>
Co-authored-by: de-nordic <56024351+de-nordic@users.noreply.github.com>
Co-authored-by: de-nordic <56024351+de-nordic@users.noreply.github.com>
Copilot AI changed the title [WIP] Add Python script to generate C code for Flash Map subsystem Add Python script to generate flash_map C code from devicetree Feb 6, 2026
Copilot AI requested a review from de-nordic February 6, 2026 11:00
de-nordic pushed a commit that referenced this pull request Mar 20, 2026
1. Mostly complete.  Supports MPU, userspace, PSPLIM-based stack
guards, and FPU/DSP features.  ARMv8-M secure mode "should" work but I
don't know how to test it.

2. Designed with an eye to uncompromising/best-in-industry cooperative
context switch performance.  No PendSV exception nor hardware
stacking/unstacking, just a traditional "musical chairs" switch.
Context gets saved on process stacks only instead of split between
there and the thread struct.  No branches in the core integer switch
code (and just one in the FPU bits that can't be avoided).

3. Minimal assembly use; arch_switch() itself is ALWAYS_INLINE, there
is an assembly stub for exception exit, and that's it beyond one/two
instruction inlines elsewhere.

4. Selectable at build time, interoperable with existing code.  Just
use the pre-existing CONFIG_USE_SWITCH=y flag to enable it.  Or turn
it off to evade regressions as this stabilizes.

5. Exception/interrupt returns in the common case need only a single C
function to be called at the tail, and then return naturally.
Effectively "all interrupts are direct now".  This isn't a benefit
currently because the existing stubs haven't been removed (see #4),
but in the long term we can look at exploiting this.  The boilerplate
previously required is now (mostly) empty.

6. No support for ARMv6 (Cortex M0 et. al.) thumb code.  The expanded
instruction encodings in ARMv7 are a big (big) win, so the older cores
really need a separate port to avoid impacting newer hardware.
Thankfully there isn't that much code to port (see #3), so this should
be doable.

Signed-off-by: Andy Ross <andyross@google.com>
@de-nordic de-nordic closed this Mar 23, 2026
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