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