From 8d7d8666ac611a2275aa20ef6937b47d96e9c2b0 Mon Sep 17 00:00:00 2001 From: hathach Date: Wed, 24 Oct 2018 17:55:07 +0700 Subject: [PATCH] adding FatFS + ExternalFS --- .../External_ListFiles/External_ListFiles.ino | 6 +- libraries/FileSystem/src/ExternalFS.cpp | 101 ++++++++++++++++++ libraries/FileSystem/src/ExternalFS.h | 9 ++ libraries/FileSystem/src/FileIO.h | 1 + .../FileSystem/src/fatfs/source/ffconf.h | 6 +- 5 files changed, 117 insertions(+), 6 deletions(-) diff --git a/libraries/FileSystem/examples/External_ListFiles/External_ListFiles.ino b/libraries/FileSystem/examples/External_ListFiles/External_ListFiles.ino index e1a4d590..3e373926 100644 --- a/libraries/FileSystem/examples/External_ListFiles/External_ListFiles.ino +++ b/libraries/FileSystem/examples/External_ListFiles/External_ListFiles.ino @@ -31,7 +31,7 @@ void setup() Serial.println("ExternalFS List Files Example"); // Initialize Internal File System - InternalFS.begin(); + ExternalFS.begin(); // Print whole directory tree of root whose level is 0 printTreeDir("/", 0); @@ -68,13 +68,13 @@ void loop() void printTreeDir(const char* cwd, uint8_t level) { // Open the input folder - File dir(cwd, FILE_READ, InternalFS); + File dir(cwd, FILE_READ, ExternalFS); // Print root if (level == 0) Serial.println("root"); // File within folder - File item(InternalFS); + File item(ExternalFS); // Loop through the directory while( (item = dir.openNextFile(FILE_READ)) ) diff --git a/libraries/FileSystem/src/ExternalFS.cpp b/libraries/FileSystem/src/ExternalFS.cpp index b78dd67c..01c75993 100644 --- a/libraries/FileSystem/src/ExternalFS.cpp +++ b/libraries/FileSystem/src/ExternalFS.cpp @@ -40,16 +40,117 @@ #include #include "ExternalFS.h" +// up to 11 characters +#define VOLUME_LABEL "BLUEFRUIT" + +//--------------------------------------------------------------------+ +// +//--------------------------------------------------------------------+ +#if !CFG_DEBUG + +#define VERIFY_FAT(...) _GET_3RD_ARG(__VA_ARGS__, VERIFY_ERR_2ARGS, VERIFY_ERR_1ARGS)(__VA_ARGS__, NULL) +#define PRINT_FAT_ERR(_err) + +#else + +#define VERIFY_FAT(...) _GET_3RD_ARG(__VA_ARGS__, VERIFY_ERR_2ARGS, VERIFY_ERR_1ARGS)(__VA_ARGS__, dbg_strerr_fat) +#define PRINT_FAT_ERR(_err) VERIFY_MESS(_err, dbg_strerr_lfs) + +static const char* dbg_strerr_fat (int32_t err) +{ + switch ( err ) + { + case FR_DISK_ERR: + return "FR_DISK_ERR"; + case FR_INT_ERR: + return "FR_INT_ERR"; + case FR_NOT_READY: + return "FR_NOT_READY"; + case FR_NO_FILE: + return "FR_NO_FILE"; + case FR_NO_PATH: + return "FR_NO_PATH"; + case FR_INVALID_NAME: + return "FR_INVALID_NAME"; + case FR_DENIED: + return "FR_DENIED"; + case FR_EXIST: + return "FR_EXIST"; + case FR_INVALID_OBJECT: + return "FR_INVALID_OBJECT"; + case FR_WRITE_PROTECTED: + return "FR_WRITE_PROTECTED"; + case FR_INVALID_DRIVE: + return "FR_INVALID_DRIVE"; + case FR_NOT_ENABLED: + return "FR_NOT_ENABLED"; + case FR_NO_FILESYSTEM: + return "FR_NO_FILESYSTEM"; + case FR_MKFS_ABORTED: + return "FR_MKFS_ABORTED"; + case FR_TIMEOUT: + return "FR_TIMEOUT"; + case FR_LOCKED: + return "FR_LOCKED"; + case FR_NOT_ENOUGH_CORE: + return "FR_NOT_ENOUGH_CORE"; + case FR_TOO_MANY_OPEN_FILES: + return "FR_TOO_MANY_OPEN_FILES"; + case FR_INVALID_PARAMETER: + return "FR_INVALID_PARAMETER"; + default: + return "Unlikely Error"; + } +} + +#endif + +//--------------------------------------------------------------------+ +// +//--------------------------------------------------------------------+ +FatFS ExternalFS; + FatFS::FatFS () { + _fs = NULL; } + FatFS::~FatFS () { + if (_fs) rtos_free(_fs); } bool FatFS::begin () { + if ( !_fs ) _fs = (FATFS*) rtos_malloc(sizeof(FATFS)); + VERIFY(_fs); + + // Mount file system + uint32_t err = f_mount(_fs, "", 1); + + // If FileSystem is not available, format it + if ( FR_NO_FILESYSTEM == err ) + { + uint8_t workbuf[FF_MAX_SS]; + VERIFY_FAT(f_mkfs("", FM_FAT | FM_SFD, 4096, workbuf, FF_MAX_SS), false); + + VERIFY_FAT(f_setlabel(VOLUME_LABEL), false); + +// FatFile readme; +// readme.open("readme.txt", FAT_FILE_WRITE | FAT_FILE_CREATE_ALWAYS); +// readme.write(README_CONTENT); +// readme.close(); + + return true; + } + else + { + VERIFY_FAT(err, false); + } + + return true; } + File FatFS::open (char const *filename, uint8_t mode) { } diff --git a/libraries/FileSystem/src/ExternalFS.h b/libraries/FileSystem/src/ExternalFS.h index e946e58c..969b4464 100644 --- a/libraries/FileSystem/src/ExternalFS.h +++ b/libraries/FileSystem/src/ExternalFS.h @@ -39,6 +39,10 @@ #include "FileIO.h" +// External Flash uses Fat FS as file system +// http://elm-chan.org/fsw/ff/00index_e.html +#include "fatfs/source/ff.h" + class FatFS: public BluefuritLib::FileSystemClass { public: @@ -64,6 +68,11 @@ class FatFS: public BluefuritLib::FileSystemClass virtual File _f_openNextFile (void* fhdl, char const* cwd, uint8_t mode); virtual void _f_rewindDirectory (void* fhdl); + private: + FATFS* _fs; + }; +extern FatFS ExternalFS; + #endif /* EXTERNALFS_H_ */ diff --git a/libraries/FileSystem/src/FileIO.h b/libraries/FileSystem/src/FileIO.h index bc46fcf2..14411761 100644 --- a/libraries/FileSystem/src/FileIO.h +++ b/libraries/FileSystem/src/FileIO.h @@ -147,5 +147,6 @@ using namespace BluefuritLib; typedef BluefuritLib::File BluefruitFile; #include "InternalFS.h" +#include "ExternalFS.h" #endif diff --git a/libraries/FileSystem/src/fatfs/source/ffconf.h b/libraries/FileSystem/src/fatfs/source/ffconf.h index ff255770..fbfe6da0 100644 --- a/libraries/FileSystem/src/fatfs/source/ffconf.h +++ b/libraries/FileSystem/src/fatfs/source/ffconf.h @@ -38,7 +38,7 @@ / f_findnext(). (0:Disable, 1:Enable 2:Enable with matching altname[] too) */ -#define FF_USE_MKFS 0 +#define FF_USE_MKFS 1 /* This option switches f_mkfs() function. (0:Disable or 1:Enable) */ @@ -55,7 +55,7 @@ / (0:Disable or 1:Enable) Also FF_FS_READONLY needs to be 0 to enable this option. */ -#define FF_USE_LABEL 0 +#define FF_USE_LABEL 1 /* This option switches volume label functions, f_getlabel() and f_setlabel(). / (0:Disable or 1:Enable) */ @@ -68,7 +68,7 @@ / Locale and Namespace Configurations /---------------------------------------------------------------------------*/ -#define FF_CODE_PAGE 932 +#define FF_CODE_PAGE 437 /* This option specifies the OEM code page to be used on the target system. / Incorrect code page setting can cause a file open failure. /