From 2ce6e900a3f6d5b8d3023104d9a1faee598a05c5 Mon Sep 17 00:00:00 2001 From: rusefi Date: Sat, 21 Sep 2019 11:09:03 -0400 Subject: [PATCH] fixing bug from yesterday --- firmware/console/status_loop.cpp | 3 -- firmware/controllers/algo/engine_state.h | 2 - firmware/hw_layer/mmc_card.cpp | 59 +++++++++++++++--------- 3 files changed, 38 insertions(+), 26 deletions(-) diff --git a/firmware/console/status_loop.cpp b/firmware/console/status_loop.cpp index 8f063c4e3e..760e2b3adc 100644 --- a/firmware/console/status_loop.cpp +++ b/firmware/console/status_loop.cpp @@ -956,9 +956,6 @@ void updateTunerStudioState(TunerStudioOutputChannels *tsOutputChannels DECLARE_ case DBG_VEHICLE_SPEED_SENSOR: tsOutputChannels->debugIntField1 = engine->engineState.vssEventCounter; break; - case DBG_SD_CARD: - tsOutputChannels->debugIntField1 = engine->engineState.totalLoggedBytes; - break; case DBG_CRANKING_DETAILS: tsOutputChannels->debugIntField1 = engine->rpmCalculator.getRevolutionCounterSinceStart(); break; diff --git a/firmware/controllers/algo/engine_state.h b/firmware/controllers/algo/engine_state.h index 833d8228c2..73a89a7a3e 100644 --- a/firmware/controllers/algo/engine_state.h +++ b/firmware/controllers/algo/engine_state.h @@ -16,7 +16,6 @@ #define BRAIN_PIN_COUNT (1 << sizeof(brain_pin_e)) - class EngineState : public engine_state2_s { public: EngineState(); @@ -87,7 +86,6 @@ public: float currentRawVE = 0; int vssEventCounter = 0; - int totalLoggedBytes = 0; /** diff --git a/firmware/hw_layer/mmc_card.cpp b/firmware/hw_layer/mmc_card.cpp index 1a750e28cd..9393c080bc 100644 --- a/firmware/hw_layer/mmc_card.cpp +++ b/firmware/hw_layer/mmc_card.cpp @@ -28,6 +28,10 @@ #include "rtc_helper.h" +#if EFI_TUNER_STUDIO +extern TunerStudioOutputChannels tsOutputChannels; +#endif + #define SD_STATE_INIT "init" #define SD_STATE_MOUNTED "MOUNTED" #define SD_STATE_MOUNT_FAILED "MOUNT_FAILED" @@ -44,7 +48,10 @@ EXTERN_ENGINE; #define F_SYNC_FREQUENCY 100 +static int totalLoggedBytes = 0; static int writeCounter = 0; +static int totalWritesCounter = 0; +static int totalSyncCounter = 0; #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 static MMCConfig mmccfg = { NULL, &ls_spicfg, &hs_spicfg }; -#define FILE_LOG_MIN_DELAY 3 - /** * fatfs MMC/SPI */ @@ -133,7 +138,7 @@ static void sdStatistics(void) { scheduleMsg(&logger, "SD enabled=%s status=%s", boolToString(CONFIGB(isSdCardEnabled)), sdStatus); 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); return; } - writeCounter++; - 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; - } + f_sync(&FDLogFile); fs_ready = true; // everything Ok unlockSpi(); } @@ -320,14 +317,34 @@ void appendToLog(const char *line) { return; } UINT lineLength = strlen(line); - engine->engineState.totalLoggedBytes += lineLength; + totalLoggedBytes += lineLength; lockSpi(SPI_NONE); FRESULT err = f_write(&FDLogFile, line, lineLength, &bytesWritten); if (bytesWritten < lineLength) { 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(); + + 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; } - if (isSdCardAlive()) + if (isSdCardAlive()) { writeLogLine(); + } else { + chThdSleepMilliseconds(100); + } - int periodMs = maxI(boardConfiguration->sdCardPeriodMs, FILE_LOG_MIN_DELAY); - chThdSleepMilliseconds(periodMs); + if (boardConfiguration->sdCardPeriodMs > 0) { + chThdSleepMilliseconds(boardConfiguration->sdCardPeriodMs); + } } } @@ -447,10 +468,6 @@ bool isSdCardAlive(void) { } void initMmcCard(void) { - // temporary value while we migrate - if (boardConfiguration->sdCardPeriodMs == 0) - boardConfiguration->sdCardPeriodMs = 50; - logName[0] = 0; addConsoleAction("sdinfo", sdStatistics); if (!CONFIGB(isSdCardEnabled)) {