Skip to content

Commit c10fbc8

Browse files
committed
tests: flash_map: Reduce flash wear in test_flash_area_get_sectors
Remove needless writes/read and erase in flash_area_get_sectors test scenario, by replacing it with comparison with layout directly obtained from device. Signed-off-by: Dominik Ermel <[email protected]>
1 parent d4b7bf9 commit c10fbc8

File tree

1 file changed

+14
-48
lines changed
  • tests/subsys/storage/flash_map/src

1 file changed

+14
-48
lines changed

tests/subsys/storage/flash_map/src/main.c

Lines changed: 14 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#define SLOT1_PARTITION_ID FIXED_PARTITION_ID(SLOT1_PARTITION)
1616
#define SLOT1_PARTITION_DEV FIXED_PARTITION_DEVICE(SLOT1_PARTITION)
1717
#define SLOT1_PARTITION_NODE DT_NODELABEL(SLOT1_PARTITION)
18+
#define SLOT1_PARTITION_OFFSET FIXED_PARTITION_OFFSET(SLOT1_PARTITION)
1819

1920
extern int flash_map_entries;
2021
struct flash_sector fs_sectors[1024];
@@ -41,8 +42,6 @@ ZTEST(flash_map, test_flash_area_get_sectors)
4142
int i;
4243
int rc;
4344
off_t off;
44-
uint8_t wd[512];
45-
uint8_t rd[512];
4645
const struct device *flash_dev;
4746
const struct device *flash_dev_a = SLOT1_PARTITION_DEV;
4847

@@ -55,61 +54,28 @@ ZTEST(flash_map, test_flash_area_get_sectors)
5554
/* Device obtained by label should match the one from fa object */
5655
zassert_equal(flash_dev, flash_dev_a, "Device for slot1_partition do not match");
5756

58-
rc = flash_erase(flash_dev, fa->fa_off, fa->fa_size);
59-
zassert_true(rc == 0, "flash area erase fail");
60-
61-
(void)memset(wd, 0xa5, sizeof(wd));
62-
6357
sec_cnt = ARRAY_SIZE(fs_sectors);
6458
rc = flash_area_get_sectors(SLOT1_PARTITION_ID, &sec_cnt, fs_sectors);
6559
zassert_true(rc == 0, "flash_area_get_sectors failed");
6660

67-
/* write stuff to beginning of every sector */
6861
off = 0;
69-
for (i = 0; i < sec_cnt; i++) {
70-
rc = flash_area_write(fa, off, wd, sizeof(wd));
71-
zassert_true(rc == 0, "flash_area_write() fail");
72-
73-
/* read it back via hal_flash_Read() */
74-
rc = flash_read(flash_dev, fa->fa_off + off, rd, sizeof(rd));
75-
zassert_true(rc == 0, "hal_flash_read() fail");
76-
77-
rc = memcmp(wd, rd, sizeof(wd));
78-
zassert_true(rc == 0, "read data != write data");
79-
80-
/* write stuff to end of area */
81-
rc = flash_write(flash_dev, fa->fa_off + off +
82-
fs_sectors[i].fs_size - sizeof(wd),
83-
wd, sizeof(wd));
84-
zassert_true(rc == 0, "hal_flash_write() fail");
85-
86-
/* and read it back */
87-
(void)memset(rd, 0, sizeof(rd));
88-
rc = flash_area_read(fa, off + fs_sectors[i].fs_size -
89-
sizeof(rd),
90-
rd, sizeof(rd));
91-
zassert_true(rc == 0, "hal_flash_read() fail");
92-
93-
rc = memcmp(wd, rd, sizeof(rd));
94-
zassert_true(rc == 0, "read data != write data");
9562

63+
/* For each reported sector, check if it corresponds to real page on device */
64+
for (i = 0; i < sec_cnt; ++i) {
65+
struct flash_pages_info fpi;
66+
67+
zassert_ok(flash_get_page_info_by_offs(flash_dev,
68+
SLOT1_PARTITION_OFFSET + off,
69+
&fpi));
70+
/* Offset of page taken directly from device corresponds to offset
71+
* within flash area
72+
*/
73+
zassert_equal(fpi.start_offset,
74+
fs_sectors[i].fs_off + SLOT1_PARTITION_OFFSET);
75+
zassert_equal(fpi.size, fs_sectors[i].fs_size);
9676
off += fs_sectors[i].fs_size;
9777
}
9878

99-
/* erase it */
100-
rc = flash_area_erase(fa, 0, fa->fa_size);
101-
zassert_true(rc == 0, "read data != write data");
102-
103-
/* should read back ff all throughout*/
104-
(void)memset(wd, 0xff, sizeof(wd));
105-
for (off = 0; off < fa->fa_size; off += sizeof(rd)) {
106-
rc = flash_area_read(fa, off, rd, sizeof(rd));
107-
zassert_true(rc == 0, "hal_flash_read() fail");
108-
109-
rc = memcmp(wd, rd, sizeof(rd));
110-
zassert_true(rc == 0, "area not erased");
111-
}
112-
11379
flash_area_close(fa);
11480
}
11581

0 commit comments

Comments
 (0)