Revised to support ability to have both SD and USB drives mounted.
Dependents: Multi-FileSystem Multi-FileSystem
Fork of FATFileSystem by
FATFileHandle.cpp@12:d816f503fc6f, 2017-07-29 (annotated)
- Committer:
- WiredHome
- Date:
- Sat Jul 29 12:39:03 2017 +0000
- Revision:
- 12:d816f503fc6f
- Parent:
- 8:f08059355141
Reduce the number of sectors definition
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
emilmont | 1:46ce1e16c870 | 1 | /* mbed Microcontroller Library |
emilmont | 1:46ce1e16c870 | 2 | * Copyright (c) 2006-2012 ARM Limited |
emilmont | 1:46ce1e16c870 | 3 | * |
emilmont | 1:46ce1e16c870 | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
emilmont | 1:46ce1e16c870 | 5 | * of this software and associated documentation files (the "Software"), to deal |
emilmont | 1:46ce1e16c870 | 6 | * in the Software without restriction, including without limitation the rights |
emilmont | 1:46ce1e16c870 | 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
emilmont | 1:46ce1e16c870 | 8 | * copies of the Software, and to permit persons to whom the Software is |
emilmont | 1:46ce1e16c870 | 9 | * furnished to do so, subject to the following conditions: |
emilmont | 1:46ce1e16c870 | 10 | * |
emilmont | 1:46ce1e16c870 | 11 | * The above copyright notice and this permission notice shall be included in |
emilmont | 1:46ce1e16c870 | 12 | * all copies or substantial portions of the Software. |
emilmont | 1:46ce1e16c870 | 13 | * |
emilmont | 1:46ce1e16c870 | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
emilmont | 1:46ce1e16c870 | 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
emilmont | 1:46ce1e16c870 | 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
emilmont | 1:46ce1e16c870 | 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
emilmont | 1:46ce1e16c870 | 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
emilmont | 1:46ce1e16c870 | 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
emilmont | 1:46ce1e16c870 | 20 | * SOFTWARE. |
emilmont | 1:46ce1e16c870 | 21 | */ |
emilmont | 1:46ce1e16c870 | 22 | #include "ff.h" |
emilmont | 1:46ce1e16c870 | 23 | #include "ffconf.h" |
emilmont | 2:b6669c987c8e | 24 | #include "mbed_debug.h" |
emilmont | 1:46ce1e16c870 | 25 | |
emilmont | 1:46ce1e16c870 | 26 | #include "FATFileHandle.h" |
emilmont | 1:46ce1e16c870 | 27 | |
WiredHome | 8:f08059355141 | 28 | //#define DEBUG "FtFH" |
WiredHome | 8:f08059355141 | 29 | // ... |
WiredHome | 8:f08059355141 | 30 | // INFO("Stuff to show %d", var); // new-line is automatically appended |
WiredHome | 8:f08059355141 | 31 | // |
WiredHome | 8:f08059355141 | 32 | #if (defined(DEBUG) && !defined(TARGET_LPC11U24)) |
WiredHome | 8:f08059355141 | 33 | #include "mbed.h" |
WiredHome | 8:f08059355141 | 34 | #define INFO(x, ...) std::printf("[INF %s %4d] "x"\r\n", DEBUG, __LINE__, ##__VA_ARGS__); |
WiredHome | 8:f08059355141 | 35 | #define WARN(x, ...) std::printf("[WRN %s %4d] "x"\r\n", DEBUG, __LINE__, ##__VA_ARGS__); |
WiredHome | 8:f08059355141 | 36 | #define ERR(x, ...) std::printf("[ERR %s %4d] "x"\r\n", DEBUG, __LINE__, ##__VA_ARGS__); |
WiredHome | 8:f08059355141 | 37 | static void HexDump(const char * title, const uint8_t * p, int count) |
WiredHome | 8:f08059355141 | 38 | { |
WiredHome | 8:f08059355141 | 39 | int i; |
WiredHome | 8:f08059355141 | 40 | char buf[100] = "0000: "; |
WiredHome | 8:f08059355141 | 41 | |
WiredHome | 8:f08059355141 | 42 | if (*title) |
WiredHome | 8:f08059355141 | 43 | INFO("%s", title); |
WiredHome | 8:f08059355141 | 44 | for (i=0; i<count; ) { |
WiredHome | 8:f08059355141 | 45 | sprintf(buf + strlen(buf), "%02X ", *(p+i)); |
WiredHome | 8:f08059355141 | 46 | if ((++i & 0x0F) == 0x00) { |
WiredHome | 8:f08059355141 | 47 | INFO("%s", buf); |
WiredHome | 8:f08059355141 | 48 | if (i < count) |
WiredHome | 8:f08059355141 | 49 | sprintf(buf, "%04X: ", i); |
WiredHome | 8:f08059355141 | 50 | else |
WiredHome | 8:f08059355141 | 51 | buf[0] = '\0'; |
WiredHome | 8:f08059355141 | 52 | } |
WiredHome | 8:f08059355141 | 53 | } |
WiredHome | 8:f08059355141 | 54 | if (strlen(buf)) |
WiredHome | 8:f08059355141 | 55 | INFO("%s", buf); |
WiredHome | 8:f08059355141 | 56 | } |
WiredHome | 8:f08059355141 | 57 | #else |
WiredHome | 8:f08059355141 | 58 | #define INFO(x, ...) |
WiredHome | 8:f08059355141 | 59 | #define WARN(x, ...) |
WiredHome | 8:f08059355141 | 60 | #define ERR(x, ...) |
WiredHome | 8:f08059355141 | 61 | #define HexDump(a, b, c) |
WiredHome | 8:f08059355141 | 62 | #endif |
WiredHome | 8:f08059355141 | 63 | |
emilmont | 1:46ce1e16c870 | 64 | FATFileHandle::FATFileHandle(FIL fh) { |
emilmont | 1:46ce1e16c870 | 65 | _fh = fh; |
emilmont | 1:46ce1e16c870 | 66 | } |
emilmont | 1:46ce1e16c870 | 67 | |
emilmont | 1:46ce1e16c870 | 68 | int FATFileHandle::close() { |
emilmont | 1:46ce1e16c870 | 69 | int retval = f_close(&_fh); |
emilmont | 1:46ce1e16c870 | 70 | delete this; |
emilmont | 1:46ce1e16c870 | 71 | return retval; |
emilmont | 1:46ce1e16c870 | 72 | } |
emilmont | 1:46ce1e16c870 | 73 | |
emilmont | 1:46ce1e16c870 | 74 | ssize_t FATFileHandle::write(const void* buffer, size_t length) { |
emilmont | 1:46ce1e16c870 | 75 | UINT n; |
emilmont | 1:46ce1e16c870 | 76 | FRESULT res = f_write(&_fh, buffer, length, &n); |
mbed_official | 4:3ff2606d5713 | 77 | if (res) { |
WiredHome | 8:f08059355141 | 78 | INFO("f_write() failed: %d", res); |
emilmont | 1:46ce1e16c870 | 79 | return -1; |
emilmont | 1:46ce1e16c870 | 80 | } |
emilmont | 1:46ce1e16c870 | 81 | return n; |
emilmont | 1:46ce1e16c870 | 82 | } |
mbed_official | 4:3ff2606d5713 | 83 | |
emilmont | 1:46ce1e16c870 | 84 | ssize_t FATFileHandle::read(void* buffer, size_t length) { |
WiredHome | 8:f08059355141 | 85 | INFO("read(%d)", length); |
emilmont | 1:46ce1e16c870 | 86 | UINT n; |
emilmont | 1:46ce1e16c870 | 87 | FRESULT res = f_read(&_fh, buffer, length, &n); |
emilmont | 1:46ce1e16c870 | 88 | if (res) { |
WiredHome | 8:f08059355141 | 89 | INFO("f_read() failed: %d", res); |
emilmont | 1:46ce1e16c870 | 90 | return -1; |
emilmont | 1:46ce1e16c870 | 91 | } |
emilmont | 1:46ce1e16c870 | 92 | return n; |
emilmont | 1:46ce1e16c870 | 93 | } |
emilmont | 1:46ce1e16c870 | 94 | |
emilmont | 1:46ce1e16c870 | 95 | int FATFileHandle::isatty() { |
emilmont | 1:46ce1e16c870 | 96 | return 0; |
emilmont | 1:46ce1e16c870 | 97 | } |
emilmont | 1:46ce1e16c870 | 98 | |
emilmont | 1:46ce1e16c870 | 99 | off_t FATFileHandle::lseek(off_t position, int whence) { |
emilmont | 1:46ce1e16c870 | 100 | if (whence == SEEK_END) { |
emilmont | 1:46ce1e16c870 | 101 | position += _fh.fsize; |
emilmont | 1:46ce1e16c870 | 102 | } else if(whence==SEEK_CUR) { |
emilmont | 1:46ce1e16c870 | 103 | position += _fh.fptr; |
emilmont | 1:46ce1e16c870 | 104 | } |
emilmont | 1:46ce1e16c870 | 105 | FRESULT res = f_lseek(&_fh, position); |
emilmont | 1:46ce1e16c870 | 106 | if (res) { |
WiredHome | 8:f08059355141 | 107 | INFO("lseek failed: %d", res); |
emilmont | 1:46ce1e16c870 | 108 | return -1; |
emilmont | 1:46ce1e16c870 | 109 | } else { |
WiredHome | 8:f08059355141 | 110 | INFO("lseek OK, returning %i", _fh.fptr); |
emilmont | 1:46ce1e16c870 | 111 | return _fh.fptr; |
emilmont | 1:46ce1e16c870 | 112 | } |
emilmont | 1:46ce1e16c870 | 113 | } |
emilmont | 1:46ce1e16c870 | 114 | |
emilmont | 1:46ce1e16c870 | 115 | int FATFileHandle::fsync() { |
emilmont | 1:46ce1e16c870 | 116 | FRESULT res = f_sync(&_fh); |
emilmont | 1:46ce1e16c870 | 117 | if (res) { |
WiredHome | 8:f08059355141 | 118 | INFO("f_sync() failed: %d", res); |
emilmont | 1:46ce1e16c870 | 119 | return -1; |
emilmont | 1:46ce1e16c870 | 120 | } |
emilmont | 1:46ce1e16c870 | 121 | return 0; |
emilmont | 1:46ce1e16c870 | 122 | } |
emilmont | 1:46ce1e16c870 | 123 | |
emilmont | 1:46ce1e16c870 | 124 | off_t FATFileHandle::flen() { |
emilmont | 1:46ce1e16c870 | 125 | return _fh.fsize; |
emilmont | 1:46ce1e16c870 | 126 | } |
WiredHome | 8:f08059355141 | 127 |