Unable to close files in STM32 with USB MSC + FATFS
I created a project to create, write, read and close a file in a USB stick, using a STM32F413 MCU. I used STM32CubeMX to generate the code. The relevant part of the code is this:
uint8_t text[]={"Hello world\r\n"},rbuff[50],test=0;
switch(Appli_state)
{
case APPLICATION_READY:
test+=f_mount(&USBDISKFatFs, (TCHAR const*)USBHPath, 1);
test+=f_open(&MyFile, "FILE.txt", FA_CREATE_ALWAYS | FA_WRITE | FA_READ);
test+=f_write(&MyFile, text, sizeof(text), (void *)&bytesw);
f_lseek(&MyFile, 0);
test+=f_read(&MyFile, rbuff, sizeof(text), (void *)&bytesr);
test+=f_close(&MyFile);
if(memcmp(text,rbuff,sizeof(text)))
test++;
break;
default:
break;
}
if(!test)
{
return test; //PASS
}
else
{
return test; //FAIL
}
I´m confused, because I find that the "test" variable only stops being equal to zero after executing the f_close function. When I check the rbuff variable after the f_read function, I see that "rbuff" it has the same value as "text", and "test" is still zero. Meaning that the program has successfully mounted, created the file, opened it, wrote it and read it. However, f_close returns a fail value and when I unplug the USB stick and plug it on the PC, it's empty. The USB stick is formated as FAT32 I have no idea what could be causing this problem. Any ideas?
