Flash Storage in nrf connect sdk with mesh

Hi,

I have successfully added nvs in my project and able to store and retrieve data in flash.

But, i can't able to write after initializing mesh.

Could you guide me on this??

Thanks.

Parents
  • Hi

    No, it shouldn't be necessary to reinitialize the entire fs every time you write or read, but one of the init functions are probably necessary. What exactly "isn't working". Do you get any error codes/messages that tell you what it is that fails exactly if you don't reinitialize the fs? Do you perhaps uninit the filesystem after a write/read in your application?

    Either way, it might be worth checking out the Emergency data storage library instead of using NVS, which lets you do frequent storing of data while preventing wear and tear of the flash memory. Please check it out, as it may be a better option for you.

    Best regards,

    Simon

Reply
  • Hi

    No, it shouldn't be necessary to reinitialize the entire fs every time you write or read, but one of the init functions are probably necessary. What exactly "isn't working". Do you get any error codes/messages that tell you what it is that fails exactly if you don't reinitialize the fs? Do you perhaps uninit the filesystem after a write/read in your application?

    Either way, it might be worth checking out the Emergency data storage library instead of using NVS, which lets you do frequent storing of data while preventing wear and tear of the flash memory. Please check it out, as it may be a better option for you.

    Best regards,

    Simon

Children
  • but one of the init functions are probably necessary.

    Yes, the nvs_mount() needs to be called again right?.

    Do you get any error codes/messages that tell you what it is that fails exactly if you don't reinitialize the fs?

    the nvs_write returns the no.of bytes written(26 in my case) as mentioned in file. But when i read using nvs_read, the content didn't get updated as it shows the previously stored content.

    Do you perhaps uninit the filesystem after a write/read in your application?

    No, i dont think so, i have only used the previously mentioned lines for initializing and nvs_read ,nvs_write only.

    I will check out the EMDS library, but now i also see in the device memory layout, whenever i reset and read the memory contents each time the size gets increased by 16 & 32 bytes erspectively on application area from F8.

    Any idea why does this happen?. This happens each time only if i call nvs_write.

  • Hi

    nvs_mount() should only be necessary to do when initializing the Flash, and not every time you do a read/write function. You can see the \<NCS_FOLDER>\v2.2.0\zephyr\samples\subsys\nvs for reference here.

    After doing a new nvs_write() function, do you update the address from which you're reading in order to get the new data. If you get the previously stored data, that would point to you reading the same address(es) again.

    Vignesh R said:
    whenever i reset and read the memory contents each time the size gets increased by 16 & 32 bytes

    Can you explain/show what functions exactly are used when you read/reset the memory contents? Does a reset also result in an nvs_write perhaps? This should only increase if you write something new to the nRF device's flash I believe.

    Best regards,

    Simon

  • nvs_mount() should only be necessary to do when initializing the Flash, and not every time you do a read/write function
    1. If i didn't call that, the nvs_write doesn't works.
    After doing a new nvs_write() function, do you update the address from which you're reading in order to get the new data. If you get the previously stored data, that would point to you reading the same address(es) again

    Actually i have only one  structure, which i will be updating and storing again and again.

    Can you explain/show what functions exactly are used when you read/reset the memory contents? Does a reset also result in an nvs_write perhaps? This should only increase if you write something new to the nRF device's flash I believe.

    Below are the functions i use for flash, in which ible_config_t is my structure which i need to update each time and store.

    int ible_flash_init(void)
    {
        /* define the nvs file system by settings with:
            *	sector_size equal to the pagesize,
            *	3 sectors
            *	starting at FLASH_AREA_OFFSET(storage)
            */
        fs.flash_device = FLASH_AREA_DEVICE(STORAGE_NODE_LABEL);
        if (!device_is_ready(fs.flash_device)) {
            printk("Flash device %s is not ready\n", fs.flash_device->name);
            return;
        }
        fs.offset = FLASH_AREA_OFFSET(storage);
        rc = flash_get_page_info_by_offs(fs.flash_device, fs.offset, &info);
        printk("index:%x\n",info.index);
        printk("size:%x\n",info.size);
        if (rc) {
            printk("Unable to get page info\n");
            return;
        }
        fs.sector_size = info.size;
        fs.sector_count = 3U;
    
    }
    
    int ible_flash_write(ible_config_t *custom_data)
    {
        /* ADDRESS_ID is used to store an address, lets see if we can
            * read it from flash, since we don't know the size read the
            * maximum possible
            */
    
            rc = nvs_mount(&fs);
            if (rc) {
                printk("Flash Init failed\n");
                return;
            }
            //printk("No address found, adding %s at id %d\n", custom_data->addr,1);
            rc = nvs_write(&fs, 1, custom_data, sizeof(ible_config_t)+1);
            printf("write_err:%d\n",rc);
    
            uint8_t err = nvs_calc_free_space(&fs);
            printf("rem_space:%x\n",err);
    
            return rc;
    }
    
    int ible_flash_read(ible_config_t *custom_data)
    {
        rc = nvs_mount(&fs);
        if (rc) {
            printk("Flash Init failed\n");
            return;
        }
        rc = nvs_read(&fs, 1, &buf, sizeof(buf));
        printf("buf:%s\n",buf);
        custom_data = (ible_config_t*)buf;
        printf("err:%d",rc);
        if (rc > 0) { /* item was found, show it */
            //printk("Count: %d, data: %s, addr: %c%c%c%c\n", flash_config_buf->count,flash_config_buf->sensor_data,flash_config_buf->addr[0],flash_config_buf->addr[1],flash_config_buf->addr[2],flash_config_buf->addr[3]);
            // printk("Id: %d, Address: %s\n", 1, buf);
        }
        memset(buf,0,sizeof(ible_config_t));
        return rc;
    }

  • Vignesh R said:
    If i didn't call that, the nvs_write doesn't works.

    If that's the case you must be unmounting or disabling the nvs between every write. The nvs_mount should optimally be in its own initialization function, but this procedure should be fine as well if you always only update the same structure. This wll be up to you.

    Best regards,

    Simon

Related