122 lines
3.2 KiB
C
122 lines
3.2 KiB
C
#include <stdio.h>
|
||
#include <stdint.h>
|
||
#include "freertos/FreeRTOS.h"
|
||
#include "freertos/task.h"
|
||
#include "esp_system.h"
|
||
#include "nvs_flash.h"
|
||
#include "nvs.h"
|
||
#include "esp_log.h"
|
||
#include "nvs_storage.h"
|
||
|
||
static const char *TAG = "nvs_storage";
|
||
|
||
nvs_handle_t my_handle;
|
||
|
||
void nvs_storage_init(void)
|
||
{
|
||
ESP_LOGI(TAG, "nvs_storage_init");
|
||
// Initialize NVS
|
||
esp_err_t err = nvs_flash_init();
|
||
if (err == ESP_ERR_NVS_NO_FREE_PAGES || err == ESP_ERR_NVS_NEW_VERSION_FOUND) {
|
||
// NVS partition was truncated and needs to be erased
|
||
// Retry nvs_flash_init
|
||
ESP_ERROR_CHECK(nvs_flash_erase());
|
||
err = nvs_flash_init();
|
||
}
|
||
ESP_ERROR_CHECK( err );
|
||
}
|
||
|
||
// 注意,key长度不能太长,要不然会失败
|
||
int nvs_storage_read(const char *key, void *value, nvs_storage_type_t type)
|
||
{
|
||
ESP_LOGI(TAG, "nvs_storage_read");
|
||
esp_err_t err = ESP_OK;
|
||
|
||
err = nvs_open("storage", NVS_READWRITE, &my_handle);
|
||
if (err != ESP_OK) {
|
||
ESP_LOGI(TAG, "Error (%s) opening NVS handle!\n", esp_err_to_name(err));
|
||
return err;
|
||
}
|
||
|
||
switch (type)
|
||
{
|
||
case NVS_STORAGE_UINT8:
|
||
err = nvs_get_u8(my_handle, key, (uint8_t *)value);
|
||
break;
|
||
case NVS_STORAGE_UINT16:
|
||
err = nvs_get_u16(my_handle, key, (uint16_t *)value);
|
||
break;
|
||
case NVS_STORAGE_UINT32:
|
||
err = nvs_get_u32(my_handle, key, (uint32_t *)value);
|
||
break;
|
||
case NVS_STORAGE_UINT64:
|
||
err = nvs_get_u64(my_handle, key, (uint64_t *)value);
|
||
break;
|
||
default:
|
||
err = -1;
|
||
break;
|
||
}
|
||
|
||
switch (err) {
|
||
case ESP_OK:
|
||
ESP_LOGI(TAG, "Done\n");
|
||
ESP_LOGI(TAG, "nvs read = 0x%x\n", *((uint16_t *)value));
|
||
break;
|
||
case ESP_ERR_NVS_NOT_FOUND:
|
||
ESP_LOGI(TAG, "The value is not initialized yet!\n");
|
||
break;
|
||
default :
|
||
ESP_LOGI(TAG, "Error (%s) reading!\n", esp_err_to_name(err));
|
||
}
|
||
|
||
nvs_close(my_handle);
|
||
return err;
|
||
}
|
||
|
||
int nvs_storage_write(const char *key, const void *value, nvs_storage_type_t type)
|
||
{
|
||
esp_err_t err = ESP_OK;
|
||
|
||
err = nvs_open("storage", NVS_READWRITE, &my_handle);
|
||
if (err != ESP_OK) {
|
||
ESP_LOGI(TAG, "Error (%s) opening NVS handle!\n", esp_err_to_name(err));
|
||
return err;
|
||
}
|
||
|
||
switch (type)
|
||
{
|
||
case NVS_STORAGE_UINT8:
|
||
err = nvs_set_u8(my_handle, key, *((uint8_t *)value));
|
||
break;
|
||
case NVS_STORAGE_UINT16:
|
||
err = nvs_set_u16(my_handle, key, *((uint16_t *)value));
|
||
break;
|
||
case NVS_STORAGE_UINT32:
|
||
err = nvs_set_u32(my_handle, key, *((uint32_t *)value));
|
||
break;
|
||
case NVS_STORAGE_UINT64:
|
||
err = nvs_set_u64(my_handle, key, *((uint64_t *)value));
|
||
break;
|
||
default:
|
||
err = ESP_ERR_NVS_NOT_FOUND;
|
||
break;
|
||
}
|
||
|
||
if (err != ESP_OK)
|
||
{
|
||
ESP_LOGI(TAG, "Error (%s) nvs set!\n", esp_err_to_name(err));
|
||
goto exit;
|
||
}
|
||
|
||
err = nvs_commit(my_handle);
|
||
if (err != ESP_OK)
|
||
{
|
||
ESP_LOGI(TAG, "Error (%s) nvs commit!\n", esp_err_to_name(err));
|
||
goto exit;
|
||
}
|
||
|
||
exit:
|
||
nvs_close(my_handle);
|
||
return err;
|
||
}
|