I'm a first-time NVS user and have read the documentation about it and looked at the example code. I was able to write a few entries to NVS and the data can be read and it persists a power cycle. So I guess it works.
But at one point in the program, the device restarts when I want to write a new value to an existing entry. I can overwrite that entry from the start of the main function, but not when requested later at runtime.
The flow is this:
- A message is received on UART, it is put to a queue
- Another task reads from the queue and processes the message
- When the message requests setting that value, that's done
It has worked once to set the value the very first time, but when I try it now, the ESP32 restarts with the message "TG1WDT_SYS_RESET" or "Guru Meditation Error: Core 0 panic'ed (Cache disabled but cached memory region accessed)."
What's going on there? Why can't I write to NVS after more than a few seconds into runtime? Do I need something else that's not documented? I'm not setting any timeouts here or for the task that runs the queue. I've doubled that stack size to 8192 already but that wasn't the cause.
The actual set code is exactly from the example:
Code: Select all
static nvs_handle_t nvsHandle;
// Called once at startup
void initNvs()
{
// Initialize NVS
esp_err_t err = nvs_flash_init();
if (err == ESP_ERR_NVS_NO_FREE_PAGES || err == ESP_ERR_NVS_NEW_VERSION_FOUND)
{
// NVS partition was truncated and needs to be erased
// Retry nvs_flash_init
ESP_ERROR_CHECK(nvs_flash_erase());
err = nvs_flash_init();
}
ESP_ERROR_CHECK(err);
// Open NVS handle
err = nvs_open("storage", NVS_READWRITE, &nvsHandle);
if (err != ESP_OK)
{
ESP_LOGE(TAG, "Error opening NVS handle: %s", esp_err_to_name(err));
return;
}
}
// Called when processing the message to save the data
bool nvsSetUInt32(const char* key, uint32_t value)
{
esp_err_t err = nvs_set_u32(nvsHandle, key, value);
if (err != ESP_OK)
{
ESP_LOGE(TAG, "Error writing uint32 value for key \"%s\" to NVS: %s", key, esp_err_to_name(err));
return false;
}
err = nvs_commit(nvsHandle);
if (err != ESP_OK)
{
ESP_LOGE(TAG, "Error committing NVS changes: %s", esp_err_to_name(err));
return false;
}
return true;
}