Skip to content

Commit 55c12f2

Browse files
de-nordickartben
authored andcommitted
storage: flash map: Add flash_area_sectors
The commit adds flash_area_sectors function that allows to get information on sector/erase page layout by flash_area object pointer instead of index. The only difference between flash_area_sectors and flash_area_get_sectors is that the later calls flash_area_open internally and as such requires flash map to be compiled in. Signed-off-by: Dominik Ermel <[email protected]>
1 parent 984be5e commit 55c12f2

File tree

2 files changed

+32
-14
lines changed

2 files changed

+32
-14
lines changed

include/zephyr/storage/flash_map.h

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -139,11 +139,10 @@ void flash_area_close(const struct flash_area *fa);
139139
* Indicates whether the provided flash area has a device known to be
140140
* in a state where it can be used with Flash Map API.
141141
*
142-
* This can be used with struct flash_area pointers captured from
143-
* FIXED_PARTITION().
142+
* This can be used with struct flash_area pointers captured from FIXED_PARTITION().
144143
* At minimum this means that the device has been successfully initialized.
145144
*
146-
* @param dev pointer to flash_area object to check.
145+
* @param fa pointer to flash_area object to check.
147146
*
148147
* @retval true If the device is ready for use.
149148
* @retval false If the device is not ready for use or if a NULL pointer is
@@ -254,6 +253,20 @@ uint32_t flash_area_align(const struct flash_area *fa);
254253
int flash_area_get_sectors(int fa_id, uint32_t *count,
255254
struct flash_sector *sectors);
256255

256+
/**
257+
* Retrieve info about sectors within the area.
258+
*
259+
* @param[in] fa pointer to flash area object.
260+
* @param[out] sectors buffer for sectors data
261+
* @param[in,out] count On input Capacity of @p sectors, on output number of
262+
* sectors retrieved.
263+
*
264+
* @return 0 on success, negative errno code on fail. Especially returns
265+
* -ENOMEM if There are too many flash pages on the flash_area to fit in the
266+
* array.
267+
*/
268+
int flash_area_sectors(const struct flash_area *fa, uint32_t *count, struct flash_sector *sectors);
269+
257270
/**
258271
* Flash map iteration callback
259272
*
@@ -407,12 +420,13 @@ uint8_t flash_area_erased_val(const struct flash_area *fa);
407420
*/
408421
#define FIXED_PARTITION(label) FIXED_PARTITION_1(DT_NODELABEL(label))
409422
#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)
423+
#define FIXED_PARTITION_0(ord) \
424+
((const struct flash_area *)&DT_CAT(global_fixed_partition_ORD_, ord))
411425

412426
/** @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);
427+
#define DECLARE_PARTITION(node) DECLARE_PARTITION_0(DT_DEP_ORD(node))
428+
#define DECLARE_PARTITION_0(ord) \
429+
extern const struct flash_area DT_CAT(global_fixed_partition_ORD_, ord);
416430
#define FOR_EACH_PARTITION_TABLE(table) DT_FOREACH_CHILD(table, DECLARE_PARTITION)
417431

418432
/* Generate declarations */

subsys/storage/flash_map/flash_map_layout.c

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -78,16 +78,24 @@ static bool get_sectors_cb(const struct flash_pages_info *info, void *datav)
7878

7979
int flash_area_get_sectors(int idx, uint32_t *cnt, struct flash_sector *ret)
8080
{
81-
struct layout_data data;
82-
const struct device *flash_dev;
8381
const struct flash_area *fa;
8482
int rc = flash_area_open(idx, &fa);
8583

8684
if (rc < 0 || fa == NULL) {
8785
return -EINVAL;
8886
}
8987

90-
data.area_idx = idx;
88+
rc = flash_area_sectors(fa, cnt, ret);
89+
flash_area_close(fa);
90+
91+
return rc;
92+
}
93+
94+
int flash_area_sectors(const struct flash_area *fa, uint32_t *cnt, struct flash_sector *ret)
95+
{
96+
struct layout_data data;
97+
const struct device *flash_dev;
98+
9199
data.area_off = fa->fa_off;
92100
data.area_len = fa->fa_size;
93101

@@ -97,10 +105,6 @@ int flash_area_get_sectors(int idx, uint32_t *cnt, struct flash_sector *ret)
97105
data.status = 0;
98106

99107
flash_dev = fa->fa_dev;
100-
flash_area_close(fa);
101-
if (flash_dev == NULL) {
102-
return -ENODEV;
103-
}
104108

105109
flash_page_foreach(flash_dev, get_sectors_cb, &data);
106110

0 commit comments

Comments
 (0)