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