From d7129e6058803743c0b183b4ac6ed10ed00d3c34 Mon Sep 17 00:00:00 2001 From: Joel Bodenmann Date: Tue, 24 Jun 2014 05:56:13 +0200 Subject: adding fatfs directory --- src/gfile/inc_fatfs.c | 89 ++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 88 insertions(+), 1 deletion(-) (limited to 'src/gfile/inc_fatfs.c') diff --git a/src/gfile/inc_fatfs.c b/src/gfile/inc_fatfs.c index d49cfe7a..0a8a4cd1 100644 --- a/src/gfile/inc_fatfs.c +++ b/src/gfile/inc_fatfs.c @@ -12,4 +12,91 @@ /******************************************************** * The FAT file-system VMT ********************************************************/ -#error "GFILE: FATFS Not implemented yet" + +static bool_t fatfsDel(const char* fname); +static bool_t fatfsExists(const char* fname); +static long int fatfsFileSize(const char* fname); +static bool_t fatfsRename(const char* oldname, const char* newname); +static bool_t fatfsOpen(GFILE* f, const char* fname); +static void fatfsClose(GFILE* f); +static int fatfsRead(GFILE* f, void* buf, int size); +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 const GFILEVMT FsFatFSVMT = { + GFILE_CHAINHEAD, + GFSFLG_SEEKABLE, + 'F', + fatfsDel, + fatfsExists, + fatfsFileSize, + fatfsRename, + fatfsOpen, + fatfsClose, + fatfsRead, + fatfsWrite, + fatfsSetPos, + fatfsGetSize, + fatfsEOF +}; + +#undef GFILE_CHAINHEAD +#define GFILE_CHAINHEAD &FsFatFSVMT + +static bool_t fatfsDel(const char* fname) +{ + +} + +static bool_t fatfsExists(const char* fname) +{ + +} + +static long int fatfsFileSize(const char* fname) +{ + +} + +static bool_t fatfsRename(const char* oldname, const char* newname) +{ + +} + +static bool_t fatfsOpen(GFILE* f, const char* fname) +{ + +} + +static void fatfsClose(GFILE* f) +{ + +} + +static int fatfsRead(GFILE* f, void* buf, int size) +{ + +} + +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) +{ + +} + -- cgit v1.2.3 From b054a7220f5d6461f9485f18419f27f14708c81c Mon Sep 17 00:00:00 2001 From: Joel Bodenmann Date: Wed, 25 Jun 2014 05:23:57 +0200 Subject: initial implementation - untested --- src/gfile/inc_fatfs.c | 75 ++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 72 insertions(+), 3 deletions(-) (limited to 'src/gfile/inc_fatfs.c') diff --git a/src/gfile/inc_fatfs.c b/src/gfile/inc_fatfs.c index 0a8a4cd1..c90b6c30 100644 --- a/src/gfile/inc_fatfs.c +++ b/src/gfile/inc_fatfs.c @@ -9,6 +9,9 @@ * This file is included by src/gfile/gfile.c */ +#include "ff.h" +#include "ffconf.h" + /******************************************************** * The FAT file-system VMT ********************************************************/ @@ -45,58 +48,124 @@ static const GFILEVMT FsFatFSVMT = { #undef GFILE_CHAINHEAD #define GFILE_CHAINHEAD &FsFatFSVMT +static void _flags2mode(GFILE* f, BYTE* mode) +{ + *mode = 0; + + if (f->flags & GFILEFLG_MUSTEXIST) + *mode |= FA_READ; + else if (f->flags & GFILEFLG_APPEND) + *mode |= 0; /* ToDO */ + else + *mode |= FA_WRITE; + + /* ToDo - Complete */ +} + static bool_t fatfsDel(const char* fname) { + FRESULT ferr; + + ferr = f_unlink( (const TCHAR*)fname ); + if (ferr != FR_OK) + return FALSE; + return TRUE; } static bool_t fatfsExists(const char* fname) { + (void)fname; + + /* ToDo */ + return TRUE; } static long int fatfsFileSize(const char* fname) { + FRESULT ferr; + FILINFO fno; + ferr = f_stat( (const TCHAR*)fname, &fno ); + if (ferr != FR_OK) + return 0; + + return (long int)fno.fsize; } static bool_t fatfsRename(const char* oldname, const char* newname) { + FRESULT ferr; + + ferr = f_rename( (const TCHAR*)oldname, (const TCHAR*)newname ); + if (ferr != FR_OK) + return FALSE; + return TRUE; } static bool_t fatfsOpen(GFILE* f, const char* fname) { + FIL* fd = 0; + BYTE mode; + FRESULT ferr; + _flags2mode(f, &mode); + + ferr = f_open(fd, fname, mode); + if (ferr != FR_OK) + return FALSE; + + f->obj = (void*)fd; + + return TRUE; } static void fatfsClose(GFILE* f) { - + f_close( (FIL*)f->obj ); } static int fatfsRead(GFILE* f, void* buf, int size) { + int br; + + f_read( (FIL*)f->obj, buf, size, (UINT*)&br); + return br; } static int fatfsWrite(GFILE* f, const void* buf, int size) { + int wr; + f_write( (FIL*)f->obj, buf, size, (UINT*)&wr); + + return wr; } static bool_t fatfsSetPos(GFILE* f, long int pos) { + FRESULT ferr; + + ferr = f_lseek( (FIL*)f->obj, (DWORD)pos ); + if (ferr != FR_OK) + return FALSE; + return TRUE; } static long int fatfsGetSize(GFILE* f) { - + return (long int)f_tell( (FIL*)f->obj ); } static bool_t fatfsEOF(GFILE* f) { - + if ( f_eof( (FIL*)f->obj ) != 0) + return TRUE; + else + return FALSE; } -- cgit v1.2.3 From 1785f32976ece91f7b483222c0eb688ed61653e6 Mon Sep 17 00:00:00 2001 From: Joel Bodenmann Date: Wed, 25 Jun 2014 05:31:41 +0200 Subject: added ToDo list --- src/gfile/inc_fatfs.c | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'src/gfile/inc_fatfs.c') diff --git a/src/gfile/inc_fatfs.c b/src/gfile/inc_fatfs.c index c90b6c30..911cf59c 100644 --- a/src/gfile/inc_fatfs.c +++ b/src/gfile/inc_fatfs.c @@ -12,6 +12,15 @@ #include "ff.h" #include "ffconf.h" +/* + * ToDo: + * + * - fatfsExists() + * - f_mount has to be called before the disk can be accessed + * - complete _flags2mode() + * - restructure provided diskio.c files + * - do full testing + */ /******************************************************** * The FAT file-system VMT ********************************************************/ -- cgit v1.2.3 From 11e3d1fa22ee8df088621dfefe5ebfa4b0697d9c Mon Sep 17 00:00:00 2001 From: Joel Bodenmann Date: Fri, 27 Jun 2014 00:38:46 +0200 Subject: first working FatFS implementation. Modes ToDo --- src/gfile/inc_fatfs.c | 38 ++++++++++++++++++++++++++------------ 1 file changed, 26 insertions(+), 12 deletions(-) (limited to 'src/gfile/inc_fatfs.c') diff --git a/src/gfile/inc_fatfs.c b/src/gfile/inc_fatfs.c index 911cf59c..a3976d24 100644 --- a/src/gfile/inc_fatfs.c +++ b/src/gfile/inc_fatfs.c @@ -15,12 +15,11 @@ /* * ToDo: * - * - fatfsExists() * - f_mount has to be called before the disk can be accessed * - complete _flags2mode() * - restructure provided diskio.c files - * - do full testing */ + /******************************************************** * The FAT file-system VMT ********************************************************/ @@ -39,7 +38,7 @@ static bool_t fatfsEOF(GFILE* f); static const GFILEVMT FsFatFSVMT = { GFILE_CHAINHEAD, - GFSFLG_SEEKABLE, + GFSFLG_WRITEABLE | GFSFLG_SEEKABLE, 'F', fatfsDel, fatfsExists, @@ -61,12 +60,14 @@ static void _flags2mode(GFILE* f, BYTE* mode) { *mode = 0; - if (f->flags & GFILEFLG_MUSTEXIST) + if (f->flags & GFILEFLG_READ) *mode |= FA_READ; - else if (f->flags & GFILEFLG_APPEND) - *mode |= 0; /* ToDO */ - else + if (f->flags & GFILEFLG_WRITE) *mode |= FA_WRITE; + if (f->flags & GFILEFLG_APPEND) + *mode |= 0; // ToDo + if (f->flags & GFILEFLG_TRUNC) + *mode |= FA_CREATE_ALWAYS; /* ToDo - Complete */ } @@ -84,9 +85,12 @@ static bool_t fatfsDel(const char* fname) static bool_t fatfsExists(const char* fname) { - (void)fname; + FRESULT ferr; + FILINFO fno; - /* ToDo */ + ferr = f_stat( (const TCHAR*)fname, &fno); + if (ferr != FR_OK) + return FALSE; return TRUE; } @@ -116,15 +120,22 @@ static bool_t fatfsRename(const char* oldname, const char* newname) static bool_t fatfsOpen(GFILE* f, const char* fname) { - FIL* fd = 0; + FIL* fd; BYTE mode; FRESULT ferr; + if (!(fd = gfxAlloc(sizeof(FIL)))) + return FALSE; + _flags2mode(f, &mode); ferr = f_open(fd, fname, mode); - if (ferr != FR_OK) + if (ferr != FR_OK) { + gfxFree( (FIL*)f->obj ); + f->obj = 0; + return FALSE; + } f->obj = (void*)fd; @@ -133,6 +144,9 @@ static bool_t fatfsOpen(GFILE* f, const char* fname) static void fatfsClose(GFILE* f) { + if ((FIL*)f->obj != 0) + gfxFree( (FIL*)f->obj ); + f_close( (FIL*)f->obj ); } @@ -167,7 +181,7 @@ static bool_t fatfsSetPos(GFILE* f, long int pos) static long int fatfsGetSize(GFILE* f) { - return (long int)f_tell( (FIL*)f->obj ); + return (long int)f_size( (FIL*)f->obj ); } static bool_t fatfsEOF(GFILE* f) -- cgit v1.2.3 From 65602895a59f9e9c79ca65342ee3b843ba37183f Mon Sep 17 00:00:00 2001 From: Joel Bodenmann Date: Fri, 27 Jun 2014 06:10:18 +0200 Subject: FatFS complete implementation --- src/gfile/inc_fatfs.c | 59 +++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 45 insertions(+), 14 deletions(-) (limited to 'src/gfile/inc_fatfs.c') 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; +} + -- cgit v1.2.3 From a9f1520e02ed5425abbfb7e621f103053c2e3799 Mon Sep 17 00:00:00 2001 From: inmarket Date: Fri, 27 Jun 2014 23:04:01 +1000 Subject: Fatfs Cleanup --- src/gfile/inc_fatfs.c | 32 +++++++++++++++----------------- 1 file changed, 15 insertions(+), 17 deletions(-) (limited to 'src/gfile/inc_fatfs.c') diff --git a/src/gfile/inc_fatfs.c b/src/gfile/inc_fatfs.c index ff75eb3b..a9066bf9 100644 --- a/src/gfile/inc_fatfs.c +++ b/src/gfile/inc_fatfs.c @@ -56,20 +56,21 @@ static const GFILEVMT FsFatFSVMT = { static bool_t fatfs_mounted = FALSE; static FATFS fatfs_fs; -static void _flags2mode(GFILE* f, BYTE* mode) +static BYTE fatfs_flags2mode(GFILE* f) { - *mode = 0; + BYTE mode = 0; if (f->flags & GFILEFLG_READ) - *mode |= FA_READ; + mode |= FA_READ; if (f->flags & GFILEFLG_WRITE) - *mode |= FA_WRITE; + mode |= FA_WRITE; if (f->flags & GFILEFLG_APPEND) - *mode |= 0; // ToDo + mode |= 0; // ToDo if (f->flags & GFILEFLG_TRUNC) - *mode |= FA_CREATE_ALWAYS; + mode |= FA_CREATE_ALWAYS; - /* ToDo - Complete */ + /* ToDo - Complete */ + return mode; } static bool_t fatfsDel(const char* fname) @@ -121,19 +122,16 @@ static bool_t fatfsRename(const char* oldname, const char* newname) static bool_t fatfsOpen(GFILE* f, const char* fname) { FIL* fd; - BYTE mode; - FRESULT ferr; -/* - if (!fatfs_mounted && !fatfsMount("")) - return FALSE; -*/ + + #if !GFILE_NEED_NOAUTOMOUNT + if (!fatfs_mounted && !fatfsMount("")) + return FALSE; + #endif + if (!(fd = gfxAlloc(sizeof(FIL)))) return FALSE; - _flags2mode(f, &mode); - - ferr = f_open(fd, fname, mode); - if (ferr != FR_OK) { + if (f_open(fd, fname, fatfs_flags2mode(f)) != FR_OK) { gfxFree(fd); f->obj = 0; -- cgit v1.2.3 From 5c13e08e76ae1d75240e1e8e46d1e9c3a8ba50cd Mon Sep 17 00:00:00 2001 From: Joel Bodenmann Date: Sun, 29 Jun 2014 16:15:46 +0200 Subject: added gfileSync() and autosync feature --- src/gfile/inc_fatfs.c | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) (limited to 'src/gfile/inc_fatfs.c') diff --git a/src/gfile/inc_fatfs.c b/src/gfile/inc_fatfs.c index a9066bf9..447b8a71 100644 --- a/src/gfile/inc_fatfs.c +++ b/src/gfile/inc_fatfs.c @@ -29,6 +29,7 @@ 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 bool_t fatfsSync(GFILE* f); static const GFILEVMT FsFatFSVMT = { GFILE_CHAINHEAD, @@ -46,7 +47,8 @@ static const GFILEVMT FsFatFSVMT = { fatfsGetSize, fatfsEOF, fatfsMount, - fatfsUnmount + fatfsUnmount, + fatfsSync }; #undef GFILE_CHAINHEAD @@ -140,6 +142,13 @@ static bool_t fatfsOpen(GFILE* f, const char* fname) f->obj = (void*)fd; + #if !GFILE_NEED_NOAUTOSYNC + // no need to sync when not opening for write + if (f->flags & GFILEFLG_WRITE) { + f_sync( (FIL*)f->obj ); + } + #endif + return TRUE; } @@ -165,6 +174,7 @@ static int fatfsWrite(GFILE* f, const void* buf, int size) int wr; f_write( (FIL*)f->obj, buf, size, (UINT*)&wr); + f_sync( (FIL*)f->obj ); return wr; } @@ -221,3 +231,15 @@ static bool_t fatfsUnmount(const char* drive) return FALSE; } +static bool_t fatfsSync(GFILE *f) +{ + FRESULT ferr; + + ferr = f_sync( (FIL*)f->obj ); + if (ferr != FR_OK) { + return FALSE; + } + + return TRUE; +} + -- cgit v1.2.3 From 1a2e98af967d4433e98c5fa388cb816b1af46e2d Mon Sep 17 00:00:00 2001 From: inmarket Date: Wed, 2 Jul 2014 09:36:00 +1000 Subject: Use the GFILE_NEED_NOAUTOSYNC for syncing in the write as well. --- src/gfile/inc_fatfs.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'src/gfile/inc_fatfs.c') diff --git a/src/gfile/inc_fatfs.c b/src/gfile/inc_fatfs.c index 447b8a71..8d7233e7 100644 --- a/src/gfile/inc_fatfs.c +++ b/src/gfile/inc_fatfs.c @@ -174,7 +174,9 @@ static int fatfsWrite(GFILE* f, const void* buf, int size) int wr; f_write( (FIL*)f->obj, buf, size, (UINT*)&wr); - f_sync( (FIL*)f->obj ); + #if !GFILE_NEED_NOAUTOSYNC + f_sync( (FIL*)f->obj ); + #endif return wr; } -- cgit v1.2.3