EspNvs is the ESP32-C6 implementation of the BaseNvs interface,
providing comprehensive NVS (Non-Volatile Storage) functionality specifically optimized for ESP32-C6
microcontrollers running ESP-IDF v5.5+.
It offers both basic and advanced NVS features with hardware-specific optimizations.
Features
ESP32-C6 NVS - Full support for ESP32-C6 NVS capabilities
Persistent Storage - Data survives power cycles and deep sleep
Multiple Namespaces - Organized storage with namespaces
Type Safety - Type-safe storage for different data types
#include"inc/mcu/esp32/EspNvs.h"// Create NVS instanceEspNvsnvs("my_app");// Initializeif(!nvs.Initialize()){printf("Failed to initialize NVS\n");return;}// Store a stringhf_nvs_err_terr=nvs.SetString("device_name","ESP32-C6_Device");if(err!=HF_NVS_ERR_OK){printf("Failed to set string: %d\n",err);return;}// Retrieve the stringchardevice_name[64];err=nvs.GetString("device_name",device_name,sizeof(device_name));if(err==HF_NVS_ERR_OK){printf("Device name: %s\n",device_name);}elseif(err==HF_NVS_ERR_NOT_FOUND){printf("Device name not found\n");}
// Store application configurationstructapp_config{charwifi_ssid[32];charwifi_password[64];hf_u8_tbrightness;hf_u16_tupdate_interval;boolauto_start;};app_configconfig;strcpy(config.wifi_ssid,"MyNetwork");strcpy(config.wifi_password,"MyPassword");config.brightness=80;config.update_interval=300;config.auto_start=true;// Store configurationhf_nvs_err_terr=nvs.SetBlob("app_config",&config,sizeof(config));if(err!=HF_NVS_ERR_OK){printf("Failed to store app config: %d\n",err);return;}// Commit changeserr=nvs.Commit();if(err!=HF_NVS_ERR_OK){printf("Failed to commit changes: %d\n",err);return;}printf("Configuration saved successfully\n");
// Check storage usagehf_size_tused_entries,free_entries;if(nvs.GetUsedEntries(&used_entries)==HF_NVS_ERR_OK&&nvs.GetFreeEntries(&free_entries)==HF_NVS_ERR_OK){printf("NVS usage: %zu used, %zu free entries\n",used_entries,free_entries);}// Erase specific keyhf_nvs_err_terr=nvs.EraseKey("old_setting");if(err==HF_NVS_ERR_OK){printf("Key erased successfully\n");}elseif(err==HF_NVS_ERR_NOT_FOUND){printf("Key not found\n");}// Erase all data (use with caution!)// err = nvs.EraseAll();// if (err == HF_NVS_ERR_OK) {// printf("All data erased\n");// }
ESP32-C6 Specific Features
Flash-based Storage
NVS uses flash memory for persistent storage, ensuring data survives power cycles.
Namespace Organization
Organize data using namespaces for better structure and access control.
Type Safety
Type-safe storage with specific functions for different data types.
Atomic Operations
Safe concurrent access with atomic operations.
Error Handling
The EspNvs class provides comprehensive error handling with specific error codes:
HF_NVS_ERR_OK - Operation successful
HF_NVS_ERR_INVALID_ARG - Invalid parameter
HF_NVS_ERR_NOT_INITIALIZED - NVS not initialized
HF_NVS_ERR_NOT_FOUND - Key not found
HF_NVS_ERR_INVALID_LENGTH - Invalid data length
HF_NVS_ERR_NO_FREE_PAGES - No free pages available