Revised to support ability to have both SD and USB drives mounted.

Dependents:   Multi-FileSystem Multi-FileSystem

Fork of FATFileSystem by mbed official

Revision:
8:f08059355141
Parent:
6:a5fcdbf92056
--- 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) {