Initial commit

Dependencies:   FastPWM

Committer:
lypinator
Date:
Wed Sep 16 01:11:49 2020 +0000
Revision:
0:bb348c97df44
Added PWM

Who changed what in which revision?

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