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;
205#define RUSEFI_LOG_PREFIX "re_"
207#define SHORT_TIME_LEN 13
209#define FILE_LIST_MAX_COUNT 20
225#if MMC_USE_MUTUAL_EXCLUSION == TRUE
227#define UNLOCK_SD_SPI()
229#define LOCK_SD_SPI() lockSpi(mmcSpiDevice)
230#define UNLOCK_SD_SPI() unlockSpi(mmcSpiDevice)
251 "FR_DISK_ERR: A hard error occurred in the low level disk I/O layer",
252 "FR_INT_ERR: Assertion failed",
253 "FR_NOT_READY: The physical drive cannot work",
254 "FR_NO_FILE: Could not find the file",
255 "FR_NO_PATH: Could not find the path",
256 "FR_INVALID_NAME: The path name format is invalid",
257 "FR_DENIED: Access denied due to prohibited access or directory full",
258 "FR_EXIST: Access denied due to prohibited access",
259 "FR_INVALID_OBJECT: The file/directory object is invalid",
260 "FR_WRITE_PROTECTED: The physical drive is write protected",
261 "FR_INVALID_DRIVE: The logical drive number is invalid",
262 "FR_NOT_ENABLED: The volume has no work area",
263 "FR_NO_FILESYSTEM: There is no valid FAT volume",
264 "FR_MKFS_ABORTED: The f_mkfs() aborted due to any problem",
265 "FR_TIMEOUT: Could not get a grant to access the volume within defined period",
266 "FR_LOCKED: The operation is rejected according to the file sharing policy",
267 "FR_NOT_ENOUGH_CORE: LFN working buffer could not be allocated",
268 "FR_TOO_MANY_OPEN_FILES: Number of open files > FF_FS_LOCK",
269 "FR_INVALID_PARAMETER: Given parameter is invalid"
274 static int fatFsErrors = 0;
276 if (fatFsErrors++ > 16) {
281 efiPrintf(
"%s FATfs Error %d %s", str, f_error, f_error <= FR_INVALID_PARAMETER ?
fatErrors[f_error] :
"unknown");
289 #define FATFS_CLUSTER_SIZE 1024
311#if HAL_USE_MMC_SPI && (defined(STM32F4XX) || defined(STM32F7XX))
324 if (strcmp(mode,
"pc") == 0) {
326 }
else if (strcmp(mode,
"ecu") == 0) {
329 efiPrintf(
"Invalid mode %s allowed modes pc and ecu", mode);
334 strcpy(
logName, RUSEFI_LOG_PREFIX);
341 ptr = &
logName[PREFIX_LEN + SHORT_TIME_LEN];
347 strcat(ptr,
".teeth");
349 strcat(ptr, DOT_MLG);
364 memset(
fd, 0,
sizeof(FIL));
367 efiPrintf(
"starting log file %s",
logName);
369 FRESULT err = f_open(
fd,
logName, FA_CREATE_ALWAYS | FA_WRITE);
370 if (err == FR_EXIST) {
384#ifdef LOGGER_MAX_FILE_SIZE
386 err = f_expand(
fd, LOGGER_MAX_FILE_SIZE, 1);
401#ifdef LOGGER_MAX_FILE_SIZE
415 efiPrintf(
"SD card should be mounted to ECU");
469 if (blkConnect(&
MMCD1) != HAL_SUCCESS) {
476 return reinterpret_cast<BaseBlockDevice*
>(&
MMCD1);
480 blkDisconnect(&
MMCD1);
490 #define RE_SDC_MODE SDC_MODE_4BIT
494 .bus_width = RE_SDC_MODE,
508 if (blkConnect(&EFI_SDC_DEVICE) != HAL_SUCCESS) {
512 return reinterpret_cast<BaseBlockDevice*
>(&EFI_SDC_DEVICE);
516 blkDisconnect(&EFI_SDC_DEVICE);
517 sdcStop(&EFI_SDC_DEVICE);
535 return usbResult == MSG_OK;
544 if (hasFirmwareError()) {
574 memset(&resources, 0x00,
sizeof(resources));
576 memset(&
MMC_FS, 0,
sizeof(FATFS));
577 ret = (f_mount(&
MMC_FS,
"", 1) == FR_OK);
581 efiPrintf(
"SD card mount failed!");
587 efiPrintf(
"SD card mounted!");
590#if EFI_STORAGE_SD == TRUE
609#if EFI_STORAGE_SD == TRUE
624 efiPrintf(
"SD card unmounted");
688 chThdSleepMilliseconds(100);
692#ifdef LOGGER_MAX_FILE_SIZE
696 if (
logBuffer.writen() + ret > LOGGER_MAX_FILE_SIZE) {
739 FRESULT ret = f_mkfs(
"",
nullptr, resources.formatBuff,
sizeof(resources.formatBuff));
746 ret = f_setlabel(SD_CARD_LABEL);
759 return (ret ?
false :
true);
781 efiPrintf(
"Invalid SD card thread state: %d",
static_cast<int>(from));
856 chThdSleepMilliseconds(100);
894 chRegSetThreadName(
"SD Card Logger");
898 chThdSleepMilliseconds(100);
903 efiPrintf(
"Card is not preset/failed to init");
915#if EFI_STORAGE_SD == TRUE
917 chThdSleepMilliseconds(1000);
940 efiPrintf(
"SD logger has died!");
944 chThdSleepMilliseconds(100);
952#if EFI_PROD_CODE && !defined(EFI_SDC_DEVICE)
970 }
else if (freq < 1) {
975 chThdSleepUntilWindowed(before, before +
period);
988 logBuffer.write(
reinterpret_cast<const char*
>(
buffer->buffer), toWrite);
1034 chThdCreateStatic(mmcThreadStack,
sizeof(mmcThreadStack), PRIO_MMC, (tfunc_t)(
void*) MMCmonThread, NULL);
1044 efiPrintf(
"sdCardRequestMode %d", (
int)mode);
1056 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 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 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)
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)
SD_MODE sdCardGetCurrentMode(void)
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 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 void sdLoggerStart(void)
static void sdLoggerStop(void)
static SD_MODE sdTargetMode
void errorHandlerWriteReportFile(FIL *fd)
static bool sdLoggerIsReady(void)
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