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