Skip to main content
Associate II
January 28, 2025
Question

FatFS write successful but read not working on w25q64

  • January 28, 2025
  • 2 replies
  • 673 views

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

This topic has been closed for replies.

2 replies

Andrew Neil
Super User
January 28, 2025

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.

A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.
ST Technical Moderator
January 28, 2025

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);

 

In order to give better visibility on the answered topics, please click on 'Best answer' on the reply which solved your issue or answered your question. Saket_Om