kopia lustrzana https://github.com/espressif/esp-idf
NVS Examples update
Updated previusly submitted example, saved it as "07_nvs_rw_value" and added a new one. Overview of examples: * 07_nvs_rw_value - simple read and write a single value * 08_nvs_rw_blob - read and write a blob that is extened with new data on each restart of ESP32 Removed LICENSE filespull/79/head
rodzic
025bb47302
commit
f7cb6fc969
|
@ -1,7 +0,0 @@
|
||||||
# Non-Volatile Storage (NVS) Read and Write Example
|
|
||||||
|
|
||||||
Demonstrates how to read and write a value using NVS. The value tracks number of ESP32 module restarts.
|
|
||||||
|
|
||||||
Example also shows how to use basic diagnostics if read / write operation was successful.
|
|
||||||
|
|
||||||
See the README.md file in the upper level 'examples' directory for more information about examples.
|
|
|
@ -1,62 +0,0 @@
|
||||||
/* Non-Volatile Storage (NVS) Read and Write Example
|
|
||||||
|
|
||||||
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()
|
|
||||||
{
|
|
||||||
nvs_flash_init();
|
|
||||||
system_init();
|
|
||||||
|
|
||||||
nvs_handle handle_to_settings;
|
|
||||||
esp_err_t err;
|
|
||||||
int32_t restart_counter = 0;
|
|
||||||
|
|
||||||
// Open the NVS
|
|
||||||
printf("Opening Non-Volatile Storage (NVS) ... ");
|
|
||||||
err = nvs_open("settings", NVS_READWRITE, &handle_to_settings);
|
|
||||||
printf((err != ESP_OK) ? "Failed!\n" : "OK\n");
|
|
||||||
|
|
||||||
// Read from the NVS
|
|
||||||
printf("Reading restart counter from NVS ... ");
|
|
||||||
err = nvs_get_i32(handle_to_settings, "restart_conter", &restart_counter);
|
|
||||||
switch (err) {
|
|
||||||
case ESP_OK:
|
|
||||||
printf("OK\n");
|
|
||||||
printf("Restart counter = %d\n", restart_counter);
|
|
||||||
break;
|
|
||||||
case ESP_ERR_NVS_NOT_FOUND:
|
|
||||||
printf("The counter is not initialized yet!\n");
|
|
||||||
break;
|
|
||||||
default :
|
|
||||||
printf("Error (%d) reading!\n", err);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Write to the NVS
|
|
||||||
printf("Updating restart counter in NVS ... ");
|
|
||||||
restart_counter++;
|
|
||||||
err = nvs_set_i32(handle_to_settings, "restart_conter", restart_counter);
|
|
||||||
printf((err != ESP_OK) ? "Failed!\n" : "OK\n");
|
|
||||||
|
|
||||||
// Close the NVS
|
|
||||||
nvs_close(handle_to_settings);
|
|
||||||
|
|
||||||
// Restart module
|
|
||||||
for (int i = 10; i >= 0; i--) {
|
|
||||||
printf("Restarting in %d seconds...\n", i);
|
|
||||||
vTaskDelay(1000 / portTICK_RATE_MS);
|
|
||||||
}
|
|
||||||
printf("Restarting now.\n");
|
|
||||||
fflush(stdout);
|
|
||||||
system_restart();
|
|
||||||
}
|
|
|
@ -3,7 +3,7 @@
|
||||||
# project subdirectory.
|
# project subdirectory.
|
||||||
#
|
#
|
||||||
|
|
||||||
PROJECT_NAME := nvs-read-write
|
PROJECT_NAME := nvs-rw-value
|
||||||
|
|
||||||
include $(IDF_PATH)/make/project.mk
|
include $(IDF_PATH)/make/project.mk
|
||||||
|
|
|
@ -0,0 +1,13 @@
|
||||||
|
# Non-Volatile Storage (NVS) Read and Write Example
|
||||||
|
|
||||||
|
Demonstrates how to read and write a single integer value using NVS.
|
||||||
|
|
||||||
|
The value holds the number of ESP32 module restarts. Since it is written to NVS, the value is preserved between restarts.
|
||||||
|
|
||||||
|
Example also shows how to check if read / write operation was successful, or certain value is not initialized in NVR. Diagnostic is provided in plain text to help track program flow and capture any issues on the way.
|
||||||
|
|
||||||
|
Check another example *08_nvs_rw_blob*, that shows how to read and write a blob (binary large object).
|
||||||
|
|
||||||
|
Detailed functional description of NVS and API is provided in [documentation](http://esp-idf.readthedocs.io/en/latest/api/nvs_flash.html).
|
||||||
|
|
||||||
|
See the README.md file in the upper level 'examples' directory for more information about examples.
|
|
@ -0,0 +1,77 @@
|
||||||
|
/* 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()
|
||||||
|
{
|
||||||
|
nvs_flash_init();
|
||||||
|
|
||||||
|
nvs_handle my_handle;
|
||||||
|
esp_err_t err;
|
||||||
|
|
||||||
|
printf("\n");
|
||||||
|
|
||||||
|
// Open
|
||||||
|
printf("Opening Non-Volatile Storage (NVS) ... ");
|
||||||
|
err = nvs_open("storage", NVS_READWRITE, &my_handle);
|
||||||
|
if (err != ESP_OK) {
|
||||||
|
printf("Error (%d) opening NVS!\n", err);
|
||||||
|
} 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
|
||||||
|
err = nvs_get_i32(my_handle, "restart_conter", &restart_counter);
|
||||||
|
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 :
|
||||||
|
printf("Error (%d) reading!\n", err);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Write
|
||||||
|
printf("Updating restart counter in NVS ... ");
|
||||||
|
restart_counter++;
|
||||||
|
err = nvs_set_i32(my_handle, "restart_conter", restart_counter);
|
||||||
|
printf((err != ESP_OK) ? "Failed!\n" : "Done\n");
|
||||||
|
|
||||||
|
// Commit
|
||||||
|
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);
|
||||||
|
vTaskDelay(1000 / portTICK_RATE_MS);
|
||||||
|
}
|
||||||
|
printf("Restarting now.\n");
|
||||||
|
fflush(stdout);
|
||||||
|
system_restart();
|
||||||
|
}
|
|
@ -0,0 +1,9 @@
|
||||||
|
#
|
||||||
|
# This is a project Makefile. It is assumed the directory this Makefile resides in is a
|
||||||
|
# project subdirectory.
|
||||||
|
#
|
||||||
|
|
||||||
|
PROJECT_NAME := nvs-rw-blob
|
||||||
|
|
||||||
|
include $(IDF_PATH)/make/project.mk
|
||||||
|
|
|
@ -0,0 +1,14 @@
|
||||||
|
# Non-Volatile Storage (NVS) Read and Write Example
|
||||||
|
|
||||||
|
Demonstrates how to read and write a single integer value and a blob (binary large object) using NVS to preserve them between ESP32 module restarts.
|
||||||
|
|
||||||
|
* value - tracks number of ESP32 module soft and hard restarts.
|
||||||
|
* blob - contains a table with module run times. The table is read from NVS to dynamically allocated RAM. New run time is added to the table on each manually triggered soft restart and written back to NVS. Triggering is done by pulling down GPIO0.
|
||||||
|
|
||||||
|
Example also shows how to implement diagnostics if read / write operation was successful.
|
||||||
|
|
||||||
|
If not done already, consider checking simpler example *07_nvs_rw_value*, that has been used as a starting point for preparing this one.
|
||||||
|
|
||||||
|
Detailed functional description of NVS and API is provided in [documentation](http://esp-idf.readthedocs.io/en/latest/api/nvs_flash.html).
|
||||||
|
|
||||||
|
See the README.md file in the upper level 'examples' directory for more information about examples.
|
|
@ -0,0 +1,10 @@
|
||||||
|
#
|
||||||
|
# Main Makefile. This is basically the same as a component makefile.
|
||||||
|
#
|
||||||
|
# This Makefile should, at the very least, just include $(SDK_PATH)/make/component_common.mk. By default,
|
||||||
|
# this will take the sources in the src/ directory, compile them and link them into
|
||||||
|
# lib(subdirectory_name).a in the build directory. This behaviour is entirely configurable,
|
||||||
|
# please read the ESP-IDF documents if you need to do this.
|
||||||
|
#
|
||||||
|
|
||||||
|
include $(IDF_PATH)/make/component_common.mk
|
|
@ -0,0 +1,183 @@
|
||||||
|
/* Non-Volatile Storage (NVS) Read and Write a Blob - 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"
|
||||||
|
#include "driver/gpio.h"
|
||||||
|
|
||||||
|
#define STORAGE_NAMESPACE "storage"
|
||||||
|
|
||||||
|
/* Save the number of module restarts in NVS
|
||||||
|
by first reading and then incrementing
|
||||||
|
the number that has been saved previously.
|
||||||
|
Return an error if anything goes wrong
|
||||||
|
during this process.
|
||||||
|
*/
|
||||||
|
esp_err_t save_restart_counter(void)
|
||||||
|
{
|
||||||
|
nvs_handle my_handle;
|
||||||
|
esp_err_t err;
|
||||||
|
|
||||||
|
// Open
|
||||||
|
err = nvs_open(STORAGE_NAMESPACE, NVS_READWRITE, &my_handle);
|
||||||
|
if (err != ESP_OK) return err;
|
||||||
|
|
||||||
|
// Read
|
||||||
|
int32_t restart_counter = 0; // value will default to 0, if not set yet in NVS
|
||||||
|
err = nvs_get_i32(my_handle, "restart_conter", &restart_counter);
|
||||||
|
if (err != ESP_OK && err != ESP_ERR_NVS_NOT_FOUND) return err;
|
||||||
|
|
||||||
|
// Write
|
||||||
|
restart_counter++;
|
||||||
|
err = nvs_set_i32(my_handle, "restart_conter", restart_counter);
|
||||||
|
if (err != ESP_OK) return err;
|
||||||
|
|
||||||
|
// Commit
|
||||||
|
err = nvs_commit(my_handle);
|
||||||
|
if (err != ESP_OK) return err;
|
||||||
|
|
||||||
|
// Close
|
||||||
|
nvs_close(my_handle);
|
||||||
|
return ESP_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Save new run time value in NVS
|
||||||
|
by first reading a table of previously saved values
|
||||||
|
and then adding the new value at the end of the table.
|
||||||
|
Return an error if anything goes wrong
|
||||||
|
during this process.
|
||||||
|
*/
|
||||||
|
esp_err_t save_run_time(void)
|
||||||
|
{
|
||||||
|
nvs_handle my_handle;
|
||||||
|
esp_err_t err;
|
||||||
|
|
||||||
|
// Open
|
||||||
|
err = nvs_open(STORAGE_NAMESPACE, NVS_READWRITE, &my_handle);
|
||||||
|
if (err != ESP_OK) return err;
|
||||||
|
|
||||||
|
// Read the size of memory space required for blob
|
||||||
|
size_t required_size = 0; // value will default to 0, if not set yet in NVS
|
||||||
|
err = nvs_get_blob(my_handle, "run_time", NULL, &required_size);
|
||||||
|
if (err != ESP_OK && err != ESP_ERR_NVS_NOT_FOUND) return err;
|
||||||
|
|
||||||
|
// Read previously saved blob if available
|
||||||
|
uint32_t* run_time;
|
||||||
|
if (required_size > 0) {
|
||||||
|
run_time = malloc(required_size);
|
||||||
|
// read previously saved blob
|
||||||
|
err = nvs_get_blob(my_handle, "run_time", run_time, &required_size);
|
||||||
|
if (err != ESP_OK) return err;
|
||||||
|
// add extra space for the new value
|
||||||
|
required_size += sizeof(uint32_t);
|
||||||
|
run_time = realloc(run_time, required_size);
|
||||||
|
} else {
|
||||||
|
// nothing saved jet - just allocate space for the first value to save
|
||||||
|
required_size = sizeof(uint32_t);
|
||||||
|
run_time = malloc(required_size);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Write value including previously saved blob if available
|
||||||
|
run_time[required_size / sizeof(uint32_t) - 1] = xTaskGetTickCount() * portTICK_PERIOD_MS;
|
||||||
|
err = nvs_set_blob(my_handle, "run_time", run_time, required_size);
|
||||||
|
if (err != ESP_OK) return err;
|
||||||
|
|
||||||
|
free(run_time);
|
||||||
|
|
||||||
|
// Commit
|
||||||
|
err = nvs_commit(my_handle);
|
||||||
|
if (err != ESP_OK) return err;
|
||||||
|
|
||||||
|
// Close
|
||||||
|
nvs_close(my_handle);
|
||||||
|
return ESP_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Read from NVS and print restart counter
|
||||||
|
and the table with run times.
|
||||||
|
Return an error if anything goes wrong
|
||||||
|
during this process.
|
||||||
|
*/
|
||||||
|
esp_err_t print_what_saved(void)
|
||||||
|
{
|
||||||
|
nvs_handle my_handle;
|
||||||
|
esp_err_t err;
|
||||||
|
|
||||||
|
// Open
|
||||||
|
err = nvs_open(STORAGE_NAMESPACE, NVS_READWRITE, &my_handle);
|
||||||
|
if (err != ESP_OK) return err;
|
||||||
|
|
||||||
|
// Read restart counter
|
||||||
|
int32_t restart_counter = 0; // value will default to 0, if not set yet in NVS
|
||||||
|
err = nvs_get_i32(my_handle, "restart_conter", &restart_counter);
|
||||||
|
if (err != ESP_OK && err != ESP_ERR_NVS_NOT_FOUND) return err;
|
||||||
|
printf("Restart counter = %d\n", restart_counter);
|
||||||
|
|
||||||
|
// Read run time blob
|
||||||
|
size_t required_size = 0; // value will default to 0, if not set yet in NVS
|
||||||
|
// obtain required memory space to store blob being read from NVS
|
||||||
|
err = nvs_get_blob(my_handle, "run_time", NULL, &required_size);
|
||||||
|
if (err != ESP_OK && err != ESP_ERR_NVS_NOT_FOUND) return err;
|
||||||
|
printf("Run time:\n");
|
||||||
|
if (required_size == 0) {
|
||||||
|
printf("Nothing saved yet!\n");
|
||||||
|
} else {
|
||||||
|
uint32_t* run_time = malloc(required_size);
|
||||||
|
err = nvs_get_blob(my_handle, "run_time", run_time, &required_size);
|
||||||
|
if (err != ESP_OK) return err;
|
||||||
|
for (int i = 0; i < required_size / sizeof(uint32_t); i++) {
|
||||||
|
printf("%d: %d\n", i + 1, run_time[i]);
|
||||||
|
}
|
||||||
|
free(run_time);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Close
|
||||||
|
nvs_close(my_handle);
|
||||||
|
return ESP_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void app_main()
|
||||||
|
{
|
||||||
|
nvs_flash_init();
|
||||||
|
|
||||||
|
esp_err_t err;
|
||||||
|
|
||||||
|
err = print_what_saved();
|
||||||
|
if (err != ESP_OK) printf("Error (%d) reading data from NVS!\n", err);
|
||||||
|
|
||||||
|
err = save_restart_counter();
|
||||||
|
if (err != ESP_OK) printf("Error (%d) saving restart counter to NVS!\n", err);
|
||||||
|
|
||||||
|
gpio_pad_select_gpio(GPIO_NUM_0);
|
||||||
|
gpio_set_direction(GPIO_NUM_0, GPIO_MODE_DEF_INPUT);
|
||||||
|
|
||||||
|
/* Read the status of GPIO0. If GPIO0 is LOW for longer than 1000 ms,
|
||||||
|
then save module's run time and restart it
|
||||||
|
*/
|
||||||
|
while (1) {
|
||||||
|
if (gpio_get_level(GPIO_NUM_0) == 0) {
|
||||||
|
vTaskDelay(1000 / portTICK_RATE_MS);
|
||||||
|
if(gpio_get_level(GPIO_NUM_0) == 0) {
|
||||||
|
err = save_run_time();
|
||||||
|
if (err != ESP_OK) printf("Error (%d) saving run time blob to NVS!\n", err);
|
||||||
|
printf("Restarting...\n");
|
||||||
|
fflush(stdout);
|
||||||
|
system_restart();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
vTaskDelay(200 / portTICK_RATE_MS);
|
||||||
|
}
|
||||||
|
}
|
Ładowanie…
Reference in New Issue