Skip to main content
Amal
Associate II
January 13, 2020
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

  • January 13, 2020
  • 8 replies
  • 2307 views

..

This topic has been closed for replies.
Best answer by berendi

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
January 13, 2020

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
January 23, 2020

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
January 23, 2020

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

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

dbgarasiya
Senior
January 18, 2020

you want to make your project to work as usbmsd ?

Amal
AmalAuthor
Associate II
January 20, 2020

yes

dbgarasiya
Senior
January 20, 2020

which controller you are using ?

Amal
AmalAuthor
Associate II
January 20, 2020

stm32 f107vc

dbgarasiya
Senior
January 20, 2020

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