Revised to support ability to have both SD and USB drives mounted.
Dependents: Multi-FileSystem Multi-FileSystem
Fork of FATFileSystem by
Revision 8:f08059355141, committed 2016-03-12
- Comitter:
- WiredHome
- Date:
- Sat Mar 12 23:58:38 2016 +0000
- Parent:
- 7:094f84646b9f
- Child:
- 9:4000fad3b21f
- Commit message:
- Changed to support simultaneous SD and USB FileSystem
Changed in this revision
--- a/ChaN/diskio.cpp Fri Dec 11 16:16:25 2015 +0000 +++ b/ChaN/diskio.cpp Sat Mar 12 23:58:38 2016 +0000 @@ -21,7 +21,7 @@ BYTE pdrv /* Physical drive nmuber to identify the drive */ ) { - debug_if(FFS_DBG, "disk_status on pdrv [%d]\n", pdrv); + debug_if(FFS_DBG, "disk_status on pdrv [%d]\r\n", pdrv); return (DSTATUS)FATFileSystem::_ffs[pdrv]->disk_status(); } @@ -33,7 +33,7 @@ BYTE pdrv /* Physical drive nmuber to identify the drive */ ) { - debug_if(FFS_DBG, "disk_initialize on pdrv [%d]\n", pdrv); + debug_if(FFS_DBG, "disk_initialize on pdrv [%d]\r\n", pdrv); return (DSTATUS)FATFileSystem::_ffs[pdrv]->disk_initialize(); } @@ -48,7 +48,7 @@ UINT count /* Number of sectors to read */ ) { - debug_if(FFS_DBG, "disk_read(sector %d, count %d) on pdrv [%d]\n", sector, count, pdrv); + debug_if(FFS_DBG, "disk_read(sector %d, count %d) on pdrv [%d]\r\n", sector, count, pdrv); if (FATFileSystem::_ffs[pdrv]->disk_read((uint8_t*)buff, sector, count)) return RES_PARERR; else @@ -67,7 +67,7 @@ UINT count /* Number of sectors to write */ ) { - debug_if(FFS_DBG, "disk_write(sector %d, count %d) on pdrv [%d]\n", sector, count, pdrv); + debug_if(FFS_DBG, "disk_write(sector %d, count %d) on pdrv [%d]\r\n", sector, count, pdrv); if (FATFileSystem::_ffs[pdrv]->disk_write((uint8_t*)buff, sector, count)) return RES_PARERR; else @@ -86,7 +86,7 @@ void* buff /* Buffer to send/receive control data */ ) { - debug_if(FFS_DBG, "disk_ioctl(%d)\n", cmd); + debug_if(FFS_DBG, "disk_ioctl(%d)\r\n", cmd); switch(cmd) { case CTRL_SYNC: if(FATFileSystem::_ffs[pdrv] == NULL) {
--- a/ChaN/ff.cpp Fri Dec 11 16:16:25 2015 +0000 +++ b/ChaN/ff.cpp Sat Mar 12 23:58:38 2016 +0000 @@ -19,6 +19,41 @@ #include "ff.h" /* Declarations of FatFs API */ #include "diskio.h" /* Declarations of disk I/O functions */ +//#define DEBUG "FF__" +// ... +// INFO("Stuff to show %d", var); // new-line is automatically appended +// +#if (defined(DEBUG) && !defined(TARGET_LPC11U24)) +#include "mbed.h" +#define INFO(x, ...) std::printf("[INF %s %4d] "x"\r\n", DEBUG, __LINE__, ##__VA_ARGS__); +#define WARN(x, ...) std::printf("[WRN %s %4d] "x"\r\n", DEBUG, __LINE__, ##__VA_ARGS__); +#define ERR(x, ...) std::printf("[ERR %s %4d] "x"\r\n", DEBUG, __LINE__, ##__VA_ARGS__); +static void HexDump(const char * title, const uint8_t * p, int count) +{ + int i; + char buf[100] = "0000: "; + + if (*title) + INFO("%s", title); + for (i=0; i<count; ) { + sprintf(buf + strlen(buf), "%02X ", *(p+i)); + if ((++i & 0x0F) == 0x00) { + INFO("%s", buf); + if (i < count) + sprintf(buf, "%04X: ", i); + else + buf[0] = '\0'; + } + } + if (strlen(buf)) + INFO("%s", buf); +} +#else +#define INFO(x, ...) +#define WARN(x, ...) +#define ERR(x, ...) +#define HexDump(a, b, c) +#endif /*-------------------------------------------------------------------------- @@ -2125,7 +2160,7 @@ TCHAR tc; #endif - + INFO("get_ldnumber(%s)", *path); if (*path) { /* If the pointer is not a null */ for (tt = *path; (UINT)*tt >= (_USE_LFN ? ' ' : '!') && *tt != ':'; tt++) ; /* Find ':' in the path */ if (*tt == ':') { /* If a ':' is exist in the path name */ @@ -2138,7 +2173,7 @@ } } #if _STR_VOLUME_ID - else { /* No numeric drive number, find string drive id */ + else { /* No numeric drive number, find string drive id */ i = 0; tt++; do { sp = str[i]; tp = *path; @@ -2153,14 +2188,17 @@ } } #endif + INFO(" ret: %d", vol); return vol; } #if _FS_RPATH && _VOLUMES >= 2 + INFO(" CurrVol: %d", CurrVol); vol = CurrVol; /* Current drive */ #else vol = 0; /* Drive 0 */ #endif } + INFO(" ret: %d", vol); return vol; } @@ -2214,14 +2252,16 @@ FATFS *fs; UINT i; - + INFO("find_volume(%p,%s,%02X)", *rfs, *path, wmode); /* Get logical drive number from the path name */ *rfs = 0; vol = get_ldnumber(path); + INFO(" vol = %d", vol); if (vol < 0) return FR_INVALID_DRIVE; /* Check if the file system object is valid or not */ fs = FatFs[vol]; /* Get pointer to the file system object */ + INFO("FatFs[%d] = %p", vol, fs); if (!fs) return FR_NOT_ENABLED; /* Is the file system object available? */ ENTER_FF(fs); /* Lock the volume */ @@ -2410,10 +2450,11 @@ FRESULT res; const TCHAR *rp = path; - + INFO("f_mount(%p,%s,%02X)", fs, path, opt); vol = get_ldnumber(&rp); if (vol < 0) return FR_INVALID_DRIVE; cfs = FatFs[vol]; /* Pointer to fs object */ + INFO(" cfs(%p) = FatFs[%d]", cfs, vol); if (cfs) { #if _FS_LOCK @@ -2431,6 +2472,7 @@ if (!ff_cre_syncobj((BYTE)vol, &fs->sobj)) return FR_INT_ERR; #endif } + INFO(" assign FatFs[%d] = %p", vol, fs); FatFs[vol] = fs; /* Register new fs object */ if (!fs || opt != 1) return FR_OK; /* Do not mount now, it will be mounted later */ @@ -2460,7 +2502,7 @@ DWORD dw, cl; #endif - + INFO("f_open(%p,%s,0x%02X)", fp, path, mode); if (!fp) return FR_INVALID_OBJECT; fp->fs = 0; /* Clear file object */ @@ -2472,11 +2514,13 @@ mode &= FA_READ; res = find_volume(&dj.fs, &path, 0); #endif + INFO(" res(%d) == FR_OK(%d)", res, FR_OK); if (res == FR_OK) { INIT_BUF(dj); res = follow_path(&dj, path); /* Follow the file path */ dir = dj.dir; #if !_FS_READONLY /* R/W configuration */ + INFO(" res(%d) == FR_OK(%d)", res, FR_OK); if (res == FR_OK) { if (!dir) /* Default directory itself */ res = FR_INVALID_NAME; @@ -2486,6 +2530,8 @@ #endif } /* Create or Open a file */ + INFO(" res(%d) == FR_OK(%d)", res, FR_OK); + INFO(" mode(%02X) & const(%02X)", mode, (FA_CREATE_ALWAYS | FA_OPEN_ALWAYS | FA_CREATE_NEW)); if (mode & (FA_CREATE_ALWAYS | FA_OPEN_ALWAYS | FA_CREATE_NEW)) { if (res != FR_OK) { /* No file, create new */ if (res == FR_NO_FILE) /* There is no file to open, create a new entry */ @@ -3547,7 +3593,7 @@ DWORD dclst = 0; DEFINE_NAMEBUF; - + INFO("f_unlink(%s)", path); /* Get logical drive number */ res = find_volume(&dj.fs, &path, 1); if (res == FR_OK) {
--- a/ChaN/ffconf.h Fri Dec 11 16:16:25 2015 +0000 +++ b/ChaN/ffconf.h Sat Mar 12 23:58:38 2016 +0000 @@ -140,8 +140,8 @@ / Drive/Volume Configurations /---------------------------------------------------------------------------*/ -#define _VOLUMES 1 -/* Number of volumes (logical drives) to be used. */ +#define _VOLUMES 2 +/* Number of volumes (logical drives) to be used. Cannot exceed 9 or code fails! */ #define _STR_VOLUME_ID 0
--- a/FATFileHandle.cpp Fri Dec 11 16:16:25 2015 +0000 +++ b/FATFileHandle.cpp Sat Mar 12 23:58:38 2016 +0000 @@ -25,6 +25,42 @@ #include "FATFileHandle.h" +//#define DEBUG "FtFH" +// ... +// INFO("Stuff to show %d", var); // new-line is automatically appended +// +#if (defined(DEBUG) && !defined(TARGET_LPC11U24)) +#include "mbed.h" +#define INFO(x, ...) std::printf("[INF %s %4d] "x"\r\n", DEBUG, __LINE__, ##__VA_ARGS__); +#define WARN(x, ...) std::printf("[WRN %s %4d] "x"\r\n", DEBUG, __LINE__, ##__VA_ARGS__); +#define ERR(x, ...) std::printf("[ERR %s %4d] "x"\r\n", DEBUG, __LINE__, ##__VA_ARGS__); +static void HexDump(const char * title, const uint8_t * p, int count) +{ + int i; + char buf[100] = "0000: "; + + if (*title) + INFO("%s", title); + for (i=0; i<count; ) { + sprintf(buf + strlen(buf), "%02X ", *(p+i)); + if ((++i & 0x0F) == 0x00) { + INFO("%s", buf); + if (i < count) + sprintf(buf, "%04X: ", i); + else + buf[0] = '\0'; + } + } + if (strlen(buf)) + INFO("%s", buf); +} +#else +#define INFO(x, ...) +#define WARN(x, ...) +#define ERR(x, ...) +#define HexDump(a, b, c) +#endif + FATFileHandle::FATFileHandle(FIL fh) { _fh = fh; } @@ -39,18 +75,18 @@ UINT n; FRESULT res = f_write(&_fh, buffer, length, &n); if (res) { - debug_if(FFS_DBG, "f_write() failed: %d", res); + INFO("f_write() failed: %d", res); return -1; } return n; } ssize_t FATFileHandle::read(void* buffer, size_t length) { - debug_if(FFS_DBG, "read(%d)\n", length); + INFO("read(%d)", length); UINT n; FRESULT res = f_read(&_fh, buffer, length, &n); if (res) { - debug_if(FFS_DBG, "f_read() failed: %d\n", res); + INFO("f_read() failed: %d", res); return -1; } return n; @@ -68,10 +104,10 @@ } FRESULT res = f_lseek(&_fh, position); if (res) { - debug_if(FFS_DBG, "lseek failed: %d\n", res); + INFO("lseek failed: %d", res); return -1; } else { - debug_if(FFS_DBG, "lseek OK, returning %i\n", _fh.fptr); + INFO("lseek OK, returning %i", _fh.fptr); return _fh.fptr; } } @@ -79,7 +115,7 @@ int FATFileHandle::fsync() { FRESULT res = f_sync(&_fh); if (res) { - debug_if(FFS_DBG, "f_sync() failed: %d\n", res); + INFO("f_sync() failed: %d", res); return -1; } return 0; @@ -88,3 +124,4 @@ off_t FATFileHandle::flen() { return _fh.fsize; } +
--- a/FATFileSystem.cpp Fri Dec 11 16:16:25 2015 +0000 +++ b/FATFileSystem.cpp Sat Mar 12 23:58:38 2016 +0000 @@ -28,6 +28,43 @@ #include "FATFileHandle.h" #include "FATDirHandle.h" + +//#define DEBUG "FtFS" +// ... +// INFO("Stuff to show %d", var); // new-line is automatically appended +// +#if (defined(DEBUG) && !defined(TARGET_LPC11U24)) +#include "mbed.h" +#define INFO(x, ...) std::printf("[INF %s %4d] "x"\r\n", DEBUG, __LINE__, ##__VA_ARGS__); +#define WARN(x, ...) std::printf("[WRN %s %4d] "x"\r\n", DEBUG, __LINE__, ##__VA_ARGS__); +#define ERR(x, ...) std::printf("[ERR %s %4d] "x"\r\n", DEBUG, __LINE__, ##__VA_ARGS__); +static void HexDump(const char * title, const uint8_t * p, int count) +{ + int i; + char buf[100] = "0000: "; + + if (*title) + INFO("%s", title); + for (i=0; i<count; ) { + sprintf(buf + strlen(buf), "%02X ", *(p+i)); + if ((++i & 0x0F) == 0x00) { + INFO("%s", buf); + if (i < count) + sprintf(buf, "%04X: ", i); + else + buf[0] = '\0'; + } + } + if (strlen(buf)) + INFO("%s", buf); +} +#else +#define INFO(x, ...) +#define WARN(x, ...) +#define ERR(x, ...) +#define HexDump(a, b, c) +#endif + DWORD get_fattime(void) { time_t rawtime; time(&rawtime); @@ -43,18 +80,24 @@ FATFileSystem *FATFileSystem::_ffs[_VOLUMES] = {0}; FATFileSystem::FATFileSystem(const char* n) : FileSystemLike(n) { - debug_if(FFS_DBG, "FATFileSystem(%s)\n", n); + INFO("FATFileSystem(%s)", n); for(int i=0; i<_VOLUMES; i++) { + if(_ffs[i] == this) // 2nd attempt to mount the same drive + return; if(_ffs[i] == 0) { _ffs[i] = this; _fsid[0] = '0' + i; - _fsid[1] = '\0'; - debug_if(FFS_DBG, "Mounting [%s] on ffs drive [%s]\n", _name, _fsid); + _fsid[1] = ':'; + _fsid[2] = '\0'; + INFO("Mounting [%s] on ffs drive [%s]", _name, _fsid); f_mount(&_fs, _fsid, 0); + for (int d=0; d<_VOLUMES; d++) { + INFO(" _ffs[%d] = %p", d, _ffs[d]); + } return; } } - error("Couldn't create %s in FATFileSystem::FATFileSystem\n", n); + error("Couldn't create %s in FATFileSystem::FATFileSystem\r\n", n); } FATFileSystem::~FATFileSystem() { @@ -67,10 +110,11 @@ } FileHandle *FATFileSystem::open(const char* name, int flags) { - debug_if(FFS_DBG, "open(%s) on filesystem [%s], drv [%s]\n", name, _name, _fsid); + INFO("open(%s) on filesystem [%s], drv [%s]", name, _name, _fsid); char n[64]; - sprintf(n, "%s:/%s", _fsid, name); - + sprintf(n, "%s/%s", _fsid, name); + INFO(" :: open(%s)", n); + /* POSIX flags -> FatFS open mode */ BYTE openmode; if (flags & O_RDWR) { @@ -91,7 +135,7 @@ FIL fh; FRESULT res = f_open(&fh, n, openmode); if (res) { - debug_if(FFS_DBG, "f_open('w') failed: %d\n", res); + INFO("f_open('w') failed: %d", res); return NULL; } if (flags & O_APPEND) { @@ -100,19 +144,27 @@ return new FATFileHandle(fh); } -int FATFileSystem::remove(const char *filename) { - FRESULT res = f_unlink(filename); +int FATFileSystem::remove(const char *name) { + INFO("remove(%s) on filesystem [%s], drv [%s]", name, _name, _fsid); + char n[64]; + sprintf(n, "%s/%s", _fsid, name); + INFO(" :: remove(%s)", n); + FRESULT res = f_unlink(n); if (res) { - debug_if(FFS_DBG, "f_unlink() failed: %d\n", res); + INFO("f_unlink() failed: %d", res); return -1; } return 0; } int FATFileSystem::rename(const char *oldname, const char *newname) { + char o[64], n[64]; + sprintf(o, "%s/%s", _fsid, oldname); + sprintf(n, "%s/%s", _fsid, newname); + INFO(" :: rename(%s,%s)", o, n); FRESULT res = f_rename(oldname, newname); if (res) { - debug_if(FFS_DBG, "f_rename() failed: %d\n", res); + INFO("f_rename() failed: %d", res); return -1; } return 0; @@ -121,7 +173,7 @@ int FATFileSystem::format() { FRESULT res = f_mkfs(_fsid, 0, 512); // Logical drive number, Partitioning rule, Allocation unit size (bytes per cluster) if (res) { - debug_if(FFS_DBG, "f_mkfs() failed: %d\n", res); + INFO("f_mkfs() failed: %d", res); return -1; } return 0; @@ -152,3 +204,4 @@ FRESULT res = f_mount(NULL, _fsid, 0); return res == 0 ? 0 : -1; } +
--- a/FATFileSystem.h Fri Dec 11 16:16:25 2015 +0000 +++ b/FATFileSystem.h Sat Mar 12 23:58:38 2016 +0000 @@ -40,7 +40,7 @@ static FATFileSystem * _ffs[_VOLUMES]; // FATFileSystem objects, as parallel to FatFs drives array FATFS _fs; // Work area (file system object) for logical drive - char _fsid[2]; + char _fsid[3]; // "0\n", "1\n" /** * Opens a file on the filesystem