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.
Dependencies: nRF51_Vdd TextLCD BME280
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 "platform/FileHandle.h" 00025 #include "SerialBase.h" 00026 #include "InterruptIn.h" 00027 #include "platform/PlatformMutex.h" 00028 #include "hal/serial_api.h" 00029 #include "platform/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 /** Check current blocking or non-blocking mode for file operations. 00146 * 00147 * @return true for blocking mode, false for non-blocking mode. 00148 */ 00149 virtual bool is_blocking() const 00150 { 00151 return _blocking; 00152 } 00153 00154 /** Register a callback on state change of the file. 00155 * 00156 * The specified callback will be called on state changes such as when 00157 * the file can be written to or read from. 00158 * 00159 * The callback may be called in an interrupt context and should not 00160 * perform expensive operations. 00161 * 00162 * Note! This is not intended as an attach-like asynchronous api, but rather 00163 * as a building block for constructing such functionality. 00164 * 00165 * The exact timing of when the registered function 00166 * is called is not guaranteed and susceptible to change. It should be used 00167 * as a cue to make read/write/poll calls to find the current state. 00168 * 00169 * @param func Function to call on state change 00170 */ 00171 virtual void sigio(Callback<void()> func); 00172 00173 /** Setup interrupt handler for DCD line 00174 * 00175 * If DCD line is connected, an IRQ handler will be setup. 00176 * Does nothing if DCD is NC, i.e., not connected. 00177 * 00178 * @param dcd_pin Pin-name for DCD 00179 * @param active_high a boolean set to true if DCD polarity is active low 00180 */ 00181 void set_data_carrier_detect(PinName dcd_pin, bool active_high = false); 00182 00183 /** Set the baud rate 00184 * 00185 * @param baud The baud rate 00186 */ 00187 void set_baud(int baud); 00188 00189 // Expose private SerialBase::Parity as UARTSerial::Parity 00190 using SerialBase::Parity; 00191 // In C++11, we wouldn't need to also have using directives for each value 00192 using SerialBase::None; 00193 using SerialBase::Odd; 00194 using SerialBase::Even; 00195 using SerialBase::Forced1; 00196 using SerialBase::Forced0; 00197 00198 /** Set the transmission format used by the serial port 00199 * 00200 * @param bits The number of bits in a word (5-8; default = 8) 00201 * @param parity The parity used (None, Odd, Even, Forced1, Forced0; default = None) 00202 * @param stop_bits The number of stop bits (1 or 2; default = 1) 00203 */ 00204 void set_format(int bits = 8, Parity parity = UARTSerial::None, int stop_bits = 1); 00205 00206 #if DEVICE_SERIAL_FC 00207 // For now use the base enum - but in future we may have extra options 00208 // such as XON/XOFF or manual GPIO RTSCTS. 00209 using SerialBase::Flow; 00210 // In C++11, we wouldn't need to also have using directives for each value 00211 using SerialBase::Disabled; 00212 using SerialBase::RTS; 00213 using SerialBase::CTS; 00214 using SerialBase::RTSCTS; 00215 00216 /** Set the flow control type on the serial port 00217 * 00218 * @param type the flow control type (Disabled, RTS, CTS, RTSCTS) 00219 * @param flow1 the first flow control pin (RTS for RTS or RTSCTS, CTS for CTS) 00220 * @param flow2 the second flow control pin (CTS for RTSCTS) 00221 */ 00222 void set_flow_control(Flow type, PinName flow1 = NC, PinName flow2 = NC); 00223 #endif 00224 00225 private: 00226 00227 void wait_ms(uint32_t millisec); 00228 00229 /** SerialBase lock override */ 00230 virtual void lock(void); 00231 00232 /** SerialBase unlock override */ 00233 virtual void unlock(void); 00234 00235 /** Acquire mutex */ 00236 virtual void api_lock(void); 00237 00238 /** Release mutex */ 00239 virtual void api_unlock(void); 00240 00241 /** Software serial buffers 00242 * By default buffer size is 256 for TX and 256 for RX. Configurable through mbed_app.json 00243 */ 00244 CircularBuffer<char, MBED_CONF_DRIVERS_UART_SERIAL_RXBUF_SIZE> _rxbuf; 00245 CircularBuffer<char, MBED_CONF_DRIVERS_UART_SERIAL_TXBUF_SIZE> _txbuf; 00246 00247 PlatformMutex _mutex; 00248 00249 Callback<void()> _sigio_cb; 00250 00251 bool _blocking; 00252 bool _tx_irq_enabled; 00253 bool _rx_irq_enabled; 00254 InterruptIn *_dcd_irq; 00255 00256 /** Device Hanged up 00257 * Determines if the device hanged up on us. 00258 * 00259 * @return True, if hanged up 00260 */ 00261 bool hup() const; 00262 00263 /** ISRs for serial 00264 * Routines to handle interrupts on serial pins. 00265 * Copies data into Circular Buffer. 00266 * Reports the state change to File handle. 00267 */ 00268 void tx_irq(void); 00269 void rx_irq(void); 00270 00271 void wake(void); 00272 00273 void dcd_irq(void); 00274 00275 }; 00276 } //namespace mbed 00277 00278 #endif //(DEVICE_SERIAL && DEVICE_INTERRUPTIN) || defined(DOXYGEN_ONLY) 00279 #endif //MBED_UARTSERIAL_H
Generated on Tue Jul 12 2022 15:16:03 by
