Skip to content

Commit 4c11f64

Browse files
bikeNomaddpgeorge
authored andcommitted
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 <[email protected]>
1 parent 7090fc5 commit 4c11f64

File tree

3 files changed

+48
-3
lines changed

3 files changed

+48
-3
lines changed

ports/zephyr/README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,28 @@ the 'io-channels' property with all the ADC channels):
163163
adc = ADC(("adc", 0))
164164
adc.read_uv()
165165

166+
Example of using FlashArea for flash storage access:
167+
168+
from zephyr import FlashArea
169+
170+
# FlashArea.areas is a dictionary mapping partition labels to tuples
171+
# Each tuple contains (partition_id, erase_block_size)
172+
print(FlashArea.areas) # e.g. {'storage': (3, 4096)}
173+
174+
# Create a FlashArea object for a specific partition
175+
# Constructor takes (partition_id, block_size)
176+
partition_id, erase_size = FlashArea.areas['storage']
177+
flash = FlashArea(partition_id, erase_size)
178+
179+
# Use with virtual filesystem (see _boot.py for automatic mounting)
180+
import os
181+
os.VfsFat(flash) # or os.VfsLfs2(flash)
182+
183+
The `FlashArea.areas` dictionary provides partition information from the Zephyr
184+
devicetree. The erase block size is obtained from the flash controller's
185+
`erase-block-size` property, or defaults to 4096 bytes for devices (like QSPI
186+
NOR flash) that don't specify this property.
187+
166188
Minimal build
167189
-------------
168190

ports/zephyr/modules/_boot.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ def create_flash_partition():
2323
Return True if successful, False otherwise.
2424
"""
2525
if _STORAGE_KEY in FlashArea.areas:
26-
# TODO: get the erase block size from DTS instead of 4K.
27-
bdev = FlashArea(FlashArea.areas[_STORAGE_KEY], 4096)
26+
bdev = FlashArea(*FlashArea.areas[_STORAGE_KEY])
2827
retval = True
2928
try:
3029
vfs.mount(bdev, _FLASH)

ports/zephyr/zephyr_storage.c

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,15 +184,39 @@ MP_DEFINE_CONST_OBJ_TYPE(
184184
#define FLASH_AREA_DEFINE_GETNAME(part) COND_CODE_1(DT_NODE_HAS_PROP(part, label), \
185185
(FLASH_AREA_DEFINE_LABEL(part)), (FLASH_AREA_DEFINE_NB(part)))
186186

187-
#define FLASH_AREA_DEFINE_DEFINE(part) { MP_ROM_QSTR(FLASH_AREA_DEFINE_GETNAME(part)), MP_ROM_INT(DT_FIXED_PARTITION_ID(part)) },
187+
// Helper macro to get erase block size from the flash device
188+
// Note: Some flash controllers have erase-block-size, others (like QSPI NOR) don't
189+
// For devices without this property, use 4096 as a common default for NOR flash
190+
#define FLASH_AREA_GET_ERASE_SIZE(part) \
191+
DT_PROP_OR(DT_MTD_FROM_FIXED_PARTITION(part), erase_block_size, 4096)
192+
193+
// Create a static tuple for each partition containing (id, erase_block_size)
194+
#define FLASH_AREA_DEFINE_TUPLE(part) \
195+
static const mp_rom_obj_tuple_t flash_area_tuple_##part = { \
196+
{&mp_type_tuple}, \
197+
2, \
198+
{ \
199+
MP_ROM_INT(DT_FIXED_PARTITION_ID(part)), \
200+
MP_ROM_INT(FLASH_AREA_GET_ERASE_SIZE(part)), \
201+
} \
202+
};
203+
204+
#define FLASH_AREA_TUPLE(part) COND_CODE_1(DT_NODE_HAS_STATUS_OKAY(DT_MTD_FROM_FIXED_PARTITION(part)), \
205+
(FLASH_AREA_DEFINE_TUPLE(part)), ())
206+
207+
#define FLASH_AREA_DEFINE_DEFINE(part) { MP_ROM_QSTR(FLASH_AREA_DEFINE_GETNAME(part)), MP_ROM_PTR(&flash_area_tuple_##part) },
188208

189209
#define FLASH_AREA_DEFINE(part) COND_CODE_1(DT_NODE_HAS_STATUS_OKAY(DT_MTD_FROM_FIXED_PARTITION(part)), \
190210
(FLASH_AREA_DEFINE_DEFINE(part)), ())
191211

192212
#define FOREACH_PARTITION(n) DT_FOREACH_CHILD(n, FLASH_AREA_DEFINE)
213+
#define FOREACH_PARTITION_TUPLE(n) DT_FOREACH_CHILD(n, FLASH_AREA_TUPLE)
193214

194215
const mp_obj_type_t zephyr_flash_area_type;
195216

217+
// Generate tuple definitions for all partitions
218+
DT_FOREACH_STATUS_OKAY(fixed_partitions, FOREACH_PARTITION_TUPLE)
219+
196220
typedef struct _zephyr_flash_area_obj_t {
197221
mp_obj_base_t base;
198222
const struct flash_area *area;

0 commit comments

Comments
 (0)