This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

stm32 F411 microSD card file read/SPI error

I have a PCB board with stm32 F411 as the MCU.

MCU communicates with a microSD card through SPI(5). I used SPI instead of SDIO because of not enough pins.

I use some open source library to read/write SD card.

Now my problem is if the file size is larger than allocation unit size (AUS), f_gets throws error once file pointer walks past the AUS size boundary.

For example, if AUS is 16k/16384, and there is a text file larger than 16k. I use f_gets to read line by line, once it reaches the line, which runs across 16384 bytes, f_gets returns error.

I tested a few AUS numbers, the error happens consistently at the same file location or line, where the total bytes read runs across AUS boundary.

However, there no such problem when I do writing. I have tried writing files of different sizes below AUS, more than AUS, and a few times of AUS: f_write does not throw any error, and I can see the file content in when I open the file on SD card in Windows as exactly what I intend to write.

Any one has any idea what causes the error in f_gets?

  • I have found where and why I got the error.

    This is my code to read a text file line by line:

      fres = f_open(&fil, volt_file_name, FA_READ);
      if(fres == FR_OK)
      {
    	  char line[32]; /* Line buffer */
    	  int i1 = 1;
    	  int total_size = 0;
     
    	  i1 = 0;
    	  while(f_gets(line, sizeof line, &fil) != 0) {
                   ...
    	  }
    	  //close your file
    	  f_close(&fil);
       }

    For file handler, fil, after f_open, the value of fil->obj->fs->fs_type is 3, i.e.FAT32; however after calling f_gets(), that value becomes a random number. So when f_read is called by f_gets and the file pointer is on the cluster boundary, get_fat is called, which checks the file type. Since the file type values is not expected, an internal error is thrown out.

    Next I need to check how and why fs_type is unintentionally changed in f_gets(). Any one has good suggestion?