Class that contain only FATFileSystem

Fork of USBFileSystem by Erik -

Committer:
mkilivan
Date:
Fri Dec 19 14:41:08 2014 +0000
Revision:
6:eeeea7fbb0a2
Parent:
2:9af05743d551
Remove USB part

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Sissors 2:9af05743d551 1 /* mbed Microcontroller Library
Sissors 2:9af05743d551 2 * Copyright (c) 2006-2012 ARM Limited
Sissors 2:9af05743d551 3 *
Sissors 2:9af05743d551 4 * Permission is hereby granted, free of charge, to any person obtaining a copy
Sissors 2:9af05743d551 5 * of this software and associated documentation files (the "Software"), to deal
Sissors 2:9af05743d551 6 * in the Software without restriction, including without limitation the rights
Sissors 2:9af05743d551 7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
Sissors 2:9af05743d551 8 * copies of the Software, and to permit persons to whom the Software is
Sissors 2:9af05743d551 9 * furnished to do so, subject to the following conditions:
Sissors 2:9af05743d551 10 *
Sissors 2:9af05743d551 11 * The above copyright notice and this permission notice shall be included in
Sissors 2:9af05743d551 12 * all copies or substantial portions of the Software.
Sissors 2:9af05743d551 13 *
Sissors 2:9af05743d551 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
Sissors 2:9af05743d551 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
Sissors 2:9af05743d551 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Sissors 2:9af05743d551 17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
Sissors 2:9af05743d551 18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
Sissors 2:9af05743d551 19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
Sissors 2:9af05743d551 20 * SOFTWARE.
Sissors 2:9af05743d551 21 */
Sissors 2:9af05743d551 22 #include "mbed.h"
Sissors 2:9af05743d551 23
Sissors 2:9af05743d551 24 #include "ffconf.h"
Sissors 2:9af05743d551 25 #include "mbed_debug.h"
Sissors 2:9af05743d551 26
Sissors 2:9af05743d551 27 #include "FATFileSystem.h"
Sissors 2:9af05743d551 28 #include "FATFileHandle.h"
Sissors 2:9af05743d551 29 #include "FATDirHandle.h"
Sissors 2:9af05743d551 30
Sissors 2:9af05743d551 31 DWORD get_fattime(void) {
Sissors 2:9af05743d551 32 time_t rawtime;
Sissors 2:9af05743d551 33 time(&rawtime);
Sissors 2:9af05743d551 34 struct tm *ptm = localtime(&rawtime);
Sissors 2:9af05743d551 35 return (DWORD)(ptm->tm_year - 80) << 25
Sissors 2:9af05743d551 36 | (DWORD)(ptm->tm_mon + 1 ) << 21
Sissors 2:9af05743d551 37 | (DWORD)(ptm->tm_mday ) << 16
Sissors 2:9af05743d551 38 | (DWORD)(ptm->tm_hour ) << 11
Sissors 2:9af05743d551 39 | (DWORD)(ptm->tm_min ) << 5
Sissors 2:9af05743d551 40 | (DWORD)(ptm->tm_sec/2 );
Sissors 2:9af05743d551 41 }
Sissors 2:9af05743d551 42
Sissors 2:9af05743d551 43 FATFileSystem *FATFileSystem::_ffs[_VOLUMES] = {0};
Sissors 2:9af05743d551 44
Sissors 2:9af05743d551 45 FATFileSystem::FATFileSystem(const char* n) : FileSystemLike(n) {
Sissors 2:9af05743d551 46 debug_if(FFS_DBG, "FATFileSystem(%s)\n", n);
Sissors 2:9af05743d551 47 for(int i=0; i<_VOLUMES; i++) {
Sissors 2:9af05743d551 48 if(_ffs[i] == 0) {
Sissors 2:9af05743d551 49 _ffs[i] = this;
Sissors 2:9af05743d551 50 _fsid = i;
Sissors 2:9af05743d551 51 debug_if(FFS_DBG, "Mounting [%s] on ffs drive [%d]\n", _name, _fsid);
Sissors 2:9af05743d551 52 f_mount(i, &_fs);
Sissors 2:9af05743d551 53 return;
Sissors 2:9af05743d551 54 }
Sissors 2:9af05743d551 55 }
Sissors 2:9af05743d551 56 error("Couldn't create %s in FATFileSystem::FATFileSystem\n", n);
Sissors 2:9af05743d551 57 }
Sissors 2:9af05743d551 58
Sissors 2:9af05743d551 59 FATFileSystem::~FATFileSystem() {
Sissors 2:9af05743d551 60 for (int i=0; i<_VOLUMES; i++) {
Sissors 2:9af05743d551 61 if (_ffs[i] == this) {
Sissors 2:9af05743d551 62 _ffs[i] = 0;
Sissors 2:9af05743d551 63 f_mount(i, NULL);
Sissors 2:9af05743d551 64 }
Sissors 2:9af05743d551 65 }
Sissors 2:9af05743d551 66 }
Sissors 2:9af05743d551 67
Sissors 2:9af05743d551 68 FileHandle *FATFileSystem::open(const char* name, int flags) {
Sissors 2:9af05743d551 69 debug_if(FFS_DBG, "open(%s) on filesystem [%s], drv [%d]\n", name, _name, _fsid);
Sissors 2:9af05743d551 70 char n[64];
Sissors 2:9af05743d551 71 sprintf(n, "%d:/%s", _fsid, name);
Sissors 2:9af05743d551 72
Sissors 2:9af05743d551 73 /* POSIX flags -> FatFS open mode */
Sissors 2:9af05743d551 74 BYTE openmode;
Sissors 2:9af05743d551 75 if (flags & O_RDWR) {
Sissors 2:9af05743d551 76 openmode = FA_READ|FA_WRITE;
Sissors 2:9af05743d551 77 } else if(flags & O_WRONLY) {
Sissors 2:9af05743d551 78 openmode = FA_WRITE;
Sissors 2:9af05743d551 79 } else {
Sissors 2:9af05743d551 80 openmode = FA_READ;
Sissors 2:9af05743d551 81 }
Sissors 2:9af05743d551 82 if(flags & O_CREAT) {
Sissors 2:9af05743d551 83 if(flags & O_TRUNC) {
Sissors 2:9af05743d551 84 openmode |= FA_CREATE_ALWAYS;
Sissors 2:9af05743d551 85 } else {
Sissors 2:9af05743d551 86 openmode |= FA_OPEN_ALWAYS;
Sissors 2:9af05743d551 87 }
Sissors 2:9af05743d551 88 }
Sissors 2:9af05743d551 89
Sissors 2:9af05743d551 90 FIL fh;
Sissors 2:9af05743d551 91 FRESULT res = f_open(&fh, n, openmode);
Sissors 2:9af05743d551 92 if (res) {
Sissors 2:9af05743d551 93 debug_if(FFS_DBG, "f_open('w') failed: %d\n", res);
Sissors 2:9af05743d551 94 return NULL;
Sissors 2:9af05743d551 95 }
Sissors 2:9af05743d551 96 if (flags & O_APPEND) {
Sissors 2:9af05743d551 97 f_lseek(&fh, fh.fsize);
Sissors 2:9af05743d551 98 }
Sissors 2:9af05743d551 99 return new FATFileHandle(fh);
Sissors 2:9af05743d551 100 }
Sissors 2:9af05743d551 101
Sissors 2:9af05743d551 102 int FATFileSystem::remove(const char *filename) {
Sissors 2:9af05743d551 103 FRESULT res = f_unlink(filename);
Sissors 2:9af05743d551 104 if (res) {
Sissors 2:9af05743d551 105 debug_if(FFS_DBG, "f_unlink() failed: %d\n", res);
Sissors 2:9af05743d551 106 return -1;
Sissors 2:9af05743d551 107 }
Sissors 2:9af05743d551 108 return 0;
Sissors 2:9af05743d551 109 }
Sissors 2:9af05743d551 110
Sissors 2:9af05743d551 111 int FATFileSystem::format() {
Sissors 2:9af05743d551 112 FRESULT res = f_mkfs(_fsid, 0, 512); // Logical drive number, Partitioning rule, Allocation unit size (bytes per cluster)
Sissors 2:9af05743d551 113 if (res) {
Sissors 2:9af05743d551 114 debug_if(FFS_DBG, "f_mkfs() failed: %d\n", res);
Sissors 2:9af05743d551 115 return -1;
Sissors 2:9af05743d551 116 }
Sissors 2:9af05743d551 117 return 0;
Sissors 2:9af05743d551 118 }
Sissors 2:9af05743d551 119
Sissors 2:9af05743d551 120 DirHandle *FATFileSystem::opendir(const char *name) {
Sissors 2:9af05743d551 121 FATFS_DIR dir;
Sissors 2:9af05743d551 122 FRESULT res = f_opendir(&dir, name);
Sissors 2:9af05743d551 123 if (res != 0) {
Sissors 2:9af05743d551 124 return NULL;
Sissors 2:9af05743d551 125 }
Sissors 2:9af05743d551 126 return new FATDirHandle(dir);
Sissors 2:9af05743d551 127 }
Sissors 2:9af05743d551 128
Sissors 2:9af05743d551 129 int FATFileSystem::mkdir(const char *name, mode_t mode) {
Sissors 2:9af05743d551 130 FRESULT res = f_mkdir(name);
Sissors 2:9af05743d551 131 return res == 0 ? 0 : -1;
Sissors 2:9af05743d551 132 }