/*
* SPDX-FileCopyrightText: 2010-2022 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: CC0-1.0
*/
#include <stdio.h>
#include "sdkconfig.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_chip_info.h"
#include "esp_flash.h"
#include "nvs_flash.h"
#include "esp_log.h"
/** 线程栈有限,咱不给他写在线程里,可以用申请变量方式,为了方便全局变量 */
char getstr[500] = {0};
void app_main(void)
{
esp_err_t ret;
ret = nvs_flash_init();
/** ESP_ERR_NVS_NO_FREE_PAGES保证flash写入页是空的,以及nvs是否是新版本 */
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND)
{
ESP_ERROR_CHECK(nvs_flash_erase()); // 擦nvs
ret = nvs_flash_init(); // 擦完再初始化
}
ESP_ERROR_CHECK(ret);
/** 写 */
nvs_handle_t nvs_handle;
nvs_open("list", NVS_READWRITE, &nvs_handle);
nvs_set_i8(nvs_handle, "test", 32);
if (nvs_commit(nvs_handle) != ESP_OK) // nvs提交
{
ESP_LOGI("NVS","写入失败");
}
/** 读 */
int8_t o_val;
nvs_get_i8(nvs_handle,"test",&o_val);
ESP_LOGI("NVS","%d",o_val);
/** 写一串 */
char test_str[] = "鸡你太美";
nvs_set_str(nvs_handle,"test_str",test_str);
if (nvs_commit(nvs_handle) != ESP_OK) // nvs提交
{
ESP_LOGI("NVS","写入失败");
}
/** 读一串 */
uint8_t len = 500;//这个500要和上面的500相同
nvs_get_str(nvs_handle,"test_str",getstr,&len);
ESP_LOGI("NVS","%u %s",len,getstr);
/** 另外还可以存链表,结构体等等,我想,类似于线程传参 */
/** nvs_get_blob(nvs_handle_t c_handle, const char* key, void* out_value, size_t* length) */
}