inport from local

Dependents:   Hobbyking_Cheetah_0511

Committer:
NYX
Date:
Mon Mar 16 06:35:48 2020 +0000
Revision:
0:85b3fd62ea1a
reinport to mbed;

Who changed what in which revision?

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