After some experimentation I have found the solution. For details, please refer to http://elm-chan.org/fsw/ff/00index_e.html, which is teh startpage of the original author of the FAT Filesystem support. This is a powerful bit of software, and the Mbed port with the 'official' functions imposes some restrictions, which in my opinion are a bit unnecessary. Fortunately access to the underlying software is still possible.
It is important to understand that the this FATFiuleSystem has a concept of drives, almost like to good old dos. In this case, the first declared filesystem becomes drive '0:', the second is drive '1:', and this is also how the roots of these filesystems should be referred to.
To read the SD card (when declared first): d = sd.opendir("0:");
To read the USB directory (2nd): d = sd.opendir("1:"); note that you cannot just use d = opendir, you must refer to the usb or sd class!
More powerful is the direct use of the libraries, which allows you to read subdirectories, file size and other attributes, see the bit of code below:
void do_fdir() {
// list directory entry
FATFS_DIR dj;
FILINFO finfo;
FRESULT res ;
char *fn;
DBG_msg("do_ffdir", inbuf);
if (inbuf[2] != NULL) {
switch (inbuf[2]) {
case f_sdcard : res = f_opendir(&dj, "0:/");
break; // SD is first in the declarations
case f_usb : res = f_opendir(&dj, "1:/");
break;
default : do_fdefault(); // command not recognized
break;
}
} else {
do_fdefault();
return;
}
if(res == FR_OK) {
for (;;) {
res = f_readdir(&dj, &finfo);
if ((res != FR_OK) || finfo.fname[0] == 0) break;
printf(" - %s - %d\n", finfo.fname, finfo.fsize);
}
} else {
DBG_msg("Could not open directory!", "");
}
DIR *d;
struct dirent *p;
d = opendir("1:/");
if(d != NULL) {
while ((p = readdir(d)) != NULL) {
printf("MBED - %s \n", p->d_name);
}
} else {
DBG_msg("Could not open directory!", "MBED method");
}
}
Be aware that the local filesystem does NOT use FATFileSystem, and only the 'regular' functions work, the above does not work on the local flash (unfortunately).
Meindert
I have problems getting the directories a flash stick and a microSD when both declarations are on the same file.
on the following program if I change the order of
SDFileSystem sd(p5, p6, p7, p8, "sd"); & MSCFileSystem msc("msc");
then I don't get the directory listing for the microSD.
If I put these 2 functions on separate programs then they work fine, but not here. Any ideas?
// sections of this code taken from mbed website
#include "mbed.h"
#include "SDFileSystem.h"
#include "MSCFileSystem.h"
LocalFileSystem local("local");
SDFileSystem sd(p5, p6, p7, p8, "sd"); // <<<<< If change this line by the next one then flash directory is OK but not SD
MSCFileSystem msc("msc"); // <<<<<< it seems that whichever is first wins.
void DIR_FLASH(void)
{
DIR *d;
struct dirent *p;
printf("\n\rFLASH directory\n\r");
d = opendir("/msc");
if (d != NULL) {
while ((p = readdir(d)) != NULL) {
printf(" - %s\n\r", p->d_name);
}
} else {
printf("Could not open directory!\n\r");
}
closedir(d);
}
void DIR_SD(void)
{
DIR *d;
struct dirent *p;
printf("\n\rSD directory\n\r");
d = opendir("/sd");
if (d != NULL) {
while ((p = readdir(d)) != NULL) {
printf(" - %s\n\r", p->d_name);
}
} else {
printf("Could not open directory!\n\r");
}
closedir(d);
}
int main() {
DIR_SD();
DIR_FLASH();
}