Preliminary main mbed library for nexpaq development

Committer:
nexpaq
Date:
Fri Nov 04 20:54:50 2016 +0000
Revision:
1:d96dbedaebdb
Parent:
0:6c56fb4bc5f0
Removed extra directories for other platforms

Who changed what in which revision?

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