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 Reply Children
  • I'm not sure if I understand your question. The read/write to firmware above should read/write to flash on the nrf9160 (as indicated by other forum posts) but instead it causes a hard fault; causing my DK to reboot once per second. It is an error because the inclusion of the above firmware in mqtt_simple projects directly causes the cyclic reboot.

    Maybe to simplify the ask, can you please help me understand why:

    DT_FLASH_AREA_STORAGE_OFFSET and DT_FLASH_DEV_NAME are seemingly undeclared.

  • cachro2 said:
    I'm not sure if I understand your question.

     It looks like you didn't properly copy/paste the output log, because all I see is

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

    and no error.

  • Unfortunately, I bricked my DK trying to run variants of this firmware so I cannot get you more logs. However, the firmware never reached the printk statements which would have indicated that it wrote to and read from the flash sectors correctly. 

  • cachro2 said:
    Unfortunately, I bricked my DK trying to run variants of this firmware so I cannot get you more logs.

     Have you tried 'nrfjprog --recover' on the bricked DK?

  • That recovered the DK!

    So I reloaded modem and application firmware after the recover and everything worked... could not repeat the rebooting failure. I assume I messed up something on the DK while I was testing which lead to the problems.

    Here is the mqtt_simple project for those interested. It reads/writes the string "Hello\n" to flash and is able to send AT commands to the modem.

    mqtt_simple_flash_at_commands.zip

    LTE Link Monitor output:

    2020-11-25T19:19:19.460Z DEBUG modem << SPM: NS image at 0x1c200
    2020-11-25T19:19:19.461Z DEBUG modem << SPM: NS MSP at 0x20024e00
    2020-11-25T19:19:19.462Z DEBUG modem << SPM: NS reset vector at 0x1f919
    2020-11-25T19:19:19.468Z DEBUG modem << SPM: prepare to jump to Non-Secure image.
    2020-11-25T19:19:19.662Z DEBUG modem << *** Booting Zephyr OS build v2.4.0-ncs1  ***
    2020-11-25T19:19:19.678Z DEBUG modem << The MQTT simple sample started
    2020-11-25T19:19:19.697Z DEBUG modem << 1
    2020-11-25T19:19:19.699Z DEBUG modem << 2
    2020-11-25T19:19:19.701Z DEBUG modem << Hello
    2020-11-25T19:19:19.703Z DEBUG modem << LTE Link Connecting ...
    2020-11-25T19:19:21.220Z DEBUG modem << +CEREG: 2,"CB64","031B9801",7,0,0,"11100000","11100000"
    2020-11-25T19:19:21.274Z DEBUG modem << +CSCON: 1
    2020-11-25T19:19:24.578Z DEBUG modem << +CEREG: 5,"CB64","031B9801",7,,,"11100000","11100000"
    2020-11-25T19:19:24.604Z DEBUG modem >> AT+COPS=3,2
    2020-11-25T19:19:24.607Z DEBUG modem << LTE Link Connected!

    There was a minor bug in the original post which I updated.

    Thanks for your support Hakon and apologies for the confusion.

Related