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