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
}
}

View File

@@ -12,12 +12,12 @@
#include "isr.h"
#include "psvector.h"
const uint32_t max_history = 1024;
const uint32_t max_history = 256;
const bool SAVE_HISTORY_TO_SPIFFS = true; // Set to true to enable saving history to SPIFFS, false to disable
struct dataSaveParams
{
PSRAMVector<ignitionBoxStatus> *history;
const PSRAMVector<ignitionBoxStatus> *history;
const std::filesystem::path file_path;
};
@@ -25,15 +25,14 @@ void save_history(const PSRAMVector<ignitionBoxStatus> &history, const std::file
static void saveHistoryTask(void *pvParameters)
{
auto *params = static_cast<dataSaveParams *>(pvParameters);
auto &history = *params->history;
auto &file_path = params->file_path;
const auto *params = static_cast<dataSaveParams *>(pvParameters);
const auto &history = *params->history;
const auto &file_path = params->file_path;
if (!params) {
LOG_ERROR("Invalid parameters for saveHistoryTask");
return;
}
LOG_INFO("Starting saving: ", file_path.c_str());
save_history(history, file_path);
history.reserve(max_history); // ensure writable buffer has the correct size
vTaskDelete(NULL);
//vTaskDelete(NULL);
}

View File

@@ -50,13 +50,12 @@ void loop()
{
// global variables
bool running = true;
const uint32_t max_queue = 128;
PSRAMVector<ignitionBoxStatus> ignA_history_0(max_history);
PSRAMVector<ignitionBoxStatus> ignA_history_1(max_history);
auto *active_history = &ignA_history_0;
auto *writable_history = &ignA_history_1;
// Resources Initialization
static Devices dev;
// Task handle
@@ -100,7 +99,6 @@ void loop()
.rt_resets = rtTaskResets{.rst_io_12p = RST_EXT_B12P, .rst_io_12n = RST_EXT_B12N, .rst_io_34p = RST_EXT_B34P, .rst_io_34n = RST_EXT_B34N}};
#endif
// Spi ok flags
bool spiA_ok = true;
bool spiB_ok = true;
@@ -184,30 +182,38 @@ void loop()
while (running)
{
if (counter >= active_history->size())
if (counter >= active_history->size()) // not concurrent with write task
{
counter = 0;
partial_save = false; // reset partial save flag on new data cycle
std::swap(active_history, writable_history); // switch active and writable buffers
auto *temp = active_history;
active_history = writable_history; // switch active and writable buffers
writable_history = temp; // ensure writable_history points to the buffer we just filled
dataSaveParams save_params{
.history = writable_history,
.file_path = "ignition_history.csv"
};
if (SAVE_HISTORY_TO_SPIFFS)
xTaskCreate(
saveHistoryTask,
"saveHistoryTask",
RT_TASK_STACK / 2,
&save_params,
RT_TASK_PRIORITY - 10, // higher priority to ensure it runs asap after buffer switch
NULL);
.file_path = "ignition_history.csv"};
saveHistoryTask(&save_params); // directly call the save task function to save without delay, since we already switched buffers and writable_history is now empty and ready for new data
// if (SAVE_HISTORY_TO_SPIFFS)
// if (pdFAIL ==
// xTaskCreatePinnedToCore(
// saveHistoryTask,
// "saveHistoryTask",
// RT_TASK_STACK,
// &save_params,
// RT_TASK_PRIORITY - 1, // higher priority to ensure it runs asap after buffer switch
// NULL,
// CORE_1))
// {
// LOG_ERROR("Unable to create saveHistoryTask");
// }
}
if (xQueueReceive(rt_taskA_queue, &ign_info, pdMS_TO_TICKS(1000)) == pdTRUE)
{
printInfo(ign_info);
// printInfo(ign_info);
auto &hist = *active_history;
hist[counter++ % active_history->size()] = ign_info;
Serial.print("Data Received: " + String(counter) + "/" + String(hist.size()) + '\r');
}
else
{