2023-11-07 11:03 AM
Hello,
I have a STM32F407 Discovery Board. I am trying out the FatFs_USBDisk demo application. I am running the application from STM32CubeIDE.
When I run the application and plug in a USB flash drive, the demo application works as expected. I see the STM32.txt file on the USB drive and the Green LED4 is on.
If I then unplug the USB flash drive and plug it back in, I get the RED error LED5.
Debugging it, I see the following is called when I unplug the USB flash drive.
f_mount(NULL, (TCHAR const*)"", 0);
When I plug the USB flash drive back in I see that
if(f_mount(&USBDISKFatFs, (TCHAR const*)USBDISKPath, 0) != FR_OK)
returns FR_OK
The demo application is failing in
if(f_open(&MyFile, "STM32.TXT", FA_CREATE_ALWAYS | FA_WRITE) != FR_OK)
returning FR_NO_FILESYSTEM.
Specifically in the find_volume() in f_open()
If I stop the debug session and restart it the USB flash drive is mounted correctly and able to open then write the file.
Any thoughts on what is going on?
I have not modified the demo application (using as is out the box).
Thanks,
Brent
Solved! Go to Solution.
2023-11-09 06:45 AM
I found the issue. In the MSC_Application() at the end the following call is made:
/* Unlink the USB disk I/O driver */
FATFS_UnLinkDriver(USBDISKPath);
Commenting this out as shown below allows me to plugin/unplug the USB flash drive over and over. The device is mounted correctly and can write/read files to it.
Why is this function call in the demo application?
Not sure when this should be used in an application?
static void MSC_Application(void)
{
FRESULT res; /* FatFs function common result code */
uint32_t byteswritten, bytesread; /* File write/read counts */
uint8_t wtext[] = "This is STM32 working with FatFs"; /* File write buffer */
uint8_t rtext[100]; /* File read buffer */
/* Register the file system object to the FatFs module */
if(f_mount(&USBDISKFatFs, (TCHAR const*)USBDISKPath, 0) != FR_OK)
{
/* FatFs Initialization Error */
Error_Handler();
}
else
{
/* Create and Open a new text file object with write access */
if(f_open(&MyFile, "STM32.TXT", FA_CREATE_ALWAYS | FA_WRITE) != FR_OK)
{
/* 'STM32.TXT' file Open for write Error */
Error_Handler();
}
else
{
/* Write data to the text file */
res = f_write(&MyFile, wtext, sizeof(wtext), (void *)&byteswritten);
if((byteswritten == 0) || (res != FR_OK))
{
/* 'STM32.TXT' file Write or EOF Error */
Error_Handler();
}
else
{
/* Close the open text file */
f_close(&MyFile);
/* Open the text file object with read access */
if(f_open(&MyFile, "STM32.TXT", FA_READ) != FR_OK)
{
/* 'STM32.TXT' file Open for read Error */
Error_Handler();
}
else
{
/* Read data from the text file */
res = f_read(&MyFile, rtext, sizeof(rtext), (void *)&bytesread);
if((bytesread == 0) || (res != FR_OK))
{
/* 'STM32.TXT' file Read or EOF Error */
Error_Handler();
}
else
{
/* Close the open text file */
f_close(&MyFile);
/* Compare read data with the expected data */
if((bytesread != byteswritten))
{
/* Read data is different from the expected data */
Error_Handler();
}
else
{
/* Success of the demo: no error occurrence */
BSP_LED_On(LED4);
}
}
}
}
}
}
/* Unlink the USB disk I/O driver */
// FATFS_UnLinkDriver(USBDISKPath);
}
2023-11-08 09:46 AM
Hello,
I am trying to dig into this a little more. When starting up the FatFs_USBDisk demo application and running it the first time I plug in the USB flash drive it works. Specifically in check_fs() where it loads a sector and checks to see if it is a FAT boot sector - the boot record signature returned from the check is 0xAA55 (see figure below):
In the case where I unplug the USB flash drive, I see unmount is called, both LEDs turn off as expected. When I plug the USB flash drive back in check_fs() is called but the boot record signature returned from the check is not 0xAA55. It is 0 (see figure below):
Could there be a bug in the FatFs code?
Or maybe I am not running the demo application as expected?
Any thoughts on what I might be able to try?
Thanks,
Brent
2023-11-08 10:27 AM - edited 2023-11-08 10:28 AM
just - what i do:
on usb mount / app_ready i use :
retUSBH = f_mount(&USBfs, (TCHAR const*)USBHPath, 1); // mount
"1" -> mount now. (you have "0" delayed mount ) --- just try it.
2023-11-09 06:12 AM
Thank you for the reply!
I changed the the last parameter of the f_mount to 1 to mount immediately.
mount_result = f_mount(&USBDISKFatFs, (TCHAR const*)USBDISKPath, 1);
/* Register the file system object to the FatFs module */
if(mount_result != FR_OK)
{
/* FatFs Initialization Error */
Error_Handler();
}
When I first run the demo application and plug in a USB flash drive the f_mount() returns FR_OK. The application continues to run properly and writes/read a file to the USB flash drive. The green LED turns on.
I then unplug the USB flash drive, the drive unmounts, and green LED turns off.
I then plug in the USB flash drive the second time. The f_mount() fails with FR_NO_FILESYSTEM.
I have tried different USB flash drives to see if that was the issue. I have the same issue with all of them.
Do you have this working where you are able to plug in and unplug a USB flash drive multiple times?
I am trying the FatFs_USBDisk from the Example Selector for STM32F407G-DISC1 within STM32CubeIDE (see below).
Are you able to get this to work or have it working in another application?
Maybe I am not understanding how this application is suppose to work? I guess I am surprised that this example out of STMicros' SDK is not working out of the box.
Any other thoughts on what to look at or try?
Thanks again,
Brent
2023-11-09 06:45 AM
I found the issue. In the MSC_Application() at the end the following call is made:
/* Unlink the USB disk I/O driver */
FATFS_UnLinkDriver(USBDISKPath);
Commenting this out as shown below allows me to plugin/unplug the USB flash drive over and over. The device is mounted correctly and can write/read files to it.
Why is this function call in the demo application?
Not sure when this should be used in an application?
static void MSC_Application(void)
{
FRESULT res; /* FatFs function common result code */
uint32_t byteswritten, bytesread; /* File write/read counts */
uint8_t wtext[] = "This is STM32 working with FatFs"; /* File write buffer */
uint8_t rtext[100]; /* File read buffer */
/* Register the file system object to the FatFs module */
if(f_mount(&USBDISKFatFs, (TCHAR const*)USBDISKPath, 0) != FR_OK)
{
/* FatFs Initialization Error */
Error_Handler();
}
else
{
/* Create and Open a new text file object with write access */
if(f_open(&MyFile, "STM32.TXT", FA_CREATE_ALWAYS | FA_WRITE) != FR_OK)
{
/* 'STM32.TXT' file Open for write Error */
Error_Handler();
}
else
{
/* Write data to the text file */
res = f_write(&MyFile, wtext, sizeof(wtext), (void *)&byteswritten);
if((byteswritten == 0) || (res != FR_OK))
{
/* 'STM32.TXT' file Write or EOF Error */
Error_Handler();
}
else
{
/* Close the open text file */
f_close(&MyFile);
/* Open the text file object with read access */
if(f_open(&MyFile, "STM32.TXT", FA_READ) != FR_OK)
{
/* 'STM32.TXT' file Open for read Error */
Error_Handler();
}
else
{
/* Read data from the text file */
res = f_read(&MyFile, rtext, sizeof(rtext), (void *)&bytesread);
if((bytesread == 0) || (res != FR_OK))
{
/* 'STM32.TXT' file Read or EOF Error */
Error_Handler();
}
else
{
/* Close the open text file */
f_close(&MyFile);
/* Compare read data with the expected data */
if((bytesread != byteswritten))
{
/* Read data is different from the expected data */
Error_Handler();
}
else
{
/* Success of the demo: no error occurrence */
BSP_LED_On(LED4);
}
}
}
}
}
}
/* Unlink the USB disk I/O driver */
// FATFS_UnLinkDriver(USBDISKPath);
}