From 3c4c5bd630669b3660c74068b4325f560cd4ed3c Mon Sep 17 00:00:00 2001
From: schneider <schneider@blinkenlichts.net>
Date: Tue, 16 Jul 2019 21:52:12 +0200
Subject: [PATCH] feat(fatfs): Print string error message when mount fails

---
 epicardium/modules/fatfs.c | 47 +++++++++++++++++++++++++++++---------
 1 file changed, 36 insertions(+), 11 deletions(-)

diff --git a/epicardium/modules/fatfs.c b/epicardium/modules/fatfs.c
index 4e4dad3e..fc76bb55 100644
--- a/epicardium/modules/fatfs.c
+++ b/epicardium/modules/fatfs.c
@@ -10,6 +10,12 @@
 #include <FreeRTOS.h>
 #include <semphr.h>
 
+static const TCHAR *rcstrings =
+	_T("OK\0DISK_ERR\0INT_ERR\0NOT_READY\0NO_FILE\0NO_PATH\0INVALID_NAME\0")
+	_T("DENIED\0EXIST\0INVALID_OBJECT\0WRITE_PROTECTED\0INVALID_DRIVE\0")
+	_T("NOT_ENABLED\0NO_FILESYSTEM\0MKFS_ABORTED\0TIMEOUT\0LOCKED\0")
+	_T("NOT_ENOUGH_CORE\0TOO_MANY_OPEN_FILES\0INVALID_PARAMETER\0");
+
 static bool mount(void);
 
 DIR dir;
@@ -21,31 +27,50 @@ static volatile struct {
 	.initiaized = false,
 };
 
-bool mount()
+void fatfs_init()
+{
+	if (mount()) {
+		s_state.initiaized = true;
+		printf("FatFs mounted\n");
+	}
+}
+
+const char *f_get_rc_string(FRESULT rc)
+{
+	FRESULT i;
+	const char *p = rcstrings;
+
+	for (i = 0; i != rc && *p; i++) {
+		while (*p++)
+			;
+	}
+	return p;
+}
+
+static bool mount()
 {
 	FRESULT res;
 	res = f_mount(&FatFs, "/", 0);
 	if (res != FR_OK) {
-		printf("f_mount error %d\n", res);
+		printf("f_mount error %s\n", f_get_rc_string(res));
 		return false;
 	}
 
 	res = f_opendir(&dir, "0:");
 	if (res != FR_OK) {
-		printf("f_opendir error %d\n", res);
+		printf("f_opendir error %s\n", f_get_rc_string(res));
 		return false;
 	}
 
 	return true;
 }
-
-void fatfs_init()
-{
-	if (mount()) {
-		s_state.initiaized = true;
-		printf("FatFs mounted\n");
-	}
-}
+/*------------------------------------------------------------------------*/
+/* Create a Synchronization Object */
+/*------------------------------------------------------------------------*/
+/* This function is called in f_mount() function to create a new
+/  synchronization object for the volume, such as semaphore and mutex.
+/  When a 0 is returned, the f_mount() function fails with FR_INT_ERR.
+*/
 
 /*
  * Return value:
-- 
GitLab