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
mbed library release version 165

Who changed what in which revision?

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