fs: nvs: introduce nvs_mount and deprecate nvs_init

Add a new API call to replace nvs_init: nvs_mount. The new API does the
same as nvs_init except that it assumes to be provided with a valid
flash device via `struct nvs_fs` `flash_device` field. Previously, it
was not possible to avoid the runtime overhead of device_get_binding()
even if the flash device was known at compile time.

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
This commit is contained in:
Gerard Marull-Paretas 2022-02-28 10:51:06 +01:00 committed by Carles Cufí
parent 0e8f556b17
commit 56d5486fbf
3 changed files with 31 additions and 12 deletions

View file

@ -41,6 +41,7 @@ For NVS the file system is declared as:
.. code-block:: c
static struct nvs_fs fs = {
.flash_device = NVS_FLASH_DEVICE,
.sector_size = NVS_SECTOR_SIZE,
.sector_count = NVS_SECTOR_COUNT,
.offset = NVS_STORAGE_OFFSET,
@ -48,6 +49,8 @@ For NVS the file system is declared as:
where
- ``NVS_FLASH_DEVICE`` is a reference to the flash device that will be used. The
device needs to be operational.
- ``NVS_SECTOR_SIZE`` is the sector size, it has to be a multiple of
the flash erase page size and a power of 2.
- ``NVS_SECTOR_COUNT`` is the number of sectors, it is at least 2, one

View file

@ -10,6 +10,7 @@
#include <sys/types.h>
#include <kernel.h>
#include <device.h>
#include <toolchain.h>
#ifdef __cplusplus
extern "C" {
@ -68,16 +69,15 @@ struct nvs_fs {
*/
/**
* @brief nvs_init
* @brief nvs_mount
*
* Initializes a NVS file system in flash.
* Mount a NVS file system onto the flash device specified in @p fs.
*
* @param fs Pointer to file system
* @param dev_name Pointer to flash device name
* @retval 0 Success
* @retval -ERRNO errno code if error
*/
int nvs_init(struct nvs_fs *fs, const char *dev_name);
int nvs_mount(struct nvs_fs *fs);
/**
* @brief nvs_clear
@ -165,6 +165,28 @@ ssize_t nvs_read_hist(struct nvs_fs *fs, uint16_t id, void *data, size_t len, ui
*/
ssize_t nvs_calc_free_space(struct nvs_fs *fs);
/**
* @brief nvs_init
*
* Initializes a NVS file system in flash.
*
* @deprecated Use nvs_mount() instead.
*
* @param fs Pointer to file system
* @param dev_name Pointer to flash device name
* @retval 0 Success
* @retval -ERRNO errno code if error
*/
__deprecated static inline int nvs_init(struct nvs_fs *fs, const char *dev_name)
{
fs->flash_device = device_get_binding(dev_name);
if (fs->flash_device == NULL) {
return -ENODEV;
}
return nvs_mount(fs);
}
/**
* @}
*/

View file

@ -623,7 +623,7 @@ static int nvs_startup(struct nvs_fs *fs)
struct nvs_ate last_ate;
size_t ate_size, empty_len;
/* Initialize addr to 0 for the case fs->sector_count == 0. This
* should never happen as this is verified in nvs_init() but both
* should never happen as this is verified in nvs_mount() but both
* Coverity and GCC believe the contrary.
*/
uint32_t addr = 0U;
@ -847,7 +847,7 @@ int nvs_clear(struct nvs_fs *fs)
return 0;
}
int nvs_init(struct nvs_fs *fs, const char *dev_name)
int nvs_mount(struct nvs_fs *fs)
{
int rc;
@ -856,12 +856,6 @@ int nvs_init(struct nvs_fs *fs, const char *dev_name)
k_mutex_init(&fs->nvs_lock);
fs->flash_device = device_get_binding(dev_name);
if (!fs->flash_device) {
LOG_ERR("No valid flash device found");
return -ENXIO;
}
fs->flash_parameters = flash_get_parameters(fs->flash_device);
if (fs->flash_parameters == NULL) {
LOG_ERR("Could not obtain flash parameters");