fixes for f7, sdio/sdmmc (#2400)

This commit is contained in:
Matthew Kennedy 2021-02-25 08:37:41 -10:00 committed by GitHub
parent 5a990fb55d
commit 6dc3c20bab
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 7 additions and 33 deletions

View File

@ -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), * 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) * which will cause disaster (usually multiple-unlock of the same mutex in UNLOCK_SD_SPI)
*/ */
spi_device_e mmcSpiDevice = SPI_NONE; spi_device_e mmcSpiDevice = SPI_NONE;
#define LOG_INDEX_FILENAME "index.txt" #define LOG_INDEX_FILENAME "index.txt"
@ -86,12 +85,16 @@ MMCDriver MMCD1;
/* MMC/SD over SPI driver configuration.*/ /* MMC/SD over SPI driver configuration.*/
static MMCConfig mmccfg = { NULL, &mmc_ls_spicfg, &mmc_hs_spicfg }; 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 */ #endif /* HAL_USE_MMC_SPI */
/** /**
* fatfs MMC/SPI * fatfs MMC/SPI
*/ */
static FATFS MMC_FS; static NO_CACHE FATFS MMC_FS;
static LoggingWithStorage logger("mmcCard"); static LoggingWithStorage logger("mmcCard");
@ -140,7 +143,6 @@ static void sdStatistics(void) {
} }
static void incLogFileName(void) { static void incLogFileName(void) {
LOCK_SD_SPI;
memset(&FDCurrFile, 0, sizeof(FIL)); // clear the memory 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 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_write(&FDCurrFile, (void*)data, strlen(data), &result);
f_close(&FDCurrFile); f_close(&FDCurrFile);
scheduleMsg(&logger, "Done %d", logFileIndex); scheduleMsg(&logger, "Done %d", logFileIndex);
UNLOCK_SD_SPI;
} }
static void prepareLogFileName(void) { static void prepareLogFileName(void) {
@ -202,13 +203,11 @@ static void prepareLogFileName(void) {
* so that we can later append to that file * so that we can later append to that file
*/ */
static void createLogFile(void) { static void createLogFile(void) {
LOCK_SD_SPI;
memset(&FDLogFile, 0, sizeof(FIL)); // clear the memory memset(&FDLogFile, 0, sizeof(FIL)); // clear the memory
prepareLogFileName(); prepareLogFileName();
FRESULT err = f_open(&FDLogFile, logName, FA_OPEN_ALWAYS | FA_WRITE); // Create new file FRESULT err = f_open(&FDLogFile, logName, FA_OPEN_ALWAYS | FA_WRITE); // Create new file
if (err != FR_OK && err != FR_EXIST) { if (err != FR_OK && err != FR_EXIST) {
UNLOCK_SD_SPI;
sdStatus = SD_STATE_OPEN_FAILED; sdStatus = SD_STATE_OPEN_FAILED;
warning(CUSTOM_ERR_SD_MOUNT_FAILED, "SD: mount failed"); warning(CUSTOM_ERR_SD_MOUNT_FAILED, "SD: mount failed");
printError("FS mount failed", err); // else - show error 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 err = f_lseek(&FDLogFile, f_size(&FDLogFile)); // Move to end of the file to append data
if (err) { if (err) {
UNLOCK_SD_SPI;
sdStatus = SD_STATE_SEEK_FAILED; sdStatus = SD_STATE_SEEK_FAILED;
warning(CUSTOM_ERR_SD_SEEK_FAILED, "SD: seek failed"); warning(CUSTOM_ERR_SD_SEEK_FAILED, "SD: seek failed");
printError("Seek error", err); printError("Seek error", err);
@ -225,7 +223,6 @@ static void createLogFile(void) {
} }
f_sync(&FDLogFile); f_sync(&FDLogFile);
setSdCardReady(true); // everything Ok setSdCardReady(true); // everything Ok
UNLOCK_SD_SPI;
} }
static void removeFile(const char *pathx) { static void removeFile(const char *pathx) {
@ -233,10 +230,8 @@ static void removeFile(const char *pathx) {
scheduleMsg(&logger, "Error: No File system is mounted"); scheduleMsg(&logger, "Error: No File system is mounted");
return; return;
} }
LOCK_SD_SPI;
f_unlink(pathx);
UNLOCK_SD_SPI; f_unlink(pathx);
} }
int int
@ -264,14 +259,12 @@ static void listDirectory(const char *path) {
scheduleMsg(&logger, "Error: No File system is mounted"); scheduleMsg(&logger, "Error: No File system is mounted");
return; return;
} }
LOCK_SD_SPI;
DIR dir; DIR dir;
FRESULT res = f_opendir(&dir, path); FRESULT res = f_opendir(&dir, path);
if (res != FR_OK) { if (res != FR_OK) {
scheduleMsg(&logger, "Error opening directory %s", path); scheduleMsg(&logger, "Error opening directory %s", path);
UNLOCK_SD_SPI;
return; 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.fdate >> 5) & 15, fno.fdate & 31, (fno.ftime >> 11), (fno.ftime >> 5) & 63, fno.fsize,
// fno.fname); // fno.fname);
} }
UNLOCK_SD_SPI;
} }
/* /*
@ -316,6 +308,7 @@ static void mmcUnMount(void) {
#if HAL_USE_MMC_SPI #if HAL_USE_MMC_SPI
mmcDisconnect(&MMCD1); // Brings the driver in a state safe for card removal. mmcDisconnect(&MMCD1); // Brings the driver in a state safe for card removal.
mmcStop(&MMCD1); // Disables the MMC peripheral. mmcStop(&MMCD1); // Disables the MMC peripheral.
UNLOCK_SD_SPI;
#endif #endif
#ifdef EFI_SDC_DEVICE #ifdef EFI_SDC_DEVICE
sdcDisconnect(&EFI_SDC_DEVICE); sdcDisconnect(&EFI_SDC_DEVICE);
@ -386,7 +379,6 @@ static BaseBlockDevice* initializeMmcBlockDevice() {
return nullptr; return nullptr;
} }
UNLOCK_SD_SPI;
return reinterpret_cast<BaseBlockDevice*>(&MMCD1); return reinterpret_cast<BaseBlockDevice*>(&MMCD1);
} }
#endif /* HAL_USE_MMC_SPI */ #endif /* HAL_USE_MMC_SPI */
@ -469,7 +461,6 @@ struct SdLogBufferWriter final : public BufferedWriter<512> {
totalLoggedBytes += count; totalLoggedBytes += count;
LOCK_SD_SPI;
FRESULT err = f_write(&FDLogFile, buffer, count, &bytesWritten); FRESULT err = f_write(&FDLogFile, buffer, count, &bytesWritten);
if (bytesWritten != count) { if (bytesWritten != count) {
@ -477,7 +468,6 @@ struct SdLogBufferWriter final : public BufferedWriter<512> {
// Close file and unmount volume // Close file and unmount volume
mmcUnMount(); mmcUnMount();
UNLOCK_SD_SPI;
failed = true; failed = true;
return 0; return 0;
} else { } else {
@ -494,7 +484,6 @@ struct SdLogBufferWriter final : public BufferedWriter<512> {
} }
} }
UNLOCK_SD_SPI;
return bytesWritten; return bytesWritten;
} }
}; };

View File

@ -23,10 +23,6 @@ void readLogFileContent(char *buffer, short fileId, short offset, short length);
void handleTsR(TsChannelBase* tsChannel, char *input); void handleTsR(TsChannelBase* tsChannel, char *input);
void handleTsW(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" extern "C"
#endif #endif
void onUsbConnectedNotifyMmcI(void); void onUsbConnectedNotifyMmcI(void);

View File

@ -164,12 +164,10 @@ void handleTsR(TsChannelBase* tsChannel, char *input) {
#endif // EFI_SIMULATOR #endif // EFI_SIMULATOR
#if EFI_FILE_LOGGING #if EFI_FILE_LOGGING
LOCK_SD_SPI;
DIR dir; DIR dir;
FRESULT res = f_opendir(&dir, ROOT_DIR); FRESULT res = f_opendir(&dir, ROOT_DIR);
if (res != FR_OK) { if (res != FR_OK) {
scheduleMsg(&sharedLogger, "Error opening directory"); scheduleMsg(&sharedLogger, "Error opening directory");
UNLOCK_SD_SPI;
} else { } else {
int index = 0; int index = 0;
while (true) { while (true) {
@ -203,7 +201,6 @@ void handleTsR(TsChannelBase* tsChannel, char *input) {
} }
} }
UNLOCK_SD_SPI;
} }
#endif // EFI_FILE_LOGGING #endif // EFI_FILE_LOGGING
@ -227,10 +224,8 @@ void handleTsR(TsChannelBase* tsChannel, char *input) {
#endif // EFI_SIMULATOR #endif // EFI_SIMULATOR
#if EFI_FILE_LOGGING #if EFI_FILE_LOGGING
LOCK_SD_SPI;
got = 0; got = 0;
f_read(&uploading, (void*) &buffer[2], TRANSFER_SIZE, (UINT*) &got); f_read(&uploading, (void*) &buffer[2], TRANSFER_SIZE, (UINT*) &got);
UNLOCK_SD_SPI;
#endif // EFI_FILE_LOGGING #endif // EFI_FILE_LOGGING
tsChannel->sendResponse(TS_CRC, buffer, 2 + got); tsChannel->sendResponse(TS_CRC, buffer, 2 + got);
@ -281,7 +276,6 @@ void handleTsW(TsChannelBase* tsChannel, char *input) {
#endif // EFI_SIMULATOR #endif // EFI_SIMULATOR
#if EFI_FILE_LOGGING #if EFI_FILE_LOGGING
LOCK_SD_SPI;
DIR dir; DIR dir;
FRESULT res = f_opendir(&dir, ROOT_DIR); FRESULT res = f_opendir(&dir, ROOT_DIR);
if (res != FR_OK) { if (res != FR_OK) {
@ -304,7 +298,6 @@ void handleTsW(TsChannelBase* tsChannel, char *input) {
} }
} }
} }
UNLOCK_SD_SPI;
#endif // EFI_FILE_LOGGING #endif // EFI_FILE_LOGGING
sendOkResponse(tsChannel, TS_CRC); sendOkResponse(tsChannel, TS_CRC);
@ -340,14 +333,11 @@ void handleTsW(TsChannelBase* tsChannel, char *input) {
#endif // EFI_SIMULATOR #endif // EFI_SIMULATOR
#if EFI_FILE_LOGGING #if EFI_FILE_LOGGING
LOCK_SD_SPI;
DIR dir; DIR dir;
FRESULT res = f_opendir(&dir, ROOT_DIR); FRESULT res = f_opendir(&dir, ROOT_DIR);
if (res != FR_OK) { if (res != FR_OK) {
scheduleMsg(&sharedLogger, "Error opening directory"); scheduleMsg(&sharedLogger, "Error opening directory");
UNLOCK_SD_SPI;
} else { } else {
memset(&uploading, 0, sizeof(FIL)); // clear the memory memset(&uploading, 0, sizeof(FIL)); // clear the memory
while (true) { while (true) {
@ -366,7 +356,6 @@ void handleTsW(TsChannelBase* tsChannel, char *input) {
} }
} }
} }
UNLOCK_SD_SPI;
} }
#endif // EFI_FILE_LOGGING #endif // EFI_FILE_LOGGING