Files
@ aee85174089e
Branch filter:
Location: protofusion-esp32-template/main/flash.c - annotation
aee85174089e
2.0 KiB
text/plain
Add missing files for squeeze and nv storage
aee85174089e aee85174089e aee85174089e aee85174089e aee85174089e aee85174089e aee85174089e aee85174089e aee85174089e aee85174089e aee85174089e aee85174089e aee85174089e aee85174089e aee85174089e aee85174089e aee85174089e aee85174089e aee85174089e aee85174089e aee85174089e aee85174089e aee85174089e aee85174089e aee85174089e aee85174089e aee85174089e aee85174089e aee85174089e aee85174089e aee85174089e aee85174089e aee85174089e aee85174089e aee85174089e aee85174089e aee85174089e aee85174089e aee85174089e aee85174089e aee85174089e aee85174089e aee85174089e aee85174089e aee85174089e aee85174089e aee85174089e aee85174089e aee85174089e aee85174089e aee85174089e aee85174089e aee85174089e aee85174089e aee85174089e aee85174089e aee85174089e aee85174089e aee85174089e aee85174089e aee85174089e aee85174089e aee85174089e aee85174089e aee85174089e aee85174089e aee85174089e aee85174089e aee85174089e aee85174089e aee85174089e aee85174089e aee85174089e aee85174089e aee85174089e aee85174089e aee85174089e aee85174089e aee85174089e aee85174089e aee85174089e aee85174089e aee85174089e aee85174089e aee85174089e aee85174089e aee85174089e aee85174089e aee85174089e aee85174089e aee85174089e aee85174089e | //
// flash: nonvolatile k/v storage
//
#include <esp_event.h>
#include <esp_log.h>
#include "nvs_flash.h"
#include "flash.h"
#include <inttypes.h>
// Private variables
static nvs_handle_t my_handle;
static const char *TAG = "flash";
// Initialize flash storage
void flash_init(void)
{
// Initialize NVS
esp_err_t ret = nvs_flash_init();
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
ESP_ERROR_CHECK(nvs_flash_erase());
ret = nvs_flash_init();
}
ESP_ERROR_CHECK(ret);
}
// Write k/v to flash
void flash_write(char* key, int32_t val)
{
ESP_LOGI(TAG, "Writing keyval %s => %ld", key, val);
// Open handle
esp_err_t 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));
}
else
{
// Write
err = nvs_set_i32(my_handle, key, val);
if(err != ESP_OK)
ESP_LOGI(TAG, "Failed!\n");
err = nvs_commit(my_handle);
if(err != ESP_OK)
ESP_LOGI(TAG, "Failed!\n");
// Close
nvs_close(my_handle);
}
}
// Read k/v from flash
int32_t flash_read(char* key)
{
int32_t val = -1; // value will default to -1 if not set
// Open
ESP_LOGI(TAG, "Reading keyval %s", key);
esp_err_t err = nvs_open("storage", NVS_READONLY, &my_handle);
if (err != ESP_OK)
{
ESP_LOGI(TAG, "Error (%s) opening NVS handle!\n", esp_err_to_name(err));
}
else
{
// Read
err = nvs_get_i32(my_handle, key, &val);
switch (err) {
case ESP_OK:
ESP_LOGI(TAG, "Read value %ld\n", val);
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));
}
// Close
nvs_close(my_handle);
}
return val;
}
|