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