This repo contains the libraries of Mbed for LPC1549 with following changes: - IAP commands. - EEPROM write and read. - UART write and read (public) - CAN can_s -> LPC_C_CAN0_Type *can
mbed/FileHandle.h@0:820a69dfd200, 2015-04-14 (annotated)
- Committer:
- gmatarrubia
- Date:
- Tue Apr 14 15:00:13 2015 +0200
- Revision:
- 0:820a69dfd200
Initial repo. IAP commands, EEPROM write/read, UART write/read, CAN
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
gmatarrubia |
0:820a69dfd200 | 1 | /* mbed Microcontroller Library |
gmatarrubia |
0:820a69dfd200 | 2 | * Copyright (c) 2006-2013 ARM Limited |
gmatarrubia |
0:820a69dfd200 | 3 | * |
gmatarrubia |
0:820a69dfd200 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
gmatarrubia |
0:820a69dfd200 | 5 | * you may not use this file except in compliance with the License. |
gmatarrubia |
0:820a69dfd200 | 6 | * You may obtain a copy of the License at |
gmatarrubia |
0:820a69dfd200 | 7 | * |
gmatarrubia |
0:820a69dfd200 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
gmatarrubia |
0:820a69dfd200 | 9 | * |
gmatarrubia |
0:820a69dfd200 | 10 | * Unless required by applicable law or agreed to in writing, software |
gmatarrubia |
0:820a69dfd200 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
gmatarrubia |
0:820a69dfd200 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
gmatarrubia |
0:820a69dfd200 | 13 | * See the License for the specific language governing permissions and |
gmatarrubia |
0:820a69dfd200 | 14 | * limitations under the License. |
gmatarrubia |
0:820a69dfd200 | 15 | */ |
gmatarrubia |
0:820a69dfd200 | 16 | #ifndef MBED_FILEHANDLE_H |
gmatarrubia |
0:820a69dfd200 | 17 | #define MBED_FILEHANDLE_H |
gmatarrubia |
0:820a69dfd200 | 18 | |
gmatarrubia |
0:820a69dfd200 | 19 | typedef int FILEHANDLE; |
gmatarrubia |
0:820a69dfd200 | 20 | |
gmatarrubia |
0:820a69dfd200 | 21 | #include <stdio.h> |
gmatarrubia |
0:820a69dfd200 | 22 | |
gmatarrubia |
0:820a69dfd200 | 23 | #if defined(__ARMCC_VERSION) || defined(__ICCARM__) |
gmatarrubia |
0:820a69dfd200 | 24 | typedef int ssize_t; |
gmatarrubia |
0:820a69dfd200 | 25 | typedef long off_t; |
gmatarrubia |
0:820a69dfd200 | 26 | |
gmatarrubia |
0:820a69dfd200 | 27 | #else |
gmatarrubia |
0:820a69dfd200 | 28 | # include <sys/types.h> |
gmatarrubia |
0:820a69dfd200 | 29 | #endif |
gmatarrubia |
0:820a69dfd200 | 30 | |
gmatarrubia |
0:820a69dfd200 | 31 | namespace mbed { |
gmatarrubia |
0:820a69dfd200 | 32 | |
gmatarrubia |
0:820a69dfd200 | 33 | /** An OO equivalent of the internal FILEHANDLE variable |
gmatarrubia |
0:820a69dfd200 | 34 | * and associated _sys_* functions. |
gmatarrubia |
0:820a69dfd200 | 35 | * |
gmatarrubia |
0:820a69dfd200 | 36 | * FileHandle is an abstract class, needing at least sys_write and |
gmatarrubia |
0:820a69dfd200 | 37 | * sys_read to be implmented for a simple interactive device. |
gmatarrubia |
0:820a69dfd200 | 38 | * |
gmatarrubia |
0:820a69dfd200 | 39 | * No one ever directly tals to/instanciates a FileHandle - it gets |
gmatarrubia |
0:820a69dfd200 | 40 | * created by FileSystem, and wrapped up by stdio. |
gmatarrubia |
0:820a69dfd200 | 41 | */ |
gmatarrubia |
0:820a69dfd200 | 42 | class FileHandle { |
gmatarrubia |
0:820a69dfd200 | 43 | |
gmatarrubia |
0:820a69dfd200 | 44 | public: |
gmatarrubia |
0:820a69dfd200 | 45 | /** Write the contents of a buffer to the file |
gmatarrubia |
0:820a69dfd200 | 46 | * |
gmatarrubia |
0:820a69dfd200 | 47 | * @param buffer the buffer to write from |
gmatarrubia |
0:820a69dfd200 | 48 | * @param length the number of characters to write |
gmatarrubia |
0:820a69dfd200 | 49 | * |
gmatarrubia |
0:820a69dfd200 | 50 | * @returns |
gmatarrubia |
0:820a69dfd200 | 51 | * The number of characters written (possibly 0) on success, -1 on error. |
gmatarrubia |
0:820a69dfd200 | 52 | */ |
gmatarrubia |
0:820a69dfd200 | 53 | virtual ssize_t write(const void* buffer, size_t length) = 0; |
gmatarrubia |
0:820a69dfd200 | 54 | |
gmatarrubia |
0:820a69dfd200 | 55 | /** Close the file |
gmatarrubia |
0:820a69dfd200 | 56 | * |
gmatarrubia |
0:820a69dfd200 | 57 | * @returns |
gmatarrubia |
0:820a69dfd200 | 58 | * Zero on success, -1 on error. |
gmatarrubia |
0:820a69dfd200 | 59 | */ |
gmatarrubia |
0:820a69dfd200 | 60 | virtual int close() = 0; |
gmatarrubia |
0:820a69dfd200 | 61 | |
gmatarrubia |
0:820a69dfd200 | 62 | /** Function read |
gmatarrubia |
0:820a69dfd200 | 63 | * Reads the contents of the file into a buffer |
gmatarrubia |
0:820a69dfd200 | 64 | * |
gmatarrubia |
0:820a69dfd200 | 65 | * @param buffer the buffer to read in to |
gmatarrubia |
0:820a69dfd200 | 66 | * @param length the number of characters to read |
gmatarrubia |
0:820a69dfd200 | 67 | * |
gmatarrubia |
0:820a69dfd200 | 68 | * @returns |
gmatarrubia |
0:820a69dfd200 | 69 | * The number of characters read (zero at end of file) on success, -1 on error. |
gmatarrubia |
0:820a69dfd200 | 70 | */ |
gmatarrubia |
0:820a69dfd200 | 71 | virtual ssize_t read(void* buffer, size_t length) = 0; |
gmatarrubia |
0:820a69dfd200 | 72 | |
gmatarrubia |
0:820a69dfd200 | 73 | /** Check if the handle is for a interactive terminal device. |
gmatarrubia |
0:820a69dfd200 | 74 | * If so, line buffered behaviour is used by default |
gmatarrubia |
0:820a69dfd200 | 75 | * |
gmatarrubia |
0:820a69dfd200 | 76 | * @returns |
gmatarrubia |
0:820a69dfd200 | 77 | * 1 if it is a terminal, |
gmatarrubia |
0:820a69dfd200 | 78 | * 0 otherwise |
gmatarrubia |
0:820a69dfd200 | 79 | */ |
gmatarrubia |
0:820a69dfd200 | 80 | virtual int isatty() = 0; |
gmatarrubia |
0:820a69dfd200 | 81 | |
gmatarrubia |
0:820a69dfd200 | 82 | /** Move the file position to a given offset from a given location. |
gmatarrubia |
0:820a69dfd200 | 83 | * |
gmatarrubia |
0:820a69dfd200 | 84 | * @param offset The offset from whence to move to |
gmatarrubia |
0:820a69dfd200 | 85 | * @param whence SEEK_SET for the start of the file, SEEK_CUR for the |
gmatarrubia |
0:820a69dfd200 | 86 | * current file position, or SEEK_END for the end of the file. |
gmatarrubia |
0:820a69dfd200 | 87 | * |
gmatarrubia |
0:820a69dfd200 | 88 | * @returns |
gmatarrubia |
0:820a69dfd200 | 89 | * new file position on success, |
gmatarrubia |
0:820a69dfd200 | 90 | * -1 on failure or unsupported |
gmatarrubia |
0:820a69dfd200 | 91 | */ |
gmatarrubia |
0:820a69dfd200 | 92 | virtual off_t lseek(off_t offset, int whence) = 0; |
gmatarrubia |
0:820a69dfd200 | 93 | |
gmatarrubia |
0:820a69dfd200 | 94 | /** Flush any buffers associated with the FileHandle, ensuring it |
gmatarrubia |
0:820a69dfd200 | 95 | * is up to date on disk |
gmatarrubia |
0:820a69dfd200 | 96 | * |
gmatarrubia |
0:820a69dfd200 | 97 | * @returns |
gmatarrubia |
0:820a69dfd200 | 98 | * 0 on success or un-needed, |
gmatarrubia |
0:820a69dfd200 | 99 | * -1 on error |
gmatarrubia |
0:820a69dfd200 | 100 | */ |
gmatarrubia |
0:820a69dfd200 | 101 | virtual int fsync() = 0; |
gmatarrubia |
0:820a69dfd200 | 102 | |
gmatarrubia |
0:820a69dfd200 | 103 | virtual off_t flen() { |
gmatarrubia |
0:820a69dfd200 | 104 | /* remember our current position */ |
gmatarrubia |
0:820a69dfd200 | 105 | off_t pos = lseek(0, SEEK_CUR); |
gmatarrubia |
0:820a69dfd200 | 106 | if(pos == -1) return -1; |
gmatarrubia |
0:820a69dfd200 | 107 | /* seek to the end to get the file length */ |
gmatarrubia |
0:820a69dfd200 | 108 | off_t res = lseek(0, SEEK_END); |
gmatarrubia |
0:820a69dfd200 | 109 | /* return to our old position */ |
gmatarrubia |
0:820a69dfd200 | 110 | lseek(pos, SEEK_SET); |
gmatarrubia |
0:820a69dfd200 | 111 | return res; |
gmatarrubia |
0:820a69dfd200 | 112 | } |
gmatarrubia |
0:820a69dfd200 | 113 | |
gmatarrubia |
0:820a69dfd200 | 114 | virtual ~FileHandle(); |
gmatarrubia |
0:820a69dfd200 | 115 | }; |
gmatarrubia |
0:820a69dfd200 | 116 | |
gmatarrubia |
0:820a69dfd200 | 117 | } // namespace mbed |
gmatarrubia |
0:820a69dfd200 | 118 | |
gmatarrubia |
0:820a69dfd200 | 119 | #endif |