MCU - stm32f407vgt6
IDE - True Studio
Additional - CubeMx
Description -
I am trying to copy a file from USB drive to the same USB drive with diffrent name.
Source File - FILE2.txt
- This file is present in the drive and Size is 3KB
Destination File - file2copy.txt
- This file will be created in the same drive and contents of FILE2.txt
will be copied.
Code -
int CopyFile(char *srcFile, char *destFile)
{
FATFS fs0;
FIL fsrc, fdest;
BYTE buffer[4096];
FRESULT res;
UINT br, bw;
f_mount(&fs0, USBHPath, 0);
res = f_open(&fsrc, (const TCHAR*)srcFile, FA_READ | FA_OPEN_EXISTING);
if (res) return 0;
else
{
Green_Blink(100);
res = f_open(&fdest, (const TCHAR*)destFile, FA_WRITE | FA_CREATE_ALWAYS);
if (res) return 0;
else
{
Green_Blink(100);
for (;;) {
res = f_read(&fsrc, buffer, sizeof buffer, &br); /* Read a chunk of source file */
if (res || br == 0) break; /* error or eof */
res = f_write(&fdest, buffer, br, &bw); /* Write it to the destination file */
if (res || bw < br) break; /* error or disk full */
f_sync(&fdest);
}
}
}
f_close(&fsrc);
f_close(&fdest);
f_mount(0, USBHPath, 0);
return 1;
}
ERROR -
I am able to open the source file but cannot create the destination file in the drive.
res = f_open(&fdest, (const TCHAR*)destFile, FA_WRITE | FA_CREATE_ALWAYS);
if (res) return 0;
res becomes true in the case.
So my question is how do i copy file in same logical drive and what is the problem with opening the destination file in that drive.
Thanks in advance.
Firstly, I don't know about your stack settings but variables FATFS fs0;
and BYTE buffer[4096];
may heavy for location on the stack. Be sure that stack size more than 4096 + FF_MAX_SS
Secondly, What kind of error did you get? This enum should be useful:
typedef enum {
FR_OK = 0, /* (0) Succeeded */
FR_DISK_ERR, /* (1) A hard error occurred in the low level disk I/O layer */
FR_INT_ERR, /* (2) Assertion failed */
FR_NOT_READY, /* (3) The physical drive cannot work */
FR_NO_FILE, /* (4) Could not find the file */
FR_NO_PATH, /* (5) Could not find the path */
FR_INVALID_NAME, /* (6) The path name format is invalid */
FR_DENIED, /* (7) Access denied due to prohibited access or directory full */
FR_EXIST, /* (8) Access denied due to prohibited access */
FR_INVALID_OBJECT, /* (9) The file/directory object is invalid */
FR_WRITE_PROTECTED, /* (10) The physical drive is write protected */
FR_INVALID_DRIVE, /* (11) The logical drive number is invalid */
FR_NOT_ENABLED, /* (12) The volume has no work area */
FR_NO_FILESYSTEM, /* (13) There is no valid FAT volume */
FR_MKFS_ABORTED, /* (14) The f_mkfs() aborted due to any problem */
FR_TIMEOUT, /* (15) Could not get a grant to access the volume within defined period */
FR_LOCKED, /* (16) The operation is rejected according to the file sharing policy */
FR_NOT_ENOUGH_CORE, /* (17) LFN working buffer could not be allocated */
FR_TOO_MANY_OPEN_FILES, /* (18) Number of open files > FF_FS_LOCK */
FR_INVALID_PARAMETER /* (19) Given parameter is invalid */
} FRESULT;
And, try to don't keep open two files simultaneously:
int CopyFile(char *srcFile, char *destFile)
{
FATFS fs0;
FIL file;
BYTE buffer[4096];
FRESULT res;
UINT br = 0, bw = 0;
f_mount(&fs0, USBHPath, 0);
res = f_open(&file, (const TCHAR*)srcFile, FA_READ | FA_OPEN_EXISTING);
if (res){
f_mount(0, USBHPath, 0);
return 0;
}
f_read(&file, buffer, sizeof(buffer), &br); /* Read a chunk of source file */
f_close(&file);
if(br) {
Green_Blink(100);
res = f_open(&file, (const TCHAR*)destFile, FA_WRITE | FA_CREATE_ALWAYS);
if (res) {
f_mount(0, USBHPath, 0);
return 0;
}
Green_Blink(100);
f_write(&file, buffer, br, &bw); /* Write it to the destination file */
f_close(&file);
if(!bw) {
f_mount(0, USBHPath, 0);
return 0;
}
}
f_mount(0, USBHPath, 0);
return 1;
}