The issue is that I am able to create a bin file, write data into the SD-Card properly. But when I read data, it is not proper, and the data is corrupted. It worked well previously but the same code is not working now. Could you please check where I am wrong. I am using STM32L496ZGT6. I am using a Micro SD Card adapter.
WRITING FUNCTION
void push_to_stack(data *newData) {
fres = f_mount(&fs, "", 0);
if (fres == FR_OK) {
transmit_uart("Micro SD card is mounted successfully for writing!\n");
} else if (fres != FR_OK) {
transmit_uart("Micro SD card's mount error!\n");
}
fres = f_open(&fil, "log-file.bin", FA_WRITE| FA_OPEN_ALWAYS );
if (fres != FR_OK) {
transmit_uart("Failed to open file for writing.\n");
f_mount(NULL, "", 1); // Unmount before returning
}
fres = f_lseek(&fil, f_size(&fil));
if (fres != FR_OK) {
transmit_uart("Error seeking to the end of the file.\n");
f_close(&fil); // Close the file if seek fails
f_mount(NULL, "", 1); // Unmount before returning
return;
}
// Write the structure to the file
fres = f_write(&fil, newData, sizeof(data), &bytesWritten);
if (fres == FR_OK && bytesWritten == sizeof(data)) {
transmit_uart("Data successfully written to stack.\n");
} else {
transmit_uart("Error writing data to stack.\n");
}
fres = f_close(&fil);
if (fres == FR_OK) {
transmit_uart("The file is closed.\n");
} else if (fres != FR_OK) {
transmit_uart("The file was not closed.\n");
}
f_mount(NULL, "",1);
}
READ FUNCTION
void read_from_stack_and_send(uint32_t index) {
// Mount the file system
fres = f_mount(&fs, "", 0);
if (fres != FR_OK) {
transmit_uart("Failed to mount SD card.\n");
return;
} else {
transmit_uart("SD card mounted successfully for reading.\n");
}
// Open file in read mode
fres = f_open(&fil, "log-file.bin", FA_READ);
if (fres != FR_OK) {
transmit_uart("Failed to open file for reading.\n");
return;
} else {
transmit_uart("File opened for reading successfully.\n");
}
fres = f_lseek(&fil,index * sizeof(data));
if(fres != FR_OK){
transmit_uart("Error reading data from file.\n");
f_close(&fil); // Close the file if seek fails
f_mount(NULL, "", 1); // Unmount before returning
}
// Read data sequentially
data readData;
UINT bytesRead;
// while (1) {
// Read a single `data` structure from the file
memset(&readData, 0, sizeof(data));
fres = f_read(&fil, &readData, sizeof(data), &bytesRead);
if (fres != FR_OK) {
transmit_uart("Error reading data from file.\n");
}
// Check if end of file reached
if (bytesRead == 0) {
transmit_uart("End of file reached.\n");
}
// Print the read data
char msg[100];
sprintf(msg, "ID: %d, Value: %d, Name: %s\n", readData.ID, readData.value, readData.ename);
transmit_uart(msg);
// }
// Close the file
fres = f_close(&fil);
if (fres != FR_OK) {
transmit_uart("Failed to close the file.\n");
} else {
transmit_uart("File closed successfully.\n");
}
// Unmount the file system
fres = f_mount(NULL, "", 1);
if (fres != FR_OK) {
transmit_uart("Failed to unmount SD card.\n");
} else {
transmit_uart("SD card unmounted successfully.\n");
}
}
MAIN FUNCTION
memset(buffer,0,sizeof(buffer));
HAL_UART_Receive(&huart3, (uint8_t *)buffer, 100, 100);
buffer[100]='\0';
if(sscanf((char *)buffer,"UPDATE:%d,%d,%s",&cmdType,&value,name)==3){
data received = {cmdType,value};
strncpy(received.ename,name,sizeof(received.ename)-1);
received.ename[sizeof(received.ename)-1]='\0';
push_to_stack(&received);
}
else if(sscanf((char *)buffer,"INDEX:%d",&Index)==1){
uint8_t buf[sizeof(data)];
read_from_stack_and_send(Index);
}