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