t

Fork of mbed-dev by mbed official

Committer:
AnnaBridge
Date:
Wed Oct 11 12:45:49 2017 +0100
Revision:
176:af195413fb11
Parent:
171:19eb464bc2be
Child:
177:447f873cad2f
This updates the lib to the mbed lib v 153

Who changed what in which revision?

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