Showing results for 
Search instead for 
Did you mean: 

STM32WLE5JC FatFS cannot open, read file, cannot get number of free clusters

Associate II

I have been trying to use the FatFS file system to create a new file on the SD card, or to read a simple txt file on the SD card. However, I seem to be running into issues after mounting the SD card. Initially, I ran code to:

- mount sd card in main

- open file in while loop

and sent value of testmount, fr via UART to confirm 1 or otherwise. I was able to receive a pass message or '1' for mount, but would not receive any of the test messages or the value of fr once the f_open line was reached.

I was unsure if I had issues with incorrectly mounting the SD card, so I then

- mounted SD card in main 

- used f_getfree function to see information about volume size in main.

Likewise, I was getting 1/receiving the pass mount message, but nothing after the getfree function.

What are some reasons this could be happening? Is there an issue with how I am mounting my SD card? Are there issues elsewhere? When I use the debugger it breaks at the getfree/open functions, but I can't discern much else.


//in main

HAL_UART_Transmit(&huart1, var_test, sizeof(var_test), 1000);


fs = malloc(sizeof (FATFS));

testmount = f_mount(fs, "", 0);

if (testmount != FR_OK){

HAL_UART_Transmit(&huart1, error_mount, sizeof(error_mount), 1000);


else {

HAL_UART_Transmit(&huart1, pass_mount, sizeof(pass_mount), 1000);





DWORD free_clusters;

FATFS* getFreeFs;


fres = f_getfree("", &free_clusters, &getFreeFs);

if (fres != FR_OK) {

sprintf(size, "%hu \r\n", fres);

HAL_UART_Transmit(&huart1, (uint8_t*)size, strlen(size), 1000);





while (1)





HAL_UART_Transmit(&huart1, tx_buff3, sizeof(tx_buff3), 1000);

fr = f_open(&fil, "write.txt", FA_CREATE_ALWAYS);

sprintf(msg, "%hu \r\n", fr);

HAL_UART_Transmit(&huart1, (uint8_t*)msg, strlen(msg), 1000);

HAL_UART_Transmit(&huart1, tx_buff4, sizeof(tx_buff4), 1000); //this is not


if (fr != FR_OK) {

HAL_UART_Transmit(&huart1, fail_open, sizeof(fail_open), 1000);


else {

HAL_UART_Transmit(&huart1, pass_open, sizeof(pass_open), 1000);


//f_puts("Hello \n", &fil);



f_mount(0, "", 0);







Andrew Neil
Evangelist III

Please use this button to properly post source code:



What return values are you getting?

I get a return value of 1 for my mount function. For the getfree function, I receive this error in debug mode: Screenshot 2024-02-20 145346.png

and it also gives a hard fault error. No message after the getfree function is transmitted. 

Associate II

More updated error message:

Screenshot 2024-02-21 224537.png

Another update: when I remove the getfree function and change the f_mount param to be able to force mount, I then get the error message at the testmount, which leads me to think I have an issue with the mount function. I'd greatly appreciate any insights into the matter. 


Associate II

I am now getting error code 3 for my mount function (I had some errors in my code that have now been fixed). However, I am now suspecting that there is no SPI communication occurring to begin with. Any advice would be appreciated.