NVS partition usage

panitaxx
Posts: 1
Joined: Tue Jul 17, 2018 2:52 pm

NVS partition usage

Postby panitaxx » Tue Jul 17, 2018 3:10 pm

Hello,

I have a question. I am using the default partitioning scheme on a ESP32-WROOM:

Code: Select all

nvs,      data, nvs,     0x9000,  0x6000,
phy_init, data, phy,     0xf000,  0x1000,
factory,  app,  factory, 0x10000, 1M,
I made a code that is something like this:

Code: Select all

#include "structs.h"
#include "nvs_flash.h"
#include "nvs.h"
#include "nvs_values.h"
#include <string.h>
#include "esp_log.h"
#include <stdlib.h>

#define TAG "NVS"
#define PARTNAME "nvs"



static esp_err_t read_string(nvs_handle *handle, const char* key,char** str){
  esp_err_t err;
  size_t szlen = 0 ;
  err = nvs_get_str(*handle,key,NULL,&szlen);
  if (err != ESP_OK) return err;
  ESP_LOGD(TAG,"SZ %d",szlen);
  *str = (char*)calloc(szlen + 1,1);
  nvs_get_str(*handle,key,*str,&szlen);
  ESP_LOGD(TAG,"%s value read => %s",key,*str);
  return err; 
}


void nvs_read_values() {
  nvs_handle handle;
  esp_err_t err = nvs_flash_init();
  if (err == ESP_ERR_NVS_NO_FREE_PAGES) {
     ESP_LOGW(TAG,"NO FREE PAGES");
      CHECK_ERROR_FATAL(nvs_flash_erase());
      err = nvs_flash_init();
  }
  CHECK_ERROR_FATAL( err );
  ESP_LOGD(TAG,"opening "PARTNAME);
  CHECK_ERROR_FATAL(nvs_open(PARTNAME, NVS_READWRITE, &handle));
  ESP_LOGD(TAG,"opened");
  CHECK_ERROR_FATAL(read_string(&handle,"ssid", &wifi_ssid));
  CHECK_ERROR_FATAL(read_string(&handle,"pass", &wifi_pwd));
  CHECK_ERROR_FATAL(read_string(&handle,"devid", &devid));
  CHECK_ERROR_FATAL(read_string(&handle,"rsa_key", &rsakey));
  rsa_sz = strlen(rsakey);
  nvs_close(handle);
  }
  
I then did a nvs partition like this:

Code: Select all

key,type,encoding,value
nvs,namespace,,
ssid,data,string,myssid
pass,data,string,mypass
devid,data,string,TEST2001
rsa_key,file,string,/Users/redacted/rsa_private.pem
Then generate the partition, flash the software and then flash the nvs partiton

Code: Select all

python ~/esp/esp-idf/components/nvs_flash/nvs_partition_generator/nvs_partition_gen.py  partition.csv partition.bin
make flash 
python ~/esp/esp-idf/components/esptool_py/esptool/esptool.py  --port /dev/cu.SLAB_USBtoUART --chip esp32 write_flash 0x9000 partition.bin

But I cannot seem to make it read the keys

Code: Select all

D (355) nvs: nvs_flash_init_custom partition=nvs start=9 count=6
D (375) NVS: opening nvs
D (375) nvs: nvs_open_from_partition nvs 1
D (375) NVS: opened
D (375) nvs: nvs_get_str_or_blob ssid
E (375) FATAL: Fatal Error ESP_ERR_NVS_NOT_FOUND
any help is appreciated

Who is online

Users browsing this forum: Bing [Bot], Bryght-Richard and 114 guests