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