ESP32 學習日誌(5) NVS

2021-10-12 10:14:42 字數 2537 閱讀 3367

非易失性儲存 (nvs) 庫主要用於在 flash 中儲存鍵值格式的資料。本文件將詳細介紹 nvs 在esp32中的使用。

nvs 的操作物件為鍵值對,其中鍵是 ascii 字串,當前支援最大鍵長為 15 個字元可以為以下幾種型別:

#include

"nvs_flash.h"

void

main

(void

)

/******************************************

* public functions

*//**

@brief 資料寫入flash

@param 無

@return 無

*/void

nvswritedatatoflash

(void),

};// 要寫入的版本號

uint8_t version_for_store[4]

=;printf

("set size:%u\r\n"

,sizeof

(wifi_config_to_store));

esp_error_check

(nvs_open

( nvs_customer, nvs_readwrite,

&handle));

esp_error_check

(nvs_set_i32

( handle, data1, value_for_store));

esp_error_check

(nvs_set_str

( handle, data2, str_for_store));

esp_error_check

(nvs_set_blob

( handle, data3,

&wifi_config_to_store,

sizeof

(wifi_config_to_store)))

;esp_error_check

(nvs_set_blob

( handle, data4, version_for_store,4)

);esp_error_check

(nvs_commit

(handle));

nvs_close

(handle)

;}

/**

@brief 讀取flash資料

@param 無

@return 無

*/void

nvsreaddatafromflash

(void);

int32_t value =0;

wifi_config_t wifi_config_stored;

uint8_t version[4]

=;uint32_t version_len =4;

memset

(&wifi_config_stored,

0x0,

sizeof

(wifi_config_stored));

uint32_t wifi_len =

sizeof

(wifi_config_stored)

;esp_error_check

(nvs_open

(nvs_customer, nvs_readwrite,

&handle));

esp_error_check (

nvs_get_i32

(handle, data1,

&value));

esp_error_check (

nvs_get_str

(handle, data2, str_data,

&str_length));

esp_error_check (

nvs_get_blob

(handle, data3,

&wifi_config_stored,

&wifi_len));

esp_error_check (

nvs_get_blob

(handle, data4, version,

&version_len));

printf

("[data1]: %s len:%u\r\n"

, str_data, str_length)

;printf

("[data2]: %d\r\n"

, value)

;printf

("[data3]: ssid:%s passwd:%s\r\n"

, wifi_config_stored.sta.ssid, wifi_config_stored.sta.password)

;nvs_close

(handle)

;}

• 由 青梅煮久 寫於 2020 年 12 月 19 日

• 參考:

esp32 使用nvs進行軟體復位

1 硬體 一塊esp32最小系統板 2 程式原始碼 include include include include freertos freertos.h include freertos task.h include freertos event groups.h include nvs.h in...

ESP32 學習記錄

首次接觸freertos和espressif的產品,例如我,那還是要先來個整體印象,然後再逐個深化。做專案的都知道,老闆們是不允許我們四平八穩的研究完然後再開始專案。那也不妨礙咱們要有自己的節奏,既要平衡專案的進度,也要按照事情的規律來做事情 這就需要快速的準備一些預備知識,磨刀不誤砍柴功,倉促上陣...

ESP32學習 外設 timer

typedef struct timer config t 定時器初始化結構體成員6個,從上到下依次是,中斷使能,計數使能,中斷模式,計數方向,自動過載,分頻係數。timer init timer group 0,timer 1,timerconfig 初始化結構體後,呼叫初始化函式,三個引數包括,...