cancel
Showing results for 
Search instead for 
Did you mean: 

I tried to add a string to a text file saved in "microSD" using "f_write" of "FATFS", but the writte

t-oka
Associate III

 

Hello everyone in the community.

■What you are having trouble with :
→I tried to add a string to a text file saved in "microSD" using "f_write" of "FATFS", but the written content was not saved.

■Specific phenomenon:
→As shown in the PG below, the result of "f_write" is "FR_OK", and "byteswritten" also returns the number of written bytes, but no data is obtained even if "f_read" is performed.
When I checked the contents of the SD card, the written contents were not saved.
*The contents of the SD card were checked by stopping debugging after "f_close" processing.
*See PG below
*When I checked the contents of the file separately using only f_read, I was able to read it.

■Environment:
・Windows
・STM32CubeIDE_1.12.1
・nucleo-f207zgt6
・C language
 
■PG
static const char* filename = "test.txt";
FATFS sDFatFs;
FIL myFile;
 
if (f_mount(&sDFatFs, "", 0) != FR_OK){
return;
}
 
if(f_open(&myFile, filename, FA_OPEN_ALWAYS | FA_WRITE | FA_READ) != FR_OK){
return;
}
 
uint32_t byteswritten, bytesread;
FRESULT res;
uint8_t wtext[] = "This is STM32 working with FatFs\r\n";
uint8_t rtext[100];
f_lseek(&myFile, f_size(&myFile));
res = f_write(&myFile, wtext, sizeof(wtext), (void *)&byteswritten);
f_printf(&myFile, wtext);
if((byteswritten == 0) || (res != FR_OK)) {
f_close(&myFile);
return;
}
f_sync(&myFile);
f_lseek(&myFile, 0);
f_read(&myFile, rtext, sizeof(rtext), (void *)&bytesread);
f_close(&myFile);
■SDIO
toka_0-1707194525671.png

 

1 ACCEPTED SOLUTION

Accepted Solutions
t-oka
Associate III

原因:SDIOCLK clock divide factorのデータ送信速度がSDIOバスの速度よりも速かったため。
対応策:SDIOCLK clock divide factorを10→20に変更するとfcloseすることが出来て、ファイルに文字列が追記されるようになりました。

View solution in original post

2 REPLIES 2
t-oka
Associate III

"else if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_TXUNDERR))" in the "HAL_SD_WriteBlocks" function in the "f_close()" process was returning an error.
The problem is not resolved.

t-oka
Associate III

原因:SDIOCLK clock divide factorのデータ送信速度がSDIOバスの速度よりも速かったため。
対応策:SDIOCLK clock divide factorを10→20に変更するとfcloseすることが出来て、ファイルに文字列が追記されるようになりました。