I have been sitting on this problem for a couple of days where I have been trying to use f_mount on a 8GB SD card (I formatted it to FAT32 and to a sector size of 4096.) I am also copying a very popular library by eziya: https://blog.naver.com/eziya76/221188701172. The chip I am using on my NUCLEO board is a STM32F103RB.
Although the program is very simple, no matter what I do, the SD card never gets mounted. When trying to read the available space and the total space, it always returns 0.
Here is my code for the main loop:
FATFS fs;
FIL fil;
FRESULT fresult;
char buffer[1024];
UINT br, bw;
FATFS *pfs;
DWORD fre_clust;
uint32_t total, free_space;
fresult = f_mount(&fs, "", 1);
printf("%d \n", fresult);
if (fresult != FR_OK) printf("Unsuccessful again\n");
else printf("Mounted successfully! \n");
f_getfree("", &fre_clust, &pfs);
total = (uint32_t)((pfs->n_fatent - 2) * pfs->csize * 0.5);
printf("Total size: \t%lu\n",total);
free_space = (uint32_t)(fre_clust * pfs->csize * 0.5);
printf("SD CARD free space: \t%lu\n", free_space);
I used SPI for the configuration, here are my configurations for the CubeIDE:
FATFS configuration:
SPI pinout in the CubeIDE:
I set the CS output on default high for the SPI communication protocol.
The clock configuration:
I use this SD card reader module
What I have tried and didn't work:
Switch out the SD card reader module.
Switch out the microSD card.
Use an internal Pull-up resistor for the MOSI and CS lines (I didn't have an option to choose pull-up resistors. for MISO and SCK.)
Have "Low" as default CS output.
Checked the SCK signal on the oscilloscope and it was reading something, which seemed to me alright, so I don't think it's a hardware issue.
Put some signs into the "" of the f_mount input.
Use 0 as the initialization input for the f_mount function (this returns always FR_OK though, so it doesn't really help.)
Double check SPI connections (everything should be connected correctly.)
Any help is welcome, thank you so much!
So in the end I was able to figure out the issue. I did what was suggested and went through the program step-by-step through breakpoints. What I found is that is was definitely a hardware issue. I switched out a bunch of these blue SD card reader modules, and it still didn't work. However, when I used a totally different SD card reader from SparkFun, it suddenly worked. NONE of these blue SD card reader modules worked. To sum it up -- Beware of these Blue Devils.