Data log in SD card using STM32 Microcontroller

Thread Starter

Jagathreddy

Joined Feb 2, 2024
1
[CODE] tags added by moderator.

Hello Team,
I am implementing data logging on an SD card using the SPI interface and FATFS libraries, with reference to the SD card libraries provided by Controllers Tech (https://controllerstech.com/sd-card-using-spi-in-s…). While I have achieved a logging speed of around 58 Hz, I am encountering data loss issues, particularly at higher logging speeds.

Function I used for SD card logging :

Data Logger:
void HandleState(void) {
uint8_t path[20];
switch (currentState) {
case STATE_INIT:
if (f_mount(&fs, “0:/”, 1) == FR_OK) {
int fileNumber = 0;
do {
sprintf((char *)path, “log_%d.csv”, fileNumber++);
} while (f_stat((char *)path, NULL) == FR_OK);

if (f_open(&file, (char *)path, FA_CREATE_ALWAYS | FA_WRITE | FA_READ) == FR_OK) {
strcpy(buffer, “Log SD card\\\\n”);
f_write(&file, buffer, sizeof(buffer), NULL);
currentState = STATE_LOG_DATA;
} else {
Error_Handler();
}
} else {
Error_Handler();
}
break;

case STATE_LOG_DATA:

sprintf(buffer, “%lu, %.2f, %d\\\\n”, HAL_GetTick(), temperature, counter++); // current time in milliseconds since the start of the program

f_write(&file, buffer, sizeof(buffer), NULL);
f_sync(&file) ;

break;

default:
break;
}
}
I would appreciate any suggestions or improvements to enhance the reliability of the implementation on the microcontroller.

Thanks in advance.
 
Last edited by a moderator:

Ya’akov

Joined Jan 27, 2019
9,167
Welcome to AAC.

Please use [CODE] tags when posting code. The formatting and syntax highlighting make reading easier, and the line numbers make referring to parts of the code practical.

You can find an [CODE] tag editor in the Insert Dropdown of the post editor toolbar at the top of the editor window.

1706869775379.png
Good luck with your project, and once again—welcome, thanks for joining us.
 
Top