ESP32存储blog笔记

基于ESP-IDF4.1

  1 #include <stdio.h>
  2 #include "freertos/FreeRTOS.h"
  3 #include "freertos/task.h"
  4 #include "esp_system.h"
  5 #include "nvs_flash.h"
  6 #include "nvs.h"
  7 #include "driver/gpio.h"
  8 
  9 #define STORAGE_NAMESPACE "storage"
 10 
 11 
 12 //保存设备重启数
 13 esp_err_t save_restart_counter(void)
 14 {
 15     nvs_handle_t my_handle;
 16     esp_err_t err;
 17 
 18     // 打开
 19     err = nvs_open(STORAGE_NAMESPACE, NVS_READWRITE, &my_handle);
 20     if (err != ESP_OK) return err;
 21 
 22     // 读取
 23     int32_t restart_counter = 0; 
 24     err = nvs_get_i32(my_handle, "restart_conter", &restart_counter);
 25     if (err != ESP_OK && err != ESP_ERR_NVS_NOT_FOUND) return err;
 26 
 27     // 写入
 28     restart_counter++;
 29     err = nvs_set_i32(my_handle, "restart_conter", restart_counter);
 30     if (err != ESP_OK) return err;
 31 
 32     // 提交写入的值
 33     err = nvs_commit(my_handle);
 34     if (err != ESP_OK) return err;
 35 
 36     // 关闭
 37     nvs_close(my_handle);
 38     return ESP_OK;
 39 }
 40 
 41 /* Save new run time value in NVS
 42    by first reading a table of previously saved values
 43    and then adding the new value at the end of the table.
 44    Return an error if anything goes wrong
 45    during this process.
 46  */
 47 esp_err_t save_run_time(void)
 48 {
 49     nvs_handle_t my_handle;
 50     esp_err_t err;
 51 
 52     // 打开
 53     err = nvs_open(STORAGE_NAMESPACE, NVS_READWRITE, &my_handle);
 54     if (err != ESP_OK) return err;
 55 
 56     // 读取内存空间大小
 57     size_t required_size = 0;  // value will default to 0, if not set yet in NVS
 58     err = nvs_get_blob(my_handle, "run_time", NULL, &required_size);
 59     if (err != ESP_OK && err != ESP_ERR_NVS_NOT_FOUND) return err;
 60 
 61     // 读取之前保存的blob
 62     uint32_t* run_time = malloc(required_size + sizeof(uint32_t));
 63     if (required_size > 0) {
 64         err = nvs_get_blob(my_handle, "run_time", run_time, &required_size);
 65         if (err != ESP_OK) {
 66             free(run_time);
 67             return err;
 68         }
 69     }
 70 
 71     // 写入值,包含之前保存的blob
 72     required_size += sizeof(uint32_t);
 73     run_time[required_size / sizeof(uint32_t) - 1] = xTaskGetTickCount() * portTICK_PERIOD_MS;
 74     err = nvs_set_blob(my_handle, "run_time", run_time, required_size);
 75     free(run_time);
 76 
 77     if (err != ESP_OK) return err;
 78 
 79     // 提交
 80     err = nvs_commit(my_handle);
 81     if (err != ESP_OK) return err;
 82 
 83     // 关闭
 84     nvs_close(my_handle);
 85     return ESP_OK;
 86 }
 87 
 88 
 89 //从NVS读取和打印重启计数和运行时表
 90 esp_err_t print_what_saved(void)
 91 {
 92     nvs_handle_t my_handle;
 93     esp_err_t err;
 94 
 95     // 打开
 96     err = nvs_open(STORAGE_NAMESPACE, NVS_READWRITE, &my_handle);
 97     if (err != ESP_OK) return err;
 98 
 99     // 读取重启数
100     int32_t restart_counter = 0; // NVS没有设置值时需要设置默认值0
101     err = nvs_get_i32(my_handle, "restart_conter", &restart_counter);
102     if (err != ESP_OK && err != ESP_ERR_NVS_NOT_FOUND) return err;
103     printf("Restart counter = %d\n", restart_counter);
104 
105     // 读取运行时blob
106     size_t required_size = 0;  
107     //获取内存空间存储从NVS读取的blob
108     err = nvs_get_blob(my_handle, "run_time", NULL, &required_size);
109     if (err != ESP_OK && err != ESP_ERR_NVS_NOT_FOUND) return err;
110     printf("Run time:\n");
111     if (required_size == 0) {
112         printf("Nothing saved yet!\n");
113     } else {
114         //malloc和free是申请内存空间与释放内存空间的函数
115         uint32_t* run_time = malloc(required_size);
116         err = nvs_get_blob(my_handle, "run_time", run_time, &required_size);
117         if (err != ESP_OK) {
118             free(run_time);
119             return err;
120         }
121         for (int i = 0; i < required_size / sizeof(uint32_t); i++) {
122             printf("%d: %d\n", i + 1, run_time[i]);
123         }
124         free(run_time);
125     }
126 
127     // 关闭
128     nvs_close(my_handle);
129     return ESP_OK;
130 }
131 
132 
133 void app_main(void)
134 {
135     esp_err_t err = nvs_flash_init();
136     if (err == ESP_ERR_NVS_NO_FREE_PAGES || err == ESP_ERR_NVS_NEW_VERSION_FOUND) {
137         // NVS分区被截断,需要擦除并重新初始化
138         ESP_ERROR_CHECK(nvs_flash_erase());
139         err = nvs_flash_init();
140     }
141     ESP_ERROR_CHECK( err );
142 
143     err = print_what_saved();
144     if (err != ESP_OK) printf("Error (%s) reading data from NVS!\n", esp_err_to_name(err));
145 
146     err = save_restart_counter();
147     if (err != ESP_OK) printf("Error (%s) saving restart counter to NVS!\n", esp_err_to_name(err));
148 
149     gpio_pad_select_gpio(GPIO_NUM_0);
150     gpio_set_direction(GPIO_NUM_0, GPIO_MODE_DEF_INPUT);
151 
152     // 读取GPIO0状态,低电平超过一秒则保存运行时并重启
153     while (1) {
154         if (gpio_get_level(GPIO_NUM_0) == 0) {
155             vTaskDelay(1000 / portTICK_PERIOD_MS);
156             if(gpio_get_level(GPIO_NUM_0) == 0) {
157                 err = save_run_time();
158                 if (err != ESP_OK) printf("Error (%s) saving run time blob to NVS!\n", esp_err_to_name(err));
159                 printf("Restarting...\n");
160                 fflush(stdout);
161                 esp_restart();
162             }
163         }
164         vTaskDelay(200 / portTICK_PERIOD_MS);
165     }
166 }

 

原文:https://gitee.com/EspressifSystems/esp-idf

posted @ 2020-11-10 17:28  kerwin cui  阅读(681)  评论(0编辑  收藏  举报