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

How to use nvs_read and nvs_write in aws_fota app?

I would like to use nvs_read and nvs_write in aws_fota app to store a mode value inside, but it doesn't work. Any help?

I added the following lines to aws_fota prj.conf

<prj.conf>

CONFIG_FLASH_PAGE_LAYOUT=y
CONFIG_MPU_ALLOW_FLASH_WRITE=y

I added the following code to aws_fota maiin.c

#include <drivers/flash.h>
#include <storage/flash_map.h>
#include <fs/nvs.h>
#define NVS_ID_MODE 1

int8_t init_nvs_sys()
{
    u8_t rc;
    struct flash_pages_info info;

	/* define the nvs file system by settings with:
	 *	sector_size equal to the pagesize,
	 *	3 sectors
	 *	starting at FLASH_AREA_OFFSET(storage)
	 */
	nvs_fs.offset = FLASH_AREA_OFFSET(storage);
	rc = flash_get_page_info_by_offs(
		device_get_binding(DT_CHOSEN_ZEPHYR_FLASH_CONTROLLER_LABEL),
		nvs_fs.offset, &info);
	if (rc) {
		printk("Unable to get page info");
        return -1;
	}
	nvs_fs.sector_size = info.size;
	nvs_fs.sector_count = 3U;

	rc = nvs_init(&nvs_fs, DT_CHOSEN_ZEPHYR_FLASH_CONTROLLER_LABEL);
	if (rc) {
		printk("Flash Init failed\n");
        return -1;
	}
}

void main(void)
{
	int err;

    /***************/
	/* Write and read params */
    int ret;
    u8_t mode;
	ret = init_nvs_sys();
	if (ret < 0) {
        printk("Error: init_nvs_sys\n");
	}
    // Write mode
	mode = 0;
	if(nvs_write(&nvs_fs, NVS_ID_MODE, &mode, 1) < 0){ // This causes Error -22
        printk("Error: nvs_write NVS_ID_MODE\n");
    }
    mode = 2;

    // Read mode
	ret = nvs_read(&nvs_fs, NVS_ID_MODE, &mode, 1);
	printk( "nvs_read ret = %d, id = %d, mode = %u\n", ret, NVS_ID_MODE, mode);
	if (ret < 0) {
		printk("Error: nvs_read NVS_ID_MODE\n");
	}

    .
    .
    .
        

	

I have inspected a while and it seems that this line in prj.conf causes the issue.

CONFIG_SETTING=y

Can you tell me how to solve this issue?

Parents Reply Children
No Data
Related