aboutsummaryrefslogtreecommitdiffstats
path: root/src/gfile/inc_fatfs.c
diff options
context:
space:
mode:
authorJoel Bodenmann <joel@unormal.org>2014-06-27 06:10:18 +0200
committerJoel Bodenmann <joel@unormal.org>2014-06-27 06:10:18 +0200
commit65602895a59f9e9c79ca65342ee3b843ba37183f (patch)
tree9110633ce9df9b2b99d550d9fd4bc46c76f6afaa /src/gfile/inc_fatfs.c
parent11e3d1fa22ee8df088621dfefe5ebfa4b0697d9c (diff)
downloaduGFX-65602895a59f9e9c79ca65342ee3b843ba37183f.tar.gz
uGFX-65602895a59f9e9c79ca65342ee3b843ba37183f.tar.bz2
uGFX-65602895a59f9e9c79ca65342ee3b843ba37183f.zip
FatFS complete implementation
Diffstat (limited to 'src/gfile/inc_fatfs.c')
-rw-r--r--src/gfile/inc_fatfs.c59
1 files changed, 45 insertions, 14 deletions
diff --git a/src/gfile/inc_fatfs.c b/src/gfile/inc_fatfs.c
index a3976d24..ff75eb3b 100644
--- a/src/gfile/inc_fatfs.c
+++ b/src/gfile/inc_fatfs.c
@@ -12,14 +12,6 @@
#include "ff.h"
#include "ffconf.h"
-/*
- * ToDo:
- *
- * - f_mount has to be called before the disk can be accessed
- * - complete _flags2mode()
- * - restructure provided diskio.c files
- */
-
/********************************************************
* The FAT file-system VMT
********************************************************/
@@ -35,6 +27,8 @@ static int fatfsWrite(GFILE* f, const void* buf, int size);
static bool_t fatfsSetPos(GFILE* f, long int pos);
static long int fatfsGetSize(GFILE* f);
static bool_t fatfsEOF(GFILE* f);
+static bool_t fatfsMount(const char* drive);
+static bool_t fatfsUnmount(const char* drive);
static const GFILEVMT FsFatFSVMT = {
GFILE_CHAINHEAD,
@@ -50,12 +44,18 @@ static const GFILEVMT FsFatFSVMT = {
fatfsWrite,
fatfsSetPos,
fatfsGetSize,
- fatfsEOF
+ fatfsEOF,
+ fatfsMount,
+ fatfsUnmount
};
#undef GFILE_CHAINHEAD
#define GFILE_CHAINHEAD &FsFatFSVMT
+// optimize these later on. Use an array to have multiple FatFS
+static bool_t fatfs_mounted = FALSE;
+static FATFS fatfs_fs;
+
static void _flags2mode(GFILE* f, BYTE* mode)
{
*mode = 0;
@@ -123,7 +123,10 @@ static bool_t fatfsOpen(GFILE* f, const char* fname)
FIL* fd;
BYTE mode;
FRESULT ferr;
-
+/*
+ if (!fatfs_mounted && !fatfsMount(""))
+ return FALSE;
+*/
if (!(fd = gfxAlloc(sizeof(FIL))))
return FALSE;
@@ -131,7 +134,7 @@ static bool_t fatfsOpen(GFILE* f, const char* fname)
ferr = f_open(fd, fname, mode);
if (ferr != FR_OK) {
- gfxFree( (FIL*)f->obj );
+ gfxFree(fd);
f->obj = 0;
return FALSE;
@@ -144,10 +147,10 @@ static bool_t fatfsOpen(GFILE* f, const char* fname)
static void fatfsClose(GFILE* f)
{
- if ((FIL*)f->obj != 0)
+ if ((FIL*)f->obj != 0) {
gfxFree( (FIL*)f->obj );
-
- f_close( (FIL*)f->obj );
+ f_close( (FIL*)f->obj );
+ }
}
static int fatfsRead(GFILE* f, void* buf, int size)
@@ -192,3 +195,31 @@ static bool_t fatfsEOF(GFILE* f)
return FALSE;
}
+static bool_t fatfsMount(const char* drive)
+{
+ FRESULT ferr;
+
+ if (!fatfs_mounted) {
+ ferr = f_mount(&fatfs_fs, drive, 1);
+ if (ferr != FR_OK)
+ return FALSE;
+ fatfs_mounted = TRUE;
+ return TRUE;
+ }
+
+ return FALSE;
+}
+
+static bool_t fatfsUnmount(const char* drive)
+{
+ (void)drive;
+
+ if (fatfs_mounted) {
+ // FatFS does not provide an unmount routine.
+ fatfs_mounted = FALSE;
+ return TRUE;
+ }
+
+ return FALSE;
+}
+