nkjnm

Dependencies:   MAX44000 nexpaq_mdk

Fork of LED_Demo by Maxim nexpaq

Committer:
nitsshukla
Date:
Fri Nov 04 12:06:04 2016 +0000
Revision:
7:3a65ef12ba31
Parent:
1:55a6170b404f
kghj;

Who changed what in which revision?

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