Library for SD card

Dependents:   City_Game_JSON saver-noenvio-1 SNOCC_V2 lab7_prog1 ... more

Committer:
AlexVC97
Date:
Wed Mar 29 07:00:22 2017 +0000
Revision:
0:3bdfc1556537
SDFileSystem Library

Who changed what in which revision?

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