This is SDFileSystem which corrected the bug for MiMicSDK.

Dependents:   MbedFileServer_1768MiniDK2 RedWireBridge IssueDebug_gcc MiMicRemoteMCU-for-Mbed ... more

Fork of SDFileSystem by mbed official

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers FATFileSystem.cpp Source File

FATFileSystem.cpp

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2006-2012 ARM Limited
00003  *
00004  * Permission is hereby granted, free of charge, to any person obtaining a copy
00005  * of this software and associated documentation files (the "Software"), to deal
00006  * in the Software without restriction, including without limitation the rights
00007  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00008  * copies of the Software, and to permit persons to whom the Software is
00009  * furnished to do so, subject to the following conditions:
00010  *
00011  * The above copyright notice and this permission notice shall be included in
00012  * all copies or substantial portions of the Software.
00013  *
00014  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00015  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00016  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00017  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00018  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00019  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
00020  * SOFTWARE.
00021  */
00022 #include "mbed.h"
00023 
00024 #include "ffconf.h"
00025 
00026 #include "FATFileSystem.h"
00027 #include "FATFileHandle.h"
00028 #include "FATDirHandle.h"
00029 
00030 
00031 
00032 
00033 DWORD get_fattime(void) {
00034     time_t rawtime;
00035     time(&rawtime);
00036     struct tm *ptm = localtime(&rawtime);
00037     return (DWORD)(ptm->tm_year - 80) << 25
00038          | (DWORD)(ptm->tm_mon + 1  ) << 21
00039          | (DWORD)(ptm->tm_mday     ) << 16
00040          | (DWORD)(ptm->tm_hour     ) << 11
00041          | (DWORD)(ptm->tm_min      ) << 5
00042          | (DWORD)(ptm->tm_sec/2    );
00043 }
00044 
00045 FATFileSystem *FATFileSystem::_ffs[_VOLUMES] = {0};
00046 
00047 FATFileSystem::FATFileSystem(const char* n) : FileSystemLike(n) {
00048     for(int i=0; i<_VOLUMES; i++) {
00049         if(_ffs[i] == 0) {
00050             _ffs[i] = this;
00051             _fsid = i;
00052             f_mount(i, &_fs);
00053             return;
00054         }
00055     }
00056     error("Couldn't create %s in FATFileSystem::FATFileSystem\n", n);
00057 }
00058 
00059 FATFileSystem::~FATFileSystem() {
00060     for (int i=0; i<_VOLUMES; i++) {
00061         if (_ffs[i] == this) {
00062             _ffs[i] = 0;
00063             f_mount(i, NULL);
00064         }
00065     }
00066 }
00067 
00068 FileHandle *FATFileSystem::open(const char* name, int flags) {
00069 //    char n[64];
00070 //    sprintf(n, "%d:/%s", _fsid, name);
00071     
00072     /* POSIX flags -> FatFS open mode */
00073     BYTE openmode;
00074     if (flags & O_RDWR) {
00075         openmode = FA_READ|FA_WRITE;
00076     } else if(flags & O_WRONLY) {
00077         openmode = FA_WRITE;
00078     } else {
00079         openmode = FA_READ;
00080     }
00081     if(flags & O_CREAT) {
00082         if(flags & O_TRUNC) {
00083             openmode |= FA_CREATE_ALWAYS;
00084         } else {
00085             openmode |= FA_OPEN_ALWAYS;
00086         }
00087     }
00088     FIL fh;
00089     FRESULT res = f_open(&fh,this->_fsid,name, openmode);
00090     if (res) { 
00091         return NULL;
00092     }
00093     if (flags & O_APPEND) {
00094         f_lseek(&fh, fh.fsize);
00095     }
00096     return new FATFileHandle(fh);
00097 }
00098     
00099 int FATFileSystem::remove(const char *filename) {
00100     FRESULT res = f_unlink(this->_fsid,filename);
00101     if (res) { 
00102         return -1;
00103     }
00104     return 0;
00105 }
00106 
00107 int FATFileSystem::format() {
00108     FRESULT res = f_mkfs(_fsid, 0, 512); // Logical drive number, Partitioning rule, Allocation unit size (bytes per cluster)
00109     if (res) {
00110         return -1;
00111     }
00112     return 0;
00113 }
00114 DirHandle *FATFileSystem::opendir(const char *name) {
00115     FATFS_DIR dir;      
00116     FRESULT res = f_opendir(&dir,this->_fsid,name);
00117     if (res != 0) {
00118         return NULL;
00119     }
00120     return new FATDirHandle(dir);
00121 }
00122 
00123 int FATFileSystem::mkdir(const char *name, mode_t mode) {
00124     FRESULT res = f_mkdir(this->_fsid,name);
00125     return res == 0 ? 0 : -1;
00126 }