cancel
Showing results for 
Search instead for 
Did you mean: 

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

Amal
Associate III
 
1 ACCEPTED SOLUTION

Accepted Solutions
berendi
Principal

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.

View solution in original post

8 REPLIES 8
berendi
Principal

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.

dbgarasiya
Senior II

you want to make your project to work as usbmsd ?

yes

dbgarasiya
Senior II

which controller you are using ?

stm32 f107vc

dbgarasiya
Senior II

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

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..

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

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