zephyr: Add erase block size to FlashArea.areas entries.

This commit changes the values in the `FlashArea.areas` dictionary from
simple integer IDs to (ID, erase-block-size) tuples.

`_boot.py` was changed to use the newly available erase block size.

Signed-off-by: Ned Konz <ned@metamagix.tech>
This commit is contained in:
Ned Konz
2025-10-17 10:51:56 -07:00
committed by Damien George
parent 7090fc5dd6
commit 4c11f64faa
3 changed files with 48 additions and 3 deletions

View File

@@ -163,6 +163,28 @@ the 'io-channels' property with all the ADC channels):
adc = ADC(("adc", 0))
adc.read_uv()
Example of using FlashArea for flash storage access:
from zephyr import FlashArea
# FlashArea.areas is a dictionary mapping partition labels to tuples
# Each tuple contains (partition_id, erase_block_size)
print(FlashArea.areas) # e.g. {'storage': (3, 4096)}
# Create a FlashArea object for a specific partition
# Constructor takes (partition_id, block_size)
partition_id, erase_size = FlashArea.areas['storage']
flash = FlashArea(partition_id, erase_size)
# Use with virtual filesystem (see _boot.py for automatic mounting)
import os
os.VfsFat(flash) # or os.VfsLfs2(flash)
The `FlashArea.areas` dictionary provides partition information from the Zephyr
devicetree. The erase block size is obtained from the flash controller's
`erase-block-size` property, or defaults to 4096 bytes for devices (like QSPI
NOR flash) that don't specify this property.
Minimal build
-------------

View File

@@ -23,8 +23,7 @@ def create_flash_partition():
Return True if successful, False otherwise.
"""
if _STORAGE_KEY in FlashArea.areas:
# TODO: get the erase block size from DTS instead of 4K.
bdev = FlashArea(FlashArea.areas[_STORAGE_KEY], 4096)
bdev = FlashArea(*FlashArea.areas[_STORAGE_KEY])
retval = True
try:
vfs.mount(bdev, _FLASH)

View File

@@ -184,15 +184,39 @@ MP_DEFINE_CONST_OBJ_TYPE(
#define FLASH_AREA_DEFINE_GETNAME(part) COND_CODE_1(DT_NODE_HAS_PROP(part, label), \
(FLASH_AREA_DEFINE_LABEL(part)), (FLASH_AREA_DEFINE_NB(part)))
#define FLASH_AREA_DEFINE_DEFINE(part) { MP_ROM_QSTR(FLASH_AREA_DEFINE_GETNAME(part)), MP_ROM_INT(DT_FIXED_PARTITION_ID(part)) },
// Helper macro to get erase block size from the flash device
// Note: Some flash controllers have erase-block-size, others (like QSPI NOR) don't
// For devices without this property, use 4096 as a common default for NOR flash
#define FLASH_AREA_GET_ERASE_SIZE(part) \
DT_PROP_OR(DT_MTD_FROM_FIXED_PARTITION(part), erase_block_size, 4096)
// Create a static tuple for each partition containing (id, erase_block_size)
#define FLASH_AREA_DEFINE_TUPLE(part) \
static const mp_rom_obj_tuple_t flash_area_tuple_##part = { \
{&mp_type_tuple}, \
2, \
{ \
MP_ROM_INT(DT_FIXED_PARTITION_ID(part)), \
MP_ROM_INT(FLASH_AREA_GET_ERASE_SIZE(part)), \
} \
};
#define FLASH_AREA_TUPLE(part) COND_CODE_1(DT_NODE_HAS_STATUS_OKAY(DT_MTD_FROM_FIXED_PARTITION(part)), \
(FLASH_AREA_DEFINE_TUPLE(part)), ())
#define FLASH_AREA_DEFINE_DEFINE(part) { MP_ROM_QSTR(FLASH_AREA_DEFINE_GETNAME(part)), MP_ROM_PTR(&flash_area_tuple_##part) },
#define FLASH_AREA_DEFINE(part) COND_CODE_1(DT_NODE_HAS_STATUS_OKAY(DT_MTD_FROM_FIXED_PARTITION(part)), \
(FLASH_AREA_DEFINE_DEFINE(part)), ())
#define FOREACH_PARTITION(n) DT_FOREACH_CHILD(n, FLASH_AREA_DEFINE)
#define FOREACH_PARTITION_TUPLE(n) DT_FOREACH_CHILD(n, FLASH_AREA_TUPLE)
const mp_obj_type_t zephyr_flash_area_type;
// Generate tuple definitions for all partitions
DT_FOREACH_STATUS_OKAY(fixed_partitions, FOREACH_PARTITION_TUPLE)
typedef struct _zephyr_flash_area_obj_t {
mp_obj_base_t base;
const struct flash_area *area;