27#define LOGGER_MAX_FILE_SIZE (32 * 1024 * 1024)
30#define F_SYNC_FREQUENCY 10
46#if EFI_STORAGE_SD == TRUE
58 efiPrintf(
"SD logger already started!");
80 return totalLoggedBytes;
84 if ((!m_fd) || (failed)) {
90 FRESULT err = f_write(m_fd,
buffer,
count, &bytesWritten);
96 }
else if (bytesWritten !=
count) {
102 totalLoggedBytes +=
count;
103 if (writeCounter >= F_SYNC_FREQUENCY) {
119 size_t totalLoggedBytes = 0;
120 size_t writeCounter = 0;
132 : m_stream(
"rusefi_simulator_log.mlg", std::ios::binary | std::ios::trunc)
144 std::ofstream m_stream;
200#define RUSEFI_LOG_PREFIX "re_"
202#define SHORT_TIME_LEN 13
204#define FILE_LIST_MAX_COUNT 20
226#if MMC_USE_MUTUAL_EXCLUSION == TRUE
228#define UNLOCK_SD_SPI()
230#define LOCK_SD_SPI() lockSpi(mmcSpiDevice)
231#define UNLOCK_SD_SPI() unlockSpi(mmcSpiDevice)
252 "FR_DISK_ERR: A hard error occurred in the low level disk I/O layer",
253 "FR_INT_ERR: Assertion failed",
254 "FR_NOT_READY: The physical drive cannot work",
255 "FR_NO_FILE: Could not find the file",
256 "FR_NO_PATH: Could not find the path",
257 "FR_INVALID_NAME: The path name format is invalid",
258 "FR_DENIED: Access denied due to prohibited access or directory full",
259 "FR_EXIST: Access denied due to prohibited access",
260 "FR_INVALID_OBJECT: The file/directory object is invalid",
261 "FR_WRITE_PROTECTED: The physical drive is write protected",
262 "FR_INVALID_DRIVE: The logical drive number is invalid",
263 "FR_NOT_ENABLED: The volume has no work area",
264 "FR_NO_FILESYSTEM: There is no valid FAT volume",
265 "FR_MKFS_ABORTED: The f_mkfs() aborted due to any problem",
266 "FR_TIMEOUT: Could not get a grant to access the volume within defined period",
267 "FR_LOCKED: The operation is rejected according to the file sharing policy",
268 "FR_NOT_ENOUGH_CORE: LFN working buffer could not be allocated",
269 "FR_TOO_MANY_OPEN_FILES: Number of open files > FF_FS_LOCK",
270 "FR_INVALID_PARAMETER: Given parameter is invalid"
275 static int fatFsErrors = 0;
277 if (fatFsErrors++ > 16) {
282 efiPrintf(
"%s FATfs Error %d %s", str, f_error, f_error <= FR_INVALID_PARAMETER ?
fatErrors[f_error] :
"unknown");
290 #define FATFS_CLUSTER_SIZE 1024
317 #if defined(STM32F4XX) || defined(STM32F7XX)
321 efiPrintf(
"not implemented");
324 efiPrintf(
"SDIO mode");
335 if (strcmp(mode,
"pc") == 0) {
337 }
else if (strcmp(mode,
"ecu") == 0) {
340 efiPrintf(
"Invalid mode %s allowed modes pc and ecu", mode);
345 strcpy(
logName, RUSEFI_LOG_PREFIX);
352 ptr = &
logName[PREFIX_LEN + SHORT_TIME_LEN];
358 strcat(ptr,
".teeth");
360 strcat(ptr, DOT_MLG);
375 memset(
fd, 0,
sizeof(FIL));
378 efiPrintf(
"starting log file %s",
logName);
380 FRESULT err = f_open(
fd,
logName, FA_CREATE_ALWAYS | FA_WRITE);
381 if (err == FR_EXIST) {
395#ifdef LOGGER_MAX_FILE_SIZE
397 err = f_expand(
fd, LOGGER_MAX_FILE_SIZE, 1);
412#ifdef LOGGER_MAX_FILE_SIZE
426 efiPrintf(
"SD card should be mounted to ECU");
480 if (blkConnect(&
MMCD1) != HAL_SUCCESS) {
487 return reinterpret_cast<BaseBlockDevice*
>(&
MMCD1);
491 blkDisconnect(&
MMCD1);
501 #define RE_SDC_MODE SDC_MODE_4BIT
505 .bus_width = RE_SDC_MODE,
519 if (blkConnect(&EFI_SDC_DEVICE) != HAL_SUCCESS) {
523 return reinterpret_cast<BaseBlockDevice*
>(&EFI_SDC_DEVICE);
527 blkDisconnect(&EFI_SDC_DEVICE);
528 sdcStop(&EFI_SDC_DEVICE);
546 return usbResult == MSG_OK;
555 if (hasFirmwareError()) {
580 FRESULT ret = FR_NOT_READY;
585 memset(&resources, 0x00,
sizeof(resources));
587 memset(&
MMC_FS, 0,
sizeof(FATFS));
588 ret = f_mount(&
MMC_FS,
"", 1);
595 efiPrintf(
"SD card mounted!");
599#if EFI_STORAGE_SD == TRUE
611 return (ret == FR_OK);
621#if EFI_STORAGE_SD == TRUE
637 efiPrintf(
"SD card unmounted");
701 chThdSleepMilliseconds(100);
705#ifdef LOGGER_MAX_FILE_SIZE
709 if (
logBuffer.writen() + ret > LOGGER_MAX_FILE_SIZE) {
752 FRESULT ret = f_mkfs(
"",
nullptr, resources.formatBuff,
sizeof(resources.formatBuff));
759 ret = f_setlabel(SD_CARD_LABEL);
772 return (ret ?
false :
true);
794 efiPrintf(
"Invalid SD card thread state: %d",
static_cast<int>(from));
869 chThdSleepMilliseconds(100);
907 chRegSetThreadName(
"SD Card Logger");
911 chThdSleepMilliseconds(100);
916 efiPrintf(
"Card is not preset/failed to init");
928#if EFI_STORAGE_SD == TRUE
930 chThdSleepMilliseconds(1000);
953 efiPrintf(
"SD logger has died!");
957 chThdSleepMilliseconds(100);
965#if EFI_PROD_CODE && !defined(EFI_SDC_DEVICE)
983 }
else if (freq < 1) {
988 chThdSleepUntilWindowed(before, before +
period);
1001 logBuffer.write(
reinterpret_cast<const char*
>(
buffer->buffer), toWrite);
1049 chThdCreateStatic(mmcThreadStack,
sizeof(mmcThreadStack), PRIO_MMC, (tfunc_t)(
void*) MMCmonThread, NULL);
1059 efiPrintf(
"sdCardRequestMode %d", (
int)mode);
1071 efiPrintf(
"SD card should be mounted to ECU");
int spiCalcClockDiv(SPIDriver *, SPIConfig *, unsigned int)
int spiGetBaseClock(SPIDriver *)
virtual size_t writeInternal(const char *buffer, size_t count)=0
TunerStudioOutputChannels outputChannels
void addConsoleActionS(const char *token, VoidCharPtr callback)
void addConsoleAction(const char *token, Void callback)
Register console action without parameters.
ioportid_t getHwPort(const char *msg, brain_pin_e brainPin)
ioportmask_t getHwPin(const char *msg, brain_pin_e brainPin)
const char * boolToString(bool value)
char * itoa10(char *p, int num)
static EngineAccessor engine
static constexpr engine_configuration_s * engineConfiguration
bool warning(ObdCode code, const char *fmt,...)
void printSpiConfig(const char *msg, spi_device_e device)
SPIDriver * getSpiDevice(spi_device_e spiDevice)
void deattachMsdSdCard(void)
void attachMsdSdCard(BaseBlockDevice *blkdev, uint8_t *blkbuf, size_t blkbufsize)
static THD_FUNCTION(MMCmonThread, arg)
static bool sdLoggerIsReady()
static bool sdLoggerFailed
static int sdTriggerLogger()
static void sdStatistics()
PUBLIC_API_WEAK bool boardSdCardEnable()
static void deinitializeMmcBlockDevide()
int errorHandlerCheckReportFiles()
static spi_device_e mmcSpiDevice
static chibios_rt::BinarySemaphore usbConnectedSemaphore(true)
static int sdReportStorageInit()
BYTE formatBuff[FATFS_CLUSTER_SIZE]
static int sdModeExecuter()
static NO_CACHE SdLogBufferWriter logBuffer
void sdCardRemoveReportFiles()
static bool sdLoggerReady
static void sdLoggerStart()
static int sdModeSwitcher()
static int sdLogger(FIL *fd)
void updateSdCardLiveFlags()
static bool isSdCardEnabled()
void sdCardRequestMode(SD_MODE mode)
static void sdLoggerCloseFile(FIL *fd)
static const char * sdStatusName(SD_STATUS status)
SD_MODE sdCardGetCurrentMode()
static union @47 NO_CACHE
static THD_WORKING_AREA(mmcThreadStack, 3 *UTILITY_THREAD_STACK_SIZE)
static NO_CACHE uint8_t mmcbuf[MMC_BUFFER_SIZE]
static const char * fatErrors[]
static bool sdNeedRemoveReports
static int sdModeSwitchToIdle(SD_MODE from)
static const SDCConfig sdcConfig
static SD_STATUS sdStatus
void onUsbConnectedNotifyMmcI()
void errorHandlerDeleteReports()
static void sdLoggerSetReady(bool value)
static const char * sdStatusNames[]
static void sdSetMode(const char *mode)
static bool sdLoggerInitDone
static char logName[_MAX_FILLER+20]
void printFatFsError(const char *str, FRESULT f_error)
static void removeFile(const char *pathx)
static void prepareLogFileName()
static void sdLoggerStop()
static int sdLoggerCreateFile(FIL *fd)
Create a new file with the specified name.
PUBLIC_API_WEAK bool boardSdCardDisable()
uint8_t blkbuf[4 *MMCSD_BLOCK_SIZE]
static SD_MODE sdTargetMode
void errorHandlerWriteReportFile(FIL *fd)
static BaseBlockDevice * initializeMmcBlockDevice()
static NO_CACHE FATFS MMC_FS
static BaseBlockDevice * cardBlockDevice
static void printMmcPinout()
void incLogFileName(FIL *fd)
int getSdCardFieldsCount()
size_t writeSdLogLine(Writer &bufferedWriter)
@ CUSTOM_ERR_SD_MOUNT_FAILED
const char * hwPortname(brain_pin_e brainPin)
bool isBrainPinValid(brain_pin_e brainPin)
bool dateToStringShort(char *destination)
Storage interface to FatFS header.
uint16_t sdCardLogFrequency
spi_device_e sdCardSpiDevice
void DisableToothLogger()
void ReturnToothLoggerBuffer(CompositeBuffer *buffer)
CompositeBuffer * GetToothLoggerBufferBlocking()
static BigBufferHandle buffer