Opencv 3.1 project on GR-PEACH board
Fork of gr-peach-opencv-project by
drivers/UARTSerial.h@170:54ff26da7eb6, 2017-07-04 (annotated)
- Committer:
- thedo
- Date:
- Tue Jul 04 06:23:13 2017 +0000
- Revision:
- 170:54ff26da7eb6
- Parent:
- 166:3a9487d57a5c
project opencv 3.1 on GR PEACH board, no use SD card.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
thedo | 166:3a9487d57a5c | 1 | /* mbed Microcontroller Library |
thedo | 166:3a9487d57a5c | 2 | * Copyright (c) 2006-2017 ARM Limited |
thedo | 166:3a9487d57a5c | 3 | * |
thedo | 166:3a9487d57a5c | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
thedo | 166:3a9487d57a5c | 5 | * you may not use this file except in compliance with the License. |
thedo | 166:3a9487d57a5c | 6 | * You may obtain a copy of the License at |
thedo | 166:3a9487d57a5c | 7 | * |
thedo | 166:3a9487d57a5c | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
thedo | 166:3a9487d57a5c | 9 | * |
thedo | 166:3a9487d57a5c | 10 | * Unless required by applicable law or agreed to in writing, software |
thedo | 166:3a9487d57a5c | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
thedo | 166:3a9487d57a5c | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
thedo | 166:3a9487d57a5c | 13 | * See the License for the specific language governing permissions and |
thedo | 166:3a9487d57a5c | 14 | * limitations under the License. |
thedo | 166:3a9487d57a5c | 15 | */ |
thedo | 166:3a9487d57a5c | 16 | |
thedo | 166:3a9487d57a5c | 17 | #ifndef MBED_UARTSERIAL_H |
thedo | 166:3a9487d57a5c | 18 | #define MBED_UARTSERIAL_H |
thedo | 166:3a9487d57a5c | 19 | |
thedo | 166:3a9487d57a5c | 20 | #include "platform/platform.h" |
thedo | 166:3a9487d57a5c | 21 | |
thedo | 166:3a9487d57a5c | 22 | #if DEVICE_SERIAL |
thedo | 166:3a9487d57a5c | 23 | |
thedo | 166:3a9487d57a5c | 24 | #include "FileHandle.h" |
thedo | 166:3a9487d57a5c | 25 | #include "SerialBase.h" |
thedo | 166:3a9487d57a5c | 26 | #include "InterruptIn.h" |
thedo | 166:3a9487d57a5c | 27 | #include "PlatformMutex.h" |
thedo | 166:3a9487d57a5c | 28 | #include "serial_api.h" |
thedo | 166:3a9487d57a5c | 29 | #include "CircularBuffer.h" |
thedo | 166:3a9487d57a5c | 30 | |
thedo | 166:3a9487d57a5c | 31 | #ifndef MBED_CONF_DRIVERS_UART_SERIAL_RXBUF_SIZE |
thedo | 166:3a9487d57a5c | 32 | #define MBED_CONF_DRIVERS_UART_SERIAL_RXBUF_SIZE 256 |
thedo | 166:3a9487d57a5c | 33 | #endif |
thedo | 166:3a9487d57a5c | 34 | |
thedo | 166:3a9487d57a5c | 35 | #ifndef MBED_CONF_DRIVERS_UART_SERIAL_TXBUF_SIZE |
thedo | 166:3a9487d57a5c | 36 | #define MBED_CONF_DRIVERS_UART_SERIAL_TXBUF_SIZE 256 |
thedo | 166:3a9487d57a5c | 37 | #endif |
thedo | 166:3a9487d57a5c | 38 | |
thedo | 166:3a9487d57a5c | 39 | namespace mbed { |
thedo | 166:3a9487d57a5c | 40 | |
thedo | 166:3a9487d57a5c | 41 | class UARTSerial : private SerialBase, public FileHandle { |
thedo | 166:3a9487d57a5c | 42 | |
thedo | 166:3a9487d57a5c | 43 | public: |
thedo | 166:3a9487d57a5c | 44 | |
thedo | 166:3a9487d57a5c | 45 | /** Create a UARTSerial port, connected to the specified transmit and receive pins, with a particular baud rate. |
thedo | 166:3a9487d57a5c | 46 | * @param tx Transmit pin |
thedo | 166:3a9487d57a5c | 47 | * @param rx Receive pin |
thedo | 166:3a9487d57a5c | 48 | * @param baud The baud rate of the serial port (optional, defaults to MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE) |
thedo | 166:3a9487d57a5c | 49 | */ |
thedo | 166:3a9487d57a5c | 50 | UARTSerial(PinName tx, PinName rx, int baud = MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE); |
thedo | 166:3a9487d57a5c | 51 | virtual ~UARTSerial(); |
thedo | 166:3a9487d57a5c | 52 | |
thedo | 166:3a9487d57a5c | 53 | /** Equivalent to POSIX poll(). Derived from FileHandle. |
thedo | 166:3a9487d57a5c | 54 | * Provides a mechanism to multiplex input/output over a set of file handles. |
thedo | 166:3a9487d57a5c | 55 | */ |
thedo | 166:3a9487d57a5c | 56 | virtual short poll(short events) const; |
thedo | 166:3a9487d57a5c | 57 | |
thedo | 166:3a9487d57a5c | 58 | /** Write the contents of a buffer to a file |
thedo | 166:3a9487d57a5c | 59 | * |
thedo | 166:3a9487d57a5c | 60 | * @param buffer The buffer to write from |
thedo | 166:3a9487d57a5c | 61 | * @param size The number of bytes to write |
thedo | 166:3a9487d57a5c | 62 | * @return The number of bytes written, negative error on failure |
thedo | 166:3a9487d57a5c | 63 | */ |
thedo | 166:3a9487d57a5c | 64 | virtual ssize_t write(const void* buffer, size_t length); |
thedo | 166:3a9487d57a5c | 65 | |
thedo | 166:3a9487d57a5c | 66 | /** Read the contents of a file into a buffer |
thedo | 166:3a9487d57a5c | 67 | * |
thedo | 166:3a9487d57a5c | 68 | * Follows POSIX semantics: |
thedo | 166:3a9487d57a5c | 69 | * |
thedo | 166:3a9487d57a5c | 70 | * * if no data is available, and non-blocking set return -EAGAIN |
thedo | 166:3a9487d57a5c | 71 | * * if no data is available, and blocking set, wait until data is available |
thedo | 166:3a9487d57a5c | 72 | * * If any data is available, call returns immediately |
thedo | 166:3a9487d57a5c | 73 | * |
thedo | 166:3a9487d57a5c | 74 | * @param buffer The buffer to read in to |
thedo | 166:3a9487d57a5c | 75 | * @param size The number of bytes to read |
thedo | 166:3a9487d57a5c | 76 | * @return The number of bytes read, 0 at end of file, negative error on failure |
thedo | 166:3a9487d57a5c | 77 | */ |
thedo | 166:3a9487d57a5c | 78 | virtual ssize_t read(void* buffer, size_t length); |
thedo | 166:3a9487d57a5c | 79 | |
thedo | 166:3a9487d57a5c | 80 | /** Acquire mutex */ |
thedo | 166:3a9487d57a5c | 81 | virtual void lock(void); |
thedo | 166:3a9487d57a5c | 82 | |
thedo | 166:3a9487d57a5c | 83 | /** Release mutex */ |
thedo | 166:3a9487d57a5c | 84 | virtual void unlock(void); |
thedo | 166:3a9487d57a5c | 85 | |
thedo | 166:3a9487d57a5c | 86 | /** Close a file |
thedo | 166:3a9487d57a5c | 87 | * |
thedo | 166:3a9487d57a5c | 88 | * @return 0 on success, negative error code on failure |
thedo | 166:3a9487d57a5c | 89 | */ |
thedo | 166:3a9487d57a5c | 90 | virtual int close(); |
thedo | 166:3a9487d57a5c | 91 | |
thedo | 166:3a9487d57a5c | 92 | /** Check if the file in an interactive terminal device |
thedo | 166:3a9487d57a5c | 93 | * |
thedo | 166:3a9487d57a5c | 94 | * @return True if the file is a terminal |
thedo | 166:3a9487d57a5c | 95 | * @return False if the file is not a terminal |
thedo | 166:3a9487d57a5c | 96 | * @return Negative error code on failure |
thedo | 166:3a9487d57a5c | 97 | */ |
thedo | 166:3a9487d57a5c | 98 | virtual int isatty(); |
thedo | 166:3a9487d57a5c | 99 | |
thedo | 166:3a9487d57a5c | 100 | /** Move the file position to a given offset from from a given location |
thedo | 166:3a9487d57a5c | 101 | * |
thedo | 166:3a9487d57a5c | 102 | * Not valid for a device type FileHandle like UARTSerial. |
thedo | 166:3a9487d57a5c | 103 | * In case of UARTSerial, returns ESPIPE |
thedo | 166:3a9487d57a5c | 104 | * |
thedo | 166:3a9487d57a5c | 105 | * @param offset The offset from whence to move to |
thedo | 166:3a9487d57a5c | 106 | * @param whence The start of where to seek |
thedo | 166:3a9487d57a5c | 107 | * SEEK_SET to start from beginning of file, |
thedo | 166:3a9487d57a5c | 108 | * SEEK_CUR to start from current position in file, |
thedo | 166:3a9487d57a5c | 109 | * SEEK_END to start from end of file |
thedo | 166:3a9487d57a5c | 110 | * @return The new offset of the file, negative error code on failure |
thedo | 166:3a9487d57a5c | 111 | */ |
thedo | 166:3a9487d57a5c | 112 | virtual off_t seek(off_t offset, int whence); |
thedo | 166:3a9487d57a5c | 113 | |
thedo | 166:3a9487d57a5c | 114 | /** Flush any buffers associated with the file |
thedo | 166:3a9487d57a5c | 115 | * |
thedo | 166:3a9487d57a5c | 116 | * @return 0 on success, negative error code on failure |
thedo | 166:3a9487d57a5c | 117 | */ |
thedo | 166:3a9487d57a5c | 118 | virtual int sync(); |
thedo | 166:3a9487d57a5c | 119 | |
thedo | 166:3a9487d57a5c | 120 | /** Set blocking or non-blocking mode |
thedo | 166:3a9487d57a5c | 121 | * The default is blocking. |
thedo | 166:3a9487d57a5c | 122 | * |
thedo | 166:3a9487d57a5c | 123 | * @param blocking true for blocking mode, false for non-blocking mode. |
thedo | 166:3a9487d57a5c | 124 | */ |
thedo | 166:3a9487d57a5c | 125 | virtual int set_blocking(bool blocking) |
thedo | 166:3a9487d57a5c | 126 | { |
thedo | 166:3a9487d57a5c | 127 | _blocking = blocking; |
thedo | 166:3a9487d57a5c | 128 | return 0; |
thedo | 166:3a9487d57a5c | 129 | } |
thedo | 166:3a9487d57a5c | 130 | |
thedo | 166:3a9487d57a5c | 131 | /** Register a callback on state change of the file. |
thedo | 166:3a9487d57a5c | 132 | * |
thedo | 166:3a9487d57a5c | 133 | * The specified callback will be called on state changes such as when |
thedo | 166:3a9487d57a5c | 134 | * the file can be written to or read from. |
thedo | 166:3a9487d57a5c | 135 | * |
thedo | 166:3a9487d57a5c | 136 | * The callback may be called in an interrupt context and should not |
thedo | 166:3a9487d57a5c | 137 | * perform expensive operations. |
thedo | 166:3a9487d57a5c | 138 | * |
thedo | 166:3a9487d57a5c | 139 | * Note! This is not intended as an attach-like asynchronous api, but rather |
thedo | 166:3a9487d57a5c | 140 | * as a building block for constructing such functionality. |
thedo | 166:3a9487d57a5c | 141 | * |
thedo | 166:3a9487d57a5c | 142 | * The exact timing of when the registered function |
thedo | 166:3a9487d57a5c | 143 | * is called is not guaranteed and susceptible to change. It should be used |
thedo | 166:3a9487d57a5c | 144 | * as a cue to make read/write/poll calls to find the current state. |
thedo | 166:3a9487d57a5c | 145 | * |
thedo | 166:3a9487d57a5c | 146 | * @param func Function to call on state change |
thedo | 166:3a9487d57a5c | 147 | */ |
thedo | 166:3a9487d57a5c | 148 | virtual void sigio(Callback<void()> func); |
thedo | 166:3a9487d57a5c | 149 | |
thedo | 166:3a9487d57a5c | 150 | /** Setup interrupt handler for DCD line |
thedo | 166:3a9487d57a5c | 151 | * |
thedo | 166:3a9487d57a5c | 152 | * If DCD line is connected, an IRQ handler will be setup. |
thedo | 166:3a9487d57a5c | 153 | * Does nothing if DCD is NC, i.e., not connected. |
thedo | 166:3a9487d57a5c | 154 | * |
thedo | 166:3a9487d57a5c | 155 | * @param dcd_pin Pin-name for DCD |
thedo | 166:3a9487d57a5c | 156 | * @param active_high a boolean set to true if DCD polarity is active low |
thedo | 166:3a9487d57a5c | 157 | */ |
thedo | 166:3a9487d57a5c | 158 | void set_data_carrier_detect(PinName dcd_pin, bool active_high = false); |
thedo | 166:3a9487d57a5c | 159 | |
thedo | 166:3a9487d57a5c | 160 | private: |
thedo | 166:3a9487d57a5c | 161 | |
thedo | 166:3a9487d57a5c | 162 | /** Software serial buffers |
thedo | 166:3a9487d57a5c | 163 | * By default buffer size is 256 for TX and 256 for RX. Configurable through mbed_app.json |
thedo | 166:3a9487d57a5c | 164 | */ |
thedo | 166:3a9487d57a5c | 165 | CircularBuffer<char, MBED_CONF_DRIVERS_UART_SERIAL_RXBUF_SIZE> _rxbuf; |
thedo | 166:3a9487d57a5c | 166 | CircularBuffer<char, MBED_CONF_DRIVERS_UART_SERIAL_TXBUF_SIZE> _txbuf; |
thedo | 166:3a9487d57a5c | 167 | |
thedo | 166:3a9487d57a5c | 168 | PlatformMutex _mutex; |
thedo | 166:3a9487d57a5c | 169 | |
thedo | 166:3a9487d57a5c | 170 | Callback<void()> _sigio_cb; |
thedo | 166:3a9487d57a5c | 171 | |
thedo | 166:3a9487d57a5c | 172 | bool _blocking; |
thedo | 166:3a9487d57a5c | 173 | bool _tx_irq_enabled; |
thedo | 166:3a9487d57a5c | 174 | InterruptIn *_dcd_irq; |
thedo | 166:3a9487d57a5c | 175 | |
thedo | 166:3a9487d57a5c | 176 | /** Device Hanged up |
thedo | 166:3a9487d57a5c | 177 | * Determines if the device hanged up on us. |
thedo | 166:3a9487d57a5c | 178 | * |
thedo | 166:3a9487d57a5c | 179 | * @return True, if hanged up |
thedo | 166:3a9487d57a5c | 180 | */ |
thedo | 166:3a9487d57a5c | 181 | bool hup() const; |
thedo | 166:3a9487d57a5c | 182 | |
thedo | 166:3a9487d57a5c | 183 | /** ISRs for serial |
thedo | 166:3a9487d57a5c | 184 | * Routines to handle interrupts on serial pins. |
thedo | 166:3a9487d57a5c | 185 | * Copies data into Circular Buffer. |
thedo | 166:3a9487d57a5c | 186 | * Reports the state change to File handle. |
thedo | 166:3a9487d57a5c | 187 | */ |
thedo | 166:3a9487d57a5c | 188 | void tx_irq(void); |
thedo | 166:3a9487d57a5c | 189 | void rx_irq(void); |
thedo | 166:3a9487d57a5c | 190 | |
thedo | 166:3a9487d57a5c | 191 | void wake(void); |
thedo | 166:3a9487d57a5c | 192 | |
thedo | 166:3a9487d57a5c | 193 | void dcd_irq(void); |
thedo | 166:3a9487d57a5c | 194 | }; |
thedo | 166:3a9487d57a5c | 195 | } //namespace mbed |
thedo | 166:3a9487d57a5c | 196 | |
thedo | 166:3a9487d57a5c | 197 | #endif //DEVICE_SERIAL |
thedo | 166:3a9487d57a5c | 198 | #endif //MBED_UARTSERIAL_H |
thedo | 166:3a9487d57a5c | 199 |