SPIFFS mount sometimes fail

This commit is contained in:
Emanuele Trabattoni
2026-04-07 17:33:08 +02:00
parent 877236ee4e
commit 7c96101cdd
6 changed files with 42 additions and 890 deletions

View File

@@ -1,6 +1,7 @@
#include "datasave.h"
static constexpr size_t min_free = 1024 * 1024; // minimum free space in SPIFFS to allow saving history (1MB)
static const size_t min_free = 1024 * 1024; // minimum free space in SPIFFS to allow saving history (1MB)
static bool first_save = true; // flag to indicate if this is the first save (to write header)
void save_history(const PSRAMVector<ignitionBoxStatus> &history, const std::filesystem::path &file_path)
{
@@ -25,11 +26,11 @@ void save_history(const PSRAMVector<ignitionBoxStatus> &history, const std::file
to_save = std::filesystem::path("/spiffs") / file_path;
auto save_flags = std::ios::out;
static bool first_save = true;
if (first_save || !SPIFFS.exists(to_save.c_str()))
if (first_save && SPIFFS.exists(to_save.c_str()))
{
save_flags |= std::ios::trunc; // overwrite existing file
first_save = false;
save_flags |= std::ios::trunc; // overwrite existing file
SPIFFS.remove(to_save.c_str()); // ensure file is removed before saving to avoid issues with appending to existing file in SPIFFS
LOG_INFO("Saving history to SPIFFS, new file: ", to_save.c_str());
}
else
@@ -51,7 +52,8 @@ void save_history(const PSRAMVector<ignitionBoxStatus> &history, const std::file
ofs << "TS,\
EVENTS_12,DLY_12,STAT_12,V_12_1,V_12_2,V_12_3,V_12_4,IGNITION_MODE_12,\
EVENTS_34,DLY_34,STAT_34,V_34_1,V_34_2,V_34_3,V_34_4,IGNITION_MODE_34,\
ENGINE_RPM,ADC_READTIME,N_QUEUE_ERRORS";
ENGINE_RPM,ADC_READTIME,N_QUEUE_ERRORS" << std::endl;
ofs.flush();
}
for (const auto &entry : history)
@@ -77,10 +79,10 @@ void save_history(const PSRAMVector<ignitionBoxStatus> &history, const std::file
<< std::to_string(entry.adc_read_time) << ","
<< std::to_string(entry.n_queue_errors);
ofs << std::endl;
ofs.flush();
}
ofs.flush();
ofs.close();
LOG_INFO("Ignition A history saved to SPIFFS, records written: ", history.size());
SPIFFS.end(); // unmount SPIFFS to ensure data is written and avoid corruption on next mount
}
}