0

I am interfacing SD card(16Gb Sandisk ultra micro SD) to STM32F407 micro-controller with SDIO protocol using chan FatFS library. When I try to write data into existing file, f_write returns FR_OK and returns numbers of written bytes(this value is equal to number of bytes to write), but f_close() returns FR_DISK_ERR, and in the end the file is empty. With more experiment i found that if i format the micro SD card using 64Kb unit allocation size and the existing file with some text in it then i am able to write 64Kb data to file but f_close() returns FR_DISK_ERR, and in the end the file is not empty. I am able to see the data in Windows 10 OS. If the existing file has no text in this then i am getting an empty file even though f_write is returning FR_OK but f_close is returning FR_DISK_ERR. In short when I try to use f_write on a text file i created from my PC I can overwrite the content of that file till 64Kb. But i can't get it to work with an empty file i created with f_open

I came across a similar post with the same issue

TMS320F2812 FatFs f_write returns FR_DISK_ERR

I tried the solutions given in the above post but it didn't work. Since i have 192K RAM in my controller, i guess its sufficient enough for this FatFs module to work. My stack size is around 13Kb and Heap size 4Kb which is too much for this application. SD card is given 3.3V supply voltage.

I went little deep in code to see where the error is occurring and found that i am getting SD_ILLEGAL_CMD error while setting block size for card. Inside f_close(ff.c file)->f_sync(ff.c file)->move_window(ff.c file)->disk_read(diskio.c file)->SD_ReadBlock(sdcard.c file) is returning SD_ILLEGAL_CMD error while setting block size for card.

Any solutions are appreciated. If more information is required please feel free to ask, i will update with more information.

Chan FatFs Version - R0.07e

  • I am writing 512 bytes and __MAX_SS(ffconf.h file) is set to 512. I am able to write 512 bytes for 128 times which gives 64Kb but f_close throws error – Vijay Anand Chandrasekaran Feb 01 '18 at 13:21
  • Chan FatFS 0.7e is really old. Current version at this time is R0.13a. The 64KB Cluster format is not compatible with FAT32 - you need to use 32KB custers on a card with more than 4GB. – Turbo J Feb 01 '18 at 19:35
  • Thank You Turbo for your reply. As you said the version i am using is old and i will update it with the new one. I formatted the SD card with 32Kb unit size and the result is the same. I am able to overwrite the content of that file till 32Kb. But i can't get it to work with an empty file i created with f_open. In both the case f_close is throwing error because of SD_ReadBlock returning SD_ILLEGAL_CMD error. I also tried by changing the unit size to 8Kb and the result is still the sane – Vijay Anand Chandrasekaran Feb 02 '18 at 07:13
  • I even tried updating the FatFs version to 13a but no progress. I am stuck with the same issue. Any suggestion or help appreciated – Vijay Anand Chandrasekaran Feb 02 '18 at 14:53

0 Answers0