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:
Sun Mar 13 00:58:52 2016 +0000
Revision:
9:4000fad3b21f
Parent:
8:f08059355141
Child:
10:e58f6254f8a2
allocate filename buffers based on _MAX_LFN, not a hard-coded '64'.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
WiredHome 9:4000fad3b21f 1 /* mbed Microcontroller Library
WiredHome 9:4000fad3b21f 2 * Copyright (c) 2006-2012 ARM Limited
WiredHome 9:4000fad3b21f 3 *
WiredHome 9:4000fad3b21f 4 * Permission is hereby granted, free of charge, to any person obtaining a copy
WiredHome 9:4000fad3b21f 5 * of this software and associated documentation files (the "Software"), to deal
WiredHome 9:4000fad3b21f 6 * in the Software without restriction, including without limitation the rights
WiredHome 9:4000fad3b21f 7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
WiredHome 9:4000fad3b21f 8 * copies of the Software, and to permit persons to whom the Software is
WiredHome 9:4000fad3b21f 9 * furnished to do so, subject to the following conditions:
WiredHome 9:4000fad3b21f 10 *
WiredHome 9:4000fad3b21f 11 * The above copyright notice and this permission notice shall be included in
WiredHome 9:4000fad3b21f 12 * all copies or substantial portions of the Software.
WiredHome 9:4000fad3b21f 13 *
WiredHome 9:4000fad3b21f 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
WiredHome 9:4000fad3b21f 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
WiredHome 9:4000fad3b21f 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
WiredHome 9:4000fad3b21f 17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
WiredHome 9:4000fad3b21f 18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
WiredHome 9:4000fad3b21f 19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
WiredHome 9:4000fad3b21f 20 * SOFTWARE.
WiredHome 9:4000fad3b21f 21 */
WiredHome 9:4000fad3b21f 22 #include "mbed.h"
WiredHome 9:4000fad3b21f 23
WiredHome 9:4000fad3b21f 24 #include "ffconf.h"
WiredHome 9:4000fad3b21f 25 #include "mbed_debug.h"
WiredHome 9:4000fad3b21f 26
WiredHome 9:4000fad3b21f 27 #include "FATFileSystem.h"
WiredHome 9:4000fad3b21f 28 #include "FATFileHandle.h"
WiredHome 9:4000fad3b21f 29 #include "FATDirHandle.h"
WiredHome 9:4000fad3b21f 30
WiredHome 9:4000fad3b21f 31
WiredHome 9:4000fad3b21f 32 //#define DEBUG "FtFS"
WiredHome 9:4000fad3b21f 33 // ...
WiredHome 9:4000fad3b21f 34 // INFO("Stuff to show %d", var); // new-line is automatically appended
WiredHome 9:4000fad3b21f 35 //
WiredHome 9:4000fad3b21f 36 #if (defined(DEBUG) && !defined(TARGET_LPC11U24))
WiredHome 9:4000fad3b21f 37 #include "mbed.h"
WiredHome 9:4000fad3b21f 38 #define INFO(x, ...) std::printf("[INF %s %4d] "x"\r\n", DEBUG, __LINE__, ##__VA_ARGS__);
WiredHome 9:4000fad3b21f 39 #define WARN(x, ...) std::printf("[WRN %s %4d] "x"\r\n", DEBUG, __LINE__, ##__VA_ARGS__);
WiredHome 9:4000fad3b21f 40 #define ERR(x, ...) std::printf("[ERR %s %4d] "x"\r\n", DEBUG, __LINE__, ##__VA_ARGS__);
WiredHome 9:4000fad3b21f 41 static void HexDump(const char * title, const uint8_t * p, int count)
WiredHome 9:4000fad3b21f 42 {
WiredHome 9:4000fad3b21f 43 int i;
WiredHome 9:4000fad3b21f 44 char buf[100] = "0000: ";
WiredHome 9:4000fad3b21f 45
WiredHome 9:4000fad3b21f 46 if (*title)
WiredHome 9:4000fad3b21f 47 INFO("%s", title);
WiredHome 9:4000fad3b21f 48 for (i=0; i<count; ) {
WiredHome 9:4000fad3b21f 49 sprintf(buf + strlen(buf), "%02X ", *(p+i));
WiredHome 9:4000fad3b21f 50 if ((++i & 0x0F) == 0x00) {
WiredHome 9:4000fad3b21f 51 INFO("%s", buf);
WiredHome 9:4000fad3b21f 52 if (i < count)
WiredHome 9:4000fad3b21f 53 sprintf(buf, "%04X: ", i);
WiredHome 9:4000fad3b21f 54 else
WiredHome 9:4000fad3b21f 55 buf[0] = '\0';
WiredHome 9:4000fad3b21f 56 }
WiredHome 9:4000fad3b21f 57 }
WiredHome 9:4000fad3b21f 58 if (strlen(buf))
WiredHome 9:4000fad3b21f 59 INFO("%s", buf);
WiredHome 9:4000fad3b21f 60 }
WiredHome 9:4000fad3b21f 61 #else
WiredHome 9:4000fad3b21f 62 #define INFO(x, ...)
WiredHome 9:4000fad3b21f 63 #define WARN(x, ...)
WiredHome 9:4000fad3b21f 64 #define ERR(x, ...)
WiredHome 9:4000fad3b21f 65 #define HexDump(a, b, c)
WiredHome 9:4000fad3b21f 66 #endif
WiredHome 9:4000fad3b21f 67
WiredHome 9:4000fad3b21f 68 DWORD get_fattime(void) {
WiredHome 9:4000fad3b21f 69 time_t rawtime;
WiredHome 9:4000fad3b21f 70 time(&rawtime);
WiredHome 9:4000fad3b21f 71 struct tm *ptm = localtime(&rawtime);
WiredHome 9:4000fad3b21f 72 return (DWORD)(ptm->tm_year - 80) << 25
WiredHome 9:4000fad3b21f 73 | (DWORD)(ptm->tm_mon + 1 ) << 21
WiredHome 9:4000fad3b21f 74 | (DWORD)(ptm->tm_mday ) << 16
WiredHome 9:4000fad3b21f 75 | (DWORD)(ptm->tm_hour ) << 11
WiredHome 9:4000fad3b21f 76 | (DWORD)(ptm->tm_min ) << 5
WiredHome 9:4000fad3b21f 77 | (DWORD)(ptm->tm_sec/2 );
WiredHome 9:4000fad3b21f 78 }
WiredHome 9:4000fad3b21f 79
WiredHome 9:4000fad3b21f 80 FATFileSystem *FATFileSystem::_ffs[_VOLUMES] = {0};
WiredHome 9:4000fad3b21f 81
WiredHome 9:4000fad3b21f 82 FATFileSystem::FATFileSystem(const char* n) : FileSystemLike(n) {
WiredHome 9:4000fad3b21f 83 INFO("FATFileSystem(%s)", n);
WiredHome 9:4000fad3b21f 84 for(int i=0; i<_VOLUMES; i++) {
WiredHome 9:4000fad3b21f 85 if(_ffs[i] == this) // 2nd attempt to mount the same drive
WiredHome 9:4000fad3b21f 86 return;
WiredHome 9:4000fad3b21f 87 if(_ffs[i] == 0) {
WiredHome 9:4000fad3b21f 88 _ffs[i] = this;
WiredHome 9:4000fad3b21f 89 _fsid[0] = '0' + i;
WiredHome 9:4000fad3b21f 90 _fsid[1] = ':';
WiredHome 9:4000fad3b21f 91 _fsid[2] = '\0';
WiredHome 9:4000fad3b21f 92 INFO("Mounting [%s] on ffs drive [%s]", _name, _fsid);
WiredHome 9:4000fad3b21f 93 f_mount(&_fs, _fsid, 0);
WiredHome 9:4000fad3b21f 94 for (int d=0; d<_VOLUMES; d++) {
WiredHome 9:4000fad3b21f 95 INFO(" _ffs[%d] = %p", d, _ffs[d]);
WiredHome 9:4000fad3b21f 96 }
WiredHome 9:4000fad3b21f 97 return;
WiredHome 9:4000fad3b21f 98 }
WiredHome 9:4000fad3b21f 99 }
WiredHome 9:4000fad3b21f 100 error("Couldn't create %s in FATFileSystem::FATFileSystem\r\n", n);
WiredHome 9:4000fad3b21f 101 }
WiredHome 8:f08059355141 102
WiredHome 9:4000fad3b21f 103 FATFileSystem::~FATFileSystem() {
WiredHome 9:4000fad3b21f 104 for (int i=0; i<_VOLUMES; i++) {
WiredHome 9:4000fad3b21f 105 if (_ffs[i] == this) {
WiredHome 9:4000fad3b21f 106 _ffs[i] = 0;
WiredHome 9:4000fad3b21f 107 f_mount(NULL, _fsid, 0);
WiredHome 9:4000fad3b21f 108 }
WiredHome 9:4000fad3b21f 109 }
WiredHome 9:4000fad3b21f 110 }
WiredHome 9:4000fad3b21f 111
WiredHome 9:4000fad3b21f 112 FileHandle *FATFileSystem::open(const char* name, int flags) {
WiredHome 9:4000fad3b21f 113 INFO("open(%s) on filesystem [%s], drv [%s]", name, _name, _fsid);
WiredHome 9:4000fad3b21f 114 char n[_MAX_LFN];
WiredHome 9:4000fad3b21f 115 sprintf(n, "%s/%s", _fsid, name);
WiredHome 9:4000fad3b21f 116 INFO(" :: open(%s)", n);
WiredHome 9:4000fad3b21f 117
WiredHome 9:4000fad3b21f 118 /* POSIX flags -> FatFS open mode */
WiredHome 9:4000fad3b21f 119 BYTE openmode;
WiredHome 9:4000fad3b21f 120 if (flags & O_RDWR) {
WiredHome 9:4000fad3b21f 121 openmode = FA_READ|FA_WRITE;
WiredHome 9:4000fad3b21f 122 } else if(flags & O_WRONLY) {
WiredHome 9:4000fad3b21f 123 openmode = FA_WRITE;
WiredHome 9:4000fad3b21f 124 } else {
WiredHome 9:4000fad3b21f 125 openmode = FA_READ;
WiredHome 9:4000fad3b21f 126 }
WiredHome 9:4000fad3b21f 127 if(flags & O_CREAT) {
WiredHome 9:4000fad3b21f 128 if(flags & O_TRUNC) {
WiredHome 9:4000fad3b21f 129 openmode |= FA_CREATE_ALWAYS;
WiredHome 9:4000fad3b21f 130 } else {
WiredHome 9:4000fad3b21f 131 openmode |= FA_OPEN_ALWAYS;
WiredHome 9:4000fad3b21f 132 }
WiredHome 9:4000fad3b21f 133 }
WiredHome 9:4000fad3b21f 134
WiredHome 9:4000fad3b21f 135 FIL fh;
WiredHome 9:4000fad3b21f 136 FRESULT res = f_open(&fh, n, openmode);
WiredHome 9:4000fad3b21f 137 if (res) {
WiredHome 9:4000fad3b21f 138 INFO("f_open('w') failed: %d", res);
WiredHome 9:4000fad3b21f 139 return NULL;
WiredHome 9:4000fad3b21f 140 }
WiredHome 9:4000fad3b21f 141 if (flags & O_APPEND) {
WiredHome 9:4000fad3b21f 142 f_lseek(&fh, fh.fsize);
WiredHome 9:4000fad3b21f 143 }
WiredHome 9:4000fad3b21f 144 return new FATFileHandle(fh);
WiredHome 9:4000fad3b21f 145 }
WiredHome 9:4000fad3b21f 146
WiredHome 9:4000fad3b21f 147 int FATFileSystem::remove(const char *name) {
WiredHome 9:4000fad3b21f 148 INFO("remove(%s) on filesystem [%s], drv [%s]", name, _name, _fsid);
WiredHome 9:4000fad3b21f 149 char n[_MAX_LFN];
WiredHome 9:4000fad3b21f 150 sprintf(n, "%s/%s", _fsid, name);
WiredHome 9:4000fad3b21f 151 INFO(" :: remove(%s)", n);
WiredHome 9:4000fad3b21f 152 FRESULT res = f_unlink(n);
WiredHome 9:4000fad3b21f 153 if (res) {
WiredHome 9:4000fad3b21f 154 INFO("f_unlink() failed: %d", res);
WiredHome 9:4000fad3b21f 155 return -1;
WiredHome 9:4000fad3b21f 156 }
WiredHome 9:4000fad3b21f 157 return 0;
WiredHome 9:4000fad3b21f 158 }
WiredHome 9:4000fad3b21f 159
WiredHome 9:4000fad3b21f 160 int FATFileSystem::rename(const char *oldname, const char *newname) {
WiredHome 9:4000fad3b21f 161 char o[_MAX_LFN], n[_MAX_LFN];
WiredHome 9:4000fad3b21f 162 sprintf(o, "%s/%s", _fsid, oldname);
WiredHome 9:4000fad3b21f 163 sprintf(n, "%s/%s", _fsid, newname);
WiredHome 9:4000fad3b21f 164 INFO(" :: rename(%s,%s)", o, n);
WiredHome 9:4000fad3b21f 165 FRESULT res = f_rename(oldname, newname);
WiredHome 9:4000fad3b21f 166 if (res) {
WiredHome 9:4000fad3b21f 167 INFO("f_rename() failed: %d", res);
WiredHome 9:4000fad3b21f 168 return -1;
WiredHome 9:4000fad3b21f 169 }
WiredHome 9:4000fad3b21f 170 return 0;
WiredHome 9:4000fad3b21f 171 }
WiredHome 9:4000fad3b21f 172
WiredHome 9:4000fad3b21f 173 int FATFileSystem::format() {
WiredHome 9:4000fad3b21f 174 FRESULT res = f_mkfs(_fsid, 0, 512); // Logical drive number, Partitioning rule, Allocation unit size (bytes per cluster)
WiredHome 9:4000fad3b21f 175 if (res) {
WiredHome 9:4000fad3b21f 176 INFO("f_mkfs() failed: %d", res);
WiredHome 9:4000fad3b21f 177 return -1;
WiredHome 9:4000fad3b21f 178 }
WiredHome 9:4000fad3b21f 179 return 0;
WiredHome 9:4000fad3b21f 180 }
WiredHome 9:4000fad3b21f 181
WiredHome 9:4000fad3b21f 182 DirHandle *FATFileSystem::opendir(const char *name) {
WiredHome 9:4000fad3b21f 183 FATFS_DIR dir;
WiredHome 9:4000fad3b21f 184 char n[_MAX_LFN];
WiredHome 9:4000fad3b21f 185 sprintf(n, "%s/%s", _fsid, name);
WiredHome 9:4000fad3b21f 186 FRESULT res = f_opendir(&dir, n);
WiredHome 9:4000fad3b21f 187 if (res != 0) {
WiredHome 9:4000fad3b21f 188 return NULL;
WiredHome 9:4000fad3b21f 189 }
WiredHome 9:4000fad3b21f 190 return new FATDirHandle(dir);
WiredHome 9:4000fad3b21f 191 }
WiredHome 9:4000fad3b21f 192
WiredHome 9:4000fad3b21f 193 int FATFileSystem::mkdir(const char *name, mode_t mode) {
WiredHome 9:4000fad3b21f 194 char n[_MAX_LFN];
WiredHome 9:4000fad3b21f 195 sprintf(n, "%s/%s", _fsid, name);
WiredHome 9:4000fad3b21f 196 FRESULT res = f_mkdir(n);
WiredHome 9:4000fad3b21f 197 return res == 0 ? 0 : -1;
WiredHome 9:4000fad3b21f 198 }
WiredHome 9:4000fad3b21f 199
WiredHome 9:4000fad3b21f 200 int FATFileSystem::mount() {
WiredHome 9:4000fad3b21f 201 FRESULT res = f_mount(&_fs, _fsid, 1);
WiredHome 9:4000fad3b21f 202 return res == 0 ? 0 : -1;
WiredHome 9:4000fad3b21f 203 }
WiredHome 9:4000fad3b21f 204
WiredHome 9:4000fad3b21f 205 int FATFileSystem::unmount() {
WiredHome 9:4000fad3b21f 206 if (disk_sync())
WiredHome 9:4000fad3b21f 207 return -1;
WiredHome 9:4000fad3b21f 208 FRESULT res = f_mount(NULL, _fsid, 0);
WiredHome 9:4000fad3b21f 209 return res == 0 ? 0 : -1;
WiredHome 9:4000fad3b21f 210 }
WiredHome 9:4000fad3b21f 211