42 #define SD_STATE_INIT "init"
43 #define SD_STATE_MOUNTED "MOUNTED"
44 #define SD_STATE_MOUNT_FAILED "MOUNT_FAILED"
45 #define SD_STATE_OPEN_FAILED "OPEN_FAILED"
46 #define SD_STATE_SEEK_FAILED "SEEK_FAILED"
47 #define SD_STATE_NOT_INSERTED "NOT_INSERTED"
48 #define SD_STATE_CONNECTING "CONNECTING"
49 #define SD_STATE_MSD "MSD"
50 #define SD_STATE_NOT_CONNECTED "NOT_CONNECTED"
51 #define SD_STATE_MMC_FAILED "MMC_CONNECT_FAILED"
57 #define F_SYNC_FREQUENCY 10
65 #define LOG_INDEX_FILENAME "index.txt"
67 #define RUSEFI_LOG_PREFIX "re_"
69 #define SHORT_TIME_LEN 13
71 #define LS_RESPONSE "ls_result"
72 #define FILE_LIST_MAX_COUNT 20
83 #if MMC_USE_MUTUAL_EXCLUSION == TRUE
85 #define UNLOCK_SD_SPI()
87 #define LOCK_SD_SPI() lockSpi(mmcSpiDevice)
88 #define UNLOCK_SD_SPI() unlockSpi(mmcSpiDevice)
113 efiPrintf(
"FATfs Error \"%s\" %d", str, f_error);
119 #define MIN_FILE_INDEX 10
142 memset(&FDLogFile, 0,
sizeof(FIL));
143 FRESULT err = f_open(&FDLogFile, LOG_INDEX_FILENAME, FA_READ);
145 char data[_MAX_FILLER];
147 if (err != FR_OK && err != FR_EXIST) {
149 efiPrintf(
"%s: not found or error: %d", LOG_INDEX_FILENAME, err);
151 f_read(&FDLogFile, (
void*)data,
sizeof(data), &result);
153 efiPrintf(
"Got content [%s] size %d", data, result);
168 err = f_open(&FDLogFile, LOG_INDEX_FILENAME, FA_OPEN_ALWAYS | FA_WRITE);
170 f_write(&FDLogFile, (
void*)data, strlen(data), &result);
176 strcpy(
logName, RUSEFI_LOG_PREFIX);
187 ptr = &
logName[PREFIX_LEN + SHORT_TIME_LEN];
193 strcat(ptr,
".teeth");
195 strcat(ptr, DOT_MLG);
206 memset(&FDLogFile, 0,
sizeof(FIL));
209 FRESULT err = f_open(&FDLogFile,
logName, FA_OPEN_ALWAYS | FA_WRITE);
210 if (err != FR_OK && err != FR_EXIST) {
217 err = f_lseek(&FDLogFile, f_size(&FDLogFile));
230 efiPrintf(
"Error: No File system is mounted");
239 const char *us1 = (
const char *)s1;
240 const char *us2 = (
const char *)s2;
256 efiPrintf(
"Error: No File system is mounted");
261 FRESULT res = f_opendir(&dir, path);
264 efiPrintf(
"Error opening directory %s", path);
268 efiPrintf(LS_RESPONSE);
270 for (
int count = 0;count < FILE_LIST_MAX_COUNT;) {
273 res = f_readdir(&dir, &fno);
274 if (res != FR_OK || fno.fname[0] == 0) {
277 if (fno.fname[0] ==
'.') {
280 if ((fno.fattrib & AM_DIR) ||
mystrncasecmp(RUSEFI_LOG_PREFIX, fno.fname,
sizeof(RUSEFI_LOG_PREFIX) - 1)) {
283 efiPrintf(
"logfile%lu:%s", fno.fsize, fno.fname);
299 efiPrintf(
"Error: No File system is mounted. \"mountsd\" first");
306 blkDisconnect(&
MMCD1);
310 #ifdef EFI_SDC_DEVICE
311 blkDisconnect(&EFI_SDC_DEVICE);
312 sdcStop(&EFI_SDC_DEVICE);
315 memset(&FDLogFile, 0,
sizeof(FIL));
317 efiPrintf(
"MMC/SD card removed");
337 if (hasFirmwareError()) {
361 mmcObjectInit(&
MMCD1);
367 if (blkConnect(&
MMCD1) != HAL_SUCCESS) {
374 return reinterpret_cast<BaseBlockDevice*
>(&
MMCD1);
379 #define RE_SDC_MODE SDC_MODE_4BIT
383 #ifdef EFI_SDC_DEVICE
395 if (blkConnect(&EFI_SDC_DEVICE) != HAL_SUCCESS) {
400 return reinterpret_cast<BaseBlockDevice*
>(&EFI_SDC_DEVICE);
418 bool hasUsb = usbResult == MSG_OK;
422 if (cardBlockDevice && hasUsb) {
433 if (!cardBlockDevice) {
438 memset(&
MMC_FS, 0,
sizeof(FATFS));
439 if (f_mount(&
MMC_FS,
"/", 1) == FR_OK) {
444 efiPrintf(
"MMC/SD mounted!");
460 FRESULT err = f_write(&FDLogFile,
buffer, count, &bytesWritten);
462 if (bytesWritten != count) {
501 : m_stream(
"rusefi_simulator_log.mlg", std::ios::binary | std::ios::trunc)
507 m_stream.write(
buffer, count);
513 std::ofstream m_stream;
529 chRegSetThreadName(
"MMC Card Logger");
531 #if HW_HELLEN && EFI_PROD_CODE
535 chThdSleepMilliseconds(100);
538 efiPrintf(
" *** turning board ON to power SD card ***");
543 chThdSleepMilliseconds(300);
566 #if EFI_PROD_CODE && !defined(EFI_SDC_DEVICE)
583 }
else if (freq < 1) {
588 chThdSleepMicroseconds((
int)
period);
626 chThdCreateStatic(mmcThreadStack,
sizeof(mmcThreadStack), PRIO_MMC, (tfunc_t)(
void*) MMCmonThread, NULL);
void writeSdLogLine(Writer &bufferedWriter)
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)
char * itoa10(char *p, int num)
const char * boolToString(bool value)
int mytolower(const char c)
efitimesec_t getTimeNowS()
Current system time in seconds (32 bits)
bool warning(ObdCode code, const char *fmt,...)
SPIDriver * getSpiDevice(spi_device_e spiDevice)
void printSpiConfig(const char *msg, spi_device_e device)
void hellenEnableEn(const char *msg)
bool getHellenBoardEnabled()
void attachMsdSdCard(BaseBlockDevice *blkdev)
static THD_FUNCTION(MMCmonThread, arg)
static void sdStatistics()
static spi_device_e mmcSpiDevice
static chibios_rt::BinarySemaphore usbConnectedSemaphore(true)
static const char * sdStatus
static int fileCreatedCounter
static void sdTriggerLogger()
static NO_CACHE SdLogBufferWriter logBuffer
static THD_WORKING_AREA(mmcThreadStack, 3 *UTILITY_THREAD_STACK_SIZE)
static void printError(const char *str, FRESULT f_error)
static const SDCConfig sdcConfig
void onUsbConnectedNotifyMmcI()
int mystrncasecmp(const char *s1, const char *s2, size_t n)
static void incLogFileName()
static void listDirectory(const char *path)
static char logName[_MAX_FILLER+20]
static void removeFile(const char *pathx)
static void prepareLogFileName()
static int totalSyncCounter
static void setSdCardReady(bool value)
static int totalWritesCounter
static BaseBlockDevice * initializeMmcBlockDevice()
static FIL FDLogFile NO_CACHE
static void createLogFile()
Create a new file with the specified name.
static NO_CACHE FATFS MMC_FS
static void printMmcPinout()
@ CUSTOM_ERR_SD_MOUNT_FAILED
@ CUSTOM_ERR_SD_SEEK_FAILED
engine_configuration_s * engineConfiguration
const char * hwPortname(brain_pin_e brainPin)
bool isBrainPinValid(brain_pin_e brainPin)
bool dateToStringShort(char *lcd_str)
uint16_t sdCardLogFrequency
spi_device_e sdCardSpiDevice
CompositeBuffer * GetToothLoggerBufferBlocking()
void ReturnToothLoggerBuffer(CompositeBuffer *buffer)
static BigBufferHandle buffer