NVS non deterministic behavior

mairswartz
Posts: 21
Joined: Tue Nov 07, 2017 9:59 pm

NVS non deterministic behavior

Postby mairswartz » Mon Apr 15, 2019 10:53 am

Hi,
I'm getting some strange behavior with NVS.
Here is the source code. Note the commented line of code where I have a delay
vTaskDelay(1000 / portTICK_PERIOD_MS);
If that line is commented the output is NVS: Value not set yet
However un-commented the output is NVS: Value is 1

To flash the chip I am using
make -j8 erase_flash flash monitor
Which should erase the flash each time.

Can anyone explain to me why the code behaves differently with a delay. It seems like its almost running asynchronously without blocking. Is this perhaps a bug?

Code: Select all

#include <stdio.h>
#include <string.h>
#include "esp_log.h"
#include "nvs.h"
#include "nvs_flash.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_system.h"

#define TAG "NVS"

void app_main()
{
  ESP_LOGW(TAG, "Started!!!");
  nvs_flash_init();
  nvs_handle handle;
  nvs_open("store", NVS_READWRITE, &handle);
  
  int32_t val = 0;
  esp_err_t err = nvs_get_i32(handle,"val1", &val);
  
  // vTaskDelay(1000 / portTICK_PERIOD_MS);
  
  switch (err)
  {
    case ESP_ERR_NVS_NOT_FOUND:
      ESP_LOGE(TAG, "Value not set yet");
      break;
    case ESP_OK:
      ESP_LOGI(TAG,"Value is %d", val);
      break;
    default:
      ESP_LOGE(TAG, "Error (%s) opening NVS handle!\n", esp_err_to_name(err));
      break;
  }
  printf("completed reading\n");
  val++; 

  nvs_set_i32(handle,"val1",val);
  nvs_commit(handle);
  nvs_close(handle);
}
check out my course https://learnesp32.com

Who is online

Users browsing this forum: No registered users and 130 guests