diff --git a/subsys/fs/nvs/nvs.c b/subsys/fs/nvs/nvs.c index ef35e677710..a07f9b3198b 100644 --- a/subsys/fs/nvs/nvs.c +++ b/subsys/fs/nvs/nvs.c @@ -308,7 +308,7 @@ static int nvs_flash_erase_sector(struct nvs_fs *fs, uint32_t addr) #ifdef CONFIG_NVS_LOOKUP_CACHE nvs_lookup_cache_invalidate(fs, addr >> ADDR_SECT_SHIFT); #endif - rc = flash_erase(fs->flash_device, offset, fs->sector_size); + rc = flash_flatten(fs->flash_device, offset, fs->sector_size); if (rc) { return rc; diff --git a/tests/subsys/fs/nvs/src/main.c b/tests/subsys/fs/nvs/src/main.c index 97e41f15b96..34a0080866d 100644 --- a/tests/subsys/fs/nvs/src/main.c +++ b/tests/subsys/fs/nvs/src/main.c @@ -207,7 +207,18 @@ ZTEST_F(nvs, test_nvs_corrupted_write) &flash_max_write_calls); stats_walk(fixture->sim_stats, flash_sim_write_calls_find, &flash_write_stat); +#if defined(CONFIG_FLASH_SIMULATOR_EXPLICIT_ERASE) *flash_max_write_calls = *flash_write_stat - 1; +#else + /* When there is no explicit erase, erase is done with write, which means + * that there are more writes needed. The nvs_write here will cause erase + * to be called, which in turn calls the flash_fill; flash_fill will + * overwrite data using buffer of size CONFIG_FLASH_FILL_BUFFER_SIZE, + * and then two additional real writes are allowed. + */ + *flash_max_write_calls = (fixture->fs.sector_size / + CONFIG_FLASH_FILL_BUFFER_SIZE) + 2; +#endif *flash_write_stat = 0; /* Flash simulator will lose part of the data at the end of this write. diff --git a/tests/subsys/fs/nvs/testcase.yaml b/tests/subsys/fs/nvs/testcase.yaml index f462a33ff0e..f02fc257fc1 100644 --- a/tests/subsys/fs/nvs/testcase.yaml +++ b/tests/subsys/fs/nvs/testcase.yaml @@ -6,6 +6,9 @@ tests: filesystem.nvs.0x00: extra_args: DTC_OVERLAY_FILE=boards/qemu_x86_ev_0x00.overlay platform_allow: qemu_x86 + filesystem.nvs.sim.no_erase: + extra_args: CONFIG_FLASH_SIMULATOR_EXPLICIT_ERASE=n + platform_allow: qemu_x86 filesystem.nvs.cache: extra_args: - CONFIG_NVS_LOOKUP_CACHE=y