1313#include <zephyr/sys/crc.h>
1414#include <zephyr/ztest.h>
1515
16+ #if CONFIG_NATIVE_SIM
17+ #include <zephyr/kernel.h>
18+ #endif
19+
1620#include <bm/fs/bm_zms.h>
1721#include <bm_zms_priv.h>
1822
1923/* Arbitrary block size. */
20- #define SECTOR_SIZE 1024U
21-
22- #define STORAGE_NODE DT_NODELABEL(storage_partition)
24+ #define TEST_SECTOR_SIZE 1024U
25+ #define TEST_SECTOR_COUNT 4U
26+ #define TEST_PARTITION_SIZE (TEST_SECTOR_SIZE * TEST_SECTOR_COUNT)
27+ #define TEST_DATA_ID 1
28+
29+ #if CONFIG_NATIVE_SIM
30+ static uint8_t mem [TEST_PARTITION_SIZE ];
31+ #define TEST_PARTITION_START ((off_t)mem)
32+ #else
33+ #define STORAGE_NODE DT_NODELABEL(storage_partition)
2334#define TEST_PARTITION_START DT_REG_ADDR(STORAGE_NODE)
24- #define TEST_PARTITION_SIZE (SECTOR_SIZE * 4)
25- #define TEST_DATA_ID 1
26- #define TEST_SECTOR_COUNT 4U
35+ #endif
2736
2837struct bm_zms_fixture {
2938 struct bm_zms_fs fs ;
3039 struct bm_zms_fs_config config ;
3140};
41+
3242static bool nvm_is_full ;
3343
34- void bm_zms_test_handler (struct bm_zms_evt const * evt )
44+ static void wait_for_ongoing_writes (struct bm_zms_fs * fs )
45+ {
46+ while (fs -> ongoing_writes ) {
47+ #if CONFIG_NATIVE_SIM
48+ k_sleep (K_MSEC (100 ));
49+ #elif CONFIG_SOFTDEVICE
50+ /* Wait for an event. */
51+ __WFE ();
52+
53+ /* Clear Event Register */
54+ __SEV ();
55+ __WFE ();
56+ #endif
57+ }
58+ }
59+
60+ static void wait_for_init (struct bm_zms_fs * fs )
61+ {
62+ while (!fs -> init_flags .initialized ) {
63+ #if CONFIG_NATIVE_SIM
64+ k_sleep (K_MSEC (100 ));
65+ #elif CONFIG_SOFTDEVICE
66+ /* Wait for an event. */
67+ __WFE ();
68+
69+ /* Clear Event Register */
70+ __SEV ();
71+ __WFE ();
72+ #endif
73+ }
74+ }
75+
76+ static void wait_for_uninit (struct bm_zms_fs * fs )
77+ {
78+ while (fs -> init_flags .initialized ) {
79+ #if CONFIG_NATIVE_SIM
80+ k_sleep (K_MSEC (100 ));
81+ #elif CONFIG_SOFTDEVICE
82+ /* Wait for an event. */
83+ __WFE ();
84+
85+ /* Clear Event Register */
86+ __SEV ();
87+ __WFE ();
88+ #endif
89+ }
90+ }
91+
92+ void bm_zms_test_evt_handler (struct bm_zms_evt const * evt )
3593{
3694 if (evt -> evt_type == BM_ZMS_EVT_MOUNT ) {
3795 zassert_true (evt -> result == 0 , "bm_zms_mount call failure: %d" ,
@@ -57,9 +115,9 @@ static void *setup(void)
57115
58116 memset (& fixture , 0 , sizeof (struct bm_zms_fixture ));
59117 fixture .config .offset = TEST_PARTITION_START ;
60- fixture .config .sector_size = SECTOR_SIZE ;
118+ fixture .config .sector_size = TEST_SECTOR_SIZE ;
61119 fixture .config .sector_count = TEST_SECTOR_COUNT ;
62- fixture .config .evt_handler = bm_zms_test_handler ;
120+ fixture .config .evt_handler = bm_zms_test_evt_handler ;
63121
64122 return & fixture ;
65123}
@@ -79,37 +137,10 @@ static void after(void *data)
79137
80138 err = bm_zms_clear (& fixture -> fs );
81139 zassert_true (err == 0 , "zms_clear call failure: %x" , err );
140+ wait_for_uninit (& fixture -> fs );
82141 }
83142
84- fixture -> fs .sector_count = TEST_SECTOR_COUNT ;
85- }
86-
87- static void wait_for_ongoing_writes (struct bm_zms_fs * fs )
88- {
89- while (fs -> ongoing_writes ) {
90- #if defined(CONFIG_SOFTDEVICE )
91- /* Wait for an event. */
92- __WFE ();
93-
94- /* Clear Event Register */
95- __SEV ();
96- __WFE ();
97- #endif
98- }
99- }
100-
101- static void wait_for_init (struct bm_zms_fs * fs )
102- {
103- while (!fs -> init_flags .initialized ) {
104- #if defined(CONFIG_SOFTDEVICE )
105- /* Wait for an event. */
106- __WFE ();
107-
108- /* Clear Event Register */
109- __SEV ();
110- __WFE ();
111- #endif
112- }
143+ fixture -> config .sector_count = TEST_SECTOR_COUNT ;
113144}
114145
115146ZTEST_SUITE (bm_zms , NULL , setup , before , after , NULL );
@@ -155,6 +186,8 @@ ZTEST_F(bm_zms, test_bm_zms_write)
155186
156187 err = bm_zms_mount (& fixture -> fs , & fixture -> config );
157188 zassert_true (err == 0 , "zms_mount call failure: %d" , err );
189+ wait_for_init (& fixture -> fs );
190+
158191 execute_long_pattern_write (TEST_DATA_ID , & fixture -> fs );
159192}
160193
@@ -168,7 +201,7 @@ ZTEST_F(bm_zms, test_zms_gc)
168201 /* 21st write will trigger GC. */
169202 const uint16_t max_writes = 21 ;
170203
171- fixture -> fs .sector_count = 2 ;
204+ fixture -> config .sector_count = 2 ;
172205
173206 err = bm_zms_mount (& fixture -> fs , & fixture -> config );
174207 wait_for_init (& fixture -> fs );
@@ -296,6 +329,7 @@ ZTEST_F(bm_zms, test_zms_gc_3sectors)
296329
297330 err = bm_zms_mount (& fixture -> fs , & fixture -> config );
298331 zassert_true (err == 0 , "bm_zms_mount call failure" );
332+ wait_for_init (& fixture -> fs );
299333
300334 zassert_equal (fixture -> fs .ate_wra >> ADDR_SECT_SHIFT , 0 , "unexpected write sector" );
301335 check_content (max_id , & fixture -> fs );
@@ -339,7 +373,7 @@ ZTEST_F(bm_zms, test_zms_full_sector)
339373 uint32_t filling_id = 0 ;
340374 uint32_t data_read ;
341375
342- fixture -> fs .sector_count = 3 ;
376+ fixture -> config .sector_count = 3 ;
343377
344378 err = bm_zms_mount (& fixture -> fs , & fixture -> config );
345379 wait_for_init (& fixture -> fs );
@@ -392,7 +426,7 @@ ZTEST_F(bm_zms, test_delete)
392426 uint32_t ate_wra ;
393427 uint32_t data_wra ;
394428
395- fixture -> fs .sector_count = 3 ;
429+ fixture -> config .sector_count = 3 ;
396430
397431 err = bm_zms_mount (& fixture -> fs , & fixture -> config );
398432 wait_for_init (& fixture -> fs );
@@ -466,7 +500,7 @@ ZTEST_F(bm_zms, test_zms_cache_init)
466500
467501 /* Test cache initialization when the store is empty */
468502
469- fixture -> fs .sector_count = 3 ;
503+ fixture -> config .sector_count = 3 ;
470504 err = bm_zms_mount (& fixture -> fs , & fixture -> config );
471505 wait_for_init (& fixture -> fs );
472506 zassert_true (err == 0 , "bm_zms_mount call failure" );
@@ -512,7 +546,7 @@ ZTEST_F(bm_zms, test_zms_cache_collission)
512546 int err ;
513547 uint16_t data ;
514548
515- fixture -> fs .sector_count = 4 ;
549+ fixture -> config .sector_count = 4 ;
516550 err = bm_zms_mount (& fixture -> fs , & fixture -> config );
517551 wait_for_init (& fixture -> fs );
518552 zassert_true (err == 0 , "bm_zms_mount call failure" );
0 commit comments