From 08829bd388b496f6cb58c892a827967107817c91 Mon Sep 17 00:00:00 2001 From: Xiang Xiao Date: Mon, 19 Aug 2024 23:08:13 +0800 Subject: [PATCH] mtd/nvs: Trigger recovery process in nvs_startup when mtd driver return -EBADMSG Signed-off-by: Xiang Xiao --- drivers/mtd/mtd_config_fs.c | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/drivers/mtd/mtd_config_fs.c b/drivers/mtd/mtd_config_fs.c index 11a1349d57d50..9e9a575d1c8aa 100644 --- a/drivers/mtd/mtd_config_fs.c +++ b/drivers/mtd/mtd_config_fs.c @@ -234,12 +234,25 @@ static int nvs_flash_rd(FAR struct nvs_fs *fs, uint32_t addr, offset += addr & ADDR_OFFS_MASK; ret = MTD_READ(fs->mtd, offset, len, data); - if (ret < 0) + if (ret == -EBADMSG) { - return ret; + /* ECC fail first time + * try again to avoid transient electronic interference + */ + + ret = MTD_READ(fs->mtd, offset, len, data); + if (ret == -EBADMSG) + { + /* ECC fail second time + * fill ~erasestate to trigger recovery process + */ + + memset(data, ~fs->erasestate, len); + ret = 0; + } } - return OK; + return ret < 0 ? ret : 0; } /****************************************************************************