Revised to support ability to have both SD and USB drives mounted.
Dependents: Multi-FileSystem Multi-FileSystem
Fork of FATFileSystem by
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 #include "mbed_debug.h" 00026 00027 #include "FATFileSystem.h" 00028 #include "FATFileHandle.h" 00029 #include "FATDirHandle.h" 00030 00031 // Define the maximum length for a filename. 00032 // 00033 // Can accept the _MAX_LFN from ffconf, but this could be big (e.g. 255). 00034 // Generally this only inflates the stack frame for these functions. 00035 // Can also set a smaller number if you know the maximum is less. 00036 // example: "0:/12345678.abc\0" would be 16. 00037 // keep in mind that something 00038 // like "/usb/12345678.abc" is translated to "0:/12345678.abc" 00039 // 00040 // Various APIs will return the failure code if you try to pass too long 00041 // of a path/name in. 00042 // 00043 #define _MAX_FN_LEN _MAX_LFN 00044 00045 //#define DEBUG "FtFS" 00046 // ... 00047 // INFO("Stuff to show %d", var); // new-line is automatically appended 00048 // 00049 #if (defined(DEBUG) && !defined(TARGET_LPC11U24)) 00050 #include "mbed.h" 00051 #define INFO(x, ...) std::printf("[INF %s %4d] "x"\r\n", DEBUG, __LINE__, ##__VA_ARGS__); 00052 #define WARN(x, ...) std::printf("[WRN %s %4d] "x"\r\n", DEBUG, __LINE__, ##__VA_ARGS__); 00053 #define ERR(x, ...) std::printf("[ERR %s %4d] "x"\r\n", DEBUG, __LINE__, ##__VA_ARGS__); 00054 static void HexDump(const char * title, const uint8_t * p, int count) 00055 { 00056 int i; 00057 char buf[100] = "0000: "; 00058 00059 if (*title) 00060 INFO("%s", title); 00061 for (i=0; i<count; ) { 00062 sprintf(buf + strlen(buf), "%02X ", *(p+i)); 00063 if ((++i & 0x0F) == 0x00) { 00064 INFO("%s", buf); 00065 if (i < count) 00066 sprintf(buf, "%04X: ", i); 00067 else 00068 buf[0] = '\0'; 00069 } 00070 } 00071 if (strlen(buf)) 00072 INFO("%s", buf); 00073 } 00074 #else 00075 #define INFO(x, ...) 00076 #define WARN(x, ...) 00077 #define ERR(x, ...) 00078 #define HexDump(a, b, c) 00079 #endif 00080 00081 DWORD get_fattime(void) { 00082 time_t rawtime; 00083 time(&rawtime); 00084 struct tm *ptm = localtime(&rawtime); 00085 return (DWORD)(ptm->tm_year - 80) << 25 00086 | (DWORD)(ptm->tm_mon + 1 ) << 21 00087 | (DWORD)(ptm->tm_mday ) << 16 00088 | (DWORD)(ptm->tm_hour ) << 11 00089 | (DWORD)(ptm->tm_min ) << 5 00090 | (DWORD)(ptm->tm_sec/2 ); 00091 } 00092 00093 FATFileSystem *FATFileSystem::_ffs[_VOLUMES] = {0}; 00094 00095 FATFileSystem::FATFileSystem(const char* n) : FileSystemLike(n) { 00096 INFO("FATFileSystem(%s)", n); 00097 for(int i=0; i<_VOLUMES; i++) { 00098 if(_ffs[i] == this) // 2nd attempt to mount the same drive 00099 return; 00100 if(_ffs[i] == 0) { 00101 _ffs[i] = this; 00102 _fsid[0] = '0' + i; 00103 _fsid[1] = ':'; 00104 _fsid[2] = '\0'; 00105 INFO("Mounting [%s] on ffs drive [%s]", _name, _fsid); 00106 f_mount(&_fs, _fsid, 0); 00107 for (int d=0; d<_VOLUMES; d++) { 00108 INFO(" _ffs[%d] = %p", d, _ffs[d]); 00109 } 00110 return; 00111 } 00112 } 00113 error("Couldn't create %s in FATFileSystem::FATFileSystem\r\n", n); 00114 } 00115 00116 FATFileSystem::~FATFileSystem() { 00117 for (int i=0; i<_VOLUMES; i++) { 00118 if (_ffs[i] == this) { 00119 _ffs[i] = 0; 00120 f_mount(NULL, _fsid, 0); 00121 } 00122 } 00123 } 00124 00125 FileHandle *FATFileSystem::open(const char* name, int flags) { 00126 INFO("open(%s) on filesystem [%s], drv [%s]", name, _name, _fsid); 00127 char n[_MAX_FN_LEN]; 00128 int sn = snprintf(n, _MAX_FN_LEN, "%s/%s", _fsid, name); 00129 if (sn < 0) 00130 return NULL; 00131 INFO(" :: open(%s)", n); 00132 00133 /* POSIX flags -> FatFS open mode */ 00134 BYTE openmode; 00135 if (flags & O_RDWR) { 00136 openmode = FA_READ|FA_WRITE; 00137 } else if(flags & O_WRONLY) { 00138 openmode = FA_WRITE; 00139 } else { 00140 openmode = FA_READ; 00141 } 00142 if(flags & O_CREAT) { 00143 if(flags & O_TRUNC) { 00144 openmode |= FA_CREATE_ALWAYS; 00145 } else { 00146 openmode |= FA_OPEN_ALWAYS; 00147 } 00148 } 00149 00150 FIL fh; 00151 FRESULT res = f_open(&fh, n, openmode); 00152 if (res) { 00153 INFO("f_open('w') failed: %d", res); 00154 return NULL; 00155 } 00156 if (flags & O_APPEND) { 00157 f_lseek(&fh, fh.fsize); 00158 } 00159 return new FATFileHandle(fh); 00160 } 00161 00162 int FATFileSystem::remove(const char *name) { 00163 INFO("remove(%s) on filesystem [%s], drv [%s]", name, _name, _fsid); 00164 char n[_MAX_FN_LEN]; 00165 int sn = snprintf(n, _MAX_FN_LEN, "%s/%s", _fsid, name); 00166 if (sn < 0) 00167 return -1; 00168 00169 INFO(" :: remove(%s)", n); 00170 FRESULT res = f_unlink(n); 00171 if (res) { 00172 INFO("f_unlink() failed: %d", res); 00173 return -1; 00174 } 00175 return 0; 00176 } 00177 00178 int FATFileSystem::rename(const char *oldname, const char *newname) { 00179 char o[_MAX_FN_LEN], n[_MAX_FN_LEN]; 00180 int sn = snprintf(o, _MAX_FN_LEN, "%s/%s", _fsid, oldname); 00181 if (sn < 0) 00182 return -1; 00183 sn = snprintf(n, _MAX_FN_LEN, "%s/%s", _fsid, newname); 00184 if (sn < 0) 00185 return -1; 00186 FRESULT res = f_rename(o, n); 00187 if (res) { 00188 INFO("f_rename() failed: %d", res); 00189 return -res; 00190 } 00191 return 0; 00192 } 00193 00194 int FATFileSystem::format() { 00195 FRESULT res = f_mkfs(_fsid, 0, 512); // Logical drive number, Partitioning rule, Allocation unit size (bytes per cluster) 00196 if (res) { 00197 INFO("f_mkfs() failed: %d", res); 00198 return -1; 00199 } 00200 return 0; 00201 } 00202 00203 DirHandle *FATFileSystem::opendir(const char *name) { 00204 FATFS_DIR dir; 00205 char n[_MAX_FN_LEN]; 00206 int sn = snprintf(n, _MAX_FN_LEN, "%s/%s", _fsid, name); 00207 if (sn < 0) 00208 return NULL; 00209 FRESULT res = f_opendir(&dir, n); 00210 if (res != 0) { 00211 return NULL; 00212 } 00213 return new FATDirHandle(dir); 00214 } 00215 00216 int FATFileSystem::mkdir(const char *name, mode_t mode) { 00217 char n[_MAX_FN_LEN]; 00218 int sn = snprintf(n, _MAX_FN_LEN, "%s/%s", _fsid, name); 00219 if (sn < 0) 00220 return -1; 00221 FRESULT res = f_mkdir(n); 00222 return res == 0 ? 0 : -1; 00223 } 00224 00225 int FATFileSystem::mount() { 00226 FRESULT res = f_mount(&_fs, _fsid, 1); 00227 return res == 0 ? 0 : -1; 00228 } 00229 00230 int FATFileSystem::unmount() { 00231 if (disk_sync()) 00232 return -1; 00233 FRESULT res = f_mount(NULL, _fsid, 0); 00234 return res == 0 ? 0 : -1; 00235 } 00236 00237 #if 0 00238 // I think this cannot work unless the FileSystemLike and other interfaces 00239 // are updated. 00240 int FATFileSystem::fstat(const char *path, FILINFO *fileinfo) { 00241 char n[_MAX_FN_LEN]; 00242 int sn = snprintf(n, _MAX_FN_LEN, "%s/%s", _fsid, path); 00243 if (sn < 0) 00244 return -1; 00245 FRESULT res = f_stat(n, fileinfo); 00246 printf("f_stat(%s) returned: %d\r\n", n, res); 00247 return res == 0 ? 0 : -res; 00248 } 00249 #endif
Generated on Tue Jul 12 2022 20:48:55 by 1.7.2