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