@@ -32,71 +32,106 @@ ZTEST(flash_map, test_flash_area_disabled_device)
3232 zassert_equal (rc , - ENOENT , "Open did not fail" );
3333}
3434
35+ ZTEST (flash_map , test_flash_area_device_is_ready )
36+ {
37+ const struct flash_area no_dev = {
38+ .fa_dev = NULL ,
39+ };
40+
41+ zassert_false (flash_area_device_is_ready (NULL ));
42+ zassert_false (flash_area_device_is_ready (& no_dev ));
43+ /* The below just assumes that tests are executed so late that
44+ * all devices are already initialized and ready.
45+ */
46+ zassert_true (flash_area_device_is_ready (
47+ FIXED_PARTITION (SLOT1_PARTITION )));
48+ }
49+
50+ static void layout_match (const struct device * flash_dev , uint32_t sec_cnt )
51+ {
52+ off_t off = 0 ;
53+ int i ;
54+
55+ /* For each reported sector, check if it corresponds to real page on device */
56+ for (i = 0 ; i < sec_cnt ; ++ i ) {
57+ struct flash_pages_info fpi ;
58+
59+ zassert_ok (
60+ flash_get_page_info_by_offs (flash_dev , SLOT1_PARTITION_OFFSET + off , & fpi ));
61+ /* Offset of page taken directly from device corresponds to offset
62+ * within flash area
63+ */
64+ zassert_equal (fpi .start_offset , fs_sectors [i ].fs_off + SLOT1_PARTITION_OFFSET );
65+ zassert_equal (fpi .size , fs_sectors [i ].fs_size );
66+ off += fs_sectors [i ].fs_size ;
67+ }
68+ }
69+
3570/**
3671 * @brief Test flash_area_get_sectors()
3772 */
3873ZTEST (flash_map , test_flash_area_get_sectors )
3974{
4075 const struct flash_area * fa ;
76+ const struct device * flash_dev_a = SLOT1_PARTITION_DEV ;
4177 uint32_t sec_cnt ;
42- int i ;
4378 int rc ;
44- off_t off ;
45- const struct device * flash_dev ;
46- const struct device * flash_dev_a = SLOT1_PARTITION_DEV ;
4779
48- rc = flash_area_open (SLOT1_PARTITION_ID , & fa );
49- zassert_true (rc == 0 , "flash_area_open() fail" );
80+ fa = FIXED_PARTITION (SLOT1_PARTITION );
5081
51- /* First erase the area so it's ready for use. */
52- flash_dev = flash_area_get_device (fa );
82+ zassert_true (flash_area_device_is_ready (fa ));
83+
84+ zassert_true (device_is_ready (flash_dev_a ));
5385
5486 /* Device obtained by label should match the one from fa object */
55- zassert_equal (flash_dev , flash_dev_a , "Device for slot1_partition do not match" );
87+ zassert_equal (fa -> fa_dev , flash_dev_a , "Device for slot1_partition do not match" );
88+
89+ memset (& fs_sectors [0 ], 0 , sizeof (fs_sectors ));
5690
5791 sec_cnt = ARRAY_SIZE (fs_sectors );
5892 rc = flash_area_get_sectors (SLOT1_PARTITION_ID , & sec_cnt , fs_sectors );
5993 zassert_true (rc == 0 , "flash_area_get_sectors failed" );
6094
61- off = 0 ;
95+ layout_match (flash_dev_a , sec_cnt );
96+ }
6297
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 ;
98+ ZTEST (flash_map , test_flash_area_sectors )
99+ {
100+ const struct flash_area * fa ;
101+ uint32_t sec_cnt ;
102+ int rc ;
103+ const struct device * flash_dev_a = SLOT1_PARTITION_DEV ;
66104
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 );
76- off += fs_sectors [i ].fs_size ;
77- }
105+ fa = FIXED_PARTITION (SLOT1_PARTITION );
106+
107+ zassert_true (flash_area_device_is_ready (fa ));
108+
109+ zassert_true (device_is_ready (flash_dev_a ));
110+
111+ /* Device obtained by label should match the one from fa object */
112+ zassert_equal (fa -> fa_dev , flash_dev_a , "Device for slot1_partition do not match" );
113+
114+ sec_cnt = ARRAY_SIZE (fs_sectors );
115+ rc = flash_area_sectors (fa , & sec_cnt , fs_sectors );
116+ zassert_true (rc == 0 , "flash_area_get_sectors failed" );
78117
79- flash_area_close ( fa );
118+ layout_match ( flash_dev_a , sec_cnt );
80119}
81120
82121ZTEST (flash_map , test_flash_area_erased_val )
83122{
84123 const struct flash_parameters * param ;
85124 const struct flash_area * fa ;
86125 uint8_t val ;
87- int rc ;
88126
89- rc = flash_area_open (SLOT1_PARTITION_ID , & fa );
90- zassert_true (rc == 0 , "flash_area_open() fail" );
127+ fa = FIXED_PARTITION (SLOT1_PARTITION );
91128
92129 val = flash_area_erased_val (fa );
93130
94131 param = flash_get_parameters (fa -> fa_dev );
95132
96133 zassert_equal (param -> erase_value , val ,
97134 "value different than the flash erase value" );
98-
99- flash_area_close (fa );
100135}
101136
102137ZTEST (flash_map , test_fixed_partition_node_macros )
@@ -118,8 +153,7 @@ ZTEST(flash_map, test_flash_area_erase_and_flatten)
118153 const struct flash_area * fa ;
119154 const struct device * flash_dev ;
120155
121- rc = flash_area_open (SLOT1_PARTITION_ID , & fa );
122- zassert_true (rc == 0 , "flash_area_open() fail" );
156+ fa = FIXED_PARTITION (SLOT1_PARTITION );
123157
124158 /* First erase the area so it's ready for use. */
125159 flash_dev = flash_area_get_device (fa );
0 commit comments