mmc_card: divide init and mount

This commit is contained in:
Andrey Gusakov 2025-01-11 12:06:36 +03:00 committed by rusefillc
parent 4eac7248ac
commit 8f8460a937
1 changed files with 76 additions and 44 deletions

View File

@ -264,15 +264,10 @@ void onUsbConnectedNotifyMmcI() {
#if HAL_USE_MMC_SPI #if HAL_USE_MMC_SPI
/* /*
* Attempts to initialize the MMC card. * Attempts to initialize the MMC card connected over SPI.
* Returns a BaseBlockDevice* corresponding to the SD card if successful, otherwise nullptr. * Returns a BaseBlockDevice* corresponding to the SD card if successful, otherwise nullptr.
*/ */
static BaseBlockDevice* initializeMmcBlockDevice() { static BaseBlockDevice* initializeMmcBlockDevice() {
// Don't try to mount SD card in case of fatal error - hardware may be in an unexpected state
if (hasFirmwareError()) {
return nullptr;
}
// Configures and activates the MMC peripheral. // Configures and activates the MMC peripheral.
mmcSpiDevice = engineConfiguration->sdCardSpiDevice; mmcSpiDevice = engineConfiguration->sdCardSpiDevice;
@ -319,6 +314,10 @@ static const SDCConfig sdcConfig = {
.bus_width = RE_SDC_MODE .bus_width = RE_SDC_MODE
}; };
/*
* Attempts to initialize the MMC card connected over SDIO.
* Returns a BaseBlockDevice* corresponding to the SD card if successful, otherwise nullptr.
*/
static BaseBlockDevice* initializeMmcBlockDevice() { static BaseBlockDevice* initializeMmcBlockDevice() {
sdcStart(&EFI_SDC_DEVICE, &sdcConfig); sdcStart(&EFI_SDC_DEVICE, &sdcConfig);
sdStatus = SD_STATE_CONNECTING; sdStatus = SD_STATE_CONNECTING;
@ -346,48 +345,48 @@ static bool useMsdMode() {
} }
#endif // HAL_USE_USB_MSD #endif // HAL_USE_USB_MSD
// Initialize and mount the SD card. static BaseBlockDevice* cardBlockDevice = nullptr;
// Returns true if the filesystem was successfully mounted for writing.
static bool mountMmc() { // Initialize SD card.
auto cardBlockDevice = initializeMmcBlockDevice(); static bool initMmc() {
// Don't try to mount SD card in case of fatal error - hardware may be in an unexpected state
if (hasFirmwareError()) {
return false;
}
cardBlockDevice = initializeMmcBlockDevice();
#if EFI_TUNER_STUDIO #if EFI_TUNER_STUDIO
// If not null, card is present // If not null, card is present
engine->outputChannels.sd_present = cardBlockDevice != nullptr; engine->outputChannels.sd_present = cardBlockDevice != nullptr;
#endif #endif
return (cardBlockDevice != nullptr);
}
// Mount the SD card.
// Returns true if the filesystem was successfully mounted for writing.
static bool mountMmc() {
// if no card, don't try to mount FS // if no card, don't try to mount FS
if (!cardBlockDevice) { if (!cardBlockDevice) {
return false; return false;
} }
#if HAL_USE_USB_MSD
// If we have a device AND USB is connected, mount the card to USB, otherwise
// mount the null device and try to mount the filesystem ourselves
if (useMsdMode()) {
// Mount the real card to USB
attachMsdSdCard(cardBlockDevice);
sdStatus = SD_STATE_MSD;
// At this point we're done: don't try to write files ourselves
return false;
}
#endif
// We were able to connect the SD card, mount the filesystem // We were able to connect the SD card, mount the filesystem
memset(&MMC_FS, 0, sizeof(FATFS)); memset(&MMC_FS, 0, sizeof(FATFS));
if (f_mount(&MMC_FS, "/", 1) == FR_OK) { if (f_mount(&MMC_FS, "/", 1) != FR_OK) {
efiPrintf("MMC/SD mounted!");
sdStatus = SD_STATE_MOUNTED;
incLogFileName();
errorHandlerWriteReportFile(&FDLogFile, logFileIndex);
f_setlabel(SD_CARD_LABEL);
createLogFile();
return true;
} else {
sdStatus = SD_STATE_MOUNT_FAILED; sdStatus = SD_STATE_MOUNT_FAILED;
return false; return false;
} }
efiPrintf("MMC/SD mounted!");
sdStatus = SD_STATE_MOUNTED;
#if EFI_TUNER_STUDIO
engine->outputChannels.sd_logging_internal = true;
#endif
return true;
} }
struct SdLogBufferWriter final : public BufferedWriter<512> { struct SdLogBufferWriter final : public BufferedWriter<512> {
@ -429,6 +428,11 @@ struct SdLogBufferWriter final : public BufferedWriter<512> {
#include <fstream> #include <fstream>
bool initMmc() {
// Stub so the loop thinks the MMC is present
return true;
}
bool mountMmc() { bool mountMmc() {
// Stub so the loop thinks the MMC mounted OK // Stub so the loop thinks the MMC mounted OK
return true; return true;
@ -467,6 +471,9 @@ static void sdTriggerLogger();
static THD_WORKING_AREA(mmcThreadStack, 3 * UTILITY_THREAD_STACK_SIZE); // MMC monitor thread static THD_WORKING_AREA(mmcThreadStack, 3 * UTILITY_THREAD_STACK_SIZE); // MMC monitor thread
static THD_FUNCTION(MMCmonThread, arg) { static THD_FUNCTION(MMCmonThread, arg) {
(void)arg; (void)arg;
bool hasUsb = false;
chRegSetThreadName("MMC Card Logger"); chRegSetThreadName("MMC Card Logger");
#if HW_HELLEN && EFI_PROD_CODE #if HW_HELLEN && EFI_PROD_CODE
@ -484,20 +491,45 @@ static THD_FUNCTION(MMCmonThread, arg) {
chThdSleepMilliseconds(300); chThdSleepMilliseconds(300);
#endif #endif
if (mountMmc()) { if (!initMmc()) {
#if EFI_TUNER_STUDIO efiPrintf("Card is not preset/failed to init");
engine->outputChannels.sd_logging_internal = true; // give up until next boot
#endif goto die;
if (engineConfiguration->sdTriggerLog) {
sdTriggerLogger();
} else {
mlgLogger();
}
} else {
// no card present (or mounted via USB), don't do internal logging
} }
#if HAL_USE_USB_MSD
// Wait for the USB stack to wake up, or a 15 second timeout, whichever occurs first
// If we have a device AND USB is connected, mount the card to USB, otherwise
// mount the null device and try to mount the filesystem ourselves
if (useMsdMode()) {
hasUsb = true;
// Mount the real card to USB
attachMsdSdCard(cardBlockDevice);
sdStatus = SD_STATE_MSD;
// At this point we're done: don't try to write files ourselves
}
#endif
if (!hasUsb) {
if (mountMmc()) {
incLogFileName();
// TODO: Do this independently of SD mode, somewhere in the start of this task!
errorHandlerWriteReportFile(&FDLogFile, logFileIndex);
f_setlabel(SD_CARD_LABEL);
createLogFile();
if (engineConfiguration->sdTriggerLog) {
sdTriggerLogger();
} else {
mlgLogger();
}
} else {
efiPrintf("failed to mount SD card for logging");
}
}
die:
efiPrintf("SD logger has died!"); efiPrintf("SD logger has died!");
// exiting thread will create zombie! // exiting thread will create zombie!