diff --git a/firmware/hw_layer/mmc_card.cpp b/firmware/hw_layer/mmc_card.cpp index c18fe3f0ad..8284e28bb5 100644 --- a/firmware/hw_layer/mmc_card.cpp +++ b/firmware/hw_layer/mmc_card.cpp @@ -55,7 +55,6 @@ static int totalSyncCounter = 0; * on't re-read SD card spi device after boot - it could change mid transaction (TS thread could preempt), * which will cause disaster (usually multiple-unlock of the same mutex in UNLOCK_SD_SPI) */ - spi_device_e mmcSpiDevice = SPI_NONE; #define LOG_INDEX_FILENAME "index.txt" @@ -86,12 +85,16 @@ MMCDriver MMCD1; /* MMC/SD over SPI driver configuration.*/ static MMCConfig mmccfg = { NULL, &mmc_ls_spicfg, &mmc_hs_spicfg }; + +#define LOCK_SD_SPI lockSpi(mmcSpiDevice) +#define UNLOCK_SD_SPI unlockSpi(mmcSpiDevice) + #endif /* HAL_USE_MMC_SPI */ /** * fatfs MMC/SPI */ -static FATFS MMC_FS; +static NO_CACHE FATFS MMC_FS; static LoggingWithStorage logger("mmcCard"); @@ -140,7 +143,6 @@ static void sdStatistics(void) { } static void incLogFileName(void) { - LOCK_SD_SPI; memset(&FDCurrFile, 0, sizeof(FIL)); // clear the memory FRESULT err = f_open(&FDCurrFile, LOG_INDEX_FILENAME, FA_READ); // This file has the index for next log file name @@ -172,7 +174,6 @@ static void incLogFileName(void) { f_write(&FDCurrFile, (void*)data, strlen(data), &result); f_close(&FDCurrFile); scheduleMsg(&logger, "Done %d", logFileIndex); - UNLOCK_SD_SPI; } static void prepareLogFileName(void) { @@ -202,13 +203,11 @@ static void prepareLogFileName(void) { * so that we can later append to that file */ static void createLogFile(void) { - LOCK_SD_SPI; memset(&FDLogFile, 0, sizeof(FIL)); // clear the memory prepareLogFileName(); FRESULT err = f_open(&FDLogFile, logName, FA_OPEN_ALWAYS | FA_WRITE); // Create new file if (err != FR_OK && err != FR_EXIST) { - UNLOCK_SD_SPI; sdStatus = SD_STATE_OPEN_FAILED; warning(CUSTOM_ERR_SD_MOUNT_FAILED, "SD: mount failed"); printError("FS mount failed", err); // else - show error @@ -217,7 +216,6 @@ static void createLogFile(void) { err = f_lseek(&FDLogFile, f_size(&FDLogFile)); // Move to end of the file to append data if (err) { - UNLOCK_SD_SPI; sdStatus = SD_STATE_SEEK_FAILED; warning(CUSTOM_ERR_SD_SEEK_FAILED, "SD: seek failed"); printError("Seek error", err); @@ -225,7 +223,6 @@ static void createLogFile(void) { } f_sync(&FDLogFile); setSdCardReady(true); // everything Ok - UNLOCK_SD_SPI; } static void removeFile(const char *pathx) { @@ -233,10 +230,8 @@ static void removeFile(const char *pathx) { scheduleMsg(&logger, "Error: No File system is mounted"); return; } - LOCK_SD_SPI; - f_unlink(pathx); - UNLOCK_SD_SPI; + f_unlink(pathx); } int @@ -264,14 +259,12 @@ static void listDirectory(const char *path) { scheduleMsg(&logger, "Error: No File system is mounted"); return; } - LOCK_SD_SPI; DIR dir; FRESULT res = f_opendir(&dir, path); if (res != FR_OK) { scheduleMsg(&logger, "Error opening directory %s", path); - UNLOCK_SD_SPI; return; } @@ -299,7 +292,6 @@ static void listDirectory(const char *path) { // (fno.fdate >> 5) & 15, fno.fdate & 31, (fno.ftime >> 11), (fno.ftime >> 5) & 63, fno.fsize, // fno.fname); } - UNLOCK_SD_SPI; } /* @@ -316,6 +308,7 @@ static void mmcUnMount(void) { #if HAL_USE_MMC_SPI mmcDisconnect(&MMCD1); // Brings the driver in a state safe for card removal. mmcStop(&MMCD1); // Disables the MMC peripheral. + UNLOCK_SD_SPI; #endif #ifdef EFI_SDC_DEVICE sdcDisconnect(&EFI_SDC_DEVICE); @@ -386,7 +379,6 @@ static BaseBlockDevice* initializeMmcBlockDevice() { return nullptr; } - UNLOCK_SD_SPI; return reinterpret_cast(&MMCD1); } #endif /* HAL_USE_MMC_SPI */ @@ -469,7 +461,6 @@ struct SdLogBufferWriter final : public BufferedWriter<512> { totalLoggedBytes += count; - LOCK_SD_SPI; FRESULT err = f_write(&FDLogFile, buffer, count, &bytesWritten); if (bytesWritten != count) { @@ -477,7 +468,6 @@ struct SdLogBufferWriter final : public BufferedWriter<512> { // Close file and unmount volume mmcUnMount(); - UNLOCK_SD_SPI; failed = true; return 0; } else { @@ -494,7 +484,6 @@ struct SdLogBufferWriter final : public BufferedWriter<512> { } } - UNLOCK_SD_SPI; return bytesWritten; } }; diff --git a/firmware/hw_layer/mmc_card.h b/firmware/hw_layer/mmc_card.h index 533a5df960..cd66ba2b15 100644 --- a/firmware/hw_layer/mmc_card.h +++ b/firmware/hw_layer/mmc_card.h @@ -23,10 +23,6 @@ void readLogFileContent(char *buffer, short fileId, short offset, short length); void handleTsR(TsChannelBase* tsChannel, char *input); void handleTsW(TsChannelBase* tsChannel, char *input); -extern spi_device_e mmcSpiDevice; -#define LOCK_SD_SPI lockSpi(mmcSpiDevice) -#define UNLOCK_SD_SPI unlockSpi(mmcSpiDevice) - extern "C" #endif void onUsbConnectedNotifyMmcI(void); diff --git a/firmware/hw_layer/mmc_card_access.cpp b/firmware/hw_layer/mmc_card_access.cpp index da8fbd59a0..e6ade6d539 100644 --- a/firmware/hw_layer/mmc_card_access.cpp +++ b/firmware/hw_layer/mmc_card_access.cpp @@ -164,12 +164,10 @@ void handleTsR(TsChannelBase* tsChannel, char *input) { #endif // EFI_SIMULATOR #if EFI_FILE_LOGGING - LOCK_SD_SPI; DIR dir; FRESULT res = f_opendir(&dir, ROOT_DIR); if (res != FR_OK) { scheduleMsg(&sharedLogger, "Error opening directory"); - UNLOCK_SD_SPI; } else { int index = 0; while (true) { @@ -203,7 +201,6 @@ void handleTsR(TsChannelBase* tsChannel, char *input) { } } - UNLOCK_SD_SPI; } #endif // EFI_FILE_LOGGING @@ -227,10 +224,8 @@ void handleTsR(TsChannelBase* tsChannel, char *input) { #endif // EFI_SIMULATOR #if EFI_FILE_LOGGING - LOCK_SD_SPI; got = 0; f_read(&uploading, (void*) &buffer[2], TRANSFER_SIZE, (UINT*) &got); - UNLOCK_SD_SPI; #endif // EFI_FILE_LOGGING tsChannel->sendResponse(TS_CRC, buffer, 2 + got); @@ -281,7 +276,6 @@ void handleTsW(TsChannelBase* tsChannel, char *input) { #endif // EFI_SIMULATOR #if EFI_FILE_LOGGING - LOCK_SD_SPI; DIR dir; FRESULT res = f_opendir(&dir, ROOT_DIR); if (res != FR_OK) { @@ -304,7 +298,6 @@ void handleTsW(TsChannelBase* tsChannel, char *input) { } } } - UNLOCK_SD_SPI; #endif // EFI_FILE_LOGGING sendOkResponse(tsChannel, TS_CRC); @@ -340,14 +333,11 @@ void handleTsW(TsChannelBase* tsChannel, char *input) { #endif // EFI_SIMULATOR #if EFI_FILE_LOGGING - LOCK_SD_SPI; - DIR dir; FRESULT res = f_opendir(&dir, ROOT_DIR); if (res != FR_OK) { scheduleMsg(&sharedLogger, "Error opening directory"); - UNLOCK_SD_SPI; } else { memset(&uploading, 0, sizeof(FIL)); // clear the memory while (true) { @@ -366,7 +356,6 @@ void handleTsW(TsChannelBase* tsChannel, char *input) { } } } - UNLOCK_SD_SPI; } #endif // EFI_FILE_LOGGING