Revised to support ability to have both SD and USB drives mounted.

Dependents:   Multi-FileSystem Multi-FileSystem

Fork of FATFileSystem by mbed official

Committer:
WiredHome
Date:
Sat Mar 12 23:58:38 2016 +0000
Revision:
8:f08059355141
Parent:
5:b3b3370574cf
Child:
9:4000fad3b21f
Changed to support simultaneous SD and USB FileSystem

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
WiredHome 8:f08059355141 31
WiredHome 8:f08059355141 32 //#define DEBUG "FtFS"
WiredHome 8:f08059355141 33 // ...
WiredHome 8:f08059355141 34 // INFO("Stuff to show %d", var); // new-line is automatically appended
WiredHome 8:f08059355141 35 //
WiredHome 8:f08059355141 36 #if (defined(DEBUG) && !defined(TARGET_LPC11U24))
WiredHome 8:f08059355141 37 #include "mbed.h"
WiredHome 8:f08059355141 38 #define INFO(x, ...) std::printf("[INF %s %4d] "x"\r\n", DEBUG, __LINE__, ##__VA_ARGS__);
WiredHome 8:f08059355141 39 #define WARN(x, ...) std::printf("[WRN %s %4d] "x"\r\n", DEBUG, __LINE__, ##__VA_ARGS__);
WiredHome 8:f08059355141 40 #define ERR(x, ...) std::printf("[ERR %s %4d] "x"\r\n", DEBUG, __LINE__, ##__VA_ARGS__);
WiredHome 8:f08059355141 41 static void HexDump(const char * title, const uint8_t * p, int count)
WiredHome 8:f08059355141 42 {
WiredHome 8:f08059355141 43 int i;
WiredHome 8:f08059355141 44 char buf[100] = "0000: ";
WiredHome 8:f08059355141 45
WiredHome 8:f08059355141 46 if (*title)
WiredHome 8:f08059355141 47 INFO("%s", title);
WiredHome 8:f08059355141 48 for (i=0; i<count; ) {
WiredHome 8:f08059355141 49 sprintf(buf + strlen(buf), "%02X ", *(p+i));
WiredHome 8:f08059355141 50 if ((++i & 0x0F) == 0x00) {
WiredHome 8:f08059355141 51 INFO("%s", buf);
WiredHome 8:f08059355141 52 if (i < count)
WiredHome 8:f08059355141 53 sprintf(buf, "%04X: ", i);
WiredHome 8:f08059355141 54 else
WiredHome 8:f08059355141 55 buf[0] = '\0';
WiredHome 8:f08059355141 56 }
WiredHome 8:f08059355141 57 }
WiredHome 8:f08059355141 58 if (strlen(buf))
WiredHome 8:f08059355141 59 INFO("%s", buf);
WiredHome 8:f08059355141 60 }
WiredHome 8:f08059355141 61 #else
WiredHome 8:f08059355141 62 #define INFO(x, ...)
WiredHome 8:f08059355141 63 #define WARN(x, ...)
WiredHome 8:f08059355141 64 #define ERR(x, ...)
WiredHome 8:f08059355141 65 #define HexDump(a, b, c)
WiredHome 8:f08059355141 66 #endif
WiredHome 8:f08059355141 67
emilmont 1:46ce1e16c870 68 DWORD get_fattime(void) {
emilmont 1:46ce1e16c870 69 time_t rawtime;
emilmont 1:46ce1e16c870 70 time(&rawtime);
emilmont 1:46ce1e16c870 71 struct tm *ptm = localtime(&rawtime);
emilmont 1:46ce1e16c870 72 return (DWORD)(ptm->tm_year - 80) << 25
emilmont 1:46ce1e16c870 73 | (DWORD)(ptm->tm_mon + 1 ) << 21
emilmont 1:46ce1e16c870 74 | (DWORD)(ptm->tm_mday ) << 16
emilmont 1:46ce1e16c870 75 | (DWORD)(ptm->tm_hour ) << 11
emilmont 1:46ce1e16c870 76 | (DWORD)(ptm->tm_min ) << 5
emilmont 1:46ce1e16c870 77 | (DWORD)(ptm->tm_sec/2 );
emilmont 1:46ce1e16c870 78 }
emilmont 1:46ce1e16c870 79
emilmont 1:46ce1e16c870 80 FATFileSystem *FATFileSystem::_ffs[_VOLUMES] = {0};
emilmont 1:46ce1e16c870 81
emilmont 1:46ce1e16c870 82 FATFileSystem::FATFileSystem(const char* n) : FileSystemLike(n) {
WiredHome 8:f08059355141 83 INFO("FATFileSystem(%s)", n);
emilmont 1:46ce1e16c870 84 for(int i=0; i<_VOLUMES; i++) {
WiredHome 8:f08059355141 85 if(_ffs[i] == this) // 2nd attempt to mount the same drive
WiredHome 8:f08059355141 86 return;
emilmont 1:46ce1e16c870 87 if(_ffs[i] == 0) {
emilmont 1:46ce1e16c870 88 _ffs[i] = this;
mbed_official 5:b3b3370574cf 89 _fsid[0] = '0' + i;
WiredHome 8:f08059355141 90 _fsid[1] = ':';
WiredHome 8:f08059355141 91 _fsid[2] = '\0';
WiredHome 8:f08059355141 92 INFO("Mounting [%s] on ffs drive [%s]", _name, _fsid);
mbed_official 5:b3b3370574cf 93 f_mount(&_fs, _fsid, 0);
WiredHome 8:f08059355141 94 for (int d=0; d<_VOLUMES; d++) {
WiredHome 8:f08059355141 95 INFO(" _ffs[%d] = %p", d, _ffs[d]);
WiredHome 8:f08059355141 96 }
emilmont 1:46ce1e16c870 97 return;
emilmont 1:46ce1e16c870 98 }
emilmont 1:46ce1e16c870 99 }
WiredHome 8:f08059355141 100 error("Couldn't create %s in FATFileSystem::FATFileSystem\r\n", n);
emilmont 1:46ce1e16c870 101 }
emilmont 1:46ce1e16c870 102
emilmont 1:46ce1e16c870 103 FATFileSystem::~FATFileSystem() {
emilmont 1:46ce1e16c870 104 for (int i=0; i<_VOLUMES; i++) {
emilmont 1:46ce1e16c870 105 if (_ffs[i] == this) {
emilmont 1:46ce1e16c870 106 _ffs[i] = 0;
mbed_official 5:b3b3370574cf 107 f_mount(NULL, _fsid, 0);
emilmont 1:46ce1e16c870 108 }
emilmont 1:46ce1e16c870 109 }
emilmont 1:46ce1e16c870 110 }
emilmont 1:46ce1e16c870 111
emilmont 1:46ce1e16c870 112 FileHandle *FATFileSystem::open(const char* name, int flags) {
WiredHome 8:f08059355141 113 INFO("open(%s) on filesystem [%s], drv [%s]", name, _name, _fsid);
emilmont 1:46ce1e16c870 114 char n[64];
WiredHome 8:f08059355141 115 sprintf(n, "%s/%s", _fsid, name);
WiredHome 8:f08059355141 116 INFO(" :: open(%s)", n);
WiredHome 8:f08059355141 117
emilmont 1:46ce1e16c870 118 /* POSIX flags -> FatFS open mode */
emilmont 1:46ce1e16c870 119 BYTE openmode;
emilmont 1:46ce1e16c870 120 if (flags & O_RDWR) {
emilmont 1:46ce1e16c870 121 openmode = FA_READ|FA_WRITE;
emilmont 1:46ce1e16c870 122 } else if(flags & O_WRONLY) {
emilmont 1:46ce1e16c870 123 openmode = FA_WRITE;
emilmont 1:46ce1e16c870 124 } else {
emilmont 1:46ce1e16c870 125 openmode = FA_READ;
emilmont 1:46ce1e16c870 126 }
emilmont 1:46ce1e16c870 127 if(flags & O_CREAT) {
emilmont 1:46ce1e16c870 128 if(flags & O_TRUNC) {
emilmont 1:46ce1e16c870 129 openmode |= FA_CREATE_ALWAYS;
emilmont 1:46ce1e16c870 130 } else {
emilmont 1:46ce1e16c870 131 openmode |= FA_OPEN_ALWAYS;
emilmont 1:46ce1e16c870 132 }
emilmont 1:46ce1e16c870 133 }
mbed_official 4:3ff2606d5713 134
emilmont 1:46ce1e16c870 135 FIL fh;
emilmont 1:46ce1e16c870 136 FRESULT res = f_open(&fh, n, openmode);
mbed_official 4:3ff2606d5713 137 if (res) {
WiredHome 8:f08059355141 138 INFO("f_open('w') failed: %d", res);
emilmont 1:46ce1e16c870 139 return NULL;
emilmont 1:46ce1e16c870 140 }
emilmont 1:46ce1e16c870 141 if (flags & O_APPEND) {
emilmont 1:46ce1e16c870 142 f_lseek(&fh, fh.fsize);
emilmont 1:46ce1e16c870 143 }
emilmont 1:46ce1e16c870 144 return new FATFileHandle(fh);
emilmont 1:46ce1e16c870 145 }
mbed_official 4:3ff2606d5713 146
WiredHome 8:f08059355141 147 int FATFileSystem::remove(const char *name) {
WiredHome 8:f08059355141 148 INFO("remove(%s) on filesystem [%s], drv [%s]", name, _name, _fsid);
WiredHome 8:f08059355141 149 char n[64];
WiredHome 8:f08059355141 150 sprintf(n, "%s/%s", _fsid, name);
WiredHome 8:f08059355141 151 INFO(" :: remove(%s)", n);
WiredHome 8:f08059355141 152 FRESULT res = f_unlink(n);
mbed_official 4:3ff2606d5713 153 if (res) {
WiredHome 8:f08059355141 154 INFO("f_unlink() failed: %d", res);
emilmont 1:46ce1e16c870 155 return -1;
emilmont 1:46ce1e16c870 156 }
emilmont 1:46ce1e16c870 157 return 0;
emilmont 1:46ce1e16c870 158 }
emilmont 1:46ce1e16c870 159
mbed_official 4:3ff2606d5713 160 int FATFileSystem::rename(const char *oldname, const char *newname) {
WiredHome 8:f08059355141 161 char o[64], n[64];
WiredHome 8:f08059355141 162 sprintf(o, "%s/%s", _fsid, oldname);
WiredHome 8:f08059355141 163 sprintf(n, "%s/%s", _fsid, newname);
WiredHome 8:f08059355141 164 INFO(" :: rename(%s,%s)", o, n);
mbed_official 4:3ff2606d5713 165 FRESULT res = f_rename(oldname, newname);
mbed_official 4:3ff2606d5713 166 if (res) {
WiredHome 8:f08059355141 167 INFO("f_rename() failed: %d", res);
mbed_official 4:3ff2606d5713 168 return -1;
mbed_official 4:3ff2606d5713 169 }
mbed_official 4:3ff2606d5713 170 return 0;
mbed_official 4:3ff2606d5713 171 }
mbed_official 4:3ff2606d5713 172
emilmont 1:46ce1e16c870 173 int FATFileSystem::format() {
emilmont 1:46ce1e16c870 174 FRESULT res = f_mkfs(_fsid, 0, 512); // Logical drive number, Partitioning rule, Allocation unit size (bytes per cluster)
emilmont 1:46ce1e16c870 175 if (res) {
WiredHome 8:f08059355141 176 INFO("f_mkfs() failed: %d", res);
emilmont 1:46ce1e16c870 177 return -1;
emilmont 1:46ce1e16c870 178 }
emilmont 1:46ce1e16c870 179 return 0;
emilmont 1:46ce1e16c870 180 }
emilmont 1:46ce1e16c870 181
emilmont 1:46ce1e16c870 182 DirHandle *FATFileSystem::opendir(const char *name) {
emilmont 1:46ce1e16c870 183 FATFS_DIR dir;
emilmont 1:46ce1e16c870 184 FRESULT res = f_opendir(&dir, name);
emilmont 1:46ce1e16c870 185 if (res != 0) {
emilmont 1:46ce1e16c870 186 return NULL;
emilmont 1:46ce1e16c870 187 }
emilmont 1:46ce1e16c870 188 return new FATDirHandle(dir);
emilmont 1:46ce1e16c870 189 }
emilmont 1:46ce1e16c870 190
emilmont 1:46ce1e16c870 191 int FATFileSystem::mkdir(const char *name, mode_t mode) {
emilmont 1:46ce1e16c870 192 FRESULT res = f_mkdir(name);
emilmont 1:46ce1e16c870 193 return res == 0 ? 0 : -1;
emilmont 1:46ce1e16c870 194 }
mbed_official 4:3ff2606d5713 195
mbed_official 4:3ff2606d5713 196 int FATFileSystem::mount() {
mbed_official 5:b3b3370574cf 197 FRESULT res = f_mount(&_fs, _fsid, 1);
mbed_official 4:3ff2606d5713 198 return res == 0 ? 0 : -1;
mbed_official 4:3ff2606d5713 199 }
mbed_official 4:3ff2606d5713 200
mbed_official 4:3ff2606d5713 201 int FATFileSystem::unmount() {
mbed_official 4:3ff2606d5713 202 if (disk_sync())
mbed_official 4:3ff2606d5713 203 return -1;
mbed_official 5:b3b3370574cf 204 FRESULT res = f_mount(NULL, _fsid, 0);
mbed_official 4:3ff2606d5713 205 return res == 0 ? 0 : -1;
mbed_official 4:3ff2606d5713 206 }
WiredHome 8:f08059355141 207