I'm novice in this OS and desk STM32F4Diskovery and ask your help.
I write a LOG module for save my logs to MMC card over SPI2 (target twice per second).
I learned a lot of samples (OS, Japan guru, other) and found good feedback about MMC over CSD or MMC over SPI in DMA mode.
What i have now:
Code: Select all
// Peripherial Clock 42MHz SPI2 SPI3
// Peripherial Clock 84MHz SPI1 SPI1 SPI2/3
#define SPI_BaudRatePrescaler_2 ((uint16_t)0x0000) // 42 MHz 21 MHZ
#define SPI_BaudRatePrescaler_4 ((uint16_t)0x0008) // 21 MHz 10.5 MHz
#define SPI_BaudRatePrescaler_8 ((uint16_t)0x0010) // 10.5 MHz 5.25 MHz
#define SPI_BaudRatePrescaler_16 ((uint16_t)0x0018) // 5.25 MHz 2.626 MHz
#define SPI_BaudRatePrescaler_32 ((uint16_t)0x0020) // 2.626 MHz 1.3125 MHz
#define SPI_BaudRatePrescaler_64 ((uint16_t)0x0028) // 1.3125 MHz 656.25 KHz
#define SPI_BaudRatePrescaler_128 ((uint16_t)0x0030) // 656.25 KHz 328.125 KHz
#define SPI_BaudRatePrescaler_256 ((uint16_t)0x0038) // 328.125 KHz 164.06 KHz
static SPIConfig hs_spicfg = { NULL, SPI_SD_MODULE_PORT, SPI_SD_MODULE_PIN,
SPI_BaudRatePrescaler_4 };
static SPIConfig ls_spicfg = { NULL, SPI_SD_MODULE_PORT, SPI_SD_MODULE_PIN,
SPI_BaudRatePrescaler_8 };
/* MMC/SD over SPI driver configuration.*/
// don't forget check if STM32_SPI_USE_SPI2 defined and spi has init with correct GPIO in hardware.c
static MMCConfig mmccfg = { &SPID2, &ls_spicfg, &hs_spicfg };
void initMmcCard(void) {
memset(&errorlineReported, '\0', MAX_LOG_STRING_SIZE); // zero flash line buffer
mmcObjectInit(&MMCD1); // Initializes an MMC instance.
mmcStart(&MMCD1, &mmccfg); // Configures and activates the MMC peripheral.
addConsoleAction("mmc_mount", &MMCmount);
addConsoleActionS("mmc_appendToLog", &appendToLog);
addConsoleAction("mmc_umount", &MMCumount);
addConsoleAction("mmc_ls", &ff_cmd_dir);
addConsoleAction("mmc_stat", &ff_cmd_stat);
}function umount & mount
Code: Select all
/*
* MMC card umount.
*/
static void MMCumount(void) {
if (!fs_ready) {
print("Error: No File system is mounted. \"mmc_mount\" first.\r\n");
return;
}
f_close(&FDLogFile); // close file
mmcDisconnect(&MMCD1); // Brings the driver in a state safe for card removal.
f_mount(NULL, fDisk, 0); // FATFS: Unregister work area prior to discard it
memset(&FDLogFile, 0, sizeof(FIL)); // clear FDLogFile
fs_ready = FALSE; // status = false
print("MMC/SD card removed.\r\n");
}
/*
* MMC card mount.
*/
static void MMCmount(void) {
if (fs_ready) {
print("Error: Already mounted. \"mmc_umount\" first\r\n");
return;
}
// start to initialize MMC/SD
if(MMCD1.state == BLK_ACTIVE) {
mmcConnect(&MMCD1);
chThdSleepMilliseconds(100);
}
if (MMCD1.state == BLK_READY) { // Performs the initialization procedure on the inserted card.
memset(&MMC_FS, 0, sizeof(FATFS)); // clear the memory
FRESULT err = f_mount(&MMC_FS, fDisk, 0); // Initialize logical drive
if (err == FR_OK) { // if Ok - FS now mounted
memset(&FDLogFile, 0, sizeof(FIL)); // clear the memory
err = f_open(&FDLogFile, LogFileName, FA_WRITE | FA_OPEN_ALWAYS ); // open for write & Create if not exist
if (err == FR_OK || err == FR_EXIST) {
err = f_lseek(&FDLogFile, f_size(&FDLogFile)); // SEEK to end of the file to append data
if (err) {
printError("Seek error", err);
return;
}
fs_ready = TRUE; // everything Ok
print("MMC/SD card inserted and mounted success.\r\nDon't forget umountsd before remove to prevent lost your data.\r\n");
return;
} else printError("Can't create Log file, check your SD.\r\nFS mount failed", err); // else - show error
} else printError("FatFs mount", err);
} else printError("MMC Connect", MMCD1.state);
}and last function - append
Code: Select all
/**
* @brief Appends specified line to the current log file
*/
void appendToLog(char *line) {
UINT bytesWrited;
if (!fs_ready) {
print("appendToLog Error: No File system is mounted.\r\n");
return;
}
size_t line_len = strlen(line);
if (line_len == 0 || line_len > MAX_LOG_STRING_SIZE-2) {
printError("appendToLog Error: String length zero or more then maximum-2 allowed = \r\n",line_len);
return;
}
if (strncmp(errorlineReported, line, line_len) != 0) { // current string to log and last string to log is not equal
strncpy(errorlineReported, line, line_len); // save for next compare
strcat(errorlineReported, "\r\n");
FRESULT err = f_write(&FDLogFile, line, line_len+2, &bytesWrited); // write to file
if (bytesWrited < line_len+2) {
printError("write error or disk full", err); // error or disk full
}
f_sync(&FDLogFile); // sync
}
}What i have in manual mode:
mmc_mount
FATfs Error "MMC Connect" 2
mmc_mount
MMC/SD card inserted and mounted success.
Don't forget umountsd before remove to prevent lost your data.
mmc_ls
----A 1980/01/01 00:00 190 rusefi.log
mmc_appendToLog 1235
mmc_ls
----A 1980/01/01 00:00 196 rusefi.log
mmc_umount
MMC/SD card removed.
1. Why mmc_mount working success from second trials. Other trials working fine.
But when i start periodically appendToLog - i receive unstable board running and panic.
2. I think problem with speed (SPI, MMC, so on). Please advice how to resolve.