Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of FatFileSystem by
FATFileSystem.cpp
00001 /* mbed Microcontroller Library - FATFileSystem 00002 * Copyright (c) 2008, sford 00003 */ 00004 00005 #include "FATFileSystem.h" 00006 00007 #include "mbed.h" 00008 00009 #include "FileSystemLike.h" 00010 #include "FATFileHandle.h" 00011 #include "FATDirHandle.h" 00012 #include "ff.h" 00013 //#include "Debug.h" 00014 #include <stdio.h> 00015 #include <stdlib.h> 00016 #include <time.h> 00017 00018 /* 00019 Currnet time is returned with packed into a DWORD value. The bit field is as follows: 00020 bit31:25 00021 Year from 1980 (0..127) 00022 bit24:21 00023 Month (1..12) 00024 bit20:16 00025 Day in month(1..31) 00026 bit15:11 00027 Hour (0..23) 00028 bit10:5 00029 Minute (0..59) 00030 bit4:0 00031 Second / 2 (0..29) 00032 00033 00034 int tm_sec; 00035 int tm_min; 00036 int tm_hour; 00037 int tm_mday; 00038 int tm_mon; 00039 int tm_year; 00040 int tm_wday; 00041 int tm_yday; 00042 int tm_isdst; 00043 00044 */ 00045 00046 DWORD get_fattime (void) { 00047 time_t rawtime; 00048 struct tm *ptm; 00049 time ( &rawtime ); 00050 ptm = localtime ( &rawtime ); 00051 FFSDEBUG("DTM: %d/%d/%d %d:%d:%d\n",ptm->tm_year,ptm->tm_mon,ptm->tm_mday,ptm->tm_hour,ptm->tm_min,ptm->tm_sec); 00052 DWORD fattime = (DWORD)(ptm->tm_year - 80) << 25 00053 | (DWORD)(ptm->tm_mon + 1) << 21 00054 | (DWORD)(ptm->tm_mday) << 16 00055 | (DWORD)(ptm->tm_hour) << 11 00056 | (DWORD)(ptm->tm_min) << 5 00057 | (DWORD)(ptm->tm_sec/2); 00058 00059 FFSDEBUG("Converted: %x\n",fattime); 00060 return fattime; 00061 } 00062 00063 namespace mbed { 00064 00065 #if FFSDEBUG_ENABLED 00066 static const char *FR_ERRORS[] = { 00067 "FR_OK = 0", 00068 "FR_NOT_READY", 00069 "FR_NO_FILE", 00070 "FR_NO_PATH", 00071 "FR_INVALID_NAME", 00072 "FR_INVALID_DRIVE", 00073 "FR_DENIED", 00074 "FR_EXIST", 00075 "FR_RW_ERROR", 00076 "FR_WRITE_PROTECTED", 00077 "FR_NOT_ENABLED", 00078 "FR_NO_FILESYSTEM", 00079 "FR_INVALID_OBJECT", 00080 "FR_MKFS_ABORTED" 00081 }; 00082 #endif 00083 00084 FATFileSystem *FATFileSystem::_ffs[_VOLUMES] = {0}; 00085 00086 FATFileSystem::FATFileSystem(const char* n) : FileSystemLike(n) { 00087 FFSDEBUG("FATFileSystem(%s)\n", n); 00088 for(int i=0; i<_VOLUMES; i++) { 00089 if(_ffs[i] == 0) { 00090 _ffs[i] = this; 00091 _fsid = i; 00092 FFSDEBUG("Mounting [%s] on ffs drive [%d]\n", _name, _fsid); 00093 f_mount(i, &_fs); 00094 return; 00095 } 00096 } 00097 error("Couldn't create %s in FATFileSystem::FATFileSystem\n",n); 00098 } 00099 00100 FATFileSystem::~FATFileSystem() { 00101 for(int i=0; i<_VOLUMES; i++) { 00102 if(_ffs[i] == this) { 00103 _ffs[i] = 0; 00104 f_mount(i, NULL); 00105 } 00106 } 00107 } 00108 00109 FileHandle *FATFileSystem::open(const char* name, int flags) { 00110 FFSDEBUG("open(%s) on filesystem [%s], drv [%d]\n", name, _name, _fsid); 00111 char n[64]; 00112 sprintf(n, "%d:/%s", _fsid, name); 00113 00114 /* POSIX flags -> FatFS open mode */ 00115 BYTE openmode; 00116 if(flags & O_RDWR) { 00117 openmode = FA_READ|FA_WRITE; 00118 } else if(flags & O_WRONLY) { 00119 openmode = FA_WRITE; 00120 } else { 00121 openmode = FA_READ; 00122 } 00123 if(flags & O_CREAT) { 00124 if(flags & O_TRUNC) { 00125 openmode |= FA_CREATE_ALWAYS; 00126 } else { 00127 openmode |= FA_OPEN_ALWAYS; 00128 } 00129 } 00130 00131 FIL fh; 00132 FRESULT res = f_open(&fh, n, openmode); 00133 if(res) { 00134 FFSDEBUG("f_open('w') failed (%d, %s)\n", res, FR_ERRORS[res]); 00135 return NULL; 00136 } 00137 if(flags & O_APPEND) { 00138 f_lseek(&fh, fh.fsize); 00139 } 00140 return new FATFileHandle(fh); 00141 } 00142 00143 int FATFileSystem::remove(const char *filename) { 00144 FRESULT res = f_unlink(filename); 00145 if(res) { 00146 FFSDEBUG("f_unlink() failed (%d, %s)\n", res, FR_ERRORS[res]); 00147 return -1; 00148 } 00149 return 0; 00150 } 00151 00152 int FATFileSystem::format() { 00153 FFSDEBUG("format()\n"); 00154 FRESULT res = f_mkfs(_fsid, 0, 512); // Logical drive number, Partitioning rule, Allocation unit size (bytes per cluster) 00155 if(res) { 00156 FFSDEBUG("f_mkfs() failed (%d, %s)\n", res, FR_ERRORS[res]); 00157 return -1; 00158 } 00159 return 0; 00160 } 00161 00162 DirHandle *FATFileSystem::opendir(const char *name) { 00163 FATFS_DIR dir; 00164 FRESULT res = f_opendir(&dir, name); 00165 if(res != 0) { 00166 return NULL; 00167 } 00168 return new FATDirHandle(dir); 00169 } 00170 00171 int FATFileSystem::mkdir(const char *name, mode_t mode) { 00172 FRESULT res = f_mkdir(name); 00173 return res == 0 ? 0 : -1; 00174 } 00175 00176 } // namespace mbed
Generated on Fri Jul 15 2022 15:34:13 by
1.7.2
