cancel
Showing results for 
Search instead for 
Did you mean: 

FatFS write successful but read not working on w25q64

NOVAL_______BOBY
Associate

write successfully but read not working . and show fail to find file in fatfs spi flash using stmcube and w25q64

void test_fatfs(void) {

printf("Mounting filesystem...\n");

fres = f_mount(&fs, "0:", 1);

if (fres != FR_OK) {

printf("Failed to mount filesystem: %d\n", fres);

return;

}

printf("Filesystem mounted successfully\n");

 

// Open file for writing

fres = f_open(&file, "test.txt", FA_WRITE | FA_OPEN_ALWAYS);

if (fres != FR_OK) {

printf("Failed to open file for writing: %d\n", fres);

return;

}

printf("File opened for writing\n");

 

// Write to file

UINT bytes_written;

fres = f_write(&file, "text\r\n", 6, &bytes_written);

if (fres != FR_OK || bytes_written < 6) {

printf("Failed to write to file: %d\n", fres);

f_close(&file);

return;

}

printf("Data written to file successfully\n");

 

// Synchronize filesystem

fres = f_sync(&file);

if (fres != FR_OK) {

printf("Failed to sync filesystem after writing: %d\n", fres);

f_close(&file);

return;

}

fres = f_close(&file);

if (fres != FR_OK) {

printf("Failed to close file after writing: %d\n", fres);

return;

}

printf("File successfully closed after writing\n"); // working thiss

 

// Verify file exists

// FILINFO fno;

// fres = f_stat("test.txt", &fno);

// if (fres == FR_OK) {

// printf("File exists, size: %lu bytes\n", fno.fsize);

// } else {

// printf("Failed to find file: %d\n", fres); // failed here. fail to find :4

// return;

// }

 

// Open file for reading

fres = f_open(&file, "test.txt", FA_OPEN_EXISTING);

if (fres != FR_OK) {

printf("Failed to open file for reading: %d\n", fres);

return;

}

printf("File opened for reading\n");

 

// Read from file

UINT bytes_read;

fres = f_read(&file, buf, 6, &bytes_read);

if (fres != FR_OK || bytes_read < 6) {

printf("Failed to read from file: %d\n", fres);

f_close(&file);

return;

}

buf[6] = '\0'; // Null-terminate the buffer

printf("Data read from file: %s\n", buf);

 

// Close file

fres = f_close(&file);

if (fres != FR_OK) {

printf("Failed to close file after reading: %d\n", fres);

return;

}

printf("File successfully closed after reading\n");

}


using nucleo board f302r8 and w25q64 , spi flash using fatfs

2 REPLIES 2

Please see the forum guidelines How to write your question to maximize your chances to find a solution for how to properly post source code, and and other details needed to help people answer.

Saket_Om
ST Employee

Hello @NOVAL_______BOBY 

Could you please check opening the file for reading with the flag "FA_READ"?

fres = f_open(&file, "test.txt", FA_READ | FA_OPEN_EXISTING);

 

If your question is answered, please close this topic by clicking "Accept as Solution".

Thanks
Omar