fixing bug from yesterday

This commit is contained in:
rusefi 2019-09-21 11:09:03 -04:00
parent a4b6787af1
commit 2ce6e900a3
3 changed files with 38 additions and 26 deletions

View File

@ -956,9 +956,6 @@ void updateTunerStudioState(TunerStudioOutputChannels *tsOutputChannels DECLARE_
case DBG_VEHICLE_SPEED_SENSOR: case DBG_VEHICLE_SPEED_SENSOR:
tsOutputChannels->debugIntField1 = engine->engineState.vssEventCounter; tsOutputChannels->debugIntField1 = engine->engineState.vssEventCounter;
break; break;
case DBG_SD_CARD:
tsOutputChannels->debugIntField1 = engine->engineState.totalLoggedBytes;
break;
case DBG_CRANKING_DETAILS: case DBG_CRANKING_DETAILS:
tsOutputChannels->debugIntField1 = engine->rpmCalculator.getRevolutionCounterSinceStart(); tsOutputChannels->debugIntField1 = engine->rpmCalculator.getRevolutionCounterSinceStart();
break; break;

View File

@ -16,7 +16,6 @@
#define BRAIN_PIN_COUNT (1 << sizeof(brain_pin_e)) #define BRAIN_PIN_COUNT (1 << sizeof(brain_pin_e))
class EngineState : public engine_state2_s { class EngineState : public engine_state2_s {
public: public:
EngineState(); EngineState();
@ -87,7 +86,6 @@ public:
float currentRawVE = 0; float currentRawVE = 0;
int vssEventCounter = 0; int vssEventCounter = 0;
int totalLoggedBytes = 0;
/** /**

View File

@ -28,6 +28,10 @@
#include "rtc_helper.h" #include "rtc_helper.h"
#if EFI_TUNER_STUDIO
extern TunerStudioOutputChannels tsOutputChannels;
#endif
#define SD_STATE_INIT "init" #define SD_STATE_INIT "init"
#define SD_STATE_MOUNTED "MOUNTED" #define SD_STATE_MOUNTED "MOUNTED"
#define SD_STATE_MOUNT_FAILED "MOUNT_FAILED" #define SD_STATE_MOUNT_FAILED "MOUNT_FAILED"
@ -44,7 +48,10 @@ EXTERN_ENGINE;
#define F_SYNC_FREQUENCY 100 #define F_SYNC_FREQUENCY 100
static int totalLoggedBytes = 0;
static int writeCounter = 0; static int writeCounter = 0;
static int totalWritesCounter = 0;
static int totalSyncCounter = 0;
#define LOG_INDEX_FILENAME "index.txt" #define LOG_INDEX_FILENAME "index.txt"
@ -94,8 +101,6 @@ static SPIConfig ls_spicfg = {
// don't forget check if STM32_SPI_USE_SPI2 defined and spi has init with correct GPIO in hardware.cpp // don't forget check if STM32_SPI_USE_SPI2 defined and spi has init with correct GPIO in hardware.cpp
static MMCConfig mmccfg = { NULL, &ls_spicfg, &hs_spicfg }; static MMCConfig mmccfg = { NULL, &ls_spicfg, &hs_spicfg };
#define FILE_LOG_MIN_DELAY 3
/** /**
* fatfs MMC/SPI * fatfs MMC/SPI
*/ */
@ -133,7 +138,7 @@ static void sdStatistics(void) {
scheduleMsg(&logger, "SD enabled=%s status=%s", boolToString(CONFIGB(isSdCardEnabled)), scheduleMsg(&logger, "SD enabled=%s status=%s", boolToString(CONFIGB(isSdCardEnabled)),
sdStatus); sdStatus);
if (fs_ready) { if (fs_ready) {
scheduleMsg(&logger, "filename=%s size=%d", logName, engine->engineState.totalLoggedBytes); scheduleMsg(&logger, "filename=%s size=%d", logName, totalLoggedBytes);
} }
} }
@ -214,15 +219,7 @@ static void createLogFile(void) {
printError("Seek error", err); printError("Seek error", err);
return; return;
} }
writeCounter++; f_sync(&FDLogFile);
if (writeCounter >= F_SYNC_FREQUENCY) {
/**
* Performance optimization: not f_sync after each line, f_sync is probably a heavy operation
* todo: one day someone should actualy measure the relative cost of f_sync
*/
f_sync(&FDLogFile);
writeCounter = 0;
}
fs_ready = true; // everything Ok fs_ready = true; // everything Ok
unlockSpi(); unlockSpi();
} }
@ -320,14 +317,34 @@ void appendToLog(const char *line) {
return; return;
} }
UINT lineLength = strlen(line); UINT lineLength = strlen(line);
engine->engineState.totalLoggedBytes += lineLength; totalLoggedBytes += lineLength;
lockSpi(SPI_NONE); lockSpi(SPI_NONE);
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
} }
f_sync(&FDLogFile); writeCounter++;
totalWritesCounter++;
if (writeCounter >= F_SYNC_FREQUENCY) {
/**
* Performance optimization: not f_sync after each line, f_sync is probably a heavy operation
* todo: one day someone should actualy measure the relative cost of f_sync
*/
f_sync(&FDLogFile);
totalSyncCounter++;
writeCounter = 0;
}
unlockSpi(); unlockSpi();
if (engineConfiguration->debugMode == DBG_SD_CARD) {
tsOutputChannels.debugIntField1 = totalLoggedBytes;
tsOutputChannels.debugIntField2 = totalWritesCounter;
tsOutputChannels.debugIntField3 = totalSyncCounter;
}
} }
/* /*
@ -434,11 +451,15 @@ static THD_FUNCTION(MMCmonThread, arg) {
sdStatus = SD_STATE_NOT_INSERTED; sdStatus = SD_STATE_NOT_INSERTED;
} }
if (isSdCardAlive()) if (isSdCardAlive()) {
writeLogLine(); writeLogLine();
} else {
chThdSleepMilliseconds(100);
}
int periodMs = maxI(boardConfiguration->sdCardPeriodMs, FILE_LOG_MIN_DELAY); if (boardConfiguration->sdCardPeriodMs > 0) {
chThdSleepMilliseconds(periodMs); chThdSleepMilliseconds(boardConfiguration->sdCardPeriodMs);
}
} }
} }
@ -447,10 +468,6 @@ bool isSdCardAlive(void) {
} }
void initMmcCard(void) { void initMmcCard(void) {
// temporary value while we migrate
if (boardConfiguration->sdCardPeriodMs == 0)
boardConfiguration->sdCardPeriodMs = 50;
logName[0] = 0; logName[0] = 0;
addConsoleAction("sdinfo", sdStatistics); addConsoleAction("sdinfo", sdStatistics);
if (!CONFIGB(isSdCardEnabled)) { if (!CONFIGB(isSdCardEnabled)) {