SD card data log

Dependents:   riset_Datalogger

Committer:
kotachi
Date:
Sun Nov 03 07:28:09 2019 +0000
Revision:
0:18d80d7403d3
SD card logging system

Who changed what in which revision?

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