Storage: format the SD card as one FAT32 partition

Repartitions the whole card (f_fdisk + f_mkfs) on the storage task.
Temporarily triggered from the diagnostics screen by typing FORMAT and
Enter; moves to Settings > Storage in step 7.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EhqxQ49eCju4CzKYNjZzwT
This commit is contained in:
2026-10-02 10:17:41 +02:00
co-authored by Claude Opus 5.5
parent 44de78ea62
commit 36ebd23417
3 changed files with 56 additions and 3 deletions
+37 -1
View File
@@ -1,6 +1,10 @@
#include "storage_service.h"
#include <SD.h>
#include <ff.h>
#include <sd_diskio.h>
#include <memory>
#include "platform/pins.h"
#include "platform/shared_spi.h"
@@ -31,11 +35,43 @@ StorageState StorageService::state() const {
void StorageService::taskEntry(void* self) {
auto* service = static_cast<StorageService*>(self);
for (;;) {
if (service->formatRequested_) {
service->format();
service->formatRequested_ = false;
}
service->poll();
vTaskDelay(pdMS_TO_TICKS(kPollMs));
// Sleep until the next poll, or until a format request wakes us early.
ulTaskNotifyTake(pdTRUE, pdMS_TO_TICKS(kPollMs));
}
}
bool StorageService::requestFormat() {
if (formatRequested_ || !task_) return false;
formatRequested_ = true;
xTaskNotifyGive(task_);
return true;
}
void StorageService::format() {
if (mounted_) SD.end();
mounted_ = false;
bool ok = false;
uint8_t pdrv = sdcard_init(pins::kSdCs, &sharedSpi(), 20000000);
if (pdrv != 0xFF) {
constexpr size_t kWorkSize = 4096; // FF_MAX_SS
std::unique_ptr<uint8_t[]> work(new uint8_t[kWorkSize]);
LBA_t partitions[] = {100, 0}; // one partition, 100 % of the card
char drive[3] = {static_cast<char>('0' + pdrv), ':', 0};
MKFS_PARM options = {FM_FAT32, 0, 0, 0, 0};
ok = f_fdisk(pdrv, partitions, work.get()) == FR_OK &&
f_mkfs(drive, &options, work.get(), kWorkSize) == FR_OK;
sdcard_uninit(pdrv);
}
bus_.publish(Event::withText(EventType::Notification, ok ? "SD card formatted" : "SD card format failed",
static_cast<int32_t>(ok ? NotificationLevel::Info : NotificationLevel::Warning)));
}
bool StorageService::mount() {
return SD.begin(pins::kSdCs, sharedSpi(), 20000000, "/sd", 5, false);
}