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