This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

NRF9160dk storing to and reading from flash

What is the best/easiest way to store a struct or string to flash persistently?  I see other boards using FDS and other tools but those don't appear to be available on this board.  Are there any examples that write and read to and from flash?  I see DT_FLASH_AREA_6_DEV_LABEL=STORAGE so I assume I'm going to want to use that section of flash for my needs?

Any guidance would be much appreciated!

Parents
  • I was able to figure it out.  Zephyr has a library called NVS.

    In my prj.conf I added:

    CONFIG_FLASH=y
    CONFIG_FLASH_PAGE_LAYOUT=y
    CONFIG_NVS=y

    and then my code looks like this:

    #include <nvs/nvs.h>
    #include <device.h>
    #include <flash.h>
    
    
    {
    
    static struct nvs_fs fs;
    
    struct flash_pages_info info;
    fs.offset = DT_FLASH_AREA_STORAGE_OFFSET;
    err = flash_get_page_info_by_offs(device_get_binding(DT_FLASH_DEV_NAME),
        fs.offset, &info);
    if (err) {
        printk("Unable to get page info");
    }
    fs.sector_size = info.size;
    fs.sector_count = 3U;
    err = nvs_init(&fs, DT_FLASH_DEV_NAME);
    if (err) {
        printk("Flash Init failed\n");
    }
    
    char wbuf[16];
    char rbuf[16];
    strcpy(wbuf, "Hello");
    nvs_write(&fs, DT_FLASH_AREA_STORAGE_OFFSET, &wbuf, strlen(wbuf)+1);
    err = nvs_read(&fs, DT_FLASH_AREA_STORAGE_OFFSET, &rbuf, sizeof(rbuf));
    printk("%s", rbuf);
    
    }

  • Wanted to point out something I noticed after posting this:

    I name the return variable of the nvs read call "err" here, short for error. This could cause confusion because nvs_read returns the number of bytes read successfully and not an error code

Reply Children
No Data
Related