avatar

松果工作室

欢迎光临

  • 首页
  • freeRTOS
  • LVGL
  • ESP
  • 开发手册
  • 快速笔记
  • 个人收藏
  • 时事记录
  • 考察日志
  • 工具
Home ESP32(七) NVS
文章

ESP32(七) NVS

Posted 2025-03-18 Updated 2025-06- 14
By YCP
15~20 min read

基本概念

  • 命名空间(Namespace)​:NVS 数据按命名空间分组,避免键名冲突。
  • 键值对(Key-Value)​:支持存储整数、字符串、二进制数据等类型。
  • 存储限制:每个键值对最大 1984KB,单个命名空间总大小取决于分区表配置(默认约 24KB)。

示例代码

#include <stdio.h>
#include "nvs_flash.h"
#include "esp_system.h"

void nvs_demo() {
    // 初始化 NVS
    esp_err_t 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);

    // 打开命名空间
    nvs_handle_t handle;
    ESP_ERROR_CHECK(nvs_open("my_storage", NVS_READWRITE, &handle));

    // 写入数据,键值对
    ESP_ERROR_CHECK(nvs_set_i32(handle, "boot_count", 100));
    ESP_ERROR_CHECK(nvs_set_str(handle, "device_id", "ESP32_1234"));

    // 读取数据
    int32_t boot_count = 0;
    ESP_ERROR_CHECK(nvs_get_i32(handle, "boot_count", &boot_count));
    printf("Boot count: %d\n", boot_count);

    // 获取存入的字符串,要先获取长度,再获取具体字符串
    size_t id_len;
    ESP_ERROR_CHECK(nvs_get_str(handle, "device_id", NULL, &id_len));
    char *id = malloc(id_len);
    ESP_ERROR_CHECK(nvs_get_str(handle, "device_id", id, &id_len));
    printf("Device ID: %s\n", id);
    free(id);

    // 关闭
    nvs_close(handle);
}

改进代码

  • 以上代码过于繁琐,由于写入不同类型数据,涉及到多个函数,其实可以写入结构体到NVS
  • 以下仅做示例
    nxs_exa.c
#include "nvs_exa.h"
#include <nvs.h>
#include <esp_log.h>
#include <string.h>

static const char *TAG = "nvs_exa";

#define LOG_ERROR(TAG, ...) ESP_LOGE(TAG, "%s:%d ", __FILE__, __LINE__, ##__VA_ARGS__)

esp_err_t save_settings_to_nvs(settings_t *settings) {
    nvs_handle_t nvs_handle;
    esp_err_t err;

    err = nvs_open("storage", NVS_READWRITE, &nvs_handle);
    if (err != ESP_OK) return err;

    err = nvs_set_blob(nvs_handle, "user_settings", settings, sizeof(settings_t));
    if (err != ESP_OK) {
        nvs_close(nvs_handle);
        return err;
    }

    err = nvs_commit(nvs_handle);
    nvs_close(nvs_handle);
    return err;
}

esp_err_t load_settings_from_nvs(settings_t *settings) {
    nvs_handle_t nvs_handle;
    size_t required_size = sizeof(settings_t);
    esp_err_t err;

    err = nvs_open("storage", NVS_READONLY, &nvs_handle);
    if (err != ESP_OK) return err;

    err = nvs_get_blob(nvs_handle, "user_settings", settings, &required_size);
    nvs_close(nvs_handle);
    return err;
}

nvs.exa.h

#pragma once
#include <nvs.h>
#include <esp_log.h>
#include <string.h>

typedef struct {
    uint8_t brightness;
    char wifi_ssid[32];
    char wifi_passwd[64];
    char ap_ssid[32];
    char ap_passwd[64];
} settings_t;

extern settings_t settings;

esp_err_t save_settings_to_nvs(settings_t *settings);
esp_err_t load_settings_from_nvs(settings_t *settings);
ESP
License:  CC BY 4.0
Share

Further Reading

OLDER

ESP32(四) STA & AP

NEWER

ESP32(八) 简单的webserver

Recently Updated

  • ESP32(十) BLE OTA
  • ESP32(九) BLE GATTS
  • LVGL(四) 动画
  • LVGL(三) 对象中创建对象
  • LVGL(二) 定时回调

Trending Tags

LVGL WCH Linux Elec freeRTOS STM ESP Flutter Others SwiftUI

Contents

©2025 松果工作室. Some rights reserved.

Using the Halo theme Chirpy