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

Fullscreen
1
2
3
4
CONFIG_FLASH=y
CONFIG_FLASH_PAGE_LAYOUT=y
CONFIG_NVS=y
CONFIG_MPU_ALLOW_FLASH_WRITE=y
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

and the following functions to mqtt_simple

Fullscreen
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#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) {
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Then in main()

Fullscreen
1
2
3
4
5
6
7
8
9
10
void main(void)
{
int err;
printk("The MQTT simple sample started\n");
read_write_nvs_sys();
...
...
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

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

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

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