From 54435e000afa08fa05ce4417abd615e2fe6788d4 Mon Sep 17 00:00:00 2001 From: Dominik Ermel Date: Wed, 4 Dec 2024 18:37:53 +0100 Subject: [PATCH] storage/flash_map: Add FIXED_PARTITION_BY_NODE macro Macro allows to instantiate flash_area object pointer from DTS node. Signed-off-by: Dominik Ermel --- include/zephyr/storage/flash_map.h | 10 ++++++++++ tests/subsys/storage/flash_map/src/main.c | 9 +++++++++ 2 files changed, 19 insertions(+) diff --git a/include/zephyr/storage/flash_map.h b/include/zephyr/storage/flash_map.h index cdd9ae00759..b43d50a1608 100644 --- a/include/zephyr/storage/flash_map.h +++ b/include/zephyr/storage/flash_map.h @@ -419,6 +419,16 @@ uint8_t flash_area_erased_val(const struct flash_area *fa); * @return Pointer to flash_area type object representing partition */ #define FIXED_PARTITION(label) FIXED_PARTITION_1(DT_NODELABEL(label)) + +/** + * Get pointer to flash_area object by partition node in DTS + * + * @param node DTS node of a partition + * + * @return Pointer to flash_area type object representing partition + */ +#define FIXED_PARTITION_BY_NODE(node) FIXED_PARTITION_1(node) + #define FIXED_PARTITION_1(node) FIXED_PARTITION_0(DT_DEP_ORD(node)) #define FIXED_PARTITION_0(ord) \ ((const struct flash_area *)&DT_CAT(global_fixed_partition_ORD_, ord)) diff --git a/tests/subsys/storage/flash_map/src/main.c b/tests/subsys/storage/flash_map/src/main.c index 9a849485eca..3e2b9d8421a 100644 --- a/tests/subsys/storage/flash_map/src/main.c +++ b/tests/subsys/storage/flash_map/src/main.c @@ -30,6 +30,11 @@ ZTEST(flash_map, test_flash_area_disabled_device) zassert_equal(rc, -ENOENT, "Open did not fail"); rc = flash_area_open(FIXED_PARTITION_ID(disabled_b), &fa); zassert_equal(rc, -ENOENT, "Open did not fail"); + + /* Note lack of tests for FIXED_PARTITION(...) instantiation, + * because this macro will fail, at compile time, if node does not + * exist or is disabled. + */ } ZTEST(flash_map, test_flash_area_device_is_ready) @@ -143,6 +148,10 @@ ZTEST(flash_map, test_fixed_partition_node_macros) DT_REG_SIZE(SLOT1_PARTITION_NODE)); zassert_equal(FIXED_PARTITION_NODE_DEVICE(SLOT1_PARTITION_NODE), DEVICE_DT_GET(DT_MTD_FROM_FIXED_PARTITION(SLOT1_PARTITION_NODE))); + + /* Taking by node and taking by label should give same device */ + zassert_equal(FIXED_PARTITION_BY_NODE(DT_NODELABEL(SLOT1_PARTITION)), + FIXED_PARTITION(SLOT1_PARTITION)); } ZTEST(flash_map, test_flash_area_erase_and_flatten)