Howdy forum,
I currently have a working test program that will mount a drive, create a folder, create a numbered file and write in it. Everything is included in the main.c file, and my goal is to move the file creation operations to my FATFS.c file. When I do this, however, I'm still able to successfully mount the drive, create an enumerated file, but reading and syncing fail. Any thoughts on what might be causing this?
So. This one works dandy:
But when I try this:
void BR_Initialize_File(FRESULT fresult, FIL fsrc, FATFS g_sFatFs){ fresult = f_mount(0, &g_sFatFs); debug_printf("f_mount error: %s\n", StringFromFresult(fresult)); //Check for/create test folder fresult = f_mkdir ("test"); debug_printf("f_mkdir error: %s\n", StringFromFresult(fresult)); //Attempt to create initial filename. If exists, iterate through until new name found fresult = f_open(&fsrc, "test/test0.txt", FA_CREATE_NEW | FA_WRITE); debug_printf("f_open: %s\n", StringFromFresult(fresult)); unsigned int i = 1; static char s[20]; while(fresult != FR_OK) { // debug_printf("START----------------------\n"); usprintf(&s,"test/test%d.txt",i); debug_printf("s= '%s'\n", s); unsigned int uIdx = 0; uIdx = strlen(s);// debug_printf("strnlen(s)= %d\n", uIdx); fresult = f_open(&fsrc, s, FA_CREATE_NEW | FA_WRITE); debug_printf("f_open: %s\n", StringFromFresult(fresult)); i++; }}
It appears to mount correctly, indicate that the folder exists (FR_EXIST), enumerates to the current file name and creates it, but then throws FR_INVALID_OBJECT for the f_write and f_sync operations. Any thoughts?
I believe you are passing, by value, the parameteres fresult, fsrc, g_sFatFs into BR_Initialize_File() BR_Initialize_File() will make changes to it's own copies. main() will not get the changed values back. It looks like you are using C++. Switch to passing by reference by adding the '&' character in function and the prototype in the .h file.
void BR_Initialize_File(FRESULT &fresult, FIL &fsrc, FATFS &g_sFatFs){ ...}
In C, you would have to pass in a pointers. Suggest returning the result:
FRESULT BR_Initialize_File(FIL *pfsrc, FATFS *psFatFs){ FRESULT fresult; fresult = f_mount(0, psFatFs); debug_printf("f_mount error: %s\n", StringFromFresult(fresult)); //Check for/create test folder fresult = f_mkdir ("test"); debug_printf("f_mkdir error: %s\n", StringFromFresult(fresult)); //Attempt to create initial filename. If exists, iterate through until new name found fresult = f_open(pfsrc, "test/test0.txt", FA_CREATE_NEW | FA_WRITE); debug_printf("f_open: %s\n", StringFromFresult(fresult)); unsigned int i = 1; static char s[20]; while(fresult != FR_OK) { // debug_printf("START----------------------\n"); usprintf(&s,"test/test%d.txt",i); debug_printf("s= '%s'\n", s); unsigned int uIdx = 0; uIdx = strlen(s); // debug_printf("strnlen(s)= %d\n", uIdx); fresult = f_open(pfsrc, s, FA_CREATE_NEW | FA_WRITE); debug_printf("f_open: %s\n", StringFromFresult(fresult)); i++; } return(fresult);}
and called in main() like so:
fresult = BR_Initialize_File(&fsrc, &g_sFatFs);