-- CHANGED -- To stop compilation errors I changed all *.c files to *.cpp. I have not tested this to see if it actually works. --- Updated FAT File System driver. Features include: * Updated to R0.09 - Sep 06, 2011 [[http://elm-chan.org/fsw/ff/00index_e.html]] * Bug fixes from Stéphane Bausseron ** [[http://mbed.org/forum/mbed/topic/2273/?page=1#comment-11521]] ** [[http://mbed.org/forum/mbed/topic/2307]] * Long filename support enabled and exposed through mbed SDK.

Dependents:   WAVEplayer_fix

Fork of FatFileSystem by Adam Green

Committer:
bridadan
Date:
Mon Feb 09 16:02:31 2015 +0000
Revision:
1:8263aa1f626a
Parent:
0:6ceefe1c53e4
Attempting to fix program for K64F platform.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
AdamGreen 0:6ceefe1c53e4 1 /* mbed Microcontroller Library - FATFileHandle
AdamGreen 0:6ceefe1c53e4 2 * Copyright (c) 2008, sford
AdamGreen 0:6ceefe1c53e4 3 */
AdamGreen 0:6ceefe1c53e4 4
AdamGreen 0:6ceefe1c53e4 5 #include "FATFileHandle.h"
AdamGreen 0:6ceefe1c53e4 6
AdamGreen 0:6ceefe1c53e4 7 #include <stdio.h>
AdamGreen 0:6ceefe1c53e4 8 #include <stdlib.h>
AdamGreen 0:6ceefe1c53e4 9 #include "ff.h"
AdamGreen 0:6ceefe1c53e4 10 #include "FATFileSystem.h"
AdamGreen 0:6ceefe1c53e4 11
AdamGreen 0:6ceefe1c53e4 12 namespace mbed {
AdamGreen 0:6ceefe1c53e4 13
AdamGreen 0:6ceefe1c53e4 14 #if FFSDEBUG_ENABLED
AdamGreen 0:6ceefe1c53e4 15 static const char *FR_ERRORS[] = {
AdamGreen 0:6ceefe1c53e4 16 "FR_OK = 0",
AdamGreen 0:6ceefe1c53e4 17 "FR_NOT_READY",
AdamGreen 0:6ceefe1c53e4 18 "FR_NO_FILE",
AdamGreen 0:6ceefe1c53e4 19 "FR_NO_PATH",
AdamGreen 0:6ceefe1c53e4 20 "FR_INVALID_NAME",
AdamGreen 0:6ceefe1c53e4 21 "FR_INVALID_DRIVE",
AdamGreen 0:6ceefe1c53e4 22 "FR_DENIED",
AdamGreen 0:6ceefe1c53e4 23 "FR_EXIST",
AdamGreen 0:6ceefe1c53e4 24 "FR_RW_ERROR",
AdamGreen 0:6ceefe1c53e4 25 "FR_WRITE_PROTECTED",
AdamGreen 0:6ceefe1c53e4 26 "FR_NOT_ENABLED",
AdamGreen 0:6ceefe1c53e4 27 "FR_NO_FILESYSTEM",
AdamGreen 0:6ceefe1c53e4 28 "FR_INVALID_OBJECT",
AdamGreen 0:6ceefe1c53e4 29 "FR_MKFS_ABORTED"
AdamGreen 0:6ceefe1c53e4 30 };
AdamGreen 0:6ceefe1c53e4 31 #endif
AdamGreen 0:6ceefe1c53e4 32
AdamGreen 0:6ceefe1c53e4 33 FATFileHandle::FATFileHandle(FIL fh) {
AdamGreen 0:6ceefe1c53e4 34 _fh = fh;
AdamGreen 0:6ceefe1c53e4 35 }
AdamGreen 0:6ceefe1c53e4 36
AdamGreen 0:6ceefe1c53e4 37 int FATFileHandle::close() {
AdamGreen 0:6ceefe1c53e4 38 FFSDEBUG("close\n");
AdamGreen 0:6ceefe1c53e4 39 int retval = f_close(&_fh);
AdamGreen 0:6ceefe1c53e4 40 delete this;
AdamGreen 0:6ceefe1c53e4 41 return retval;
AdamGreen 0:6ceefe1c53e4 42 }
AdamGreen 0:6ceefe1c53e4 43
AdamGreen 0:6ceefe1c53e4 44 ssize_t FATFileHandle::write(const void* buffer, size_t length) {
AdamGreen 0:6ceefe1c53e4 45 FFSDEBUG("write(%d)\n", length);
AdamGreen 0:6ceefe1c53e4 46 UINT n;
AdamGreen 0:6ceefe1c53e4 47 FRESULT res = f_write(&_fh, buffer, length, &n);
AdamGreen 0:6ceefe1c53e4 48 if(res) {
AdamGreen 0:6ceefe1c53e4 49 FFSDEBUG("f_write() failed (%d, %s)", res, FR_ERRORS[res]);
AdamGreen 0:6ceefe1c53e4 50 return -1;
AdamGreen 0:6ceefe1c53e4 51 }
AdamGreen 0:6ceefe1c53e4 52 return n;
AdamGreen 0:6ceefe1c53e4 53 }
AdamGreen 0:6ceefe1c53e4 54
AdamGreen 0:6ceefe1c53e4 55 ssize_t FATFileHandle::read(void* buffer, size_t length) {
AdamGreen 0:6ceefe1c53e4 56 FFSDEBUG("read(%d)\n", length);
AdamGreen 0:6ceefe1c53e4 57 UINT n;
AdamGreen 0:6ceefe1c53e4 58 FRESULT res = f_read(&_fh, buffer, length, &n);
AdamGreen 0:6ceefe1c53e4 59 if(res) {
AdamGreen 0:6ceefe1c53e4 60 FFSDEBUG("f_read() failed (%d, %s)\n", res, FR_ERRORS[res]);
AdamGreen 0:6ceefe1c53e4 61 return -1;
AdamGreen 0:6ceefe1c53e4 62 }
AdamGreen 0:6ceefe1c53e4 63 return n;
AdamGreen 0:6ceefe1c53e4 64 }
AdamGreen 0:6ceefe1c53e4 65
AdamGreen 0:6ceefe1c53e4 66 int FATFileHandle::isatty() {
AdamGreen 0:6ceefe1c53e4 67 return 0;
AdamGreen 0:6ceefe1c53e4 68 }
AdamGreen 0:6ceefe1c53e4 69
AdamGreen 0:6ceefe1c53e4 70 off_t FATFileHandle::lseek(off_t position, int whence) {
AdamGreen 0:6ceefe1c53e4 71 FFSDEBUG("lseek(%i,%i)\n",position,whence);
AdamGreen 0:6ceefe1c53e4 72 if(whence == SEEK_END) {
AdamGreen 0:6ceefe1c53e4 73 position += _fh.fsize;
AdamGreen 0:6ceefe1c53e4 74 } else if(whence==SEEK_CUR) {
AdamGreen 0:6ceefe1c53e4 75 position += _fh.fptr;
AdamGreen 0:6ceefe1c53e4 76 }
AdamGreen 0:6ceefe1c53e4 77 FRESULT res = f_lseek(&_fh, position);
AdamGreen 0:6ceefe1c53e4 78 if(res) {
AdamGreen 0:6ceefe1c53e4 79 FFSDEBUG("lseek failed (%d, %s)\n", res, FR_ERRORS[res]);
AdamGreen 0:6ceefe1c53e4 80 return -1;
AdamGreen 0:6ceefe1c53e4 81 } else {
AdamGreen 0:6ceefe1c53e4 82 FFSDEBUG("lseek OK, returning %i\n", _fh.fptr);
AdamGreen 0:6ceefe1c53e4 83 return _fh.fptr;
AdamGreen 0:6ceefe1c53e4 84 }
AdamGreen 0:6ceefe1c53e4 85 }
AdamGreen 0:6ceefe1c53e4 86
AdamGreen 0:6ceefe1c53e4 87 int FATFileHandle::fsync() {
AdamGreen 0:6ceefe1c53e4 88 FFSDEBUG("fsync()\n");
AdamGreen 0:6ceefe1c53e4 89 FRESULT res = f_sync(&_fh);
AdamGreen 0:6ceefe1c53e4 90 if (res) {
AdamGreen 0:6ceefe1c53e4 91 FFSDEBUG("f_sync() failed (%d, %s)\n", res, FR_ERRORS[res]);
AdamGreen 0:6ceefe1c53e4 92 return -1;
AdamGreen 0:6ceefe1c53e4 93 }
AdamGreen 0:6ceefe1c53e4 94 return 0;
AdamGreen 0:6ceefe1c53e4 95 }
AdamGreen 0:6ceefe1c53e4 96
AdamGreen 0:6ceefe1c53e4 97 off_t FATFileHandle::flen() {
AdamGreen 0:6ceefe1c53e4 98 FFSDEBUG("flen\n");
AdamGreen 0:6ceefe1c53e4 99 return _fh.fsize;
AdamGreen 0:6ceefe1c53e4 100 }
AdamGreen 0:6ceefe1c53e4 101
AdamGreen 0:6ceefe1c53e4 102 } // namespace mbed