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?
#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
*/
fs.offset = DT_FLASH_AREA_STORAGE_OFFSET;
rc = flash_get_page_info_by_offs(device_get_binding(DT_FLASH_DEV_NAME),
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_FLASH_DEV_NAME);
if (rc) {
printk("Flash Init failed\n");
return -1;
}
return 0;
}
uint8_t write_params(uint8_t id, char *prefix){
char tx_buf[32];
count++;
snprintk(tx_buf, 32, "%s = %u, c = %u", prefix, id*id, count);
printk("WRITE | %s\n", tx_buf);
nvs_write(&fs, id, &tx_buf, strlen(tx_buf)+1);
return 0;
}
uint8_t read_params(uint8_t id, char *prefix){
char rx_buf[32];
nvs_read(&fs, id, &rx_buf, sizeof(rx_buf));
printk("READ | Id: %d, string: %s\n", id, rx_buf);
return 0;
}
void main(void)
{
while(1){
write_params(SERIALNUM_ID, "SERIALNUM_ID");
write_params(MQTT_PASSWORD_ID, "MQTT_PASSWORD_ID");
write_params(MODE_ID, "MODE_ID");
read_params(SERIALNUM_ID, "SERIALNUM_ID");
read_params(MQTT_PASSWORD_ID, "MQTT_PASSWORD_ID");
read_params(MODE_ID, "MODE_ID");
k_sleep(1000);
}
}
***** 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 [00:00:01.046,539] <err> fs_nvs: NVS not initialized [00:00:01.046,539] <err> fs_nvs: NVS not initialized [00:00:01.050,231] <err> fs_nvs: NVS not initialized [00:00:01.053,894] <err> fs_nvs: NVS not initialized WRITE | SERIALNUM_ID = 1, c = 7 WRITE | MQTT_PASSWORD_ID = 4, c = 8 WRITE | MODE_ID = 9, c = 9 READ | Id: 1, string: MODE_ID = 9, c = 9 READ | Id: 2, string: MODE_ID = 9, c = 9 READ | Id: 3, string: MODE_ID = 9, c = 9 [00:00:02.058,685] <err> fs_nvs: NVS not initialized [00:00:02.061,950] <err> fs_nvs: NVS not initialized [00:00:02.064,422] <err> fs_nvs: NVS not initialized [00:00:02.064,422] <err> fs_nvs: NVS not initialized [00:00:02.068,115] <err> fs_nvs: NVS not initialized [00:00:02.071,807] <err> fs_nvs: NVS not initialized