[ESP8266学习笔记]components_nvs 非易失性存储 Non-Volatile Storage(NVS),保存数据到flash

本文介绍了ESP8266如何利用components_nvs进行非易失性存储(NVS),保存数据到flash。NVS适合存储小数据,如整数和字符串。主要讲解了NVS的基本概念、部分API用法,包括初始化、读写操作,并提供了一个代码示例来演示如何在ESP8266中使用NVS保存和读取数据。
摘要由CSDN通过智能技术生成

1.简介

有时我们需要保存一些信息,然后在下次启动时使用。
我们希望这些数据在掉电的时候不会丢失。esp8266没有自己的rom,所以我们需要保存在外部flash中。
官方已经为我们提供了很多api,我们可以直接使用SDK中的components。

SDK编译环境搭建:https://blog.csdn.net/valonshen/article/details/104124127
由于esp8266文档中没有相关说明,参考esp32:https://docs.espressif.com/projects/esp-idf/zh_CN/latest/api-reference/storage/nvs_flash.html#

非易失性存储 (NVS) 库主要用于在 flash 中存储键值格式的数据。NVS 最适合存储一些较小的数据,而非字符串或二进制大对象 (BLOB) 等较大的数据。如需存储较大的 BLOB 或者字符串,请考虑使用基于磨损均衡库的 FAT 文件系统。

NVS 的操作对象为键值对,其中键是 ASCII 字符串,当前支持最大键长为 15 个字符,值可以为以下几种类型:

  1. 整数型:uint8_t、int8_t、uint16_t、int16_t、uint32_t、int32_t、uint64_t 和 int64_t;
  2. 以 \0 结尾的字符串;
  3. 可变长度的二进制数据 (BLOB)

2. 部分API介绍

esp_err_t nvs_flash_init(void)

初始化默认NVS分区,需要在其他API调用之前调用。esp8266将flash分成几个分区,每个分区的地址偏移和大小都可以自行配置,我会在另一个文章在介绍。

返回值

  • ESP_OK if storage was successfully initialized.
  • ESP_ERR_NVS_NO_FREE_PAGES if the NVS storage contains no empty pages (which may happen if NVS partition was truncated)
  • ESP_ERR_NOT_FOUND if no partition with label “nvs” is found in the partition table one of the error codes from the underlying
    flash storage driver

esp_err_t nvs_flash_erase(void)

擦除默认NVS分区所有数据。若要继续使用,需调用nvs_flash_init初始化。

返回值:

  • ESP_OK on success
  • ESP_ERR_NOT_FOUND if there is no NVS partition labeled “nvs” in the partition table

esp_err_t nvs_open(const char *name, nvs_open_mode_t open_mode, nvs_handle_t * out_handle)
void nvs_close(nvs_handle_t handle)

用于打开和关闭 NVS分区。name为最多15字节的命名空间,用于防止键值对名称的冲突。在不同的命名空间中允许出现相同名称的键值对,但在同一个命名空间中键值对的名称是唯一的。handle用于后续读写键值对操作。
目前使用esp8266的SDK,nvs打开的句柄类型需改成 nvs_handle,而esp-ief是nvs_handle_t

返回值:

  • ESP_OK if storage handle was opened successfully
  • ESP_ERR_NVS_NOT_INITIALIZED if the storage driver is not initialized
  • ESP_ERR_NVS_PART_NOT_FOUND if the partition with label “nvs” is not found
  • ESP_ERR_NVS_NOT_FOUND id namespace doesn’t exist yet and mode is NVS_READONLY
  • ESP_ERR_NVS_INVALID_NAME if namespace name doesn’t satisfy constraints
  • other error codes from the underlying storage driver

参数:

  • name: Namespace name. Maximal length is determined by the underlying implementation, but is guaranteed to be at least 15 characters. Shouldn’t be empty.
  • open_mode: NVS_READWRITE or NVS_READONLY. If NVS_READONLY, will open a handle for reading only. All write requests will be rejected for this handle.
  • out_handle: If successful (return code is zero), handle will be returned in this argument.

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(</
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值