nRF52840 Dongle Settings / NVS / Flash doesn't work

If anyone has successfully stored and retrieved settings on the nRF52840 dongle using any method, please share your approach.

I'm trying to store and retrieve settings on an nRF52840 dongle, but the Settings, NVS, and Flash APIs are not working.

I suspect there may be an issue with the Flash configuration due to the nrf flash controller. How can I resolve this?

OS: Intel Mac 15.1.1(24B91)

IDE: nRF Connect for VS Code

nRF Connect SDK Toolchain: v2.8.0

nRF Connect SDK: v.2.8.99

Below is the code for using the NVS API, but when it enters nvs_mount, it never returns.

#include <zephyr/kernel.h>
#include <zephyr/device.h>
#include <string.h>
#include <zephyr/drivers/flash.h>
#include <zephyr/storage/flash_map.h>
#include <zephyr/fs/nvs.h>

static struct nvs_fs fs;

#define NVS_PARTITION storage_partition
#define NVS_PARTITION_DEVICE FIXED_PARTITION_DEVICE(NVS_PARTITION)
#define NVS_PARTITION_OFFSET FIXED_PARTITION_OFFSET(NVS_PARTITION)

int nvs_setup(void) {
  int err;
  struct flash_pages_info info;

  fs.flash_device = NVS_PARTITION_DEVICE;
  if (!device_is_ready(fs.flash_device)) {
    LOG_ERR("device_is_ready %s err", fs.flash_device->name);
    return -EIO;
  }

  fs.offset = NVS_PARTITION_OFFSET;
  err = flash_get_page_info_by_offs(fs.flash_device, fs.offset, &info);
  if (err) {
    LOG_ERR("flash_get_page_info_by_offs err=%d", err);
    return err;
  }

  fs.sector_size = info.size;
  fs.sector_count = 2U;

  LOG_INF("fs.flash_device->name=%s", fs.flash_device->name);
  LOG_INF("fs.offset=%lu", fs.offset);
  LOG_INF("fs.sector_size=%d", fs.sector_size);
  LOG_INF("fs.sector_count=%d", fs.sector_count);

  err = nvs_mount(&fs);
  if (err) {
    LOG_ERR("nvs_mount err=%d", err);
    return err;
  }

  return 0;
}

CONFIG_NVS=y
CONFIG_FLASH=y
CONFIG_FLASH_MAP=y
CONFIG_MPU_ALLOW_FLASH_WRITE=y

I found flash_flatten in nvs_flash_erase_sector causes the error in zephry/subsys/fs/nvs/nvs.c

Below is the log manually returns -999 before calling flash_flatten.


[00:00:00.000,762] <inf> main: fs.flash_device->name=flash-controller@4001e000
[00:00:00.000,762] <inf> main: fs.offset=1040384
[00:00:00.000,793] <inf> main: fs.sector_size=4096
[00:00:00.000,793] <inf> main: fs.sector_count=2
[00:00:00.000,854] <dbg> fs_nvs: nvs_recover_last_ate: Recovering last ate from sector 0
[00:00:00.004,333] <inf> fs_nvs: No GC Done marker found: restarting gc
[00:00:00.004,333] <dbg> fs_nvs: nvs_flash_erase_sector: Erasing flash at fe000, len 4096
[00:00:00.004,364] <err> main: nvs_mount err=-999

Related