Mark Schwarzer / SDFileSystem

Dependents:   Schwarzer_A7_1

Committer:
markschwarzer
Date:
Mon Nov 09 14:25:13 2020 +0000
Revision:
0:964d386ab059
logged data in SD card

Who changed what in which revision?

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