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

Why doesn't NVS write data to flash correctly?

I'm testing a simple program below, which writes three params followed by read the three params.

Each params is set in tx_buf correctly, but nvs_write does not seem to work as expected.

Any help?

Fullscreen
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <zephyr.h>
#include <misc/reboot.h>
#include <device.h>
#include <string.h>
#include <flash.h>
#include <nvs/nvs.h>
#define SERIALNUM_ID 1
#define MQTT_PASSWORD_ID 2
#define MODE_ID 3
static struct nvs_fs fs;
struct flash_pages_info info;
uint8_t count = 0;
u8_t rc;
uint8_t init_nvs_sys(){
/* define the nvs file system by settings with:
* sector_size equal to the pagesize,
* 3 sectors
* starting at DT_FLASH_AREA_STORAGE_OFFSET
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Fullscreen
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
***** Booting Zephyr OS v1.14.99-ncs1 *****
WRITE | SERIALNUM_ID = 1, c = 1 # value is set in tx_buf correctly
WRITE | MQTT_PASSWORD_ID = 4, c = 2 # value is set in tx_buf correctly
WRITE | MODE_ID = 9, c = 3 # value is set in tx_buf correctly
READ | Id: 1, string: MODE_ID = 9, c = 3 # THIS IS WRONG VALUE. WHY?
READ | Id: 2, string: MODE_ID = 9, c = 3 # THIS IS WRONG VALUE. WHY?
READ | Id: 3, string: MODE_ID = 9, c = 3 # THIS IS WRONG VALUE. WHY?
[00:00:00.009,307] <err> fs_nvs: NVS not initialized # WHY DOES THIS HAPPEN?
[00:00:00.013,763] <err> fs_nvs: NVS not initialized
[00:00:00.017,120] <err> fs_nvs: NVS not initialized
[00:00:00.017,120] <err> fs_nvs: NVS not initialized
[00:00:00.022,094] <err> fs_nvs: NVS not initialized
[00:00:00.027,099] <err> fs_nvs: NVS not initialized
WRITE | SERIALNUM_ID = 1, c = 4
WRITE | MQTT_PASSWORD_ID = 4, c = 5
WRITE | MODE_ID = 9, c = 6
READ | Id: 1, string: MODE_ID = 9, c = 6
READ | Id: 2, string: MODE_ID = 9, c = 6
READ | Id: 3, string: MODE_ID = 9, c = 6
[00:00:01.040,802] <err> fs_nvs: NVS not initialized
[00:00:01.044,067] <err> fs_nvs: NVS not initialized
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX