TS SD integration #1653

This commit is contained in:
rusefi 2020-08-02 17:58:57 -04:00
parent dc623ba7f0
commit aa2ee8a776
4 changed files with 31 additions and 25 deletions

View File

@ -74,7 +74,7 @@ static void sendToPot(Mcp42010Driver *driver, int channel, int value) {
spiSend(driver->spi, 1, &word); spiSend(driver->spi, 1, &word);
spiUnselect(driver->spi); spiUnselect(driver->spi);
spiStop(driver->spi); spiStop(driver->spi);
unlockSpi(); unlockSpi(SPI_NONE);
} }
void setPotResistance(Mcp42010Driver *driver, int channel, int resistance) { void setPotResistance(Mcp42010Driver *driver, int channel, int resistance) {

View File

@ -88,7 +88,7 @@ void lockSpi(spi_device_e device) {
chMtxLock(&spiMtx); chMtxLock(&spiMtx);
} }
void unlockSpi(void) { void unlockSpi(spi_device_e device) {
chMtxUnlock(&spiMtx); chMtxUnlock(&spiMtx);
} }

View File

@ -35,7 +35,7 @@ int getSpiPrescaler(spi_speed_e speed, spi_device_e device);
EXTERNC SPIDriver * getSpiDevice(spi_device_e spiDevice); EXTERNC SPIDriver * getSpiDevice(spi_device_e spiDevice);
void turnOnSpi(spi_device_e device); void turnOnSpi(spi_device_e device);
void lockSpi(spi_device_e device); void lockSpi(spi_device_e device);
void unlockSpi(void); void unlockSpi(spi_device_e device);
brain_pin_e getMisoPin(spi_device_e device); brain_pin_e getMisoPin(spi_device_e device);
brain_pin_e getMosiPin(spi_device_e device); brain_pin_e getMosiPin(spi_device_e device);
brain_pin_e getSckPin(spi_device_e device); brain_pin_e getSckPin(spi_device_e device);

View File

@ -52,8 +52,10 @@ static int totalSyncCounter = 0;
#define LOG_INDEX_FILENAME "index.txt" #define LOG_INDEX_FILENAME "index.txt"
#define RUSEFI_LOG_PREFIX "rusefi_" #define DOT_MLG ".mlg"
#define PREFIX_LEN 7
#define RUSEFI_LOG_PREFIX "re_"
#define PREFIX_LEN 3
#define SHORT_TIME_LEN 13 #define SHORT_TIME_LEN 13
#define LS_RESPONSE "ls_result" #define LS_RESPONSE "ls_result"
@ -69,6 +71,8 @@ static int totalSyncCounter = 0;
extern const USBConfig msdusbcfg; extern const USBConfig msdusbcfg;
#endif /* HAL_USE_USB_MSD */ #endif /* HAL_USE_USB_MSD */
#define LOCK_SD_SPI lockSpi(engineConfiguration->sdCardSpiDevice)
#define UNLOCK_SD_SPI unlockSpi(engineConfiguration->sdCardSpiDevice)
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
@ -145,7 +149,7 @@ static void sdStatistics(void) {
} }
static void incLogFileName(void) { static void incLogFileName(void) {
lockSpi(SPI_NONE); 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
@ -177,20 +181,22 @@ 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);
unlockSpi(); UNLOCK_SD_SPI;
} }
static void prepareLogFileName(void) { static void prepareLogFileName(void) {
strcpy(logName, RUSEFI_LOG_PREFIX); strcpy(logName, RUSEFI_LOG_PREFIX);
bool result = dateToStringShort(&logName[PREFIX_LEN]);
char *ptr; char *ptr;
/* TS SD protocol supports only short 8 symbol file names :(
bool result = dateToStringShort(&logName[PREFIX_LEN]);
if (result) { if (result) {
ptr = &logName[PREFIX_LEN + SHORT_TIME_LEN]; ptr = &logName[PREFIX_LEN + SHORT_TIME_LEN];
} else { } else {
*/
ptr = itoa10(&logName[PREFIX_LEN], logFileIndex); ptr = itoa10(&logName[PREFIX_LEN], logFileIndex);
} // }
strcat(ptr, ".mlg"); strcat(ptr, DOT_MLG);
} }
/** /**
@ -200,13 +206,13 @@ 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) {
lockSpi(SPI_NONE); 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) {
unlockSpi(); 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
@ -215,7 +221,7 @@ 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) {
unlockSpi(); 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);
@ -223,7 +229,7 @@ static void createLogFile(void) {
} }
f_sync(&FDLogFile); f_sync(&FDLogFile);
setSdCardReady(true); // everything Ok setSdCardReady(true); // everything Ok
unlockSpi(); UNLOCK_SD_SPI;
} }
static void removeFile(const char *pathx) { static void removeFile(const char *pathx) {
@ -231,10 +237,10 @@ static void removeFile(const char *pathx) {
scheduleMsg(&logger, "Error: No File system is mounted"); scheduleMsg(&logger, "Error: No File system is mounted");
return; return;
} }
lockSpi(SPI_NONE); LOCK_SD_SPI;
f_unlink(pathx); f_unlink(pathx);
unlockSpi(); UNLOCK_SD_SPI;
} }
int int
@ -262,14 +268,14 @@ static void listDirectory(const char *path) {
scheduleMsg(&logger, "Error: No File system is mounted"); scheduleMsg(&logger, "Error: No File system is mounted");
return; return;
} }
lockSpi(SPI_NONE); 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);
unlockSpi(); UNLOCK_SD_SPI;
return; return;
} }
@ -295,7 +301,7 @@ 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);
} }
unlockSpi(); UNLOCK_SD_SPI;
} }
static int errorReported = FALSE; // this is used to report the error only once static int errorReported = FALSE; // this is used to report the error only once
@ -319,7 +325,7 @@ void appendToLog(const char *line, size_t lineLength) {
} }
totalLoggedBytes += lineLength; totalLoggedBytes += lineLength;
lockSpi(SPI_NONE); LOCK_SD_SPI;
FRESULT err = f_write(&FDLogFile, line, lineLength, &bytesWritten); FRESULT err = f_write(&FDLogFile, line, lineLength, &bytesWritten);
if (bytesWritten < lineLength) { if (bytesWritten < lineLength) {
printError("write error or disk full", err); // error or disk full printError("write error or disk full", err); // error or disk full
@ -338,7 +344,7 @@ void appendToLog(const char *line, size_t lineLength) {
} }
} }
unlockSpi(); UNLOCK_SD_SPI;
} }
/* /*
@ -382,12 +388,12 @@ static void MMCmount(void) {
} }
// Performs the initialization procedure on the inserted card. // Performs the initialization procedure on the inserted card.
lockSpi(SPI_NONE); LOCK_SD_SPI;
sdStatus = SD_STATE_CONNECTING; sdStatus = SD_STATE_CONNECTING;
if (mmcConnect(&MMCD1) != HAL_SUCCESS) { if (mmcConnect(&MMCD1) != HAL_SUCCESS) {
sdStatus = SD_STATE_NOT_CONNECTED; sdStatus = SD_STATE_NOT_CONNECTED;
warning(CUSTOM_OBD_MMC_ERROR, "Can't connect or mount MMC/SD"); warning(CUSTOM_OBD_MMC_ERROR, "Can't connect or mount MMC/SD");
unlockSpi(); UNLOCK_SD_SPI;
return; return;
} }
@ -415,7 +421,7 @@ static void MMCmount(void) {
//} //}
unlockSpi(); UNLOCK_SD_SPI;
#if HAL_USE_USB_MSD #if HAL_USE_USB_MSD
sdStatus = SD_STATE_MOUNTED; sdStatus = SD_STATE_MOUNTED;
return; return;