簡介:NVS的主要功能是:存儲鍵值(存在flash上面);html
NVS利用spi_flash_{read|write|erase}這些API來操做數據在內存上的刪改寫,內存上data類型nvs子類型所表明的空間所有是NVS使用的;api
NVS操做 數據的刪改些運用一些API,就像是在電腦上打開文件,寫文件,關閉文件同樣。如nvs_open, nvs_close, nvs_write;安全
NVS通常存儲一下比較小的數據仍是很好使用的,若是要存儲比較大的內容,仍是用FAT更合適;app
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字節ide
key值要求是惟一的,若是給存在的key更新對應的數據,若是更新的數據和老數據是同一個類型,那麼數據會更新,若是新數據老數據是不一樣類型,報錯;ui
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.spa
上面這段英文翻譯:NVS經過spi_flash_{read|write|erase}
API來使用內存上指定的分區,data類型,nvs分類型分區的存儲均可以被NVS使用,能夠用nvs_open這個API打開,或者用nvs_open_from_part
這個API打開指定類型和分類型名稱的內存分區;翻譯
未來NVS可能會支持將數據存儲在另外的存儲上(好比SPI或者I2C)code
若是NVS存儲被破壞了,那麼就會被總體清楚掉,make erase_flash就是用來幹這個事情的;component
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)
讀取分區的安全設置;