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.
Fork of mbed-dev by
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 class UARTSerial : private SerialBase, public FileHandle, private NonCopyable<UARTSerial> { 00043 00044 public: 00045 00046 /** Create a UARTSerial port, connected to the specified transmit and receive pins, with a particular baud rate. 00047 * @param tx Transmit pin 00048 * @param rx Receive pin 00049 * @param baud The baud rate of the serial port (optional, defaults to MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE) 00050 */ 00051 UARTSerial(PinName tx, PinName rx, int baud = MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE); 00052 virtual ~UARTSerial(); 00053 00054 /** Equivalent to POSIX poll(). Derived from FileHandle. 00055 * Provides a mechanism to multiplex input/output over a set of file handles. 00056 */ 00057 virtual short poll(short events) const; 00058 00059 /** Write the contents of a buffer to a file 00060 * 00061 * @param buffer The buffer to write from 00062 * @param length The number of bytes to write 00063 * @return The number of bytes written, negative error on failure 00064 */ 00065 virtual ssize_t write(const void* buffer, size_t length); 00066 00067 /** Read the contents of a file into a buffer 00068 * 00069 * Follows POSIX semantics: 00070 * 00071 * * if no data is available, and non-blocking set return -EAGAIN 00072 * * if no data is available, and blocking set, wait until data is available 00073 * * If any data is available, call returns immediately 00074 * 00075 * @param buffer The buffer to read in to 00076 * @param length The number of bytes to read 00077 * @return The number of bytes read, 0 at end of file, negative error on failure 00078 */ 00079 virtual ssize_t read(void* buffer, size_t length); 00080 00081 /** Close a file 00082 * 00083 * @return 0 on success, negative error code on failure 00084 */ 00085 virtual int close(); 00086 00087 /** Check if the file in an interactive terminal device 00088 * 00089 * @return True if the file is a terminal 00090 * @return False if the file is not a terminal 00091 * @return Negative error code on failure 00092 */ 00093 virtual int isatty(); 00094 00095 /** Move the file position to a given offset from from a given location 00096 * 00097 * Not valid for a device type FileHandle like UARTSerial. 00098 * In case of UARTSerial, returns ESPIPE 00099 * 00100 * @param offset The offset from whence to move to 00101 * @param whence The start of where to seek 00102 * SEEK_SET to start from beginning of file, 00103 * SEEK_CUR to start from current position in file, 00104 * SEEK_END to start from end of file 00105 * @return The new offset of the file, negative error code on failure 00106 */ 00107 virtual off_t seek(off_t offset, int whence); 00108 00109 /** Flush any buffers associated with the file 00110 * 00111 * @return 0 on success, negative error code on failure 00112 */ 00113 virtual int sync(); 00114 00115 /** Set blocking or non-blocking mode 00116 * The default is blocking. 00117 * 00118 * @param blocking true for blocking mode, false for non-blocking mode. 00119 */ 00120 virtual int set_blocking(bool blocking) 00121 { 00122 _blocking = blocking; 00123 return 0; 00124 } 00125 00126 /** Register a callback on state change of the file. 00127 * 00128 * The specified callback will be called on state changes such as when 00129 * the file can be written to or read from. 00130 * 00131 * The callback may be called in an interrupt context and should not 00132 * perform expensive operations. 00133 * 00134 * Note! This is not intended as an attach-like asynchronous api, but rather 00135 * as a building block for constructing such functionality. 00136 * 00137 * The exact timing of when the registered function 00138 * is called is not guaranteed and susceptible to change. It should be used 00139 * as a cue to make read/write/poll calls to find the current state. 00140 * 00141 * @param func Function to call on state change 00142 */ 00143 virtual void sigio(Callback<void()> func); 00144 00145 /** Setup interrupt handler for DCD line 00146 * 00147 * If DCD line is connected, an IRQ handler will be setup. 00148 * Does nothing if DCD is NC, i.e., not connected. 00149 * 00150 * @param dcd_pin Pin-name for DCD 00151 * @param active_high a boolean set to true if DCD polarity is active low 00152 */ 00153 void set_data_carrier_detect(PinName dcd_pin, bool active_high = false); 00154 00155 /** Set the baud rate 00156 * 00157 * @param baud The baud rate 00158 */ 00159 void set_baud(int baud); 00160 00161 private: 00162 00163 /** SerialBase lock override */ 00164 virtual void lock(void); 00165 00166 /** SerialBase unlock override */ 00167 virtual void unlock(void); 00168 00169 /** Acquire mutex */ 00170 virtual void api_lock(void); 00171 00172 /** Release mutex */ 00173 virtual void api_unlock(void); 00174 00175 /** Software serial buffers 00176 * By default buffer size is 256 for TX and 256 for RX. Configurable through mbed_app.json 00177 */ 00178 CircularBuffer<char, MBED_CONF_DRIVERS_UART_SERIAL_RXBUF_SIZE> _rxbuf; 00179 CircularBuffer<char, MBED_CONF_DRIVERS_UART_SERIAL_TXBUF_SIZE> _txbuf; 00180 00181 PlatformMutex _mutex; 00182 00183 Callback<void()> _sigio_cb; 00184 00185 bool _blocking; 00186 bool _tx_irq_enabled; 00187 InterruptIn *_dcd_irq; 00188 00189 /** Device Hanged up 00190 * Determines if the device hanged up on us. 00191 * 00192 * @return True, if hanged up 00193 */ 00194 bool hup() const; 00195 00196 /** ISRs for serial 00197 * Routines to handle interrupts on serial pins. 00198 * Copies data into Circular Buffer. 00199 * Reports the state change to File handle. 00200 */ 00201 void tx_irq(void); 00202 void rx_irq(void); 00203 00204 void wake(void); 00205 00206 void dcd_irq(void); 00207 00208 }; 00209 } //namespace mbed 00210 00211 #endif //(DEVICE_SERIAL && DEVICE_INTERRUPTIN) || defined(DOXYGEN_ONLY) 00212 #endif //MBED_UARTSERIAL_H
Generated on Tue Jul 12 2022 12:11:35 by
1.7.2
