diff --git a/examples/07_nvs_read_write/Makefile b/examples/07_nvs_read_write/Makefile new file mode 100644 index 0000000000..3d6adb4d02 --- /dev/null +++ b/examples/07_nvs_read_write/Makefile @@ -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-read-write + +include $(IDF_PATH)/make/project.mk + diff --git a/examples/07_nvs_read_write/README.md b/examples/07_nvs_read_write/README.md new file mode 100644 index 0000000000..bac8ee8d54 --- /dev/null +++ b/examples/07_nvs_read_write/README.md @@ -0,0 +1,7 @@ +# 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. diff --git a/examples/07_nvs_read_write/main/component.mk b/examples/07_nvs_read_write/main/component.mk new file mode 100644 index 0000000000..24356f23ed --- /dev/null +++ b/examples/07_nvs_read_write/main/component.mk @@ -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 diff --git a/examples/07_nvs_read_write/main/nvs_read_write.c b/examples/07_nvs_read_write/main/nvs_read_write.c new file mode 100644 index 0000000000..40d330f62f --- /dev/null +++ b/examples/07_nvs_read_write/main/nvs_read_write.c @@ -0,0 +1,62 @@ +/* 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 +#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(); +}