cancel
Showing results for 
Search instead for 
Did you mean: 

FatFs f_open() hard fault

meco54
Associate

int main(void)
{
FATFS FatFs;

FIL fil;

FRESULT fr;

UINT bw;

 

fr = f_mount(&FatFs, "", 0);

if (fr != FR_OK) {

return (int)fr;

}

fr = f_open(&fil, "example.txt", FA_WRITE | FA_CREATE_ALWAYS);

if (fr != FR_OK) {

 

f_mount(NULL, "", 0);

return (int)fr;

}

f_close(&fil);

}
This is the main.c file. While running the code I get a hard fault issue at f_open. 

I use SDIO mode SD 1 bit

Kindly help

2 REPLIES 2

Whatever is happening, it's not in this top-level code. No idea of STM32 or board involved.

Establish where the hard fault is actually occurring. Do this with a suitably instrumented HardFault_Handler() rather than a while(1) loop

Would suggest moving FATFS structure off the stack, and clearing the other structures.

Returning from main() will fail and potentially fault the MCU, where would it return too?

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
AScha.3
Chief III

First you have to mount - successful ! -> check return , ok or not.

And do mount with "1" parameter, = now. 

And WHAT you mount ? an empty string ??

see :

fresult = f_mount(&SDfs, (TCHAR const*)SDPath, 1);			// SD card mount NOW

Then only if fresult = ok , can do any open or read ; NOT write, until this working fine !!!

If you feel a post has answered your question, please click "Accept as Solution".