From 0caf28c86a2120ab21f4cfd8f458847507925474 Mon Sep 17 00:00:00 2001 From: Mitchell Marino Date: Fri, 22 Aug 2025 12:52:24 -0500 Subject: [PATCH] work on nvs --- main/drivers/CMakeLists.txt | 1 + main/drivers/all.cpp | 1 + main/drivers/all.h | 1 + main/drivers/hwdata.cpp | 3 +++ main/drivers/hwdata.h | 6 ++++++ main/drivers/nvs.cpp | 35 +++++++++++++++++++++++++++++++++++ main/drivers/nvs.h | 3 ++- main/steps/step0.cpp | 2 +- 8 files changed, 50 insertions(+), 2 deletions(-) create mode 100644 main/drivers/hwdata.cpp create mode 100644 main/drivers/hwdata.h diff --git a/main/drivers/CMakeLists.txt b/main/drivers/CMakeLists.txt index bf4a6ca..a0ad5db 100644 --- a/main/drivers/CMakeLists.txt +++ b/main/drivers/CMakeLists.txt @@ -8,6 +8,7 @@ set(SOURCES "char_lcd.cpp" "game_info.cpp" "game_timer.cpp" + "hwdata.cpp" "i2c_lcd_pcf8574.c" "i2c.cpp" "leds.cpp" diff --git a/main/drivers/all.cpp b/main/drivers/all.cpp index 3e38868..4d571ed 100644 --- a/main/drivers/all.cpp +++ b/main/drivers/all.cpp @@ -4,6 +4,7 @@ void init_drivers() { init_i2c(); // init char_lcd so we can use it to report other initialization errors. init_lcd(); + init_nvs(); init_star_code_system(); // init the bottom half so that we can get user input. init_bottom_half(); diff --git a/main/drivers/all.h b/main/drivers/all.h index 48047d9..67965d0 100644 --- a/main/drivers/all.h +++ b/main/drivers/all.h @@ -6,6 +6,7 @@ #include "game_timer.h" #include "i2c.h" #include "leds.h" +#include "nvs.h" #include "power.h" #include "sd.h" #include "speaker.h" diff --git a/main/drivers/hwdata.cpp b/main/drivers/hwdata.cpp new file mode 100644 index 0000000..f8c1e30 --- /dev/null +++ b/main/drivers/hwdata.cpp @@ -0,0 +1,3 @@ +#include "hwdata.h" + + diff --git a/main/drivers/hwdata.h b/main/drivers/hwdata.h new file mode 100644 index 0000000..597e243 --- /dev/null +++ b/main/drivers/hwdata.h @@ -0,0 +1,6 @@ +#ifndef HWDATA_H +#define HWDATA_H + + + +#endif /* HWDATA_H */ diff --git a/main/drivers/nvs.cpp b/main/drivers/nvs.cpp index bb36ce9..09d81ea 100644 --- a/main/drivers/nvs.cpp +++ b/main/drivers/nvs.cpp @@ -1,3 +1,38 @@ #include "nvs.h" +#include "nvs_flash.h" +#include "esp_log.h" +#include "bottom_half.h" +#include "char_lcd.h" + +static const char* TAG = "nvs"; + +nvs_handle_t hw_handle; + +bool init_nvs() { + esp_err_t ret = nvs_flash_init(); + if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) { + // NVS partition was truncated, erase and retry + ESP_LOGE(TAG, "Failed to init nvs flash: %s.", esp_err_to_name(ret)); + lcd_print(1, 0, "NVS: "); + lcd_print(1, 4, esp_err_to_name(ret)); + lcd_print(2, 0, "Press Yellow to skip"); + lcd_print(3, 0, "Press Red to erase"); + + ButtonKey button; + while (!( get_button_pressed(&button) && (button == ButtonKey::button_red || button == ButtonKey::button_yellow) )) vTaskDelay(pdMS_TO_TICKS(10)); + + lcd_clear(); + + if (button == ButtonKey::button_yellow) { + return false; + } + if (button == ButtonKey::button_red) { + ESP_ERROR_CHECK(nvs_flash_erase()); + ret = nvs_flash_init(); + } + } + ESP_ERROR_CHECK(ret); + return true; +} diff --git a/main/drivers/nvs.h b/main/drivers/nvs.h index 06f96e5..7523fab 100644 --- a/main/drivers/nvs.h +++ b/main/drivers/nvs.h @@ -1,6 +1,7 @@ #ifndef NVS_H #define NVS_H - +/// @brief Initializes the NVS. +bool init_nvs(); #endif /* NVS_H */ \ No newline at end of file diff --git a/main/steps/step0.cpp b/main/steps/step0.cpp index fe2b218..182d40f 100644 --- a/main/steps/step0.cpp +++ b/main/steps/step0.cpp @@ -71,7 +71,7 @@ void step0() { .code = "59860", .display_text = "Hardware Config", .delay_us = 2'000'000, - .callback = hardware_config + .callback = hardware_config, .triggered_sem = nullptr, }, {