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@9:b2ca0e66c1f7, 2013-11-12 (annotated)
- Committer:
- nyatla
- Date:
- Tue Nov 12 03:41:14 2013 +0000
- Revision:
- 9:b2ca0e66c1f7
- Parent:
- 8:22ce3449b224
fix warning.;
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 | |
nyatla | 5:2c7b6bd5b079 | 26 | #include "FATFileSystem.h" |
nyatla | 5:2c7b6bd5b079 | 27 | #include "FATFileHandle.h" |
nyatla | 5:2c7b6bd5b079 | 28 | #include "FATDirHandle.h" |
nyatla | 5:2c7b6bd5b079 | 29 | |
nyatla | 8:22ce3449b224 | 30 | |
nyatla | 7:5429e81addde | 31 | |
nyatla | 7:5429e81addde | 32 | |
nyatla | 5:2c7b6bd5b079 | 33 | DWORD get_fattime(void) { |
nyatla | 5:2c7b6bd5b079 | 34 | time_t rawtime; |
nyatla | 5:2c7b6bd5b079 | 35 | time(&rawtime); |
nyatla | 5:2c7b6bd5b079 | 36 | struct tm *ptm = localtime(&rawtime); |
nyatla | 5:2c7b6bd5b079 | 37 | return (DWORD)(ptm->tm_year - 80) << 25 |
nyatla | 5:2c7b6bd5b079 | 38 | | (DWORD)(ptm->tm_mon + 1 ) << 21 |
nyatla | 5:2c7b6bd5b079 | 39 | | (DWORD)(ptm->tm_mday ) << 16 |
nyatla | 5:2c7b6bd5b079 | 40 | | (DWORD)(ptm->tm_hour ) << 11 |
nyatla | 5:2c7b6bd5b079 | 41 | | (DWORD)(ptm->tm_min ) << 5 |
nyatla | 5:2c7b6bd5b079 | 42 | | (DWORD)(ptm->tm_sec/2 ); |
nyatla | 5:2c7b6bd5b079 | 43 | } |
nyatla | 5:2c7b6bd5b079 | 44 | |
nyatla | 5:2c7b6bd5b079 | 45 | FATFileSystem *FATFileSystem::_ffs[_VOLUMES] = {0}; |
nyatla | 5:2c7b6bd5b079 | 46 | |
nyatla | 5:2c7b6bd5b079 | 47 | FATFileSystem::FATFileSystem(const char* n) : FileSystemLike(n) { |
nyatla | 5:2c7b6bd5b079 | 48 | for(int i=0; i<_VOLUMES; i++) { |
nyatla | 5:2c7b6bd5b079 | 49 | if(_ffs[i] == 0) { |
nyatla | 5:2c7b6bd5b079 | 50 | _ffs[i] = this; |
nyatla | 5:2c7b6bd5b079 | 51 | _fsid = i; |
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 | 6:972a52e4d92c | 69 | // char n[64]; |
nyatla | 6:972a52e4d92c | 70 | // sprintf(n, "%d:/%s", _fsid, name); |
nyatla | 5:2c7b6bd5b079 | 71 | |
nyatla | 5:2c7b6bd5b079 | 72 | /* POSIX flags -> FatFS open mode */ |
nyatla | 5:2c7b6bd5b079 | 73 | BYTE openmode; |
nyatla | 5:2c7b6bd5b079 | 74 | if (flags & O_RDWR) { |
nyatla | 5:2c7b6bd5b079 | 75 | openmode = FA_READ|FA_WRITE; |
nyatla | 5:2c7b6bd5b079 | 76 | } else if(flags & O_WRONLY) { |
nyatla | 5:2c7b6bd5b079 | 77 | openmode = FA_WRITE; |
nyatla | 5:2c7b6bd5b079 | 78 | } else { |
nyatla | 5:2c7b6bd5b079 | 79 | openmode = FA_READ; |
nyatla | 5:2c7b6bd5b079 | 80 | } |
nyatla | 5:2c7b6bd5b079 | 81 | if(flags & O_CREAT) { |
nyatla | 5:2c7b6bd5b079 | 82 | if(flags & O_TRUNC) { |
nyatla | 5:2c7b6bd5b079 | 83 | openmode |= FA_CREATE_ALWAYS; |
nyatla | 5:2c7b6bd5b079 | 84 | } else { |
nyatla | 5:2c7b6bd5b079 | 85 | openmode |= FA_OPEN_ALWAYS; |
nyatla | 5:2c7b6bd5b079 | 86 | } |
nyatla | 5:2c7b6bd5b079 | 87 | } |
nyatla | 5:2c7b6bd5b079 | 88 | FIL fh; |
nyatla | 6:972a52e4d92c | 89 | FRESULT res = f_open(&fh,this->_fsid,name, openmode); |
nyatla | 5:2c7b6bd5b079 | 90 | if (res) { |
nyatla | 5:2c7b6bd5b079 | 91 | return NULL; |
nyatla | 5:2c7b6bd5b079 | 92 | } |
nyatla | 5:2c7b6bd5b079 | 93 | if (flags & O_APPEND) { |
nyatla | 5:2c7b6bd5b079 | 94 | f_lseek(&fh, fh.fsize); |
nyatla | 5:2c7b6bd5b079 | 95 | } |
nyatla | 5:2c7b6bd5b079 | 96 | return new FATFileHandle(fh); |
nyatla | 5:2c7b6bd5b079 | 97 | } |
nyatla | 5:2c7b6bd5b079 | 98 | |
nyatla | 5:2c7b6bd5b079 | 99 | int FATFileSystem::remove(const char *filename) { |
nyatla | 6:972a52e4d92c | 100 | FRESULT res = f_unlink(this->_fsid,filename); |
nyatla | 5:2c7b6bd5b079 | 101 | if (res) { |
nyatla | 5:2c7b6bd5b079 | 102 | return -1; |
nyatla | 5:2c7b6bd5b079 | 103 | } |
nyatla | 5:2c7b6bd5b079 | 104 | return 0; |
nyatla | 5:2c7b6bd5b079 | 105 | } |
nyatla | 5:2c7b6bd5b079 | 106 | |
nyatla | 5:2c7b6bd5b079 | 107 | int FATFileSystem::format() { |
nyatla | 5:2c7b6bd5b079 | 108 | FRESULT res = f_mkfs(_fsid, 0, 512); // Logical drive number, Partitioning rule, Allocation unit size (bytes per cluster) |
nyatla | 5:2c7b6bd5b079 | 109 | if (res) { |
nyatla | 5:2c7b6bd5b079 | 110 | return -1; |
nyatla | 5:2c7b6bd5b079 | 111 | } |
nyatla | 5:2c7b6bd5b079 | 112 | return 0; |
nyatla | 5:2c7b6bd5b079 | 113 | } |
nyatla | 5:2c7b6bd5b079 | 114 | DirHandle *FATFileSystem::opendir(const char *name) { |
nyatla | 6:972a52e4d92c | 115 | FATFS_DIR dir; |
nyatla | 6:972a52e4d92c | 116 | FRESULT res = f_opendir(&dir,this->_fsid,name); |
nyatla | 5:2c7b6bd5b079 | 117 | if (res != 0) { |
nyatla | 5:2c7b6bd5b079 | 118 | return NULL; |
nyatla | 5:2c7b6bd5b079 | 119 | } |
nyatla | 5:2c7b6bd5b079 | 120 | return new FATDirHandle(dir); |
nyatla | 5:2c7b6bd5b079 | 121 | } |
nyatla | 5:2c7b6bd5b079 | 122 | |
nyatla | 5:2c7b6bd5b079 | 123 | int FATFileSystem::mkdir(const char *name, mode_t mode) { |
nyatla | 6:972a52e4d92c | 124 | FRESULT res = f_mkdir(this->_fsid,name); |
nyatla | 5:2c7b6bd5b079 | 125 | return res == 0 ? 0 : -1; |
nyatla | 5:2c7b6bd5b079 | 126 | } |