drivers/mtd:init commit of power-loss resilient cfg

Signed-off-by: 田昕 <tianxin7@xiaomi.com>
This commit is contained in:
田昕 2022-08-10 22:11:29 +08:00 committed by Xiang Xiao
parent 7d877fbbc3
commit f6ebbe4429
8 changed files with 2238 additions and 1 deletions

View File

@ -109,6 +109,9 @@ choice ESP32C3_SPIFLASH_FS
bool "LittleFS"
select FS_LITTLEFS
config ESP32C3_SPIFLASH_MTD_CONFIG
bool "Non-volatile storage"
endchoice
config ESP32C3_SPIFLASH_FS_MOUNT_PT

View File

@ -0,0 +1,52 @@
#
# This file is autogenerated: PLEASE DO NOT EDIT IT.
#
# You can use "make menuconfig" to make any modifications to the installed .config file.
# You can then do "make savedefconfig" to generate a new defconfig file that includes your
# modifications.
#
# CONFIG_NSH_ARGCAT is not set
# CONFIG_NSH_CMDOPT_HEXDUMP is not set
# CONFIG_NSH_CMDPARMS is not set
CONFIG_ARCH="risc-v"
CONFIG_ARCH_BOARD="esp32c3-devkit"
CONFIG_ARCH_BOARD_ESP32C3_DEVKIT=y
CONFIG_ARCH_CHIP="esp32c3"
CONFIG_ARCH_CHIP_ESP32C3=y
CONFIG_ARCH_CHIP_ESP32C3WROOM02=y
CONFIG_ARCH_INTERRUPTSTACK=1536
CONFIG_ARCH_RISCV=y
CONFIG_ARCH_STACKDUMP=y
CONFIG_BCH=y
CONFIG_BOARD_LOOPSPERMSEC=15000
CONFIG_BUILTIN=y
CONFIG_DEV_ZERO=y
CONFIG_ESP32C3_SPIFLASH=y
CONFIG_ESP32C3_SPIFLASH_MTD_CONFIG=y
CONFIG_ESP32C3_STORAGE_MTD_OFFSET=0x3e6000
CONFIG_ESP32C3_STORAGE_MTD_SIZE=0x3000
CONFIG_FS_PROCFS=y
CONFIG_IDLETHREAD_STACKSIZE=2048
CONFIG_INIT_ENTRYPOINT="nsh_main"
CONFIG_INTELHEX_BINARY=y
CONFIG_LIBC_PERROR_STDOUT=y
CONFIG_LIBC_STRERROR=y
CONFIG_MTD_CONFIG=y
CONFIG_MTD_CONFIG_FAIL_SAFE=y
CONFIG_MTD_CONFIG_NAMED=y
CONFIG_NFILE_DESCRIPTORS_PER_BLOCK=6
CONFIG_NSH_ARCHINIT=y
CONFIG_NSH_BUILTIN_APPS=y
CONFIG_NSH_FILEIOSIZE=512
CONFIG_NSH_READLINE=y
CONFIG_NSH_STRERROR=y
CONFIG_PREALLOC_TIMERS=0
CONFIG_RR_INTERVAL=200
CONFIG_SCHED_WAITPID=y
CONFIG_START_DAY=29
CONFIG_START_MONTH=11
CONFIG_START_YEAR=2019
CONFIG_SYSTEM_CFGDATA=y
CONFIG_SYSTEM_NSH=y
CONFIG_TESTING_MTD_CONFIG_FAIL_SAFE=y
CONFIG_UART0_SERIAL_CONSOLE=y

View File

@ -37,6 +37,7 @@
#include <nuttx/kmalloc.h>
#include <nuttx/spi/spi.h>
#include <nuttx/mtd/mtd.h>
#include <nuttx/mtd/configdata.h>
#include <nuttx/fs/nxffs.h>
#ifdef CONFIG_BCH
#include <nuttx/drivers/drivers.h>
@ -500,6 +501,36 @@ static int init_storage_partition(void)
return ret;
}
#elif defined (CONFIG_ESP32C3_SPIFLASH_MTD_CONFIG)
#if defined (CONFIG_TESTING_MTD_CONFIG_FAIL_SAFE)
/* To test power-loss resilient kv system,
* we write possible power-loss flash layout into flash
* then start kv system to see if it recovers.
* To do so, we need a mtd driver so that test code can
* write into flash.
*/
const char *path = CONFIG_TESTING_MTD_CONFIG_FAIL_SAFE_MOUNTPT_NAME;
ret = register_mtddriver(path, mtd, 0777, NULL);
if (ret < 0)
{
ferr("ERROR: Failed to register MTD: %d\n", ret);
return ret;
}
#else
ret = mtdconfig_register(mtd);
if (ret < 0)
{
ferr("ERROR: Failed to setup mtd config\n");
return ret;
}
#endif
#else
ret = register_mtddriver("/dev/esp32c3flash", mtd, 0755, NULL);

View File

@ -184,6 +184,15 @@ config MTD_CONFIG_NAME_LEN
---help---
Sets the maximum length of config item names.
config MTD_CONFIG_FAIL_SAFE
bool "Enable Fail Safe MTD Config"
default n
depends on MTD_BYTE_WRITE
---help---
Enable the new storage layout to support the resilient to power loss.
This replaces the drivers/mtd/mtd_config, which
is resilient to power loss.
endif # MTD_CONFIG
comment "MTD Device Drivers"

View File

@ -27,7 +27,13 @@
ifeq ($(CONFIG_MTD),y)
CSRCS += ftl.c mtd_config.c
CSRCS += ftl.c
ifeq ($(CONFIG_MTD_CONFIG_FAIL_SAFE),y)
CSRCS += mtd_config_fs.c
else ifeq ($(CONFIG_MTD_CONFIG),y)
CSRCS += mtd_config.c
endif
ifeq ($(CONFIG_MTD_PARTITION),y)
CSRCS += mtd_partition.c

View File

@ -1786,4 +1786,39 @@ int mtdconfig_register(FAR struct mtd_dev_s *mtd)
errout:
return ret;
}
/****************************************************************************
* Name: mtdconfig_unregister
*
* Description:
* Unregister a /dev/config device backed by a MTD.
*
****************************************************************************/
int mtdconfig_unregister(void)
{
int ret;
struct file file;
FAR struct inode *inode;
FAR struct mtdconfig_struct_s *dev;
ret = file_open(&file, "/dev/config", 0);
if (ret < 0)
{
ferr("ERROR: open /dev/config failed: %d\n", ret);
return ret;
}
inode = file.f_inode;
dev = (FAR struct mtdconfig_struct_s *)inode->i_private;
nxmutex_destroy(&dev->exclsem);
kmm_free(dev);
file_close(&file);
unregister_driver("/dev/config");
return OK;
}
#endif /* CONFIG_MTD_CONFIG */

2086
drivers/mtd/mtd_config_fs.c Normal file

File diff suppressed because it is too large Load Diff

View File

@ -120,6 +120,21 @@ extern "C"
struct mtd_dev_s;
int mtdconfig_register(FAR struct mtd_dev_s *mtd);
/****************************************************************************
* Name: mtdconfig_unregister
*
* Description:
* This function unregisters /dev/config device.
*
* Input Parameters:
*
* Returned Value:
* Zero on success; a negated errno value on failure.
*
****************************************************************************/
int mtdconfig_unregister(void);
#undef EXTERN
#ifdef __cplusplus
}