Uses a more recent version of official mbed libraries than that of Neil Thiessen

Dependencies:   FATFileSystem

Dependents:   ThermalWake STM32F407VET6_SDCard STM32F407VET6_SD_HTTP_Server ZZ_SSL_Main_L476 ... more

Fork of SDFileSystem by Neil Thiessen

Committer:
neilt6
Date:
Thu Aug 14 22:27:07 2014 +0000
Revision:
11:67ddc53e3983
Major performance improvements with custom FATFileSystem

Who changed what in which revision?

UserRevisionLine numberNew contents of line
neilt6 11:67ddc53e3983 1 /* mbed Microcontroller Library
neilt6 11:67ddc53e3983 2 * Copyright (c) 2006-2012 ARM Limited
neilt6 11:67ddc53e3983 3 *
neilt6 11:67ddc53e3983 4 * Permission is hereby granted, free of charge, to any person obtaining a copy
neilt6 11:67ddc53e3983 5 * of this software and associated documentation files (the "Software"), to deal
neilt6 11:67ddc53e3983 6 * in the Software without restriction, including without limitation the rights
neilt6 11:67ddc53e3983 7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
neilt6 11:67ddc53e3983 8 * copies of the Software, and to permit persons to whom the Software is
neilt6 11:67ddc53e3983 9 * furnished to do so, subject to the following conditions:
neilt6 11:67ddc53e3983 10 *
neilt6 11:67ddc53e3983 11 * The above copyright notice and this permission notice shall be included in
neilt6 11:67ddc53e3983 12 * all copies or substantial portions of the Software.
neilt6 11:67ddc53e3983 13 *
neilt6 11:67ddc53e3983 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
neilt6 11:67ddc53e3983 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
neilt6 11:67ddc53e3983 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
neilt6 11:67ddc53e3983 17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
neilt6 11:67ddc53e3983 18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
neilt6 11:67ddc53e3983 19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
neilt6 11:67ddc53e3983 20 * SOFTWARE.
neilt6 11:67ddc53e3983 21 */
neilt6 11:67ddc53e3983 22 #include "ff.h"
neilt6 11:67ddc53e3983 23 #include "ffconf.h"
neilt6 11:67ddc53e3983 24 #include "mbed_debug.h"
neilt6 11:67ddc53e3983 25
neilt6 11:67ddc53e3983 26 #include "FATFileHandle.h"
neilt6 11:67ddc53e3983 27
neilt6 11:67ddc53e3983 28 FATFileHandle::FATFileHandle(FIL fh) {
neilt6 11:67ddc53e3983 29 _fh = fh;
neilt6 11:67ddc53e3983 30 }
neilt6 11:67ddc53e3983 31
neilt6 11:67ddc53e3983 32 int FATFileHandle::close() {
neilt6 11:67ddc53e3983 33 int retval = f_close(&_fh);
neilt6 11:67ddc53e3983 34 delete this;
neilt6 11:67ddc53e3983 35 return retval;
neilt6 11:67ddc53e3983 36 }
neilt6 11:67ddc53e3983 37
neilt6 11:67ddc53e3983 38 ssize_t FATFileHandle::write(const void* buffer, size_t length) {
neilt6 11:67ddc53e3983 39 UINT n;
neilt6 11:67ddc53e3983 40 FRESULT res = f_write(&_fh, buffer, length, &n);
neilt6 11:67ddc53e3983 41 if (res) {
neilt6 11:67ddc53e3983 42 debug_if(FFS_DBG, "f_write() failed: %d", res);
neilt6 11:67ddc53e3983 43 return -1;
neilt6 11:67ddc53e3983 44 }
neilt6 11:67ddc53e3983 45 return n;
neilt6 11:67ddc53e3983 46 }
neilt6 11:67ddc53e3983 47
neilt6 11:67ddc53e3983 48 ssize_t FATFileHandle::read(void* buffer, size_t length) {
neilt6 11:67ddc53e3983 49 debug_if(FFS_DBG, "read(%d)\n", length);
neilt6 11:67ddc53e3983 50 UINT n;
neilt6 11:67ddc53e3983 51 FRESULT res = f_read(&_fh, buffer, length, &n);
neilt6 11:67ddc53e3983 52 if (res) {
neilt6 11:67ddc53e3983 53 debug_if(FFS_DBG, "f_read() failed: %d\n", res);
neilt6 11:67ddc53e3983 54 return -1;
neilt6 11:67ddc53e3983 55 }
neilt6 11:67ddc53e3983 56 return n;
neilt6 11:67ddc53e3983 57 }
neilt6 11:67ddc53e3983 58
neilt6 11:67ddc53e3983 59 int FATFileHandle::isatty() {
neilt6 11:67ddc53e3983 60 return 0;
neilt6 11:67ddc53e3983 61 }
neilt6 11:67ddc53e3983 62
neilt6 11:67ddc53e3983 63 off_t FATFileHandle::lseek(off_t position, int whence) {
neilt6 11:67ddc53e3983 64 if (whence == SEEK_END) {
neilt6 11:67ddc53e3983 65 position += _fh.fsize;
neilt6 11:67ddc53e3983 66 } else if(whence==SEEK_CUR) {
neilt6 11:67ddc53e3983 67 position += _fh.fptr;
neilt6 11:67ddc53e3983 68 }
neilt6 11:67ddc53e3983 69 FRESULT res = f_lseek(&_fh, position);
neilt6 11:67ddc53e3983 70 if (res) {
neilt6 11:67ddc53e3983 71 debug_if(FFS_DBG, "lseek failed: %d\n", res);
neilt6 11:67ddc53e3983 72 return -1;
neilt6 11:67ddc53e3983 73 } else {
neilt6 11:67ddc53e3983 74 debug_if(FFS_DBG, "lseek OK, returning %i\n", _fh.fptr);
neilt6 11:67ddc53e3983 75 return _fh.fptr;
neilt6 11:67ddc53e3983 76 }
neilt6 11:67ddc53e3983 77 }
neilt6 11:67ddc53e3983 78
neilt6 11:67ddc53e3983 79 int FATFileHandle::fsync() {
neilt6 11:67ddc53e3983 80 FRESULT res = f_sync(&_fh);
neilt6 11:67ddc53e3983 81 if (res) {
neilt6 11:67ddc53e3983 82 debug_if(FFS_DBG, "f_sync() failed: %d\n", res);
neilt6 11:67ddc53e3983 83 return -1;
neilt6 11:67ddc53e3983 84 }
neilt6 11:67ddc53e3983 85 return 0;
neilt6 11:67ddc53e3983 86 }
neilt6 11:67ddc53e3983 87
neilt6 11:67ddc53e3983 88 off_t FATFileHandle::flen() {
neilt6 11:67ddc53e3983 89 return _fh.fsize;
neilt6 11:67ddc53e3983 90 }