Example for reading and writing SD card using SPI mode. SD カード書き込み,読み出しの例 インターフェース:SPI モード使用

Dependencies:   mbed

Committer:
MikamiUitOpen
Date:
Sun Nov 13 11:37:35 2016 +0000
Revision:
0:08b4a59451e5
1

Who changed what in which revision?

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