RTC auf true
drivers/UARTSerial.h@0:38ceb79fef03, 2018-11-28 (annotated)
- Committer:
- kevman
- Date:
- Wed Nov 28 15:10:15 2018 +0000
- Revision:
- 0:38ceb79fef03
RTC modified
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kevman | 0:38ceb79fef03 | 1 | /* mbed Microcontroller Library |
kevman | 0:38ceb79fef03 | 2 | * Copyright (c) 2006-2017 ARM Limited |
kevman | 0:38ceb79fef03 | 3 | * |
kevman | 0:38ceb79fef03 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
kevman | 0:38ceb79fef03 | 5 | * you may not use this file except in compliance with the License. |
kevman | 0:38ceb79fef03 | 6 | * You may obtain a copy of the License at |
kevman | 0:38ceb79fef03 | 7 | * |
kevman | 0:38ceb79fef03 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
kevman | 0:38ceb79fef03 | 9 | * |
kevman | 0:38ceb79fef03 | 10 | * Unless required by applicable law or agreed to in writing, software |
kevman | 0:38ceb79fef03 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
kevman | 0:38ceb79fef03 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
kevman | 0:38ceb79fef03 | 13 | * See the License for the specific language governing permissions and |
kevman | 0:38ceb79fef03 | 14 | * limitations under the License. |
kevman | 0:38ceb79fef03 | 15 | */ |
kevman | 0:38ceb79fef03 | 16 | |
kevman | 0:38ceb79fef03 | 17 | #ifndef MBED_UARTSERIAL_H |
kevman | 0:38ceb79fef03 | 18 | #define MBED_UARTSERIAL_H |
kevman | 0:38ceb79fef03 | 19 | |
kevman | 0:38ceb79fef03 | 20 | #include "platform/platform.h" |
kevman | 0:38ceb79fef03 | 21 | |
kevman | 0:38ceb79fef03 | 22 | #if (DEVICE_SERIAL && DEVICE_INTERRUPTIN) || defined(DOXYGEN_ONLY) |
kevman | 0:38ceb79fef03 | 23 | |
kevman | 0:38ceb79fef03 | 24 | #include "platform/FileHandle.h" |
kevman | 0:38ceb79fef03 | 25 | #include "SerialBase.h" |
kevman | 0:38ceb79fef03 | 26 | #include "InterruptIn.h" |
kevman | 0:38ceb79fef03 | 27 | #include "platform/PlatformMutex.h" |
kevman | 0:38ceb79fef03 | 28 | #include "hal/serial_api.h" |
kevman | 0:38ceb79fef03 | 29 | #include "platform/CircularBuffer.h" |
kevman | 0:38ceb79fef03 | 30 | #include "platform/NonCopyable.h" |
kevman | 0:38ceb79fef03 | 31 | |
kevman | 0:38ceb79fef03 | 32 | #ifndef MBED_CONF_DRIVERS_UART_SERIAL_RXBUF_SIZE |
kevman | 0:38ceb79fef03 | 33 | #define MBED_CONF_DRIVERS_UART_SERIAL_RXBUF_SIZE 256 |
kevman | 0:38ceb79fef03 | 34 | #endif |
kevman | 0:38ceb79fef03 | 35 | |
kevman | 0:38ceb79fef03 | 36 | #ifndef MBED_CONF_DRIVERS_UART_SERIAL_TXBUF_SIZE |
kevman | 0:38ceb79fef03 | 37 | #define MBED_CONF_DRIVERS_UART_SERIAL_TXBUF_SIZE 256 |
kevman | 0:38ceb79fef03 | 38 | #endif |
kevman | 0:38ceb79fef03 | 39 | |
kevman | 0:38ceb79fef03 | 40 | namespace mbed { |
kevman | 0:38ceb79fef03 | 41 | |
kevman | 0:38ceb79fef03 | 42 | /** \addtogroup drivers */ |
kevman | 0:38ceb79fef03 | 43 | |
kevman | 0:38ceb79fef03 | 44 | /** Class providing buffered UART communication functionality using separate circular buffer for send and receive channels |
kevman | 0:38ceb79fef03 | 45 | * |
kevman | 0:38ceb79fef03 | 46 | * @ingroup drivers |
kevman | 0:38ceb79fef03 | 47 | */ |
kevman | 0:38ceb79fef03 | 48 | |
kevman | 0:38ceb79fef03 | 49 | class UARTSerial : private SerialBase, public FileHandle, private NonCopyable<UARTSerial> { |
kevman | 0:38ceb79fef03 | 50 | |
kevman | 0:38ceb79fef03 | 51 | public: |
kevman | 0:38ceb79fef03 | 52 | |
kevman | 0:38ceb79fef03 | 53 | /** Create a UARTSerial port, connected to the specified transmit and receive pins, with a particular baud rate. |
kevman | 0:38ceb79fef03 | 54 | * @param tx Transmit pin |
kevman | 0:38ceb79fef03 | 55 | * @param rx Receive pin |
kevman | 0:38ceb79fef03 | 56 | * @param baud The baud rate of the serial port (optional, defaults to MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE) |
kevman | 0:38ceb79fef03 | 57 | */ |
kevman | 0:38ceb79fef03 | 58 | UARTSerial(PinName tx, PinName rx, int baud = MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE); |
kevman | 0:38ceb79fef03 | 59 | virtual ~UARTSerial(); |
kevman | 0:38ceb79fef03 | 60 | |
kevman | 0:38ceb79fef03 | 61 | /** Equivalent to POSIX poll(). Derived from FileHandle. |
kevman | 0:38ceb79fef03 | 62 | * Provides a mechanism to multiplex input/output over a set of file handles. |
kevman | 0:38ceb79fef03 | 63 | */ |
kevman | 0:38ceb79fef03 | 64 | virtual short poll(short events) const; |
kevman | 0:38ceb79fef03 | 65 | |
kevman | 0:38ceb79fef03 | 66 | /* Resolve ambiguities versus our private SerialBase |
kevman | 0:38ceb79fef03 | 67 | * (for writable, spelling differs, but just in case) |
kevman | 0:38ceb79fef03 | 68 | */ |
kevman | 0:38ceb79fef03 | 69 | using FileHandle::readable; |
kevman | 0:38ceb79fef03 | 70 | using FileHandle::writable; |
kevman | 0:38ceb79fef03 | 71 | |
kevman | 0:38ceb79fef03 | 72 | /** Write the contents of a buffer to a file |
kevman | 0:38ceb79fef03 | 73 | * |
kevman | 0:38ceb79fef03 | 74 | * Follows POSIX semantics: |
kevman | 0:38ceb79fef03 | 75 | * |
kevman | 0:38ceb79fef03 | 76 | * * if blocking, block until all data is written |
kevman | 0:38ceb79fef03 | 77 | * * if no data can be written, and non-blocking set, return -EAGAIN |
kevman | 0:38ceb79fef03 | 78 | * * if some data can be written, and non-blocking set, write partial |
kevman | 0:38ceb79fef03 | 79 | * |
kevman | 0:38ceb79fef03 | 80 | * @param buffer The buffer to write from |
kevman | 0:38ceb79fef03 | 81 | * @param length The number of bytes to write |
kevman | 0:38ceb79fef03 | 82 | * @return The number of bytes written, negative error on failure |
kevman | 0:38ceb79fef03 | 83 | */ |
kevman | 0:38ceb79fef03 | 84 | virtual ssize_t write(const void *buffer, size_t length); |
kevman | 0:38ceb79fef03 | 85 | |
kevman | 0:38ceb79fef03 | 86 | /** Read the contents of a file into a buffer |
kevman | 0:38ceb79fef03 | 87 | * |
kevman | 0:38ceb79fef03 | 88 | * Follows POSIX semantics: |
kevman | 0:38ceb79fef03 | 89 | * |
kevman | 0:38ceb79fef03 | 90 | * * if no data is available, and non-blocking set return -EAGAIN |
kevman | 0:38ceb79fef03 | 91 | * * if no data is available, and blocking set, wait until data is available |
kevman | 0:38ceb79fef03 | 92 | * * If any data is available, call returns immediately |
kevman | 0:38ceb79fef03 | 93 | * |
kevman | 0:38ceb79fef03 | 94 | * @param buffer The buffer to read in to |
kevman | 0:38ceb79fef03 | 95 | * @param length The number of bytes to read |
kevman | 0:38ceb79fef03 | 96 | * @return The number of bytes read, 0 at end of file, negative error on failure |
kevman | 0:38ceb79fef03 | 97 | */ |
kevman | 0:38ceb79fef03 | 98 | virtual ssize_t read(void *buffer, size_t length); |
kevman | 0:38ceb79fef03 | 99 | |
kevman | 0:38ceb79fef03 | 100 | /** Close a file |
kevman | 0:38ceb79fef03 | 101 | * |
kevman | 0:38ceb79fef03 | 102 | * @return 0 on success, negative error code on failure |
kevman | 0:38ceb79fef03 | 103 | */ |
kevman | 0:38ceb79fef03 | 104 | virtual int close(); |
kevman | 0:38ceb79fef03 | 105 | |
kevman | 0:38ceb79fef03 | 106 | /** Check if the file in an interactive terminal device |
kevman | 0:38ceb79fef03 | 107 | * |
kevman | 0:38ceb79fef03 | 108 | * @return True if the file is a terminal |
kevman | 0:38ceb79fef03 | 109 | * @return False if the file is not a terminal |
kevman | 0:38ceb79fef03 | 110 | * @return Negative error code on failure |
kevman | 0:38ceb79fef03 | 111 | */ |
kevman | 0:38ceb79fef03 | 112 | virtual int isatty(); |
kevman | 0:38ceb79fef03 | 113 | |
kevman | 0:38ceb79fef03 | 114 | /** Move the file position to a given offset from from a given location |
kevman | 0:38ceb79fef03 | 115 | * |
kevman | 0:38ceb79fef03 | 116 | * Not valid for a device type FileHandle like UARTSerial. |
kevman | 0:38ceb79fef03 | 117 | * In case of UARTSerial, returns ESPIPE |
kevman | 0:38ceb79fef03 | 118 | * |
kevman | 0:38ceb79fef03 | 119 | * @param offset The offset from whence to move to |
kevman | 0:38ceb79fef03 | 120 | * @param whence The start of where to seek |
kevman | 0:38ceb79fef03 | 121 | * SEEK_SET to start from beginning of file, |
kevman | 0:38ceb79fef03 | 122 | * SEEK_CUR to start from current position in file, |
kevman | 0:38ceb79fef03 | 123 | * SEEK_END to start from end of file |
kevman | 0:38ceb79fef03 | 124 | * @return The new offset of the file, negative error code on failure |
kevman | 0:38ceb79fef03 | 125 | */ |
kevman | 0:38ceb79fef03 | 126 | virtual off_t seek(off_t offset, int whence); |
kevman | 0:38ceb79fef03 | 127 | |
kevman | 0:38ceb79fef03 | 128 | /** Flush any buffers associated with the file |
kevman | 0:38ceb79fef03 | 129 | * |
kevman | 0:38ceb79fef03 | 130 | * @return 0 on success, negative error code on failure |
kevman | 0:38ceb79fef03 | 131 | */ |
kevman | 0:38ceb79fef03 | 132 | virtual int sync(); |
kevman | 0:38ceb79fef03 | 133 | |
kevman | 0:38ceb79fef03 | 134 | /** Set blocking or non-blocking mode |
kevman | 0:38ceb79fef03 | 135 | * The default is blocking. |
kevman | 0:38ceb79fef03 | 136 | * |
kevman | 0:38ceb79fef03 | 137 | * @param blocking true for blocking mode, false for non-blocking mode. |
kevman | 0:38ceb79fef03 | 138 | */ |
kevman | 0:38ceb79fef03 | 139 | virtual int set_blocking(bool blocking) |
kevman | 0:38ceb79fef03 | 140 | { |
kevman | 0:38ceb79fef03 | 141 | _blocking = blocking; |
kevman | 0:38ceb79fef03 | 142 | return 0; |
kevman | 0:38ceb79fef03 | 143 | } |
kevman | 0:38ceb79fef03 | 144 | |
kevman | 0:38ceb79fef03 | 145 | /** Check current blocking or non-blocking mode for file operations. |
kevman | 0:38ceb79fef03 | 146 | * |
kevman | 0:38ceb79fef03 | 147 | * @return true for blocking mode, false for non-blocking mode. |
kevman | 0:38ceb79fef03 | 148 | */ |
kevman | 0:38ceb79fef03 | 149 | virtual bool is_blocking() const |
kevman | 0:38ceb79fef03 | 150 | { |
kevman | 0:38ceb79fef03 | 151 | return _blocking; |
kevman | 0:38ceb79fef03 | 152 | } |
kevman | 0:38ceb79fef03 | 153 | |
kevman | 0:38ceb79fef03 | 154 | /** Register a callback on state change of the file. |
kevman | 0:38ceb79fef03 | 155 | * |
kevman | 0:38ceb79fef03 | 156 | * The specified callback will be called on state changes such as when |
kevman | 0:38ceb79fef03 | 157 | * the file can be written to or read from. |
kevman | 0:38ceb79fef03 | 158 | * |
kevman | 0:38ceb79fef03 | 159 | * The callback may be called in an interrupt context and should not |
kevman | 0:38ceb79fef03 | 160 | * perform expensive operations. |
kevman | 0:38ceb79fef03 | 161 | * |
kevman | 0:38ceb79fef03 | 162 | * Note! This is not intended as an attach-like asynchronous api, but rather |
kevman | 0:38ceb79fef03 | 163 | * as a building block for constructing such functionality. |
kevman | 0:38ceb79fef03 | 164 | * |
kevman | 0:38ceb79fef03 | 165 | * The exact timing of when the registered function |
kevman | 0:38ceb79fef03 | 166 | * is called is not guaranteed and susceptible to change. It should be used |
kevman | 0:38ceb79fef03 | 167 | * as a cue to make read/write/poll calls to find the current state. |
kevman | 0:38ceb79fef03 | 168 | * |
kevman | 0:38ceb79fef03 | 169 | * @param func Function to call on state change |
kevman | 0:38ceb79fef03 | 170 | */ |
kevman | 0:38ceb79fef03 | 171 | virtual void sigio(Callback<void()> func); |
kevman | 0:38ceb79fef03 | 172 | |
kevman | 0:38ceb79fef03 | 173 | /** Setup interrupt handler for DCD line |
kevman | 0:38ceb79fef03 | 174 | * |
kevman | 0:38ceb79fef03 | 175 | * If DCD line is connected, an IRQ handler will be setup. |
kevman | 0:38ceb79fef03 | 176 | * Does nothing if DCD is NC, i.e., not connected. |
kevman | 0:38ceb79fef03 | 177 | * |
kevman | 0:38ceb79fef03 | 178 | * @param dcd_pin Pin-name for DCD |
kevman | 0:38ceb79fef03 | 179 | * @param active_high a boolean set to true if DCD polarity is active low |
kevman | 0:38ceb79fef03 | 180 | */ |
kevman | 0:38ceb79fef03 | 181 | void set_data_carrier_detect(PinName dcd_pin, bool active_high = false); |
kevman | 0:38ceb79fef03 | 182 | |
kevman | 0:38ceb79fef03 | 183 | /** Set the baud rate |
kevman | 0:38ceb79fef03 | 184 | * |
kevman | 0:38ceb79fef03 | 185 | * @param baud The baud rate |
kevman | 0:38ceb79fef03 | 186 | */ |
kevman | 0:38ceb79fef03 | 187 | void set_baud(int baud); |
kevman | 0:38ceb79fef03 | 188 | |
kevman | 0:38ceb79fef03 | 189 | // Expose private SerialBase::Parity as UARTSerial::Parity |
kevman | 0:38ceb79fef03 | 190 | using SerialBase::Parity; |
kevman | 0:38ceb79fef03 | 191 | // In C++11, we wouldn't need to also have using directives for each value |
kevman | 0:38ceb79fef03 | 192 | using SerialBase::None; |
kevman | 0:38ceb79fef03 | 193 | using SerialBase::Odd; |
kevman | 0:38ceb79fef03 | 194 | using SerialBase::Even; |
kevman | 0:38ceb79fef03 | 195 | using SerialBase::Forced1; |
kevman | 0:38ceb79fef03 | 196 | using SerialBase::Forced0; |
kevman | 0:38ceb79fef03 | 197 | |
kevman | 0:38ceb79fef03 | 198 | /** Set the transmission format used by the serial port |
kevman | 0:38ceb79fef03 | 199 | * |
kevman | 0:38ceb79fef03 | 200 | * @param bits The number of bits in a word (5-8; default = 8) |
kevman | 0:38ceb79fef03 | 201 | * @param parity The parity used (None, Odd, Even, Forced1, Forced0; default = None) |
kevman | 0:38ceb79fef03 | 202 | * @param stop_bits The number of stop bits (1 or 2; default = 1) |
kevman | 0:38ceb79fef03 | 203 | */ |
kevman | 0:38ceb79fef03 | 204 | void set_format(int bits = 8, Parity parity = UARTSerial::None, int stop_bits = 1); |
kevman | 0:38ceb79fef03 | 205 | |
kevman | 0:38ceb79fef03 | 206 | #if DEVICE_SERIAL_FC |
kevman | 0:38ceb79fef03 | 207 | // For now use the base enum - but in future we may have extra options |
kevman | 0:38ceb79fef03 | 208 | // such as XON/XOFF or manual GPIO RTSCTS. |
kevman | 0:38ceb79fef03 | 209 | using SerialBase::Flow; |
kevman | 0:38ceb79fef03 | 210 | // In C++11, we wouldn't need to also have using directives for each value |
kevman | 0:38ceb79fef03 | 211 | using SerialBase::Disabled; |
kevman | 0:38ceb79fef03 | 212 | using SerialBase::RTS; |
kevman | 0:38ceb79fef03 | 213 | using SerialBase::CTS; |
kevman | 0:38ceb79fef03 | 214 | using SerialBase::RTSCTS; |
kevman | 0:38ceb79fef03 | 215 | |
kevman | 0:38ceb79fef03 | 216 | /** Set the flow control type on the serial port |
kevman | 0:38ceb79fef03 | 217 | * |
kevman | 0:38ceb79fef03 | 218 | * @param type the flow control type (Disabled, RTS, CTS, RTSCTS) |
kevman | 0:38ceb79fef03 | 219 | * @param flow1 the first flow control pin (RTS for RTS or RTSCTS, CTS for CTS) |
kevman | 0:38ceb79fef03 | 220 | * @param flow2 the second flow control pin (CTS for RTSCTS) |
kevman | 0:38ceb79fef03 | 221 | */ |
kevman | 0:38ceb79fef03 | 222 | void set_flow_control(Flow type, PinName flow1 = NC, PinName flow2 = NC); |
kevman | 0:38ceb79fef03 | 223 | #endif |
kevman | 0:38ceb79fef03 | 224 | |
kevman | 0:38ceb79fef03 | 225 | private: |
kevman | 0:38ceb79fef03 | 226 | |
kevman | 0:38ceb79fef03 | 227 | void wait_ms(uint32_t millisec); |
kevman | 0:38ceb79fef03 | 228 | |
kevman | 0:38ceb79fef03 | 229 | /** SerialBase lock override */ |
kevman | 0:38ceb79fef03 | 230 | virtual void lock(void); |
kevman | 0:38ceb79fef03 | 231 | |
kevman | 0:38ceb79fef03 | 232 | /** SerialBase unlock override */ |
kevman | 0:38ceb79fef03 | 233 | virtual void unlock(void); |
kevman | 0:38ceb79fef03 | 234 | |
kevman | 0:38ceb79fef03 | 235 | /** Acquire mutex */ |
kevman | 0:38ceb79fef03 | 236 | virtual void api_lock(void); |
kevman | 0:38ceb79fef03 | 237 | |
kevman | 0:38ceb79fef03 | 238 | /** Release mutex */ |
kevman | 0:38ceb79fef03 | 239 | virtual void api_unlock(void); |
kevman | 0:38ceb79fef03 | 240 | |
kevman | 0:38ceb79fef03 | 241 | /** Software serial buffers |
kevman | 0:38ceb79fef03 | 242 | * By default buffer size is 256 for TX and 256 for RX. Configurable through mbed_app.json |
kevman | 0:38ceb79fef03 | 243 | */ |
kevman | 0:38ceb79fef03 | 244 | CircularBuffer<char, MBED_CONF_DRIVERS_UART_SERIAL_RXBUF_SIZE> _rxbuf; |
kevman | 0:38ceb79fef03 | 245 | CircularBuffer<char, MBED_CONF_DRIVERS_UART_SERIAL_TXBUF_SIZE> _txbuf; |
kevman | 0:38ceb79fef03 | 246 | |
kevman | 0:38ceb79fef03 | 247 | PlatformMutex _mutex; |
kevman | 0:38ceb79fef03 | 248 | |
kevman | 0:38ceb79fef03 | 249 | Callback<void()> _sigio_cb; |
kevman | 0:38ceb79fef03 | 250 | |
kevman | 0:38ceb79fef03 | 251 | bool _blocking; |
kevman | 0:38ceb79fef03 | 252 | bool _tx_irq_enabled; |
kevman | 0:38ceb79fef03 | 253 | bool _rx_irq_enabled; |
kevman | 0:38ceb79fef03 | 254 | InterruptIn *_dcd_irq; |
kevman | 0:38ceb79fef03 | 255 | |
kevman | 0:38ceb79fef03 | 256 | /** Device Hanged up |
kevman | 0:38ceb79fef03 | 257 | * Determines if the device hanged up on us. |
kevman | 0:38ceb79fef03 | 258 | * |
kevman | 0:38ceb79fef03 | 259 | * @return True, if hanged up |
kevman | 0:38ceb79fef03 | 260 | */ |
kevman | 0:38ceb79fef03 | 261 | bool hup() const; |
kevman | 0:38ceb79fef03 | 262 | |
kevman | 0:38ceb79fef03 | 263 | /** ISRs for serial |
kevman | 0:38ceb79fef03 | 264 | * Routines to handle interrupts on serial pins. |
kevman | 0:38ceb79fef03 | 265 | * Copies data into Circular Buffer. |
kevman | 0:38ceb79fef03 | 266 | * Reports the state change to File handle. |
kevman | 0:38ceb79fef03 | 267 | */ |
kevman | 0:38ceb79fef03 | 268 | void tx_irq(void); |
kevman | 0:38ceb79fef03 | 269 | void rx_irq(void); |
kevman | 0:38ceb79fef03 | 270 | |
kevman | 0:38ceb79fef03 | 271 | void wake(void); |
kevman | 0:38ceb79fef03 | 272 | |
kevman | 0:38ceb79fef03 | 273 | void dcd_irq(void); |
kevman | 0:38ceb79fef03 | 274 | |
kevman | 0:38ceb79fef03 | 275 | }; |
kevman | 0:38ceb79fef03 | 276 | } //namespace mbed |
kevman | 0:38ceb79fef03 | 277 | |
kevman | 0:38ceb79fef03 | 278 | #endif //(DEVICE_SERIAL && DEVICE_INTERRUPTIN) || defined(DOXYGEN_ONLY) |
kevman | 0:38ceb79fef03 | 279 | #endif //MBED_UARTSERIAL_H |