Skip to main content
Amal
Associate II
Jan 13, 2020 6:17 AM UTC
Solved

hi all i am doing sdcard(spi) and usb in stm32 using fatfs .I did both separately and both are working properly.How do integrate both the functionality using same fatfs? please help

..

This topic has been closed for replies.
berendi
berendiBest answer
Principal
Jan 13, 2020 9:13 AM UTC

There are the disk_read(), disk_write() etc functions that your projects provide, and fatfs calls these to handle the actual I/O requests. They each take a drive number as the first parameter, which is probably ignored in your projects.

Merge these functions to call the respective disk i/o functions according to the disk number parameter. E.g. you can rename disk_read() from one project to disk_read_sd, and the other one to disk_read_usb(), then write

DRESULT disk_read(BYTE pdrv,BYTE* buff,LBA_t sector,UINT count) {
 switch(pdrv) {
 case 0:
 return disk_read_sd(pdrv,buff,sector,count);
 case 1:
 return disk_read_usb(pdrv,buff,sector,count);
 }
 // handle wrong drive number error here
}

Now you can use filenames starting with the drive number and a colon, e.g.

f_open(FIL *f1, "0:file1.txt", FA_READ); // opens a file on the SD card
f_open(FIL *f2, "1:file2.txt", FA_READ); // opens a file on the USB drive

and disk accesses will be directed to the appropriate i/o functions.

8 replies

berendi
berendiBest answer
Principal
Jan 13, 2020 9:13 AM UTC

There are the disk_read(), disk_write() etc functions that your projects provide, and fatfs calls these to handle the actual I/O requests. They each take a drive number as the first parameter, which is probably ignored in your projects.

Merge these functions to call the respective disk i/o functions according to the disk number parameter. E.g. you can rename disk_read() from one project to disk_read_sd, and the other one to disk_read_usb(), then write

DRESULT disk_read(BYTE pdrv,BYTE* buff,LBA_t sector,UINT count) {
 switch(pdrv) {
 case 0:
 return disk_read_sd(pdrv,buff,sector,count);
 case 1:
 return disk_read_usb(pdrv,buff,sector,count);
 }
 // handle wrong drive number error here
}

Now you can use filenames starting with the drive number and a colon, e.g.

f_open(FIL *f1, "0:file1.txt", FA_READ); // opens a file on the SD card
f_open(FIL *f2, "1:file2.txt", FA_READ); // opens a file on the USB drive

and disk accesses will be directed to the appropriate i/o functions.

Amal
AmalAuthor
Associate II
Jan 23, 2020 3:46 PM UTC

Hi ,thanks for your advice..i did as you said..i am able to mount both drives and open ,read,write files in both drives...now i need to copy folders from pendr​ive to sd card..is it possible? .fatfs don't have copydirectory function..

berendi
Principal
Jan 23, 2020 10:11 PM UTC

Sometimes you have to write a few lines of code yourself...

​

Watch out for stack overflow while recursively copying deep directory structures.​

dbgarasiya
Senior
Jan 18, 2020 10:43 AM UTC

you want to make your project to work as usbmsd ?

Amal
AmalAuthor
Associate II
Jan 20, 2020 4:15 AM UTC

yes

dbgarasiya
Senior
Jan 20, 2020 6:58 AM UTC

which controller you are using ?

Amal
AmalAuthor
Associate II
Jan 20, 2020 7:01 AM UTC

stm32 f107vc

dbgarasiya
Senior
Jan 20, 2020 7:14 AM UTC

Follow below steps

1)just configure sd card as you configure normally

2)select usb device as mass stroage class

3)regenerate code

after that you have to make some changes in one file "usbd_storage_if.c"

you have to make sd card configuration in above file also

Best of luck