Backup 1

Committer:
borlanic
Date:
Tue Apr 24 11:45:18 2018 +0000
Revision:
0:02dd72d1d465
BaBoRo_test2 - backup 1

Who changed what in which revision?

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