STM32 copy file from sd card to usb drive (pen drive) using FATFS
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2023-08-16 5:14 AM - edited ‎2023-08-16 9:22 PM
Hey there I've just started learning STM32 so I don't know if its a small error or big one but can you help me solve.
I'm using STM32f407z and the task is to copy a file from SD card to the USB drive (pen drive) using reference blog of elm-chan FATFS. (http://elm-chan.org/fsw/ff/doc/open.html)
The given code shows 0 error after compiling(build) but while debugging, It shows FR_DISK_ERR when f_open function is running for "myUSBFile". Thus it sometimes create the file but does not copy the content. Is there an issue with buffer or anything else I cant solve this error. @Tesla DeLorean , @TDK , @waclawek.jan, @Peter BENSCH
these are the config settings in CUBE MX,
SDIO->enabled -> SD 4 bit wide bus,
USB_OTG_FS-> HOST_ONLY,
FATFS-> SD card and USB Disk both are enabled, (USE_LFN->enabled with static working buffer on the BSS, MAX_SS->4096)
USB_host->MSC (mass storage class),
oscillator->8MHZ
CLOCK -> 168MHz
USB CLOCK -> 48MHZ
This is my Code:
- Labels:
-
FatFS
-
STM32Cube MCU Packages
-
STM32CubeMX
-
USB
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2023-08-16 1:54 PM
I'd imagine MX_USB_HOST_Process(); has to be called periodically, you can't have blocking code within the loop.
Up vote any posts that you find helpful, it shows what's working..
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2023-08-16 9:27 PM
I did remove the loop but still it doesn't work, while debugging it shows FR_DISK_ERR.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2023-08-16 10:46 PM
Hello
I suppose the problem is that you try to mount usb disk too early. Check your usb_host.c file and connect mount operation to the state when usb connection is estabilished. You must call mx_usb_host_process in loop to get usb connected.
After the connection is ready, you can start file copy. During the coping, calling the mx_usb_host_process is not necessary.
Br J.T
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2023-08-18 1:39 AM
Thank you for your reply,
I did what you said but still the error is same,
and I tried to call the "switch(Appli_state)" function to the main and put the logic inside the ready state but when I did debugging it stopped at ready state.
see the code below (I took a variable "int step=0;" to stop the copy function after the first time because it's in the loop.),
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_SDIO_SD_Init();
MX_FATFS_Init();
MX_USB_HOST_Init();
f_mount(&SDFatFS, "0:", 0);
while (1)
{
/* USER CODE END WHILE */
MX_USB_HOST_Process();
/* USER CODE BEGIN 3 */
switch(Appli_state)
{
case APPLICATION_IDLE:
break;
case APPLICATION_START:
HAL_GPIO_WritePin(GPIOG,GPIO_PIN_1,GPIO_PIN_SET);
break;
case APPLICATION_READY:
if (step==0)
{
step=1;
f_mount(&USBHFatFS, "1:", 1);
fr = f_open(&USBHFile, "1:Blue.jpg", FA_READ);
fr = f_open(&SDFile, "0:color.jpg", FA_WRITE | FA_CREATE_ALWAYS);
for (;;)
{
fr = f_read(&USBHFile, buffer, sizeof buffer, &br); /* Read a chunk of data from the source file */
if (br == 0) break; /* error or eof */
fr = f_write(&SDFile, buffer, br, &bw); /* Write it to the destination file */
if (bw < br) break; /* error or disk full */
}
f_close(&USBHFile);
f_close(&SDFile);
}
break;
case APPLICATION_DISCONNECT:
HAL_GPIO_WritePin(GPIOG,GPIO_PIN_1,GPIO_PIN_RESET);
step=0;
break;
default:
break;
}
/* USER CODE END 3 */
}
}
-----------------------------------------------------------------------------------------------------------------------------
And I also tried to copy a file from same usb to usb with different file names and it works, also the same case for the sd card it works But when I try to copy from sd to usb or vice versa it get stuck on ready state.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2025-02-17 11:55 PM
1) Don't mix up the problems & make the situation get worse: Why you need to inplement switch(Appli_state) before you fix the bug? Let try to mount & open a file in SD/USB only first. And then try another one.
2) f_mount will report error, too. Try to read back & analysis
3) The driver number (0: & 1:) is not assigned by used. It is writen in diskio.c. Try to make sure if you assign the correct number
