FatFS complete implementation

ugfx_release_2.6
Joel Bodenmann 2014-06-27 06:10:18 +02:00
parent 11e3d1fa22
commit 65602895a5
9 changed files with 126 additions and 14 deletions

View File

@ -219,6 +219,7 @@
// #define GFILE_ALLOW_FLOATS FALSE
// #define GFILE_ALLOW_DEVICESPECIFIC FALSE
// #define GFILE_MAX_GFILES 3
//#define GFILE_NEED_AUTOMOUNT FALSE
//#define GFILE_NEED_MEMFS FALSE
//#define GFILE_NEED_ROMFS FALSE

View File

@ -56,6 +56,8 @@ typedef struct GFILEVMT {
bool_t (*setpos) (GFILE *f, long int pos);
long int (*getsize) (GFILE *f);
bool_t (*eof) (GFILE *f);
bool_t (*mount) (const char *drive);
bool_t (*unmount) (const char *drive);
} GFILEVMT;
// The chain of FileSystems
@ -475,6 +477,36 @@ bool_t gfileEOF(GFILE *f) {
return f->vmt->eof(f);
}
#if GFILE_NEED_FATFS
bool_t gfileMount(char fs, const char* drive) {
const GFILEVMT *p;
// Find the correct VMT
for(p = FsChain; p; p = p->next) {
if (p->prefix == fs) {
if (!p->mount)
return FALSE;
return p->mount(drive);
}
}
return FALSE;
}
bool_t gfileUnmount(char fs, const char* drive) {
const GFILEVMT *p;
// Find the correct VMT
for(p = FsChain; p; p = p->next) {
if (p->prefix == fs) {
if (!p->mount)
return FALSE;
return p->unmount(drive);
}
}
return FALSE;
}
#endif
/********************************************************
* String VMT routines
********************************************************/

View File

@ -27,6 +27,7 @@ static const GFILEVMT FsCHIBIOSVMT = {
0, 0, 0, 0,
0, ChibiOSBFSClose, ChibiOSBFSRead, ChibiOSBFSWrite,
ChibiOSBFSSetpos, ChibiOSBFSGetsize, ChibiOSBFSEof,
0, 0
};
static void ChibiOSBFSClose(GFILE *f) {

View File

@ -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;
}

View File

@ -26,6 +26,7 @@ static const GFILEVMT FsMemVMT = {
0, 0, 0, 0,
0, 0, MEMRead, MEMWrite,
MEMSetpos, 0, 0,
0, 0
};
static int MEMRead(GFILE *f, void *buf, int size) {

View File

@ -46,6 +46,7 @@ static const GFILEVMT FsNativeVMT = {
NativeDel, NativeExists, NativeFilesize, NativeRen,
NativeOpen, NativeClose, NativeRead, NativeWrite,
NativeSetpos, NativeGetsize, NativeEof,
0, 0
};
#undef GFILE_CHAINHEAD
#define GFILE_CHAINHEAD &FsNativeVMT

View File

@ -52,6 +52,7 @@ static const GFILEVMT FsROMVMT = {
0, ROMExists, ROMFilesize, 0,
ROMOpen, ROMClose, ROMRead, 0,
ROMSetpos, ROMGetsize, ROMEof,
0, 0
};
#undef GFILE_CHAINHEAD
#define GFILE_CHAINHEAD &FsROMVMT

View File

@ -200,10 +200,42 @@ extern "C" {
#if GFILE_NEED_CHIBIOSFS && GFX_USE_OS_CHIBIOS
GFILE * gfileOpenBaseFileStream(void *BaseFileStreamPtr, const char *mode);
#endif
#if GFILE_NEED_MEMFS
GFILE * gfileOpenMemory(void *memptr, const char *mode);
#endif
#if GFILE_NEED_FATFS
/**
* @brief Mount a logical drive (aka partition)
*
* @details Not supported by every file system
* @details Currently just one drive at one is supported.
*
* @param[in] fs The file system (F for FatFS)
* @param[in] drive The logical drive prefix
*
* @return TRUE on success, FALSE otherwise
*
* @api
*/
bool_t gfileMount(char fs, const char *drive);
/**
* @brief Unmount a logical drive (aka partition)
*
* @details Does have no effect if @p gfileMount() as been called before hand
*
* @param[in] fs The file system (F for FatFS)
* @param[in] drive The logical drive prefix
*
* @return TRUE on success, FALSE otherwise
*
* @api
*/
bool_t gfileUnmount(char fs, const char *drive);
#endif
#if GFILE_NEED_PRINTG
#include <stdarg.h>

View File

@ -20,6 +20,18 @@
* @name GFILE Functionality to be included
* @{
*/
/**
* @brief Should the filesystem be mounted automatically
* @details The filesystem will be mounted automatically if the
* user does not do it manually.
* @details Defaults to FALSE
*
* @note Only available for filesystems that actually implement a
* mounting routine.
*/
#ifndef GFILE_NEED_AUTOMOUNT
#define GFILE_NEED_AUTOMOUNT FALSE
#endif
/**
* @brief Include printg, fprintg etc functions
* @details Defaults to FALSE