mbed library sources. Supersedes mbed-src.
Dependents: Nucleo_Hello_Encoder BLE_iBeaconScan AM1805_DEMO DISCO-F429ZI_ExportTemplate1 ... more
UARTSerial.h
00001 /* mbed Microcontroller Library 00002 * Copyright (c) 2006-2017 ARM Limited 00003 * SPDX-License-Identifier: Apache-2.0 00004 * 00005 * Licensed under the Apache License, Version 2.0 (the "License"); 00006 * you may not use this file except in compliance with the License. 00007 * You may obtain a copy of the License at 00008 * 00009 * http://www.apache.org/licenses/LICENSE-2.0 00010 * 00011 * Unless required by applicable law or agreed to in writing, software 00012 * distributed under the License is distributed on an "AS IS" BASIS, 00013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00014 * See the License for the specific language governing permissions and 00015 * limitations under the License. 00016 */ 00017 00018 #ifndef MBED_UARTSERIAL_H 00019 #define MBED_UARTSERIAL_H 00020 00021 #include "platform/platform.h" 00022 00023 #if (DEVICE_SERIAL && DEVICE_INTERRUPTIN) || defined(DOXYGEN_ONLY) 00024 00025 #include "platform/FileHandle.h" 00026 #include "SerialBase.h" 00027 #include "InterruptIn.h" 00028 #include "platform/PlatformMutex.h" 00029 #include "hal/serial_api.h" 00030 #include "platform/CircularBuffer.h" 00031 #include "platform/NonCopyable.h" 00032 00033 #ifndef MBED_CONF_DRIVERS_UART_SERIAL_RXBUF_SIZE 00034 #define MBED_CONF_DRIVERS_UART_SERIAL_RXBUF_SIZE 256 00035 #endif 00036 00037 #ifndef MBED_CONF_DRIVERS_UART_SERIAL_TXBUF_SIZE 00038 #define MBED_CONF_DRIVERS_UART_SERIAL_TXBUF_SIZE 256 00039 #endif 00040 00041 namespace mbed { 00042 00043 /** \addtogroup drivers */ 00044 00045 /** Class providing buffered UART communication functionality using separate circular buffer for send and receive channels 00046 * 00047 * @ingroup drivers 00048 */ 00049 00050 class UARTSerial : private SerialBase, public FileHandle, private NonCopyable<UARTSerial> { 00051 00052 public: 00053 00054 /** Create a UARTSerial port, connected to the specified transmit and receive pins, with a particular baud rate. 00055 * @param tx Transmit pin 00056 * @param rx Receive pin 00057 * @param baud The baud rate of the serial port (optional, defaults to MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE) 00058 */ 00059 UARTSerial(PinName tx, PinName rx, int baud = MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE); 00060 virtual ~UARTSerial(); 00061 00062 /** Equivalent to POSIX poll(). Derived from FileHandle. 00063 * Provides a mechanism to multiplex input/output over a set of file handles. 00064 */ 00065 virtual short poll(short events) const; 00066 00067 /* Resolve ambiguities versus our private SerialBase 00068 * (for writable, spelling differs, but just in case) 00069 */ 00070 using FileHandle::readable; 00071 using FileHandle::writable; 00072 00073 /** Write the contents of a buffer to a file 00074 * 00075 * Follows POSIX semantics: 00076 * 00077 * * if blocking, block until all data is written 00078 * * if no data can be written, and non-blocking set, return -EAGAIN 00079 * * if some data can be written, and non-blocking set, write partial 00080 * 00081 * @param buffer The buffer to write from 00082 * @param length The number of bytes to write 00083 * @return The number of bytes written, negative error on failure 00084 */ 00085 virtual ssize_t write(const void *buffer, size_t length); 00086 00087 /** Read the contents of a file into a buffer 00088 * 00089 * Follows POSIX semantics: 00090 * 00091 * * if no data is available, and non-blocking set return -EAGAIN 00092 * * if no data is available, and blocking set, wait until data is available 00093 * * If any data is available, call returns immediately 00094 * 00095 * @param buffer The buffer to read in to 00096 * @param length The number of bytes to read 00097 * @return The number of bytes read, 0 at end of file, negative error on failure 00098 */ 00099 virtual ssize_t read(void *buffer, size_t length); 00100 00101 /** Close a file 00102 * 00103 * @return 0 on success, negative error code on failure 00104 */ 00105 virtual int close(); 00106 00107 /** Check if the file in an interactive terminal device 00108 * 00109 * @return True if the file is a terminal 00110 * @return False if the file is not a terminal 00111 * @return Negative error code on failure 00112 */ 00113 virtual int isatty(); 00114 00115 /** Move the file position to a given offset from from a given location 00116 * 00117 * Not valid for a device type FileHandle like UARTSerial. 00118 * In case of UARTSerial, returns ESPIPE 00119 * 00120 * @param offset The offset from whence to move to 00121 * @param whence The start of where to seek 00122 * SEEK_SET to start from beginning of file, 00123 * SEEK_CUR to start from current position in file, 00124 * SEEK_END to start from end of file 00125 * @return The new offset of the file, negative error code on failure 00126 */ 00127 virtual off_t seek(off_t offset, int whence); 00128 00129 /** Flush any buffers associated with the file 00130 * 00131 * @return 0 on success, negative error code on failure 00132 */ 00133 virtual int sync(); 00134 00135 /** Set blocking or non-blocking mode 00136 * The default is blocking. 00137 * 00138 * @param blocking true for blocking mode, false for non-blocking mode. 00139 */ 00140 virtual int set_blocking(bool blocking) 00141 { 00142 _blocking = blocking; 00143 return 0; 00144 } 00145 00146 /** Check current blocking or non-blocking mode for file operations. 00147 * 00148 * @return true for blocking mode, false for non-blocking mode. 00149 */ 00150 virtual bool is_blocking() const 00151 { 00152 return _blocking; 00153 } 00154 00155 /** Register a callback on state change of the file. 00156 * 00157 * The specified callback will be called on state changes such as when 00158 * the file can be written to or read from. 00159 * 00160 * The callback may be called in an interrupt context and should not 00161 * perform expensive operations. 00162 * 00163 * Note! This is not intended as an attach-like asynchronous api, but rather 00164 * as a building block for constructing such functionality. 00165 * 00166 * The exact timing of when the registered function 00167 * is called is not guaranteed and susceptible to change. It should be used 00168 * as a cue to make read/write/poll calls to find the current state. 00169 * 00170 * @param func Function to call on state change 00171 */ 00172 virtual void sigio(Callback<void()> func); 00173 00174 /** Setup interrupt handler for DCD line 00175 * 00176 * If DCD line is connected, an IRQ handler will be setup. 00177 * Does nothing if DCD is NC, i.e., not connected. 00178 * 00179 * @param dcd_pin Pin-name for DCD 00180 * @param active_high a boolean set to true if DCD polarity is active low 00181 */ 00182 void set_data_carrier_detect(PinName dcd_pin, bool active_high = false); 00183 00184 /** Set the baud rate 00185 * 00186 * @param baud The baud rate 00187 */ 00188 void set_baud(int baud); 00189 00190 // Expose private SerialBase::Parity as UARTSerial::Parity 00191 using SerialBase::Parity; 00192 // In C++11, we wouldn't need to also have using directives for each value 00193 using SerialBase::None; 00194 using SerialBase::Odd; 00195 using SerialBase::Even; 00196 using SerialBase::Forced1; 00197 using SerialBase::Forced0; 00198 00199 /** Set the transmission format used by the serial port 00200 * 00201 * @param bits The number of bits in a word (5-8; default = 8) 00202 * @param parity The parity used (None, Odd, Even, Forced1, Forced0; default = None) 00203 * @param stop_bits The number of stop bits (1 or 2; default = 1) 00204 */ 00205 void set_format(int bits = 8, Parity parity = UARTSerial::None, int stop_bits = 1); 00206 00207 #if DEVICE_SERIAL_FC 00208 // For now use the base enum - but in future we may have extra options 00209 // such as XON/XOFF or manual GPIO RTSCTS. 00210 using SerialBase::Flow; 00211 // In C++11, we wouldn't need to also have using directives for each value 00212 using SerialBase::Disabled; 00213 using SerialBase::RTS; 00214 using SerialBase::CTS; 00215 using SerialBase::RTSCTS; 00216 00217 /** Set the flow control type on the serial port 00218 * 00219 * @param type the flow control type (Disabled, RTS, CTS, RTSCTS) 00220 * @param flow1 the first flow control pin (RTS for RTS or RTSCTS, CTS for CTS) 00221 * @param flow2 the second flow control pin (CTS for RTSCTS) 00222 */ 00223 void set_flow_control(Flow type, PinName flow1 = NC, PinName flow2 = NC); 00224 #endif 00225 00226 private: 00227 00228 void wait_ms(uint32_t millisec); 00229 00230 /** SerialBase lock override */ 00231 virtual void lock(void); 00232 00233 /** SerialBase unlock override */ 00234 virtual void unlock(void); 00235 00236 /** Acquire mutex */ 00237 virtual void api_lock(void); 00238 00239 /** Release mutex */ 00240 virtual void api_unlock(void); 00241 00242 /** Unbuffered write - invoked when write called from critical section */ 00243 ssize_t write_unbuffered(const char *buf_ptr, size_t length); 00244 00245 /** Software serial buffers 00246 * By default buffer size is 256 for TX and 256 for RX. Configurable through mbed_app.json 00247 */ 00248 CircularBuffer<char, MBED_CONF_DRIVERS_UART_SERIAL_RXBUF_SIZE> _rxbuf; 00249 CircularBuffer<char, MBED_CONF_DRIVERS_UART_SERIAL_TXBUF_SIZE> _txbuf; 00250 00251 PlatformMutex _mutex; 00252 00253 Callback<void()> _sigio_cb; 00254 00255 bool _blocking; 00256 bool _tx_irq_enabled; 00257 bool _rx_irq_enabled; 00258 InterruptIn *_dcd_irq; 00259 00260 /** Device Hanged up 00261 * Determines if the device hanged up on us. 00262 * 00263 * @return True, if hanged up 00264 */ 00265 bool hup() const; 00266 00267 /** ISRs for serial 00268 * Routines to handle interrupts on serial pins. 00269 * Copies data into Circular Buffer. 00270 * Reports the state change to File handle. 00271 */ 00272 void tx_irq(void); 00273 void rx_irq(void); 00274 00275 void wake(void); 00276 00277 void dcd_irq(void); 00278 00279 }; 00280 } //namespace mbed 00281 00282 #endif //(DEVICE_SERIAL && DEVICE_INTERRUPTIN) || defined(DOXYGEN_ONLY) 00283 #endif //MBED_UARTSERIAL_H
Generated on Tue Jul 12 2022 20:41:16 by 1.7.2