zephyr/subsys/fs/nvs/nvs_priv.h

78 lines
1.8 KiB
C
Raw Normal View History

subsys: fs: Add Non Volatile Storage (NVS) for zephyr Zephyr supports fatfs, nffs and fcb as storage layer. fatfs and nffs are less suited for application in memory restricted IC's. fcb has a smaller footprint but has a complex api. The proposed module is a module with a even smaller footprint compared to fcb and a simple interface for reading and writing entries. The module provides wear levelling of flash. This allows the module to be used not only to store configuration settings but to store device state (e.g. state of a light switch over reboots) of a zephyr device. Fixes buffer overflow by introducing maximum read length in nvs_read() and nvs_read_hist(). Fixes nvs_write() not to reflash the same data. Allows the user to do call nvs_write() for all defined entries without worries about flash wear. Fixes garbage collection error where wrong data could be copied. Add nvs_delete() to allow deleting a stored entry. A deleted entry will not be copied to a new flash sector Include flash wear information in the README.md documentation 0/25 Update module after reviewers remarks, added documentation to nvs.h, removed README.md by nvs.rst in doc/subsystems folder 04/26 Update module after reviewers remarks, updated nvs.rst, added more documentation to samples/subsys/nvs/src/main.c, updated doxygen info in nvs.h (hope this time it works). 04/26 Update subsystems.rst to include nvs.restart 04/27 Updated nvs.c and nvs.h to avoid a possible flash deletion loop when the file system is full. 04/29 Updated nvs_write to detect and ignore deletes of non-existing items 05/06 Update NVS module to return standard error codes, removed low level API, added configuration options. NVS now uses the board dts to determine the flash storage location (FLASH_AREA_STORAGE_OFFSET). 05/06 Update nvs.rst. Updated intendation and added intermediate variables in nvs.c to make the code easier to read. 05/06 Update nvs.rst. 05/07 Update nvs.rst 05/08 Changed the API to a more standard file system API. 05/08 Removed cnt_max from nvs_read() as it is not used. 05/08 Removed #ifdef(CONFIG_NVS_LOG) from nvs_priv.h, now the module can be build with debugging off. 05/09 Removed configuration options for SECTOR_SIZE, SECTOR_COUNT and MAX_ELEM_SIZE. It is now easy to support multiple NVS filesystems on one or multiple devices. Changed logging to support newlib systems. Thanks to Olivier Martin for reporting and proposed changes. Signed-off-by: Laczen JMS <laczenjms@gmail.com>
2018-03-02 05:09:17 +08:00
/* NVS: non volatile storage in flash
*
* Copyright (c) 2018 Laczen
*
* SPDX-License-Identifier: Apache-2.0
*/
#ifndef __NVS_PRIV_H_
#define __NVS_PRIV_H_
#ifdef __cplusplus
extern "C" {
#endif
#define SYS_LOG_DOMAIN "fs/nvs"
#define SYS_LOG_LEVEL CONFIG_NVS_LOG_LEVEL
#include <logging/sys_log.h>
#define NVS_ID_GT(a, b) (((a) > (b)) ? ((((a)-(b)) > 0x7FFF) ? (0):(1)) : \
((((b)-(a)) > 0x7FFF) ? (1):(0)))
/*
* Special id values
*/
#define NVS_ID_EMPTY 0xFFFF
#define NVS_ID_SECTOR_END 0xFFFE
/*
* Status return values
*/
#define NVS_STATUS_NOSPACE 1
#define NVS_MOVE_BLOCK_SIZE 8
struct nvs_entry {
off_t data_addr; /* address in flash to write data */
u16_t len; /* entry length in bytes */
u16_t id; /* entry id, is 0xFFFFF when empty, set to 0xFFFE
* for the last entry in a sector
*/
};
struct _nvs_sector_hdr {
u32_t fd_magic;
u16_t fd_id;
};
struct _nvs_data_hdr {
u16_t id;
u16_t len;
};
struct _nvs_data_slt {
u16_t crc16;
};
int nvs_append(struct nvs_fs *fs, struct nvs_entry *entry);
int nvs_append_close(struct nvs_fs *fs, const struct nvs_entry *entry);
void nvs_set_start_entry(struct nvs_fs *fs, struct nvs_entry *entry);
int nvs_walk_entry(struct nvs_fs *fs, struct nvs_entry *entry);
int nvs_get_first_entry(struct nvs_fs *fs, struct nvs_entry *entry);
int nvs_get_last_entry(struct nvs_fs *fs, struct nvs_entry *entry);
int nvs_check_crc(struct nvs_fs *fs, struct nvs_entry *entry);
int nvs_rotate(struct nvs_fs *fs);
int nvs_compute_crc(struct nvs_fs *fs, const struct nvs_entry *entry,
u16_t *crc16);
subsys: fs: Add Non Volatile Storage (NVS) for zephyr Zephyr supports fatfs, nffs and fcb as storage layer. fatfs and nffs are less suited for application in memory restricted IC's. fcb has a smaller footprint but has a complex api. The proposed module is a module with a even smaller footprint compared to fcb and a simple interface for reading and writing entries. The module provides wear levelling of flash. This allows the module to be used not only to store configuration settings but to store device state (e.g. state of a light switch over reboots) of a zephyr device. Fixes buffer overflow by introducing maximum read length in nvs_read() and nvs_read_hist(). Fixes nvs_write() not to reflash the same data. Allows the user to do call nvs_write() for all defined entries without worries about flash wear. Fixes garbage collection error where wrong data could be copied. Add nvs_delete() to allow deleting a stored entry. A deleted entry will not be copied to a new flash sector Include flash wear information in the README.md documentation 0/25 Update module after reviewers remarks, added documentation to nvs.h, removed README.md by nvs.rst in doc/subsystems folder 04/26 Update module after reviewers remarks, updated nvs.rst, added more documentation to samples/subsys/nvs/src/main.c, updated doxygen info in nvs.h (hope this time it works). 04/26 Update subsystems.rst to include nvs.restart 04/27 Updated nvs.c and nvs.h to avoid a possible flash deletion loop when the file system is full. 04/29 Updated nvs_write to detect and ignore deletes of non-existing items 05/06 Update NVS module to return standard error codes, removed low level API, added configuration options. NVS now uses the board dts to determine the flash storage location (FLASH_AREA_STORAGE_OFFSET). 05/06 Update nvs.rst. Updated intendation and added intermediate variables in nvs.c to make the code easier to read. 05/06 Update nvs.rst. 05/07 Update nvs.rst 05/08 Changed the API to a more standard file system API. 05/08 Removed cnt_max from nvs_read() as it is not used. 05/08 Removed #ifdef(CONFIG_NVS_LOG) from nvs_priv.h, now the module can be build with debugging off. 05/09 Removed configuration options for SECTOR_SIZE, SECTOR_COUNT and MAX_ELEM_SIZE. It is now easy to support multiple NVS filesystems on one or multiple devices. Changed logging to support newlib systems. Thanks to Olivier Martin for reporting and proposed changes. Signed-off-by: Laczen JMS <laczenjms@gmail.com>
2018-03-02 05:09:17 +08:00
int nvs_flash_read(struct nvs_fs *fs, off_t offset, void *data, size_t len);
int nvs_flash_write(struct nvs_fs *fs, off_t offset, const void *data,
size_t len);
/**
* @}
*/
#ifdef __cplusplus
}
#endif
#endif /* __NVS_PRIV_H_ */