Emulation of LocalFileSystem with virtual COM.
Dependents: KL46Z-lpc81isp lpcterm2
#include "USBLocalFileSystem.h" int main() { USBLocalFileSystem* usb_local = new USBLocalFileSystem(); // RamDisk(64KB) while(1) { usb_local->lock(true); usb_local->remount(); char filename[32]; if (usb_local->find(filename, sizeof(filename), "*.TXT")) { FILE* fp = fopen(filename, "r"); if (fp) { int c; while((c = fgetc(fp)) != EOF) { usb_local->putc(c); } fclose(fp); } } usb_local->lock(false); wait_ms(1000*5); } }
Sample application:
Import programKL46Z-lpc81isp
ISP example program.
Import programlpcterm2
semihost server example program
fat/FATFileHandle.cpp@0:39eb4d5b97df, 2014-05-03 (annotated)
- Committer:
- va009039
- Date:
- Sat May 03 11:21:37 2014 +0000
- Revision:
- 0:39eb4d5b97df
first commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
va009039 | 0:39eb4d5b97df | 1 | /* mbed Microcontroller Library |
va009039 | 0:39eb4d5b97df | 2 | * Copyright (c) 2006-2012 ARM Limited |
va009039 | 0:39eb4d5b97df | 3 | * |
va009039 | 0:39eb4d5b97df | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
va009039 | 0:39eb4d5b97df | 5 | * of this software and associated documentation files (the "Software"), to deal |
va009039 | 0:39eb4d5b97df | 6 | * in the Software without restriction, including without limitation the rights |
va009039 | 0:39eb4d5b97df | 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
va009039 | 0:39eb4d5b97df | 8 | * copies of the Software, and to permit persons to whom the Software is |
va009039 | 0:39eb4d5b97df | 9 | * furnished to do so, subject to the following conditions: |
va009039 | 0:39eb4d5b97df | 10 | * |
va009039 | 0:39eb4d5b97df | 11 | * The above copyright notice and this permission notice shall be included in |
va009039 | 0:39eb4d5b97df | 12 | * all copies or substantial portions of the Software. |
va009039 | 0:39eb4d5b97df | 13 | * |
va009039 | 0:39eb4d5b97df | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
va009039 | 0:39eb4d5b97df | 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
va009039 | 0:39eb4d5b97df | 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
va009039 | 0:39eb4d5b97df | 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
va009039 | 0:39eb4d5b97df | 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
va009039 | 0:39eb4d5b97df | 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
va009039 | 0:39eb4d5b97df | 20 | * SOFTWARE. |
va009039 | 0:39eb4d5b97df | 21 | */ |
va009039 | 0:39eb4d5b97df | 22 | #include "ff.h" |
va009039 | 0:39eb4d5b97df | 23 | #include "ffconf.h" |
va009039 | 0:39eb4d5b97df | 24 | #include "mbed_debug.h" |
va009039 | 0:39eb4d5b97df | 25 | |
va009039 | 0:39eb4d5b97df | 26 | #include "FATFileHandle.h" |
va009039 | 0:39eb4d5b97df | 27 | |
va009039 | 0:39eb4d5b97df | 28 | FATFileHandle::FATFileHandle(FIL fh) { |
va009039 | 0:39eb4d5b97df | 29 | _fh = fh; |
va009039 | 0:39eb4d5b97df | 30 | } |
va009039 | 0:39eb4d5b97df | 31 | |
va009039 | 0:39eb4d5b97df | 32 | int FATFileHandle::close() { |
va009039 | 0:39eb4d5b97df | 33 | int retval = f_close(&_fh); |
va009039 | 0:39eb4d5b97df | 34 | delete this; |
va009039 | 0:39eb4d5b97df | 35 | return retval; |
va009039 | 0:39eb4d5b97df | 36 | } |
va009039 | 0:39eb4d5b97df | 37 | |
va009039 | 0:39eb4d5b97df | 38 | ssize_t FATFileHandle::write(const void* buffer, size_t length) { |
va009039 | 0:39eb4d5b97df | 39 | UINT n; |
va009039 | 0:39eb4d5b97df | 40 | FRESULT res = f_write(&_fh, buffer, length, &n); |
va009039 | 0:39eb4d5b97df | 41 | if (res) { |
va009039 | 0:39eb4d5b97df | 42 | debug_if(FFS_DBG, "f_write() failed: %d", res); |
va009039 | 0:39eb4d5b97df | 43 | return -1; |
va009039 | 0:39eb4d5b97df | 44 | } |
va009039 | 0:39eb4d5b97df | 45 | return n; |
va009039 | 0:39eb4d5b97df | 46 | } |
va009039 | 0:39eb4d5b97df | 47 | |
va009039 | 0:39eb4d5b97df | 48 | ssize_t FATFileHandle::read(void* buffer, size_t length) { |
va009039 | 0:39eb4d5b97df | 49 | debug_if(FFS_DBG, "read(%d)\n", length); |
va009039 | 0:39eb4d5b97df | 50 | UINT n; |
va009039 | 0:39eb4d5b97df | 51 | FRESULT res = f_read(&_fh, buffer, length, &n); |
va009039 | 0:39eb4d5b97df | 52 | if (res) { |
va009039 | 0:39eb4d5b97df | 53 | debug_if(FFS_DBG, "f_read() failed: %d\n", res); |
va009039 | 0:39eb4d5b97df | 54 | return -1; |
va009039 | 0:39eb4d5b97df | 55 | } |
va009039 | 0:39eb4d5b97df | 56 | return n; |
va009039 | 0:39eb4d5b97df | 57 | } |
va009039 | 0:39eb4d5b97df | 58 | |
va009039 | 0:39eb4d5b97df | 59 | int FATFileHandle::isatty() { |
va009039 | 0:39eb4d5b97df | 60 | return 0; |
va009039 | 0:39eb4d5b97df | 61 | } |
va009039 | 0:39eb4d5b97df | 62 | |
va009039 | 0:39eb4d5b97df | 63 | off_t FATFileHandle::lseek(off_t position, int whence) { |
va009039 | 0:39eb4d5b97df | 64 | if (whence == SEEK_END) { |
va009039 | 0:39eb4d5b97df | 65 | position += _fh.fsize; |
va009039 | 0:39eb4d5b97df | 66 | } else if(whence==SEEK_CUR) { |
va009039 | 0:39eb4d5b97df | 67 | position += _fh.fptr; |
va009039 | 0:39eb4d5b97df | 68 | } |
va009039 | 0:39eb4d5b97df | 69 | FRESULT res = f_lseek(&_fh, position); |
va009039 | 0:39eb4d5b97df | 70 | if (res) { |
va009039 | 0:39eb4d5b97df | 71 | debug_if(FFS_DBG, "lseek failed: %d\n", res); |
va009039 | 0:39eb4d5b97df | 72 | return -1; |
va009039 | 0:39eb4d5b97df | 73 | } else { |
va009039 | 0:39eb4d5b97df | 74 | debug_if(FFS_DBG, "lseek OK, returning %i\n", _fh.fptr); |
va009039 | 0:39eb4d5b97df | 75 | return _fh.fptr; |
va009039 | 0:39eb4d5b97df | 76 | } |
va009039 | 0:39eb4d5b97df | 77 | } |
va009039 | 0:39eb4d5b97df | 78 | |
va009039 | 0:39eb4d5b97df | 79 | int FATFileHandle::fsync() { |
va009039 | 0:39eb4d5b97df | 80 | FRESULT res = f_sync(&_fh); |
va009039 | 0:39eb4d5b97df | 81 | if (res) { |
va009039 | 0:39eb4d5b97df | 82 | debug_if(FFS_DBG, "f_sync() failed: %d\n", res); |
va009039 | 0:39eb4d5b97df | 83 | return -1; |
va009039 | 0:39eb4d5b97df | 84 | } |
va009039 | 0:39eb4d5b97df | 85 | return 0; |
va009039 | 0:39eb4d5b97df | 86 | } |
va009039 | 0:39eb4d5b97df | 87 | |
va009039 | 0:39eb4d5b97df | 88 | off_t FATFileHandle::flen() { |
va009039 | 0:39eb4d5b97df | 89 | return _fh.fsize; |
va009039 | 0:39eb4d5b97df | 90 | } |