mbed library sources. Supersedes mbed-src.

Dependents:   Nucleo_Hello_Encoder BLE_iBeaconScan AM1805_DEMO DISCO-F429ZI_ExportTemplate1 ... more

Committer:
AnnaBridge
Date:
Wed Jun 21 17:46:44 2017 +0100
Revision:
167:e84263d55307
Child:
168:9672193075cf
This updates the lib to the mbed lib v 145

Who changed what in which revision?

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