ESP32上对flash的读取操作

通过按键控制一个LED灯的亮和灭,每次重启时读上一次的状态

#include 
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_system.h"
#include "nvs_flash.h"
#include "nvs.h"

#include "driver/gpio.h"
#include "sdkconfig.h"

#define GPIO_LED_1 0
#define KEY_CONTROLL_1 18

void app_main()
{
    int32_t restart_counter = 0;
    // Initialize NVS
    esp_err_t err = nvs_flash_init();//int32类型unsignd int
    if (err == ESP_ERR_NVS_NO_FREE_PAGES || err == ESP_ERR_NVS_NEW_VERSION_FOUND) {// (ESP_ERR_NVS_BASE + 0x0d)一个地址
        // NVS partition was truncated and needs to be erased      NVS分区被截断并且需要被擦除
        // Retry nvs_flash_init重试
        ESP_ERROR_CHECK(nvs_flash_erase());//可用于检查错误代码的宏
        err = nvs_flash_init();
    }
    ESP_ERROR_CHECK( err );

    // Open
    printf("\n");
    printf("Opening Non-Volatile Storage (NVS) handle... ");
    nvs_handle_t my_handle;//unsigned int
    err = nvs_open("storage", NVS_READWRITE, &my_handle);//使用来自默认 NVS 分区的给定命名空间打开非易失性存储
    if (err != ESP_OK) {
        printf("Error (%s) opening NVS handle!\n", esp_err_to_name(err));//为 esp_err_t 错误代码返回字符串 此函数在预先生成的查找表中查找错误代码并返回其字符串表示。
    } 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_counter", &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 (%s) reading!\n", esp_err_to_name(err));
        }

        /*// Write
        printf("Updating restart counter in NVS ... ");
       restart_counter++;
        err = nvs_set_i32(my_handle, "restart_counter", restart_counter);//为给定键设置值这一系列函数为键设置值,给定它的名字。注意在调用 nvs_commit 函数之前,不会更新实际存储。
        printf((err != ESP_OK) ? "Failed!\n" : "Done\n");

        // Commit written value.提交写入的值
        // After setting any values, nvs_commit() must be called to ensure changes are written设置任何值后,必须调用 nvs_commit() 以确保写入更改
        // to flash storage. Implementations may write to storage at other times,到闪存。 实现可能会在其他时间写入存储
        // but this is not guaranteed.
        printf("Committing updates in NVS ... ");
        err = nvs_commit(my_handle);//将任何挂起的更改写入非易失性存储设置任何值后,必须调用 nvs_commit() 以确保写入更改
        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_PERIOD_MS);
    }
    printf("Restarting now.\n");
    //fflush(stdout);
    //esp_restart();

     gpio_pad_select_gpio(GPIO_LED_1);
    /* Set the GPIO as a push/pull output */
    gpio_set_direction(GPIO_LED_1, GPIO_MODE_OUTPUT);
    gpio_pad_select_gpio(KEY_CONTROLL_1);
    gpio_set_direction(KEY_CONTROLL_1,GPIO_MODE_INPUT);
        while(1)
        {
            //gpio_set_level(GPIO_LED_1,restart_counter%2);
            //vTaskDelay(10);
            //printf("key level [%d]\n",gpio_get_level(KEY_CONTROLL_1));
            if(0==gpio_get_level(KEY_CONTROLL_1))
            {
                vTaskDelay(10);
                if(0==gpio_get_level(KEY_CONTROLL_1))
                {
                    restart_counter++;
                    if(restart_counter>=100)
                        restart_counter=0;
                    printf("Updating restart counter in NVS ... ");
                    err = nvs_set_i32(my_handle, "restart_counter", restart_counter);//为给定键设置值这一系列函数为键设置值,给定它的名字。注意在调用 nvs_commit 函数之前,不会更新实际存储。
                    printf((err != ESP_OK) ? "Failed!\n" : "Done\n");
                    printf("Committing updates in NVS ... ");
                    err = nvs_commit(my_handle);//将任何挂起的更改写入非易失性存储设置任何值后,必须调用 nvs_commit() 以确保写入更改
                    printf((err != ESP_OK) ? "Failed!\n" : "Done\n");
                }
                while(!gpio_get_level(KEY_CONTROLL_1))
                    vTaskDelay(10);
            }
                gpio_set_level(GPIO_LED_1,restart_counter%2);
                vTaskDelay(100);
        }
}

你可能感兴趣的:(ESP32,嵌入式)