#include "storage_task.h" #include "../storage/storage_config.h" StorageTask::StorageTask(SDManager& manager, StorageState& state) : storage(manager), storageState(state), taskHandle(nullptr) { } void StorageTask::start() { // Dedicated task on core 0 so SD card work never blocks the services // running on core 1 (SystemTask / DiagnosticsTask). The 8 KB stack is // sized for the recursive boot-time file listing; the eventual // producer/consumer logging loop will also live here. xTaskCreatePinnedToCore( taskEntry, "StorageTask", 8192, this, 3, &taskHandle, 0 ); } void StorageTask::taskEntry(void* parameter) { StorageTask* task = static_cast(parameter); task->run(); } void StorageTask::run() { Serial.println("[Storage] Initializing SD card..."); storageState.begin(); // While the mount has not succeeded the dashboard must show "SD not found". storageState.setMounted(false); // Retry the mount until it succeeds. This keeps the card dead-pin-capable: // reseating the card, fixing a wire, or powering the module will bring it // up without rebooting the Hub. (The SD init handshake runs at 400 kHz, so // a failure here is wiring/power/seating/pull-up related, not speed.) while (!storage.begin()) { Serial.println("[Storage] SD card mount FAILED."); Serial.println("[Storage] Check: 3.3V power + common ground, CS/SCK/MOSI/MISO wiring,"); Serial.println("[Storage] card fully seated (click), and module pull-ups."); Serial.println("[Storage] Retrying in 5 s..."); vTaskDelay(pdMS_TO_TICKS(5000)); } storageState.setMounted(true); storageState.setCardType(storage.cardTypeName()); storage.printCardInfo(); storage.listFiles(); // Initial write-speed estimate for the dashboard. storageState.setWriteSpeedBps(storage.measureWriteSpeed()); storageState.setCapacity(storage.totalBytes(), storage.usedBytes()); TickType_t lastWake = xTaskGetTickCount(); uint32_t lastSpeedMeasure = millis(); while (true) { // Periodically re-estimate write speed and refresh capacity so the // dashboard stays current. An interval of 0 disables re-measuring. if (STORAGE_SPEED_MEASURE_INTERVAL_MS != 0 && millis() - lastSpeedMeasure >= STORAGE_SPEED_MEASURE_INTERVAL_MS) { lastSpeedMeasure = millis(); storageState.setWriteSpeedBps(storage.measureWriteSpeed()); storageState.setCapacity(storage.totalBytes(), storage.usedBytes()); } // Future integration point: a producer/consumer queue will feed // audio data here to be flushed to the card. vTaskDelayUntil( &lastWake, pdMS_TO_TICKS(1000) ); } }