mbed os with nrf51 internal bandgap enabled to read battery level
Dependents: BLE_file_test BLE_Blink ExternalEncoder
features/unsupported/fs/fat/FATFileHandle.cpp@0:f269e3021894, 2016-10-23 (annotated)
- Committer:
- elessair
- Date:
- Sun Oct 23 15:10:02 2016 +0000
- Revision:
- 0:f269e3021894
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
elessair | 0:f269e3021894 | 1 | /* mbed Microcontroller Library |
elessair | 0:f269e3021894 | 2 | * Copyright (c) 2006-2012 ARM Limited |
elessair | 0:f269e3021894 | 3 | * |
elessair | 0:f269e3021894 | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
elessair | 0:f269e3021894 | 5 | * of this software and associated documentation files (the "Software"), to deal |
elessair | 0:f269e3021894 | 6 | * in the Software without restriction, including without limitation the rights |
elessair | 0:f269e3021894 | 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
elessair | 0:f269e3021894 | 8 | * copies of the Software, and to permit persons to whom the Software is |
elessair | 0:f269e3021894 | 9 | * furnished to do so, subject to the following conditions: |
elessair | 0:f269e3021894 | 10 | * |
elessair | 0:f269e3021894 | 11 | * The above copyright notice and this permission notice shall be included in |
elessair | 0:f269e3021894 | 12 | * all copies or substantial portions of the Software. |
elessair | 0:f269e3021894 | 13 | * |
elessair | 0:f269e3021894 | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
elessair | 0:f269e3021894 | 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
elessair | 0:f269e3021894 | 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
elessair | 0:f269e3021894 | 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
elessair | 0:f269e3021894 | 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
elessair | 0:f269e3021894 | 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
elessair | 0:f269e3021894 | 20 | * SOFTWARE. |
elessair | 0:f269e3021894 | 21 | */ |
elessair | 0:f269e3021894 | 22 | #include "ff.h" |
elessair | 0:f269e3021894 | 23 | #include "ffconf.h" |
elessair | 0:f269e3021894 | 24 | #include "mbed_debug.h" |
elessair | 0:f269e3021894 | 25 | |
elessair | 0:f269e3021894 | 26 | #include "FATFileHandle.h" |
elessair | 0:f269e3021894 | 27 | |
elessair | 0:f269e3021894 | 28 | FATFileHandle::FATFileHandle(FIL fh, PlatformMutex * mutex): _mutex(mutex) { |
elessair | 0:f269e3021894 | 29 | _fh = fh; |
elessair | 0:f269e3021894 | 30 | } |
elessair | 0:f269e3021894 | 31 | |
elessair | 0:f269e3021894 | 32 | int FATFileHandle::close() { |
elessair | 0:f269e3021894 | 33 | lock(); |
elessair | 0:f269e3021894 | 34 | int retval = f_close(&_fh); |
elessair | 0:f269e3021894 | 35 | unlock(); |
elessair | 0:f269e3021894 | 36 | delete this; |
elessair | 0:f269e3021894 | 37 | return retval; |
elessair | 0:f269e3021894 | 38 | } |
elessair | 0:f269e3021894 | 39 | |
elessair | 0:f269e3021894 | 40 | ssize_t FATFileHandle::write(const void* buffer, size_t length) { |
elessair | 0:f269e3021894 | 41 | lock(); |
elessair | 0:f269e3021894 | 42 | UINT n; |
elessair | 0:f269e3021894 | 43 | FRESULT res = f_write(&_fh, buffer, length, &n); |
elessair | 0:f269e3021894 | 44 | if (res) { |
elessair | 0:f269e3021894 | 45 | debug_if(FFS_DBG, "f_write() failed: %d", res); |
elessair | 0:f269e3021894 | 46 | unlock(); |
elessair | 0:f269e3021894 | 47 | return -1; |
elessair | 0:f269e3021894 | 48 | } |
elessair | 0:f269e3021894 | 49 | unlock(); |
elessair | 0:f269e3021894 | 50 | return n; |
elessair | 0:f269e3021894 | 51 | } |
elessair | 0:f269e3021894 | 52 | |
elessair | 0:f269e3021894 | 53 | ssize_t FATFileHandle::read(void* buffer, size_t length) { |
elessair | 0:f269e3021894 | 54 | lock(); |
elessair | 0:f269e3021894 | 55 | debug_if(FFS_DBG, "read(%d)\n", length); |
elessair | 0:f269e3021894 | 56 | UINT n; |
elessair | 0:f269e3021894 | 57 | FRESULT res = f_read(&_fh, buffer, length, &n); |
elessair | 0:f269e3021894 | 58 | if (res) { |
elessair | 0:f269e3021894 | 59 | debug_if(FFS_DBG, "f_read() failed: %d\n", res); |
elessair | 0:f269e3021894 | 60 | unlock(); |
elessair | 0:f269e3021894 | 61 | return -1; |
elessair | 0:f269e3021894 | 62 | } |
elessair | 0:f269e3021894 | 63 | unlock(); |
elessair | 0:f269e3021894 | 64 | return n; |
elessair | 0:f269e3021894 | 65 | } |
elessair | 0:f269e3021894 | 66 | |
elessair | 0:f269e3021894 | 67 | int FATFileHandle::isatty() { |
elessair | 0:f269e3021894 | 68 | return 0; |
elessair | 0:f269e3021894 | 69 | } |
elessair | 0:f269e3021894 | 70 | |
elessair | 0:f269e3021894 | 71 | off_t FATFileHandle::lseek(off_t position, int whence) { |
elessair | 0:f269e3021894 | 72 | lock(); |
elessair | 0:f269e3021894 | 73 | if (whence == SEEK_END) { |
elessair | 0:f269e3021894 | 74 | position += _fh.fsize; |
elessair | 0:f269e3021894 | 75 | } else if(whence==SEEK_CUR) { |
elessair | 0:f269e3021894 | 76 | position += _fh.fptr; |
elessair | 0:f269e3021894 | 77 | } |
elessair | 0:f269e3021894 | 78 | FRESULT res = f_lseek(&_fh, position); |
elessair | 0:f269e3021894 | 79 | if (res) { |
elessair | 0:f269e3021894 | 80 | debug_if(FFS_DBG, "lseek failed: %d\n", res); |
elessair | 0:f269e3021894 | 81 | unlock(); |
elessair | 0:f269e3021894 | 82 | return -1; |
elessair | 0:f269e3021894 | 83 | } else { |
elessair | 0:f269e3021894 | 84 | debug_if(FFS_DBG, "lseek OK, returning %i\n", _fh.fptr); |
elessair | 0:f269e3021894 | 85 | unlock(); |
elessair | 0:f269e3021894 | 86 | return _fh.fptr; |
elessair | 0:f269e3021894 | 87 | } |
elessair | 0:f269e3021894 | 88 | } |
elessair | 0:f269e3021894 | 89 | |
elessair | 0:f269e3021894 | 90 | int FATFileHandle::fsync() { |
elessair | 0:f269e3021894 | 91 | lock(); |
elessair | 0:f269e3021894 | 92 | FRESULT res = f_sync(&_fh); |
elessair | 0:f269e3021894 | 93 | if (res) { |
elessair | 0:f269e3021894 | 94 | debug_if(FFS_DBG, "f_sync() failed: %d\n", res); |
elessair | 0:f269e3021894 | 95 | unlock(); |
elessair | 0:f269e3021894 | 96 | return -1; |
elessair | 0:f269e3021894 | 97 | } |
elessair | 0:f269e3021894 | 98 | unlock(); |
elessair | 0:f269e3021894 | 99 | return 0; |
elessair | 0:f269e3021894 | 100 | } |
elessair | 0:f269e3021894 | 101 | |
elessair | 0:f269e3021894 | 102 | off_t FATFileHandle::flen() { |
elessair | 0:f269e3021894 | 103 | lock(); |
elessair | 0:f269e3021894 | 104 | off_t size = _fh.fsize; |
elessair | 0:f269e3021894 | 105 | unlock(); |
elessair | 0:f269e3021894 | 106 | return size; |
elessair | 0:f269e3021894 | 107 | } |
elessair | 0:f269e3021894 | 108 | |
elessair | 0:f269e3021894 | 109 | void FATFileHandle::lock() { |
elessair | 0:f269e3021894 | 110 | _mutex->lock(); |
elessair | 0:f269e3021894 | 111 | } |
elessair | 0:f269e3021894 | 112 | |
elessair | 0:f269e3021894 | 113 | void FATFileHandle::unlock() { |
elessair | 0:f269e3021894 | 114 | _mutex->unlock(); |
elessair | 0:f269e3021894 | 115 | } |