ricreato il link mancante

Committer:
marcodesilva
Date:
Mon Jan 10 20:38:41 2022 +0000
Revision:
4:31695331ce17
compila al 10/01/2022, da testare sul tool

Who changed what in which revision?

UserRevisionLine numberNew contents of line
marcodesilva 4:31695331ce17 1
marcodesilva 4:31695331ce17 2 #ifndef MBED_UARTSERIAL_MIO_H
marcodesilva 4:31695331ce17 3 #define MBED_UARTSERIAL_MIO_H
marcodesilva 4:31695331ce17 4
marcodesilva 4:31695331ce17 5 #include "platform/platform.h"
marcodesilva 4:31695331ce17 6
marcodesilva 4:31695331ce17 7 #if (DEVICE_SERIAL && DEVICE_INTERRUPTIN) || defined(DOXYGEN_ONLY)
marcodesilva 4:31695331ce17 8
marcodesilva 4:31695331ce17 9 #include "platform/FileHandle.h"
marcodesilva 4:31695331ce17 10 #include "SerialBase.h"
marcodesilva 4:31695331ce17 11 #include "InterruptIn.h"
marcodesilva 4:31695331ce17 12 #include "platform/PlatformMutex.h"
marcodesilva 4:31695331ce17 13 #include "hal/serial_api.h"
marcodesilva 4:31695331ce17 14 #include "platform/CircularBuffer.h"
marcodesilva 4:31695331ce17 15 #include "platform/NonCopyable.h"
marcodesilva 4:31695331ce17 16
marcodesilva 4:31695331ce17 17 #include "mbed.h"
marcodesilva 4:31695331ce17 18
marcodesilva 4:31695331ce17 19 #ifndef MBED_CONF_DRIVERS_UART_SERIAL_RXBUF_SIZE
marcodesilva 4:31695331ce17 20 #define MBED_CONF_DRIVERS_UART_SERIAL_RXBUF_SIZE 256
marcodesilva 4:31695331ce17 21 #endif
marcodesilva 4:31695331ce17 22
marcodesilva 4:31695331ce17 23 #ifndef MBED_CONF_DRIVERS_UART_SERIAL_TXBUF_SIZE
marcodesilva 4:31695331ce17 24 #define MBED_CONF_DRIVERS_UART_SERIAL_TXBUF_SIZE 256
marcodesilva 4:31695331ce17 25 #endif
marcodesilva 4:31695331ce17 26
marcodesilva 4:31695331ce17 27 namespace mbed {
marcodesilva 4:31695331ce17 28
marcodesilva 4:31695331ce17 29 /** \addtogroup drivers */
marcodesilva 4:31695331ce17 30
marcodesilva 4:31695331ce17 31 /** Class providing buffered UART communication functionality using separate circular buffer for send and receive channels
marcodesilva 4:31695331ce17 32 *
marcodesilva 4:31695331ce17 33 * @ingroup drivers
marcodesilva 4:31695331ce17 34 */
marcodesilva 4:31695331ce17 35
marcodesilva 4:31695331ce17 36 class UARTSerial_mio : private SerialBase, public FileHandle, private NonCopyable<UARTSerial_mio> {
marcodesilva 4:31695331ce17 37
marcodesilva 4:31695331ce17 38 public:
marcodesilva 4:31695331ce17 39
marcodesilva 4:31695331ce17 40 /** Create a UARTSerial_mio port, connected to the specified transmit and receive pins, with a particular baud rate.
marcodesilva 4:31695331ce17 41 * @param tx Transmit pin
marcodesilva 4:31695331ce17 42 * @param rx Receive pin
marcodesilva 4:31695331ce17 43 * @param baud The baud rate of the serial port (optional, defaults to MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE)
marcodesilva 4:31695331ce17 44 */
marcodesilva 4:31695331ce17 45 UARTSerial_mio(PinName tx, PinName rx, int baud = MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE);
marcodesilva 4:31695331ce17 46 virtual ~UARTSerial_mio();
marcodesilva 4:31695331ce17 47
marcodesilva 4:31695331ce17 48 /** Equivalent to POSIX poll(). Derived from FileHandle.
marcodesilva 4:31695331ce17 49 * Provides a mechanism to multiplex input/output over a set of file handles.
marcodesilva 4:31695331ce17 50 */
marcodesilva 4:31695331ce17 51 virtual short poll(short events) const;
marcodesilva 4:31695331ce17 52
marcodesilva 4:31695331ce17 53 /* Resolve ambiguities versus our private SerialBase
marcodesilva 4:31695331ce17 54 * (for writable, spelling differs, but just in case)
marcodesilva 4:31695331ce17 55 */
marcodesilva 4:31695331ce17 56 using FileHandle::readable;
marcodesilva 4:31695331ce17 57 using FileHandle::writable;
marcodesilva 4:31695331ce17 58
marcodesilva 4:31695331ce17 59 /** Write the contents of a buffer to a file
marcodesilva 4:31695331ce17 60 *
marcodesilva 4:31695331ce17 61 * Follows POSIX semantics:
marcodesilva 4:31695331ce17 62 *
marcodesilva 4:31695331ce17 63 * * if blocking, block until all data is written
marcodesilva 4:31695331ce17 64 * * if no data can be written, and non-blocking set, return -EAGAIN
marcodesilva 4:31695331ce17 65 * * if some data can be written, and non-blocking set, write partial
marcodesilva 4:31695331ce17 66 *
marcodesilva 4:31695331ce17 67 * @param buffer The buffer to write from
marcodesilva 4:31695331ce17 68 * @param length The number of bytes to write
marcodesilva 4:31695331ce17 69 * @return The number of bytes written, negative error on failure
marcodesilva 4:31695331ce17 70 */
marcodesilva 4:31695331ce17 71 virtual ssize_t write(const void *buffer, size_t length);
marcodesilva 4:31695331ce17 72
marcodesilva 4:31695331ce17 73 /** Read the contents of a file into a buffer
marcodesilva 4:31695331ce17 74 *
marcodesilva 4:31695331ce17 75 * Follows POSIX semantics:
marcodesilva 4:31695331ce17 76 *
marcodesilva 4:31695331ce17 77 * * if no data is available, and non-blocking set return -EAGAIN
marcodesilva 4:31695331ce17 78 * * if no data is available, and blocking set, wait until data is available
marcodesilva 4:31695331ce17 79 * * If any data is available, call returns immediately
marcodesilva 4:31695331ce17 80 *
marcodesilva 4:31695331ce17 81 * @param buffer The buffer to read in to
marcodesilva 4:31695331ce17 82 * @param length The number of bytes to read
marcodesilva 4:31695331ce17 83 * @return The number of bytes read, 0 at end of file, negative error on failure
marcodesilva 4:31695331ce17 84 */
marcodesilva 4:31695331ce17 85 virtual ssize_t read(void *buffer, size_t length);
marcodesilva 4:31695331ce17 86 virtual ssize_t read_timeout(void *buffer, size_t length, double _timeOut);
marcodesilva 4:31695331ce17 87
marcodesilva 4:31695331ce17 88 /** Close a file
marcodesilva 4:31695331ce17 89 *
marcodesilva 4:31695331ce17 90 * @return 0 on success, negative error code on failure
marcodesilva 4:31695331ce17 91 */
marcodesilva 4:31695331ce17 92 virtual int close();
marcodesilva 4:31695331ce17 93
marcodesilva 4:31695331ce17 94 /** Check if the file in an interactive terminal device
marcodesilva 4:31695331ce17 95 *
marcodesilva 4:31695331ce17 96 * @return True if the file is a terminal
marcodesilva 4:31695331ce17 97 * @return False if the file is not a terminal
marcodesilva 4:31695331ce17 98 * @return Negative error code on failure
marcodesilva 4:31695331ce17 99 */
marcodesilva 4:31695331ce17 100 virtual int isatty();
marcodesilva 4:31695331ce17 101
marcodesilva 4:31695331ce17 102 /** Move the file position to a given offset from from a given location
marcodesilva 4:31695331ce17 103 *
marcodesilva 4:31695331ce17 104 * Not valid for a device type FileHandle like UARTSerial_mio.
marcodesilva 4:31695331ce17 105 * In case of UARTSerial_mio, returns ESPIPE
marcodesilva 4:31695331ce17 106 *
marcodesilva 4:31695331ce17 107 * @param offset The offset from whence to move to
marcodesilva 4:31695331ce17 108 * @param whence The start of where to seek
marcodesilva 4:31695331ce17 109 * SEEK_SET to start from beginning of file,
marcodesilva 4:31695331ce17 110 * SEEK_CUR to start from current position in file,
marcodesilva 4:31695331ce17 111 * SEEK_END to start from end of file
marcodesilva 4:31695331ce17 112 * @return The new offset of the file, negative error code on failure
marcodesilva 4:31695331ce17 113 */
marcodesilva 4:31695331ce17 114 virtual off_t seek(off_t offset, int whence);
marcodesilva 4:31695331ce17 115
marcodesilva 4:31695331ce17 116 /** Flush any buffers associated with the file
marcodesilva 4:31695331ce17 117 *
marcodesilva 4:31695331ce17 118 * @return 0 on success, negative error code on failure
marcodesilva 4:31695331ce17 119 */
marcodesilva 4:31695331ce17 120 virtual int sync();
marcodesilva 4:31695331ce17 121 virtual int flush();
marcodesilva 4:31695331ce17 122 /** Set blocking or non-blocking mode
marcodesilva 4:31695331ce17 123 * The default is blocking.
marcodesilva 4:31695331ce17 124 *
marcodesilva 4:31695331ce17 125 * @param blocking true for blocking mode, false for non-blocking mode.
marcodesilva 4:31695331ce17 126 */
marcodesilva 4:31695331ce17 127 virtual int set_blocking(bool blocking)
marcodesilva 4:31695331ce17 128 {
marcodesilva 4:31695331ce17 129 _blocking = blocking;
marcodesilva 4:31695331ce17 130 return 0;
marcodesilva 4:31695331ce17 131 }
marcodesilva 4:31695331ce17 132
marcodesilva 4:31695331ce17 133 /** Check current blocking or non-blocking mode for file operations.
marcodesilva 4:31695331ce17 134 *
marcodesilva 4:31695331ce17 135 * @return true for blocking mode, false for non-blocking mode.
marcodesilva 4:31695331ce17 136 */
marcodesilva 4:31695331ce17 137 virtual bool is_blocking() const
marcodesilva 4:31695331ce17 138 {
marcodesilva 4:31695331ce17 139 return _blocking;
marcodesilva 4:31695331ce17 140 }
marcodesilva 4:31695331ce17 141
marcodesilva 4:31695331ce17 142 /** Enable or disable input
marcodesilva 4:31695331ce17 143 *
marcodesilva 4:31695331ce17 144 * Control enabling of device for input. This is primarily intended
marcodesilva 4:31695331ce17 145 * for temporary power-saving; the overall ability of the device to operate for
marcodesilva 4:31695331ce17 146 * input and/or output may be fixed at creation time, but this call can
marcodesilva 4:31695331ce17 147 * allow input to be temporarily disabled to permit power saving without
marcodesilva 4:31695331ce17 148 * losing device state.
marcodesilva 4:31695331ce17 149 *
marcodesilva 4:31695331ce17 150 * @param enabled true to enable input, false to disable.
marcodesilva 4:31695331ce17 151 *
marcodesilva 4:31695331ce17 152 * @return 0 on success
marcodesilva 4:31695331ce17 153 * @return Negative error code on failure
marcodesilva 4:31695331ce17 154 */
marcodesilva 4:31695331ce17 155 virtual int enable_input(bool enabled);
marcodesilva 4:31695331ce17 156
marcodesilva 4:31695331ce17 157 /** Enable or disable output
marcodesilva 4:31695331ce17 158 *
marcodesilva 4:31695331ce17 159 * Control enabling of device for output. This is primarily intended
marcodesilva 4:31695331ce17 160 * for temporary power-saving; the overall ability of the device to operate for
marcodesilva 4:31695331ce17 161 * input and/or output may be fixed at creation time, but this call can
marcodesilva 4:31695331ce17 162 * allow output to be temporarily disabled to permit power saving without
marcodesilva 4:31695331ce17 163 * losing device state.
marcodesilva 4:31695331ce17 164 *
marcodesilva 4:31695331ce17 165 * @param enabled true to enable output, false to disable.
marcodesilva 4:31695331ce17 166 *
marcodesilva 4:31695331ce17 167 * @return 0 on success
marcodesilva 4:31695331ce17 168 * @return Negative error code on failure
marcodesilva 4:31695331ce17 169 */
marcodesilva 4:31695331ce17 170 virtual int enable_output(bool enabled);
marcodesilva 4:31695331ce17 171
marcodesilva 4:31695331ce17 172 /** Register a callback on state change of the file.
marcodesilva 4:31695331ce17 173 *
marcodesilva 4:31695331ce17 174 * The specified callback will be called on state changes such as when
marcodesilva 4:31695331ce17 175 * the file can be written to or read from.
marcodesilva 4:31695331ce17 176 *
marcodesilva 4:31695331ce17 177 * The callback may be called in an interrupt context and should not
marcodesilva 4:31695331ce17 178 * perform expensive operations.
marcodesilva 4:31695331ce17 179 *
marcodesilva 4:31695331ce17 180 * Note! This is not intended as an attach-like asynchronous api, but rather
marcodesilva 4:31695331ce17 181 * as a building block for constructing such functionality.
marcodesilva 4:31695331ce17 182 *
marcodesilva 4:31695331ce17 183 * The exact timing of when the registered function
marcodesilva 4:31695331ce17 184 * is called is not guaranteed and susceptible to change. It should be used
marcodesilva 4:31695331ce17 185 * as a cue to make read/write/poll calls to find the current state.
marcodesilva 4:31695331ce17 186 *
marcodesilva 4:31695331ce17 187 * @param func Function to call on state change
marcodesilva 4:31695331ce17 188 */
marcodesilva 4:31695331ce17 189 virtual void sigio(Callback<void()> func);
marcodesilva 4:31695331ce17 190
marcodesilva 4:31695331ce17 191 /** Setup interrupt handler for DCD line
marcodesilva 4:31695331ce17 192 *
marcodesilva 4:31695331ce17 193 * If DCD line is connected, an IRQ handler will be setup.
marcodesilva 4:31695331ce17 194 * Does nothing if DCD is NC, i.e., not connected.
marcodesilva 4:31695331ce17 195 *
marcodesilva 4:31695331ce17 196 * @param dcd_pin Pin-name for DCD
marcodesilva 4:31695331ce17 197 * @param active_high a boolean set to true if DCD polarity is active low
marcodesilva 4:31695331ce17 198 */
marcodesilva 4:31695331ce17 199 void set_data_carrier_detect(PinName dcd_pin, bool active_high = false);
marcodesilva 4:31695331ce17 200
marcodesilva 4:31695331ce17 201 /** Set the baud rate
marcodesilva 4:31695331ce17 202 *
marcodesilva 4:31695331ce17 203 * @param baud The baud rate
marcodesilva 4:31695331ce17 204 */
marcodesilva 4:31695331ce17 205 void set_baud(int baud);
marcodesilva 4:31695331ce17 206
marcodesilva 4:31695331ce17 207 // Expose private SerialBase::Parity as UARTSerial_mio::Parity
marcodesilva 4:31695331ce17 208 using SerialBase::Parity;
marcodesilva 4:31695331ce17 209 // In C++11, we wouldn't need to also have using directives for each value
marcodesilva 4:31695331ce17 210 using SerialBase::None;
marcodesilva 4:31695331ce17 211 using SerialBase::Odd;
marcodesilva 4:31695331ce17 212 using SerialBase::Even;
marcodesilva 4:31695331ce17 213 using SerialBase::Forced1;
marcodesilva 4:31695331ce17 214 using SerialBase::Forced0;
marcodesilva 4:31695331ce17 215
marcodesilva 4:31695331ce17 216 /** Set the transmission format used by the serial port
marcodesilva 4:31695331ce17 217 *
marcodesilva 4:31695331ce17 218 * @param bits The number of bits in a word (5-8; default = 8)
marcodesilva 4:31695331ce17 219 * @param parity The parity used (None, Odd, Even, Forced1, Forced0; default = None)
marcodesilva 4:31695331ce17 220 * @param stop_bits The number of stop bits (1 or 2; default = 1)
marcodesilva 4:31695331ce17 221 */
marcodesilva 4:31695331ce17 222 void set_format(int bits = 8, Parity parity = UARTSerial_mio::None, int stop_bits = 1);
marcodesilva 4:31695331ce17 223
marcodesilva 4:31695331ce17 224 #if DEVICE_SERIAL_FC
marcodesilva 4:31695331ce17 225 // For now use the base enum - but in future we may have extra options
marcodesilva 4:31695331ce17 226 // such as XON/XOFF or manual GPIO RTSCTS.
marcodesilva 4:31695331ce17 227 using SerialBase::Flow;
marcodesilva 4:31695331ce17 228 // In C++11, we wouldn't need to also have using directives for each value
marcodesilva 4:31695331ce17 229 using SerialBase::Disabled;
marcodesilva 4:31695331ce17 230 using SerialBase::RTS;
marcodesilva 4:31695331ce17 231 using SerialBase::CTS;
marcodesilva 4:31695331ce17 232 using SerialBase::RTSCTS;
marcodesilva 4:31695331ce17 233
marcodesilva 4:31695331ce17 234 /** Set the flow control type on the serial port
marcodesilva 4:31695331ce17 235 *
marcodesilva 4:31695331ce17 236 * @param type the flow control type (Disabled, RTS, CTS, RTSCTS)
marcodesilva 4:31695331ce17 237 * @param flow1 the first flow control pin (RTS for RTS or RTSCTS, CTS for CTS)
marcodesilva 4:31695331ce17 238 * @param flow2 the second flow control pin (CTS for RTSCTS)
marcodesilva 4:31695331ce17 239 */
marcodesilva 4:31695331ce17 240 void set_flow_control(Flow type, PinName flow1 = NC, PinName flow2 = NC);
marcodesilva 4:31695331ce17 241 #endif
marcodesilva 4:31695331ce17 242
marcodesilva 4:31695331ce17 243 private:
marcodesilva 4:31695331ce17 244
marcodesilva 4:31695331ce17 245 void wait_ms(uint32_t millisec);
marcodesilva 4:31695331ce17 246 void wait_us(uint32_t microsec);
marcodesilva 4:31695331ce17 247
marcodesilva 4:31695331ce17 248 /** SerialBase lock override */
marcodesilva 4:31695331ce17 249 virtual void lock(void);
marcodesilva 4:31695331ce17 250
marcodesilva 4:31695331ce17 251 /** SerialBase unlock override */
marcodesilva 4:31695331ce17 252 virtual void unlock(void);
marcodesilva 4:31695331ce17 253
marcodesilva 4:31695331ce17 254 /** Acquire mutex */
marcodesilva 4:31695331ce17 255 virtual void api_lock(void);
marcodesilva 4:31695331ce17 256
marcodesilva 4:31695331ce17 257 /** Release mutex */
marcodesilva 4:31695331ce17 258 virtual void api_unlock(void);
marcodesilva 4:31695331ce17 259
marcodesilva 4:31695331ce17 260 /** Unbuffered write - invoked when write called from critical section */
marcodesilva 4:31695331ce17 261 ssize_t write_unbuffered(const char *buf_ptr, size_t length);
marcodesilva 4:31695331ce17 262
marcodesilva 4:31695331ce17 263 void enable_rx_irq();
marcodesilva 4:31695331ce17 264 void disable_rx_irq();
marcodesilva 4:31695331ce17 265 void enable_tx_irq();
marcodesilva 4:31695331ce17 266 void disable_tx_irq();
marcodesilva 4:31695331ce17 267
marcodesilva 4:31695331ce17 268 /** Software serial buffers
marcodesilva 4:31695331ce17 269 * By default buffer size is 256 for TX and 256 for RX. Configurable through mbed_app.json
marcodesilva 4:31695331ce17 270 */
marcodesilva 4:31695331ce17 271 CircularBuffer<char, MBED_CONF_DRIVERS_UART_SERIAL_RXBUF_SIZE> _rxbuf;
marcodesilva 4:31695331ce17 272 CircularBuffer<char, MBED_CONF_DRIVERS_UART_SERIAL_TXBUF_SIZE> _txbuf;
marcodesilva 4:31695331ce17 273
marcodesilva 4:31695331ce17 274 PlatformMutex _mutex;
marcodesilva 4:31695331ce17 275
marcodesilva 4:31695331ce17 276 Callback<void()> _sigio_cb;
marcodesilva 4:31695331ce17 277
marcodesilva 4:31695331ce17 278 bool _blocking;
marcodesilva 4:31695331ce17 279 bool _tx_irq_enabled;
marcodesilva 4:31695331ce17 280 bool _rx_irq_enabled;
marcodesilva 4:31695331ce17 281 bool _tx_enabled;
marcodesilva 4:31695331ce17 282 bool _rx_enabled;
marcodesilva 4:31695331ce17 283 InterruptIn *_dcd_irq;
marcodesilva 4:31695331ce17 284
marcodesilva 4:31695331ce17 285 /** Device Hanged up
marcodesilva 4:31695331ce17 286 * Determines if the device hanged up on us.
marcodesilva 4:31695331ce17 287 *
marcodesilva 4:31695331ce17 288 * @return True, if hanged up
marcodesilva 4:31695331ce17 289 */
marcodesilva 4:31695331ce17 290 bool hup() const;
marcodesilva 4:31695331ce17 291
marcodesilva 4:31695331ce17 292 /** ISRs for serial
marcodesilva 4:31695331ce17 293 * Routines to handle interrupts on serial pins.
marcodesilva 4:31695331ce17 294 * Copies data into Circular Buffer.
marcodesilva 4:31695331ce17 295 * Reports the state change to File handle.
marcodesilva 4:31695331ce17 296 */
marcodesilva 4:31695331ce17 297 void tx_irq(void);
marcodesilva 4:31695331ce17 298 void rx_irq(void);
marcodesilva 4:31695331ce17 299
marcodesilva 4:31695331ce17 300 void wake(void);
marcodesilva 4:31695331ce17 301
marcodesilva 4:31695331ce17 302 void dcd_irq(void);
marcodesilva 4:31695331ce17 303
marcodesilva 4:31695331ce17 304 };
marcodesilva 4:31695331ce17 305 } //namespace mbed
marcodesilva 4:31695331ce17 306
marcodesilva 4:31695331ce17 307 #endif //(DEVICE_SERIAL && DEVICE_INTERRUPTIN) || defined(DOXYGEN_ONLY)
marcodesilva 4:31695331ce17 308 #endif //MBED_UARTSERIAL_H