在 ESP32 IDF 中,NVS(Non-Volatile Storage)是一种非易失性存储器,用于存储应用程序和设备配置信息,例如 Wi-Fi SSID 和密码、设备 ID、运行时参数等。NVS 使用 ESP32 内部闪存来存储信息,并提供简单的 API 函数访问操作。
使用 NVS 的主要步骤如下:
1. 应用程序初始化 NVS
使用 nvs_flash_init() 函数初始化 NVS。
2. 打开 NVS 分区
使用 nvs_open() 函数打开需要访问的 NVS 分区。
3. 读取 NVS 值
使用 nvs_get_xxx() 系列函数读取 NVS 中的值。
4. 写入 NVS 值
使用 nvs_set_xxx() 系列函数将值写入 NVS。
5. 将更改保存到 NVS
使用 nvs_commit() 函数保存更新的值。
下面是一些常用的 ESP32 NVS API 函数:
1. nvs_flash_init()
用于初始化 NVS。函数原型如下:
esp_err_t nvs_flash_init(void);
2. nvs_open()
用于打开指定名称的 NVS 分区。函数原型如下:
esp_err_t nvs_open(const char *name, nvs_open_mode mode, nvs_handle_t *out_handle);
可以指定的模式包括:NVS_READONLY(只读)、NVS_READWRITE(读写)、NVS_AUTOREPAIR(自动修复)。
3. nvs_get_i8(), nvs_get_u8(), nvs_get_i16(), nvs_get_u16(), nvs_get_i32(), nvs_get_u32(), nvs_get_i64(), nvs_get_u64(), nvs_get_blob()
用于从 NVS 中获取相应类型的值。函数原型分别为:
esp_err_t nvs_get_i8(nvs_handle_t handle, const char *key, int8_t *out_value);
esp_err_t nvs_get_u8(nvs_handle_t handle, const char *key, uint8_t *out_value);
esp_err_t nvs_get_i16(nvs_handle_t handle, const char *key, int16_t *out_value);
esp_err_t nvs_get_u16(nvs_handle_t handle, const char *key, uint16_t *out_value);
esp_err_t nvs_get_i32(nvs_handle_t handle, const char *key, int32_t *out_value);
esp_err_t nvs_get_u32(nvs_handle_t handle, const char *key, uint32_t *out_value);
esp_err_t nvs_get_i64(nvs_handle_t handle, const char *key, int64_t *out_value);
esp_err_t nvs_get_u64(nvs_handle_t handle, const char *key, uint64_t *out_value);
esp_err_t nvs_get_blob(nvs_handle_t handle, const char *key, void *out_value, size_t *length);
4. nvs_set_i8(), nvs_set_u8(), nvs_set_i16(), nvs_set_u16(), nvs_set_i32(), nvs_set_u32(), nvs_set_i64(), nvs_set_u64(), nvs_set_blob()
用于向 NVS 中写入相应类型的值。函数原型分别为:
esp_err_t nvs_set_i8(nvs_handle_t handle, const char *key, int8_t value);
esp_err_t nvs_set_u8(nvs_handle_t handle, const char *key, uint8_t value);
esp_err_t nvs_set_i16(nvs_handle_t handle, const char *key, int16_t value);
esp_err_t nvs_set_u16(nvs_handle_t handle, const char *key, uint16_t value);
esp_err_t nvs_set_i32(nvs_handle_t handle, const char *key, int32_t value);
esp_err_t nvs_set_u32(nvs_handle_t handle, const char *key, uint32_t value);
esp_err_t nvs_set_i64(nvs_handle_t handle, const char *key, int64_t value);
esp_err_t nvs_set_u64(nvs_handle_t handle, const char *key, uint64_t value);
esp_err_t nvs_set_blob(nvs_handle_t handle, const char *key, const void *value, size_t length);
通过使用这些 API 函数,我们可以很方便地存储和读取应用程序和设备配置信息。下面是一个简单的 ESP32 NVS 示例程序,演示如何使用 NVS 存储和读取一个字符串变量。
#include "nvs_flash.h"
#include "nvs.h"
#include "esp_log.h"
static const char *TAG = "nvs_example";
void app_main()
{
esp_err_t ret;
nvs_handle_t my_nvs_handle;
char my_string[32] = "Hello NVS!";
// 初始化 NVS
ret = nvs_flash_init();
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
ESP_ERROR_CHECK(nvs_flash_erase());
ret = nvs_flash_init();
}
ESP_ERROR_CHECK(ret);
// 打开 'my_nvs' 分区
ret = nvs_open("my_nvs", NVS_READWRITE, &my_nvs_handle);
if (ret != ESP_OK) {
ESP_LOGE(TAG, "Error (%d) opening NVS handle!", ret);
} else {
// 读取并输出字符串
size_t string_size = sizeof(my_string);
ret = nvs_get_str(my_nvs_handle, "my_string", my_string, &string_size);
switch (ret) {
case ESP_OK:
ESP_LOGI(TAG, "Read string from NVS: %s", my_string);
break;
case ESP_ERR_NVS_NOT_FOUND:
ESP_LOGW(TAG, "Value not found in NVS");
break;
default:
ESP_LOGE(TAG, "Error (%d) reading from NVS", ret);
}
// 写入新的字符串
strncpy(my_string, "Welcome to NVS!", sizeof(my_string));
ret = nvs_set_str(my_nvs_handle, "my_string", my_string);
if (ret != ESP_OK) {
ESP_LOGE(TAG, "Error (%d) writing to NVS", ret);
} else {
// 将更改保存到 NVS
ret = nvs_commit(my_nvs_handle);
if (ret != ESP_OK) {
ESP_LOGE(TAG, "Error (%d) committing NVS", ret);
}
}
// 关闭 NVS 句柄
nvs_close(my_nvs_handle);
}
}
在代码中,我们首先使用 nvs_flash_init() 函数初始化 NVS。如果已使用过 NVS,可能需要使用 nvs_flash_erase() 函数擦除闪存。在初始化后,我们使用 nvs_open() 函数打开名称为 “my_nvs” 的 NVS 分区,并检查返回结果以确保 NVS 分区以读写模式打开成功。
在打开 NVS 后,我们使用 nvs_get_str() 函数读取名称为 “my_string” 的字符串值,如果没有找到值,则输出警告日志。然后,我们使用 strncpy() 函数将新的字符串值写入 my_string 变量,使用 nvs_set_str() 函数将其写入 “my_string” 名称下的 NVS,并使用 nvs_commit() 函数保存更改。最后,我们使用 nvs_close() 函数关闭 NVS 句柄。
总之,在 ESP32 IDF 中,使用 NVS 存储应用程序和设备配置信息非常方便,可以使用简单的 API 函数进行读写操作。您可以使用 nvs_get_xxx() 和 nvs_set_xxx() 系列函数中的各种数据类型来存储您需要的数据,并通过 nvs_commit() 函数将数据更改保存到 NVS。
文章介绍了ESP32IDF中NVS(Non-VolatileStorage)的使用,包括初始化、读写操作和常用API函数,如nvs_flash_init(),nvs_open(),nvs_get_xxx(),nvs_set_xxx()等。示例代码展示了如何存储和读取字符串变量。



1756

被折叠的 条评论
为什么被折叠?



