mbed library sources. Supersedes mbed-src.

Dependents:   Nucleo_Hello_Encoder BLE_iBeaconScan AM1805_DEMO DISCO-F429ZI_ExportTemplate1 ... more

Committer:
AnnaBridge
Date:
Wed Feb 20 22:31:08 2019 +0000
Revision:
189:f392fc9709a3
Parent:
188:bcfe06ba3d64
mbed library release version 165

Who changed what in which revision?

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