Committer:
borlanic
Date:
Thu Mar 29 07:02:09 2018 +0000
Revision:
0:380207fcb5c1
Encoder, IMU --> OK; Controller --> in bearbeitung

Who changed what in which revision?

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