summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNed Konz <ned@metamagix.tech>2025-10-17 10:51:56 -0700
committerDamien George <damien@micropython.org>2025-10-22 10:24:32 +1100
commit4c11f64faa3841e2939ccf11aad618d213a236a4 (patch)
treee05fd4fa8165b565f5207594ba9a0f86076633fe
parent7090fc5dd63bc1dcbe5470e2000ed333cb867d64 (diff)
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>
-rw-r--r--ports/zephyr/README.md22
-rw-r--r--ports/zephyr/modules/_boot.py3
-rw-r--r--ports/zephyr/zephyr_storage.c26
3 files changed, 48 insertions, 3 deletions
diff --git a/ports/zephyr/README.md b/ports/zephyr/README.md
index 480ff5705..eedcdcb58 100644
--- a/ports/zephyr/README.md
+++ b/ports/zephyr/README.md
@@ -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
-------------
diff --git a/ports/zephyr/modules/_boot.py b/ports/zephyr/modules/_boot.py
index 5af302fad..d0ba21d3f 100644
--- a/ports/zephyr/modules/_boot.py
+++ b/ports/zephyr/modules/_boot.py
@@ -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)
diff --git a/ports/zephyr/zephyr_storage.c b/ports/zephyr/zephyr_storage.c
index e72e047d0..b6396e711 100644
--- a/ports/zephyr/zephyr_storage.c
+++ b/ports/zephyr/zephyr_storage.c
@@ -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;