Skip to content

Commit 984be5e

Browse files
de-nordickartben
authored andcommitted
storage: flash map: Add FIXED_PARTITION(label) macro
The commit adds FIXED_PARTITION(label) macro that allows to obtain struct flash_area object for partition of given label. The macro allows instantiation of partition at point of usage and will be replacing need for defining flash map with all partition entries. Area obtained with the macro should not be passed to open, instead flash_area_device_is_ready, basically equivalent of device_is_ready should be called on the obtained pointer to check if area is ready for use. Signed-off-by: Dominik Ermel <[email protected]>
1 parent 5a02aaa commit 984be5e

File tree

2 files changed

+66
-1
lines changed

2 files changed

+66
-1
lines changed

include/zephyr/storage/flash_map.h

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,28 @@ int flash_area_open(uint8_t id, const struct flash_area **fa);
133133
*/
134134
void flash_area_close(const struct flash_area *fa);
135135

136+
/**
137+
* @brief Verify that a device assigned to flash area is ready for use.
138+
*
139+
* Indicates whether the provided flash area has a device known to be
140+
* in a state where it can be used with Flash Map API.
141+
*
142+
* This can be used with struct flash_area pointers captured from
143+
* FIXED_PARTITION().
144+
* At minimum this means that the device has been successfully initialized.
145+
*
146+
* @param dev pointer to flash_area object to check.
147+
*
148+
* @retval true If the device is ready for use.
149+
* @retval false If the device is not ready for use or if a NULL pointer is
150+
* passed as flash area pointer or device pointer within flash area object
151+
* is NULL.
152+
*/
153+
static ALWAYS_INLINE bool flash_area_device_is_ready(const struct flash_area *fa)
154+
{
155+
return (fa != NULL && device_is_ready(fa->fa_dev));
156+
}
157+
136158
/**
137159
* @brief Read flash area data
138160
*
@@ -376,6 +398,31 @@ uint8_t flash_area_erased_val(const struct flash_area *fa);
376398
#define FIXED_PARTITION_NODE_DEVICE(node) \
377399
DEVICE_DT_GET(DT_MTD_FROM_FIXED_PARTITION(node))
378400

401+
/**
402+
* Get pointer to flash_area object by partition label
403+
*
404+
* @param label DTS node label of a partition
405+
*
406+
* @return Pointer to flash_area type object representing partition
407+
*/
408+
#define FIXED_PARTITION(label) FIXED_PARTITION_1(DT_NODELABEL(label))
409+
#define FIXED_PARTITION_1(node) FIXED_PARTITION_0(DT_DEP_ORD(node))
410+
#define FIXED_PARTITION_0(ord) (const struct flash_area *)&DT_CAT(global_fixed_partition_ORD_, part)
411+
412+
/** @cond INTERNAL_HIDDEN */
413+
#define DECLARE_PARTITION(part) DECLARE_PARTITION_0(DT_DEP_ORD(part))
414+
#define DECLARE_PARTITION_0(part) \
415+
extern const struct flash_area DT_CAT(global_fixed_partition_ORD_, part);
416+
#define FOR_EACH_PARTITION_TABLE(table) DT_FOREACH_CHILD(table, DECLARE_PARTITION)
417+
418+
/* Generate declarations */
419+
DT_FOREACH_STATUS_OKAY(fixed_partitions, FOR_EACH_PARTITION_TABLE)
420+
421+
#undef DECLARE_PARTITION
422+
#undef DECLARE_PARTITION_0
423+
#undef FOR_EACH_PARTITION_TABLE
424+
/** @endcond */
425+
379426
#ifdef __cplusplus
380427
}
381428
#endif

subsys/storage/flash_map/flash_map_default.c

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2017 Nordic Semiconductor ASA
2+
* Copyright (c) 2017-2024 Nordic Semiconductor ASA
33
* Copyright (c) 2015 Runtime Inc
44
* Copyright (c) 2023 Sensorfy B.V.
55
*
@@ -43,3 +43,21 @@ const struct flash_area default_flash_map[] = {
4343

4444
const int flash_map_entries = ARRAY_SIZE(default_flash_map);
4545
const struct flash_area *flash_map = default_flash_map;
46+
47+
/* Generate objects representing each partition in system. In the end only
48+
* objects referenced by code will be included into build.
49+
*/
50+
#define DEFINE_PARTITION(part) DEFINE_PARTITION_1(part, DT_DEP_ORD(part))
51+
#define DEFINE_PARTITION_1(part, ord) \
52+
COND_CODE_1(DT_NODE_HAS_STATUS_OKAY(DT_MTD_FROM_FIXED_PARTITION(part)), \
53+
(DEFINE_PARTITION_0(part, ord)), ())
54+
#define DEFINE_PARTITION_0(part, ord) \
55+
const struct flash_area DT_CAT(global_fixed_partition_ORD_, ord) = { \
56+
.fa_id = DT_FIXED_PARTITION_ID(part), \
57+
.fa_off = DT_REG_ADDR(part), \
58+
.fa_dev = DEVICE_DT_GET(DT_MTD_FROM_FIXED_PARTITION(part)), \
59+
.fa_size = DT_REG_SIZE(part), \
60+
};
61+
62+
#define FOR_EACH_PARTITION_TABLE(table) DT_FOREACH_CHILD(table, DEFINE_PARTITION)
63+
DT_FOREACH_STATUS_OKAY(fixed_partitions, FOR_EACH_PARTITION_TABLE)

0 commit comments

Comments
 (0)