Entrega 3er corte - sistemas embebidos

Committer:
Bethory
Date:
Wed May 30 00:01:50 2018 +0000
Revision:
0:6ad07c9019fd
Codigo de tales para todos los pasculaes

Who changed what in which revision?

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