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