2016-11-09 19:26:28 +00:00
|
|
|
/* Non-Volatile Storage (NVS) Read and Write a Value - Example
|
|
|
|
|
|
|
|
For other examples please check:
|
|
|
|
https://github.com/espressif/esp-idf/tree/master/examples
|
|
|
|
|
|
|
|
This example code is in the Public Domain (or CC0 licensed, at your option.)
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, this
|
|
|
|
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
|
|
|
CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
*/
|
|
|
|
#include <stdio.h>
|
|
|
|
#include "freertos/FreeRTOS.h"
|
|
|
|
#include "freertos/task.h"
|
|
|
|
#include "esp_system.h"
|
|
|
|
#include "nvs_flash.h"
|
|
|
|
#include "nvs.h"
|
|
|
|
|
|
|
|
void app_main()
|
|
|
|
{
|
2017-03-14 13:39:44 +00:00
|
|
|
// Initialize NVS
|
|
|
|
esp_err_t err = nvs_flash_init();
|
2018-07-25 15:11:09 +00:00
|
|
|
if (err == ESP_ERR_NVS_NO_FREE_PAGES || err == ESP_ERR_NVS_NEW_VERSION_FOUND) {
|
2017-03-14 13:39:44 +00:00
|
|
|
// NVS partition was truncated and needs to be erased
|
|
|
|
// Retry nvs_flash_init
|
2017-07-13 15:46:19 +00:00
|
|
|
ESP_ERROR_CHECK(nvs_flash_erase());
|
2017-03-14 13:39:44 +00:00
|
|
|
err = nvs_flash_init();
|
|
|
|
}
|
|
|
|
ESP_ERROR_CHECK( err );
|
2016-11-09 19:26:28 +00:00
|
|
|
|
|
|
|
// Open
|
2017-03-14 13:39:44 +00:00
|
|
|
printf("\n");
|
|
|
|
printf("Opening Non-Volatile Storage (NVS) handle... ");
|
|
|
|
nvs_handle my_handle;
|
2016-11-09 19:26:28 +00:00
|
|
|
err = nvs_open("storage", NVS_READWRITE, &my_handle);
|
|
|
|
if (err != ESP_OK) {
|
2018-02-22 13:48:53 +00:00
|
|
|
printf("Error (%s) opening NVS handle!\n", esp_err_to_name(err));
|
2016-11-09 19:26:28 +00:00
|
|
|
} else {
|
|
|
|
printf("Done\n");
|
|
|
|
|
|
|
|
// Read
|
|
|
|
printf("Reading restart counter from NVS ... ");
|
|
|
|
int32_t restart_counter = 0; // value will default to 0, if not set yet in NVS
|
2017-11-29 08:26:44 +00:00
|
|
|
err = nvs_get_i32(my_handle, "restart_counter", &restart_counter);
|
2016-11-09 19:26:28 +00:00
|
|
|
switch (err) {
|
|
|
|
case ESP_OK:
|
|
|
|
printf("Done\n");
|
|
|
|
printf("Restart counter = %d\n", restart_counter);
|
|
|
|
break;
|
|
|
|
case ESP_ERR_NVS_NOT_FOUND:
|
|
|
|
printf("The value is not initialized yet!\n");
|
|
|
|
break;
|
|
|
|
default :
|
2018-02-22 13:48:53 +00:00
|
|
|
printf("Error (%s) reading!\n", esp_err_to_name(err));
|
2016-11-09 19:26:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Write
|
|
|
|
printf("Updating restart counter in NVS ... ");
|
|
|
|
restart_counter++;
|
2017-11-29 08:26:44 +00:00
|
|
|
err = nvs_set_i32(my_handle, "restart_counter", restart_counter);
|
2016-11-09 19:26:28 +00:00
|
|
|
printf((err != ESP_OK) ? "Failed!\n" : "Done\n");
|
|
|
|
|
2016-11-11 04:16:54 +00:00
|
|
|
// Commit written value.
|
|
|
|
// After setting any values, nvs_commit() must be called to ensure changes are written
|
|
|
|
// to flash storage. Implementations may write to storage at other times,
|
|
|
|
// but this is not guaranteed.
|
2016-11-09 19:26:28 +00:00
|
|
|
printf("Committing updates in NVS ... ");
|
|
|
|
err = nvs_commit(my_handle);
|
|
|
|
printf((err != ESP_OK) ? "Failed!\n" : "Done\n");
|
|
|
|
|
|
|
|
// Close
|
|
|
|
nvs_close(my_handle);
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("\n");
|
|
|
|
|
|
|
|
// Restart module
|
|
|
|
for (int i = 10; i >= 0; i--) {
|
|
|
|
printf("Restarting in %d seconds...\n", i);
|
2016-12-22 01:42:21 +00:00
|
|
|
vTaskDelay(1000 / portTICK_PERIOD_MS);
|
2016-11-09 19:26:28 +00:00
|
|
|
}
|
|
|
|
printf("Restarting now.\n");
|
|
|
|
fflush(stdout);
|
2016-11-21 15:05:23 +00:00
|
|
|
esp_restart();
|
2016-11-09 19:26:28 +00:00
|
|
|
}
|