feat(nvs_flash): Added support for storing flash and double types

- added backward compatibility test for unknown NVS entry types
 - axtended documentation with the support of double and float
This commit is contained in:
radek.tandler
2026-03-31 19:02:43 +02:00
parent e07b6dec77
commit a5443bbb94
12 changed files with 1146 additions and 21 deletions
+48 -1
View File
@@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2015-2025 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2015-2026 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
@@ -12,6 +12,8 @@
#include "nvs_partition_manager.hpp"
#include "esp_partition.h"
#include <functional>
#include <cmath>
#include <limits>
#include "nvs_handle_simple.hpp"
#include "nvs_memory_management.hpp"
#include "esp_err.h"
@@ -449,6 +451,41 @@ extern "C" esp_err_t nvs_set_u64 (nvs_handle_t handle, const char* key, uint64_t
return nvs_set(handle, key, value);
}
static_assert(std::numeric_limits<float>::is_iec559, "float must conform to IEEE 754");
static_assert(sizeof(float) == 4, "float must be 4 bytes");
static_assert(std::numeric_limits<double>::is_iec559, "double must conform to IEEE 754");
static_assert(sizeof(double) == 8, "double must be 8 bytes");
extern "C" esp_err_t nvs_set_float (nvs_handle_t c_handle, const char* key, float value)
{
if (std::isnan(value)) {
return ESP_ERR_INVALID_ARG;
}
Lock lock;
ESP_LOGD(TAG, "%s %s %f", __func__, key, static_cast<double>(value));
NVSHandleSimple *handle;
auto err = nvs_find_ns_handle(c_handle, &handle);
if (err != ESP_OK) {
return err;
}
return handle->set_item(key, value);
}
extern "C" esp_err_t nvs_set_double (nvs_handle_t c_handle, const char* key, double value)
{
if (std::isnan(value)) {
return ESP_ERR_INVALID_ARG;
}
Lock lock;
ESP_LOGD(TAG, "%s %s %f", __func__, key, value);
NVSHandleSimple *handle;
auto err = nvs_find_ns_handle(c_handle, &handle);
if (err != ESP_OK) {
return err;
}
return handle->set_item(key, value);
}
extern "C" esp_err_t nvs_commit(nvs_handle_t c_handle)
{
Lock lock;
@@ -539,6 +576,16 @@ extern "C" esp_err_t nvs_get_u64 (nvs_handle_t c_handle, const char* key, uint64
return nvs_get(c_handle, key, out_value);
}
extern "C" esp_err_t nvs_get_float (nvs_handle_t c_handle, const char* key, float* out_value)
{
return nvs_get(c_handle, key, out_value);
}
extern "C" esp_err_t nvs_get_double (nvs_handle_t c_handle, const char* key, double* out_value)
{
return nvs_get(c_handle, key, out_value);
}
static esp_err_t nvs_get_str_or_blob(nvs_handle_t c_handle, nvs::ItemType type, const char* key, void* out_value, size_t* length)
{
Lock lock;
+3 -1
View File
@@ -61,7 +61,9 @@ bool Item::checkHeaderConsistency(const uint8_t entryIndex) const
case ItemType::U32:
case ItemType::I32:
case ItemType::U64:
case ItemType::I64: {
case ItemType::I64:
case ItemType::FLOAT:
case ItemType::DOUBLE: {
if (span != 1) {
ESP_LOGD(TAG, "Invalid span %u for datatype %#04x", (unsigned int)span, (unsigned int)datatype);
return false;