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

nRF9160DK read/write string to Flash in mqtt_simple

Hi,

I would like to write a string to flash in mqtt_simple SDK example using latest modem firmware and ncs v1.4.0. I based my code off the zephyr example (samples/subsys/nvs/src/main.c)

Added the following to proj.conf

CONFIG_FLASH=y
CONFIG_FLASH_PAGE_LAYOUT=y
CONFIG_NVS=y
CONFIG_MPU_ALLOW_FLASH_WRITE=y

and the following functions to mqtt_simple

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

static struct nvs_fs fs;

int8_t init_nvs_sys()
{
    uint8_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)
	 */
	fs.offset = FLASH_AREA_OFFSET(storage);
	rc = flash_get_page_info_by_offs(
		device_get_binding(DT_CHOSEN_ZEPHYR_FLASH_CONTROLLER_LABEL),
		fs.offset, &info);
	if (rc) {
		printk("Unable to get page info");
        return -1;
	}
	fs.sector_size = info.size;
	fs.sector_count = 3U;

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

int8_t read_write_nvs_sys() 
{
        /* Write and read params */
        if (init_nvs_sys() < 0) {
                printk("Error: init_nvs_sys\n");
        }
        char wbuf[16];
        char rbuf[16];

        strcpy(wbuf, "Hello\n");

        // Write mode
        if(nvs_write(&fs, fs.offset, &wbuf, strlen(wbuf)+1) < 0) {
                printk("Error: nvs_write\n");
        }
        // Read mode
        if (nvs_read(&fs, fs.offset, &rbuf, sizeof(rbuf)) < 0) {
                printk("Error: nvs_read\n");
        }
        else {
                printk("%s", rbuf);
        }
}

Then in main()

void main(void)
{
	int err;

	printk("The MQTT simple sample started\n");

    read_write_nvs_sys();
    
    ...
    ...
        

It compiles and runs but I get the following error in LTE Link Monitor which causes a cyclic reboot:

2020-11-17T15:04:06.608Z DEBUG modem << Non-secure callable region 0 placed in flash region 2 with size 32.

I tried running the example code given in this post: 
https://devzone.nordicsemi.com/f/nordic-q-a/52095/nrf9160dk-storing-to-and-reading-from-flash
and here: 
https://devzone.nordicsemi.com/f/nordic-q-a/47275/why-doesn-t-nvs-write-data-to-flash-correctly 

but they both use:

DT_FLASH_AREA_STORAGE_OFFSET and DT_FLASH_DEV_NAME 

which return as undefined in my project. Seems like defining these and replacing:

FLASH_AREA_OFFSET(storage) and DT_CHOSEN_ZEPHYR_FLASH_CONTROLLER_LABEL)

would solve my problem.

Thoughts?

Thank you,

Robert

Parents
  • Grep'ing through the file system found the following:

    C:\ncs\v1.4.0\zephyr\doc\releases\release-notes-2.3.rst:

      989: * :github:`24356` - MCUboot (and other users of DT_FLASH_DEV_NAME) broken with current zephyr master

    and

    devicetree_legacy_unfixed.h

    C:\ncs\v1.4.0\nrf\samples\nrf9160\mqtt_simple\build_nrf9160dk_nrf9160ns\zephyr\include\generated\devicetree_legacy_unfixed.h:
     2171  #define DT_FLASH_AREA_STORAGE_ID                    6
     2172  #define DT_FLASH_AREA_STORAGE_READ_ONLY             0
     2173: #define DT_FLASH_AREA_STORAGE_OFFSET_0              1024000
     2174: #define DT_FLASH_AREA_STORAGE_OFFSET                DT_FLASH_AREA_STORAGE_OFFSET_0
     2175  #define DT_FLASH_AREA_STORAGE_SIZE_0                24576
     2176  #define DT_FLASH_AREA_STORAGE_SIZE                  DT_FLASH_AREA_STORAGE_SIZE_0


    I am new to zephyr, any thoughts to resolve? 

Reply
  • Grep'ing through the file system found the following:

    C:\ncs\v1.4.0\zephyr\doc\releases\release-notes-2.3.rst:

      989: * :github:`24356` - MCUboot (and other users of DT_FLASH_DEV_NAME) broken with current zephyr master

    and

    devicetree_legacy_unfixed.h

    C:\ncs\v1.4.0\nrf\samples\nrf9160\mqtt_simple\build_nrf9160dk_nrf9160ns\zephyr\include\generated\devicetree_legacy_unfixed.h:
     2171  #define DT_FLASH_AREA_STORAGE_ID                    6
     2172  #define DT_FLASH_AREA_STORAGE_READ_ONLY             0
     2173: #define DT_FLASH_AREA_STORAGE_OFFSET_0              1024000
     2174: #define DT_FLASH_AREA_STORAGE_OFFSET                DT_FLASH_AREA_STORAGE_OFFSET_0
     2175  #define DT_FLASH_AREA_STORAGE_SIZE_0                24576
     2176  #define DT_FLASH_AREA_STORAGE_SIZE                  DT_FLASH_AREA_STORAGE_SIZE_0


    I am new to zephyr, any thoughts to resolve? 

Children
No Data
Related