mbed-os

Fork of mbed-os by erkin yucel

Committer:
elessair
Date:
Sun Oct 23 15:10:02 2016 +0000
Revision:
0:f269e3021894
Initial commit

Who changed what in which revision?

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