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