FatFS complete implementation
This commit is contained in:
parent
11e3d1fa22
commit
65602895a5
9 changed files with 126 additions and 14 deletions
|
@ -219,6 +219,7 @@
|
||||||
// #define GFILE_ALLOW_FLOATS FALSE
|
// #define GFILE_ALLOW_FLOATS FALSE
|
||||||
// #define GFILE_ALLOW_DEVICESPECIFIC FALSE
|
// #define GFILE_ALLOW_DEVICESPECIFIC FALSE
|
||||||
// #define GFILE_MAX_GFILES 3
|
// #define GFILE_MAX_GFILES 3
|
||||||
|
//#define GFILE_NEED_AUTOMOUNT FALSE
|
||||||
|
|
||||||
//#define GFILE_NEED_MEMFS FALSE
|
//#define GFILE_NEED_MEMFS FALSE
|
||||||
//#define GFILE_NEED_ROMFS FALSE
|
//#define GFILE_NEED_ROMFS FALSE
|
||||||
|
|
|
@ -56,6 +56,8 @@ typedef struct GFILEVMT {
|
||||||
bool_t (*setpos) (GFILE *f, long int pos);
|
bool_t (*setpos) (GFILE *f, long int pos);
|
||||||
long int (*getsize) (GFILE *f);
|
long int (*getsize) (GFILE *f);
|
||||||
bool_t (*eof) (GFILE *f);
|
bool_t (*eof) (GFILE *f);
|
||||||
|
bool_t (*mount) (const char *drive);
|
||||||
|
bool_t (*unmount) (const char *drive);
|
||||||
} GFILEVMT;
|
} GFILEVMT;
|
||||||
|
|
||||||
// The chain of FileSystems
|
// The chain of FileSystems
|
||||||
|
@ -475,6 +477,36 @@ bool_t gfileEOF(GFILE *f) {
|
||||||
return f->vmt->eof(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
|
* String VMT routines
|
||||||
********************************************************/
|
********************************************************/
|
||||||
|
|
|
@ -27,6 +27,7 @@ static const GFILEVMT FsCHIBIOSVMT = {
|
||||||
0, 0, 0, 0,
|
0, 0, 0, 0,
|
||||||
0, ChibiOSBFSClose, ChibiOSBFSRead, ChibiOSBFSWrite,
|
0, ChibiOSBFSClose, ChibiOSBFSRead, ChibiOSBFSWrite,
|
||||||
ChibiOSBFSSetpos, ChibiOSBFSGetsize, ChibiOSBFSEof,
|
ChibiOSBFSSetpos, ChibiOSBFSGetsize, ChibiOSBFSEof,
|
||||||
|
0, 0
|
||||||
};
|
};
|
||||||
|
|
||||||
static void ChibiOSBFSClose(GFILE *f) {
|
static void ChibiOSBFSClose(GFILE *f) {
|
||||||
|
|
|
@ -12,14 +12,6 @@
|
||||||
#include "ff.h"
|
#include "ff.h"
|
||||||
#include "ffconf.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
|
* 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 bool_t fatfsSetPos(GFILE* f, long int pos);
|
||||||
static long int fatfsGetSize(GFILE* f);
|
static long int fatfsGetSize(GFILE* f);
|
||||||
static bool_t fatfsEOF(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 = {
|
static const GFILEVMT FsFatFSVMT = {
|
||||||
GFILE_CHAINHEAD,
|
GFILE_CHAINHEAD,
|
||||||
|
@ -50,12 +44,18 @@ static const GFILEVMT FsFatFSVMT = {
|
||||||
fatfsWrite,
|
fatfsWrite,
|
||||||
fatfsSetPos,
|
fatfsSetPos,
|
||||||
fatfsGetSize,
|
fatfsGetSize,
|
||||||
fatfsEOF
|
fatfsEOF,
|
||||||
|
fatfsMount,
|
||||||
|
fatfsUnmount
|
||||||
};
|
};
|
||||||
|
|
||||||
#undef GFILE_CHAINHEAD
|
#undef GFILE_CHAINHEAD
|
||||||
#define GFILE_CHAINHEAD &FsFatFSVMT
|
#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)
|
static void _flags2mode(GFILE* f, BYTE* mode)
|
||||||
{
|
{
|
||||||
*mode = 0;
|
*mode = 0;
|
||||||
|
@ -123,7 +123,10 @@ static bool_t fatfsOpen(GFILE* f, const char* fname)
|
||||||
FIL* fd;
|
FIL* fd;
|
||||||
BYTE mode;
|
BYTE mode;
|
||||||
FRESULT ferr;
|
FRESULT ferr;
|
||||||
|
/*
|
||||||
|
if (!fatfs_mounted && !fatfsMount(""))
|
||||||
|
return FALSE;
|
||||||
|
*/
|
||||||
if (!(fd = gfxAlloc(sizeof(FIL))))
|
if (!(fd = gfxAlloc(sizeof(FIL))))
|
||||||
return FALSE;
|
return FALSE;
|
||||||
|
|
||||||
|
@ -131,7 +134,7 @@ static bool_t fatfsOpen(GFILE* f, const char* fname)
|
||||||
|
|
||||||
ferr = f_open(fd, fname, mode);
|
ferr = f_open(fd, fname, mode);
|
||||||
if (ferr != FR_OK) {
|
if (ferr != FR_OK) {
|
||||||
gfxFree( (FIL*)f->obj );
|
gfxFree(fd);
|
||||||
f->obj = 0;
|
f->obj = 0;
|
||||||
|
|
||||||
return FALSE;
|
return FALSE;
|
||||||
|
@ -144,10 +147,10 @@ static bool_t fatfsOpen(GFILE* f, const char* fname)
|
||||||
|
|
||||||
static void fatfsClose(GFILE* f)
|
static void fatfsClose(GFILE* f)
|
||||||
{
|
{
|
||||||
if ((FIL*)f->obj != 0)
|
if ((FIL*)f->obj != 0) {
|
||||||
gfxFree( (FIL*)f->obj );
|
gfxFree( (FIL*)f->obj );
|
||||||
|
f_close( (FIL*)f->obj );
|
||||||
f_close( (FIL*)f->obj );
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static int fatfsRead(GFILE* f, void* buf, int size)
|
static int fatfsRead(GFILE* f, void* buf, int size)
|
||||||
|
@ -192,3 +195,31 @@ static bool_t fatfsEOF(GFILE* f)
|
||||||
return FALSE;
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
|
@ -26,6 +26,7 @@ static const GFILEVMT FsMemVMT = {
|
||||||
0, 0, 0, 0,
|
0, 0, 0, 0,
|
||||||
0, 0, MEMRead, MEMWrite,
|
0, 0, MEMRead, MEMWrite,
|
||||||
MEMSetpos, 0, 0,
|
MEMSetpos, 0, 0,
|
||||||
|
0, 0
|
||||||
};
|
};
|
||||||
|
|
||||||
static int MEMRead(GFILE *f, void *buf, int size) {
|
static int MEMRead(GFILE *f, void *buf, int size) {
|
||||||
|
|
|
@ -46,6 +46,7 @@ static const GFILEVMT FsNativeVMT = {
|
||||||
NativeDel, NativeExists, NativeFilesize, NativeRen,
|
NativeDel, NativeExists, NativeFilesize, NativeRen,
|
||||||
NativeOpen, NativeClose, NativeRead, NativeWrite,
|
NativeOpen, NativeClose, NativeRead, NativeWrite,
|
||||||
NativeSetpos, NativeGetsize, NativeEof,
|
NativeSetpos, NativeGetsize, NativeEof,
|
||||||
|
0, 0
|
||||||
};
|
};
|
||||||
#undef GFILE_CHAINHEAD
|
#undef GFILE_CHAINHEAD
|
||||||
#define GFILE_CHAINHEAD &FsNativeVMT
|
#define GFILE_CHAINHEAD &FsNativeVMT
|
||||||
|
|
|
@ -52,6 +52,7 @@ static const GFILEVMT FsROMVMT = {
|
||||||
0, ROMExists, ROMFilesize, 0,
|
0, ROMExists, ROMFilesize, 0,
|
||||||
ROMOpen, ROMClose, ROMRead, 0,
|
ROMOpen, ROMClose, ROMRead, 0,
|
||||||
ROMSetpos, ROMGetsize, ROMEof,
|
ROMSetpos, ROMGetsize, ROMEof,
|
||||||
|
0, 0
|
||||||
};
|
};
|
||||||
#undef GFILE_CHAINHEAD
|
#undef GFILE_CHAINHEAD
|
||||||
#define GFILE_CHAINHEAD &FsROMVMT
|
#define GFILE_CHAINHEAD &FsROMVMT
|
||||||
|
|
|
@ -200,10 +200,42 @@ extern "C" {
|
||||||
#if GFILE_NEED_CHIBIOSFS && GFX_USE_OS_CHIBIOS
|
#if GFILE_NEED_CHIBIOSFS && GFX_USE_OS_CHIBIOS
|
||||||
GFILE * gfileOpenBaseFileStream(void *BaseFileStreamPtr, const char *mode);
|
GFILE * gfileOpenBaseFileStream(void *BaseFileStreamPtr, const char *mode);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if GFILE_NEED_MEMFS
|
#if GFILE_NEED_MEMFS
|
||||||
GFILE * gfileOpenMemory(void *memptr, const char *mode);
|
GFILE * gfileOpenMemory(void *memptr, const char *mode);
|
||||||
#endif
|
#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
|
#if GFILE_NEED_PRINTG
|
||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
|
|
||||||
|
|
|
@ -20,6 +20,18 @@
|
||||||
* @name GFILE Functionality to be included
|
* @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
|
* @brief Include printg, fprintg etc functions
|
||||||
* @details Defaults to FALSE
|
* @details Defaults to FALSE
|
||||||
|
|
Loading…
Add table
Reference in a new issue