Pierre Provent / USBHost

Dependents:   TEST_USB_Nucleo_F429ZI Essais_USB_Nucleo_F429ZI SID_V3_Nucleo_F429ZI SID_V4_Nucleo_F429ZI_copy

Committer:
pierreprovent
Date:
Fri Sep 25 10:17:49 2020 +0000
Revision:
0:77ca32e8e04e
Programme acquisition en enregistrement sur clef USB carte Nucleo F429ZI cours ELE118 Cnam

Who changed what in which revision?

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