forked version of FATFileSystemEx

Fork of FATFileSystem by mbed official

Committer:
bkc_mbed
Date:
Wed Aug 20 00:20:41 2014 +0000
Revision:
4:c9dcfcfd0c5a
Parent:
2:b6669c987c8e
added tiny mode

Who changed what in which revision?

UserRevisionLine numberNew contents of line
emilmont 1:46ce1e16c870 1 /* mbed Microcontroller Library
emilmont 1:46ce1e16c870 2 * Copyright (c) 2006-2012 ARM Limited
emilmont 1:46ce1e16c870 3 *
emilmont 1:46ce1e16c870 4 * Permission is hereby granted, free of charge, to any person obtaining a copy
emilmont 1:46ce1e16c870 5 * of this software and associated documentation files (the "Software"), to deal
emilmont 1:46ce1e16c870 6 * in the Software without restriction, including without limitation the rights
emilmont 1:46ce1e16c870 7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
emilmont 1:46ce1e16c870 8 * copies of the Software, and to permit persons to whom the Software is
emilmont 1:46ce1e16c870 9 * furnished to do so, subject to the following conditions:
emilmont 1:46ce1e16c870 10 *
emilmont 1:46ce1e16c870 11 * The above copyright notice and this permission notice shall be included in
emilmont 1:46ce1e16c870 12 * all copies or substantial portions of the Software.
emilmont 1:46ce1e16c870 13 *
emilmont 1:46ce1e16c870 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
emilmont 1:46ce1e16c870 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
emilmont 1:46ce1e16c870 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
emilmont 1:46ce1e16c870 17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
emilmont 1:46ce1e16c870 18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
emilmont 1:46ce1e16c870 19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
emilmont 1:46ce1e16c870 20 * SOFTWARE.
emilmont 1:46ce1e16c870 21 */
emilmont 1:46ce1e16c870 22 #include "mbed.h"
emilmont 1:46ce1e16c870 23
emilmont 1:46ce1e16c870 24 #include "ffconf.h"
emilmont 2:b6669c987c8e 25 #include "mbed_debug.h"
emilmont 1:46ce1e16c870 26
emilmont 1:46ce1e16c870 27 #include "FATFileSystem.h"
emilmont 1:46ce1e16c870 28 #include "FATFileHandle.h"
emilmont 1:46ce1e16c870 29 #include "FATDirHandle.h"
emilmont 1:46ce1e16c870 30
bkc_mbed 4:c9dcfcfd0c5a 31 #ifdef FAT_TINY
bkc_mbed 4:c9dcfcfd0c5a 32 #define FA_RDWR FA_READ
bkc_mbed 4:c9dcfcfd0c5a 33 #define FA_WRITE FA_READ
bkc_mbed 4:c9dcfcfd0c5a 34 #define FA_CREATE_ALWAYS 0
bkc_mbed 4:c9dcfcfd0c5a 35 #define FA_OPEN_ALWAYS 0
bkc_mbed 4:c9dcfcfd0c5a 36 #else
bkc_mbed 4:c9dcfcfd0c5a 37 #define FA_RDWR (FA_READ|FA_WRITE)
bkc_mbed 4:c9dcfcfd0c5a 38 #endif
bkc_mbed 4:c9dcfcfd0c5a 39
bkc_mbed 4:c9dcfcfd0c5a 40
emilmont 1:46ce1e16c870 41 DWORD get_fattime(void) {
emilmont 1:46ce1e16c870 42 time_t rawtime;
emilmont 1:46ce1e16c870 43 time(&rawtime);
emilmont 1:46ce1e16c870 44 struct tm *ptm = localtime(&rawtime);
emilmont 1:46ce1e16c870 45 return (DWORD)(ptm->tm_year - 80) << 25
emilmont 1:46ce1e16c870 46 | (DWORD)(ptm->tm_mon + 1 ) << 21
emilmont 1:46ce1e16c870 47 | (DWORD)(ptm->tm_mday ) << 16
emilmont 1:46ce1e16c870 48 | (DWORD)(ptm->tm_hour ) << 11
emilmont 1:46ce1e16c870 49 | (DWORD)(ptm->tm_min ) << 5
emilmont 1:46ce1e16c870 50 | (DWORD)(ptm->tm_sec/2 );
emilmont 1:46ce1e16c870 51 }
emilmont 1:46ce1e16c870 52
emilmont 1:46ce1e16c870 53 FATFileSystem *FATFileSystem::_ffs[_VOLUMES] = {0};
emilmont 1:46ce1e16c870 54
emilmont 1:46ce1e16c870 55 FATFileSystem::FATFileSystem(const char* n) : FileSystemLike(n) {
emilmont 1:46ce1e16c870 56 debug_if(FFS_DBG, "FATFileSystem(%s)\n", n);
emilmont 1:46ce1e16c870 57 for(int i=0; i<_VOLUMES; i++) {
emilmont 1:46ce1e16c870 58 if(_ffs[i] == 0) {
emilmont 1:46ce1e16c870 59 _ffs[i] = this;
emilmont 1:46ce1e16c870 60 _fsid = i;
emilmont 1:46ce1e16c870 61 debug_if(FFS_DBG, "Mounting [%s] on ffs drive [%d]\n", _name, _fsid);
emilmont 1:46ce1e16c870 62 f_mount(i, &_fs);
emilmont 1:46ce1e16c870 63 return;
emilmont 1:46ce1e16c870 64 }
emilmont 1:46ce1e16c870 65 }
emilmont 1:46ce1e16c870 66 error("Couldn't create %s in FATFileSystem::FATFileSystem\n", n);
emilmont 1:46ce1e16c870 67 }
emilmont 1:46ce1e16c870 68
emilmont 1:46ce1e16c870 69 FATFileSystem::~FATFileSystem() {
emilmont 1:46ce1e16c870 70 for (int i=0; i<_VOLUMES; i++) {
emilmont 1:46ce1e16c870 71 if (_ffs[i] == this) {
emilmont 1:46ce1e16c870 72 _ffs[i] = 0;
emilmont 1:46ce1e16c870 73 f_mount(i, NULL);
emilmont 1:46ce1e16c870 74 }
emilmont 1:46ce1e16c870 75 }
emilmont 1:46ce1e16c870 76 }
emilmont 1:46ce1e16c870 77
emilmont 1:46ce1e16c870 78 FileHandle *FATFileSystem::open(const char* name, int flags) {
emilmont 1:46ce1e16c870 79 debug_if(FFS_DBG, "open(%s) on filesystem [%s], drv [%d]\n", name, _name, _fsid);
emilmont 1:46ce1e16c870 80 char n[64];
emilmont 1:46ce1e16c870 81 sprintf(n, "%d:/%s", _fsid, name);
emilmont 1:46ce1e16c870 82
emilmont 1:46ce1e16c870 83 /* POSIX flags -> FatFS open mode */
emilmont 1:46ce1e16c870 84 BYTE openmode;
emilmont 1:46ce1e16c870 85 if (flags & O_RDWR) {
bkc_mbed 4:c9dcfcfd0c5a 86 openmode = FA_RDWR;
emilmont 1:46ce1e16c870 87 } else if(flags & O_WRONLY) {
emilmont 1:46ce1e16c870 88 openmode = FA_WRITE;
emilmont 1:46ce1e16c870 89 } else {
emilmont 1:46ce1e16c870 90 openmode = FA_READ;
emilmont 1:46ce1e16c870 91 }
emilmont 1:46ce1e16c870 92 if(flags & O_CREAT) {
emilmont 1:46ce1e16c870 93 if(flags & O_TRUNC) {
emilmont 1:46ce1e16c870 94 openmode |= FA_CREATE_ALWAYS;
emilmont 1:46ce1e16c870 95 } else {
emilmont 1:46ce1e16c870 96 openmode |= FA_OPEN_ALWAYS;
emilmont 1:46ce1e16c870 97 }
emilmont 1:46ce1e16c870 98 }
emilmont 1:46ce1e16c870 99
emilmont 1:46ce1e16c870 100 FIL fh;
emilmont 1:46ce1e16c870 101 FRESULT res = f_open(&fh, n, openmode);
emilmont 1:46ce1e16c870 102 if (res) {
emilmont 1:46ce1e16c870 103 debug_if(FFS_DBG, "f_open('w') failed: %d\n", res);
emilmont 1:46ce1e16c870 104 return NULL;
emilmont 1:46ce1e16c870 105 }
emilmont 1:46ce1e16c870 106 if (flags & O_APPEND) {
emilmont 1:46ce1e16c870 107 f_lseek(&fh, fh.fsize);
emilmont 1:46ce1e16c870 108 }
emilmont 1:46ce1e16c870 109 return new FATFileHandle(fh);
emilmont 1:46ce1e16c870 110 }
emilmont 1:46ce1e16c870 111
emilmont 1:46ce1e16c870 112 int FATFileSystem::remove(const char *filename) {
bkc_mbed 4:c9dcfcfd0c5a 113 #ifdef FAT_TINY
bkc_mbed 4:c9dcfcfd0c5a 114 return -1;
bkc_mbed 4:c9dcfcfd0c5a 115 #else
emilmont 1:46ce1e16c870 116 FRESULT res = f_unlink(filename);
emilmont 1:46ce1e16c870 117 if (res) {
emilmont 1:46ce1e16c870 118 debug_if(FFS_DBG, "f_unlink() failed: %d\n", res);
emilmont 1:46ce1e16c870 119 return -1;
emilmont 1:46ce1e16c870 120 }
emilmont 1:46ce1e16c870 121 return 0;
bkc_mbed 4:c9dcfcfd0c5a 122 #endif
emilmont 1:46ce1e16c870 123 }
emilmont 1:46ce1e16c870 124
emilmont 1:46ce1e16c870 125 int FATFileSystem::format() {
bkc_mbed 4:c9dcfcfd0c5a 126 #ifdef FAT_TINY
bkc_mbed 4:c9dcfcfd0c5a 127 return -1;
bkc_mbed 4:c9dcfcfd0c5a 128 #else
emilmont 1:46ce1e16c870 129 FRESULT res = f_mkfs(_fsid, 0, 512); // Logical drive number, Partitioning rule, Allocation unit size (bytes per cluster)
emilmont 1:46ce1e16c870 130 if (res) {
emilmont 1:46ce1e16c870 131 debug_if(FFS_DBG, "f_mkfs() failed: %d\n", res);
emilmont 1:46ce1e16c870 132 return -1;
emilmont 1:46ce1e16c870 133 }
emilmont 1:46ce1e16c870 134 return 0;
bkc_mbed 4:c9dcfcfd0c5a 135 #endif
emilmont 1:46ce1e16c870 136 }
emilmont 1:46ce1e16c870 137
emilmont 1:46ce1e16c870 138 DirHandle *FATFileSystem::opendir(const char *name) {
emilmont 1:46ce1e16c870 139 FATFS_DIR dir;
emilmont 1:46ce1e16c870 140 FRESULT res = f_opendir(&dir, name);
emilmont 1:46ce1e16c870 141 if (res != 0) {
emilmont 1:46ce1e16c870 142 return NULL;
emilmont 1:46ce1e16c870 143 }
emilmont 1:46ce1e16c870 144 return new FATDirHandle(dir);
emilmont 1:46ce1e16c870 145 }
emilmont 1:46ce1e16c870 146
emilmont 1:46ce1e16c870 147 int FATFileSystem::mkdir(const char *name, mode_t mode) {
bkc_mbed 4:c9dcfcfd0c5a 148 #ifdef FAT_TINY
bkc_mbed 4:c9dcfcfd0c5a 149 return -1;
bkc_mbed 4:c9dcfcfd0c5a 150 #else
emilmont 1:46ce1e16c870 151 FRESULT res = f_mkdir(name);
emilmont 1:46ce1e16c870 152 return res == 0 ? 0 : -1;
bkc_mbed 4:c9dcfcfd0c5a 153 #endif
emilmont 1:46ce1e16c870 154 }