ESP NVS
简介:NVS的主要功能是:存储键值(存在flash上面);
NVS利用spi_flash_{read|write|erase}这些API来操作数据在内存上的删改写,内存上data类型nvs子类型所代表的空间全部是NVS使用的;
NVS操作 数据的删改些运用一些API,就像是在电脑上打开文件,写文件,关闭文件一样。如nvs_open, nvs_close, nvs_write;
NVS一般存储一下比较小的数据还是很好使用的,如果要存储比较大的内容,还是用FAT更合适;
NVS操作的数据为键值对,key是ASCII字符串,最大长度是15个字符,数值的话可以包含下列类型:uint8_t
, int8_t
, uint16_t
, int16_t
, uint32_t
, int32_t
, uint64_t
, int64_t;数据类型还包括以0结尾的字符串,可变长度的二进制数据;String values are currently limited to 4000 bytes. This includes the null terminator. Blob values are limited to 508000 bytes or (97.6% of the partition size - 4000) bytes whichever is lower.
字符串数据当前限制为4000字节,这4000自己包括NULL,BLOB数据限制为508000字节
key值要求是唯一的,如果给存在的key更新对应的数据,如果更新的数据和老数据是同一个类型,那么数据会更新,如果新数据老数据是不同类型,报错;
Currently NVS uses a portion of main flash memory through spi_flash_{read|write|erase}
APIs. The library uses the all the partitions with data
type and nvs
subtype. The application can choose to use the partition with label nvs
through nvs_open
API or any of the other partition by specifying its name through nvs_open_from_part
API.
上面这段英文翻译:NVS通过spi_flash_{read|write|erase}
API来使用内存上指定的分区,data类型,nvs分类型分区的存储都可以被NVS使用,可以用nvs_open这个API打开,或者用nvs_open_from_part
这个API打开指定类型和分类型名称的内存分区;
将来NVS可能会支持将数据存储在另外的存储上(比如SPI或者I2C)
如果NVS存储被破坏了,那么就会被整体清楚掉,make erase_flash就是用来干这个事情的;
To mitigate potential conflicts in key names between different components, NVS assigns each key-value pair to one of namespaces. Namespace names follow the same rules as key names, i.e. 15 character maximum length. Namespace name is specified in the nvs_open
or nvs_open_from_part
call. This call returns an opaque handle, which is used in subsequent calls to nvs_read_*
, nvs_write_*
, and nvs_commit
functions. This way, handle is associated with a namespace, and key names will not collide with same names in other namespaces. Please note that the namespaces with same name in different NVS partitions are considered as separate namespaces.
上面英文的翻译如下:为了减少不同组件之间键名之间的冲突(至于什么组件,这段英文没有提),NVS给每个键值对分配了一个名空间(这段英文的意思是名空间有很多),名空间的命名规则和键的命名规则是相同的,比如说最多允许15个字符的字符串。名空间的名字在nvs_open
或者nvs_open_from_part这两个API中都有明确写出。这两个API都会返回句柄,返回的句柄在
nvs_read_*
, nvs_write_*
, 和 nvs_commit
这些API中都有用到,句柄和名空间的名字之间存在对应关系。不同名空间下即使存在同名的key值也是不冲突的。请注意不同的NVS存储空间内同名的名空间是相互独立的,没有任何互相影响;
NVS stores key-value pairs sequentially, with new key-value pairs being added at the end. When a value of any given key has to be updated, new key-value pair is added at the end of the log and old key-value pair is marked as erased.
上面这段翻译:NVS存储键值对在内存中是连续的,新存储的键值对 放在最后头,当更新一个已经存在的键值对时,也是放在最后头,原先的键值对就标记删除。
esp_err_tnvs_flash_init
(void)
初始化NVS分区,就是在分区表上标为NVS的那部分;
esp_err_tnvs_flash_init_partition
(const char *partition_label)
初始化NVS分区,这个分区是特别指定的分区;
esp_err_tnvs_flash_deinit
(void)
取消NVS分区初始化;
esp_err_tnvs_flash_deinit_partition
(const char *partition_label)
NVS特别指定分区的去初始化;
esp_err_tnvs_flash_erase
(void)
擦除NVS分区;
esp_err_tnvs_flash_erase_partition
(const char *part_name)
擦除NVS指定分区;
esp_err_tnvs_flash_secure_init
(nvs_sec_cfg_t *cfg)
初始化NVS默认分区,但是不知道和第一个NVS初始化的区别,可能这个是安全级别上的初始化把;
esp_err_tnvs_flash_secure_init_partition
(const char *partition_label, nvs_sec_cfg_t*cfg)
初始化指定分区的NVS,可能也是安全级别上的初始化;
esp_err_tnvs_flash_generate_keys
(constesp_partition_t *partition, nvs_sec_cfg_t *cfg)
产生并存储NVS键;
esp_err_tnvs_flash_read_security_cfg
(constesp_partition_t *partition, nvs_sec_cfg_t*cfg)
读取分区的安全设置;