Daniel Vizcaya
/
04_RTOS_Embebidos
Entrega 3er corte - sistemas embebidos
mbed-os/drivers/SerialBase.h@1:fcdb45ee95b9, 2018-05-30 (annotated)
- Committer:
- Bethory
- Date:
- Wed May 30 04:46:28 2018 +0000
- Revision:
- 1:fcdb45ee95b9
- Parent:
- 0:6ad07c9019fd
Entrega Final
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Bethory | 0:6ad07c9019fd | 1 | /* mbed Microcontroller Library |
Bethory | 0:6ad07c9019fd | 2 | * Copyright (c) 2006-2013 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 | #ifndef MBED_SERIALBASE_H |
Bethory | 0:6ad07c9019fd | 17 | #define MBED_SERIALBASE_H |
Bethory | 0:6ad07c9019fd | 18 | |
Bethory | 0:6ad07c9019fd | 19 | #include "platform/platform.h" |
Bethory | 0:6ad07c9019fd | 20 | |
Bethory | 0:6ad07c9019fd | 21 | #if defined (DEVICE_SERIAL) || defined(DOXYGEN_ONLY) |
Bethory | 0:6ad07c9019fd | 22 | |
Bethory | 0:6ad07c9019fd | 23 | #include "Callback.h" |
Bethory | 0:6ad07c9019fd | 24 | #include "serial_api.h" |
Bethory | 0:6ad07c9019fd | 25 | #include "mbed_toolchain.h" |
Bethory | 0:6ad07c9019fd | 26 | #include "platform/NonCopyable.h" |
Bethory | 0:6ad07c9019fd | 27 | |
Bethory | 0:6ad07c9019fd | 28 | #if DEVICE_SERIAL_ASYNCH |
Bethory | 0:6ad07c9019fd | 29 | #include "CThunk.h" |
Bethory | 0:6ad07c9019fd | 30 | #include "dma_api.h" |
Bethory | 0:6ad07c9019fd | 31 | #endif |
Bethory | 0:6ad07c9019fd | 32 | |
Bethory | 0:6ad07c9019fd | 33 | namespace mbed { |
Bethory | 0:6ad07c9019fd | 34 | /** \addtogroup drivers */ |
Bethory | 0:6ad07c9019fd | 35 | |
Bethory | 0:6ad07c9019fd | 36 | /** A base class for serial port implementations |
Bethory | 0:6ad07c9019fd | 37 | * Can't be instantiated directly (use Serial or RawSerial) |
Bethory | 0:6ad07c9019fd | 38 | * |
Bethory | 0:6ad07c9019fd | 39 | * @note Synchronization level: Set by subclass |
Bethory | 0:6ad07c9019fd | 40 | * @ingroup drivers |
Bethory | 0:6ad07c9019fd | 41 | */ |
Bethory | 0:6ad07c9019fd | 42 | class SerialBase : private NonCopyable<SerialBase> { |
Bethory | 0:6ad07c9019fd | 43 | |
Bethory | 0:6ad07c9019fd | 44 | public: |
Bethory | 0:6ad07c9019fd | 45 | /** Set the baud rate of the serial port |
Bethory | 0:6ad07c9019fd | 46 | * |
Bethory | 0:6ad07c9019fd | 47 | * @param baudrate The baudrate of the serial port (default = 9600). |
Bethory | 0:6ad07c9019fd | 48 | */ |
Bethory | 0:6ad07c9019fd | 49 | void baud(int baudrate); |
Bethory | 0:6ad07c9019fd | 50 | |
Bethory | 0:6ad07c9019fd | 51 | enum Parity { |
Bethory | 0:6ad07c9019fd | 52 | None = 0, |
Bethory | 0:6ad07c9019fd | 53 | Odd, |
Bethory | 0:6ad07c9019fd | 54 | Even, |
Bethory | 0:6ad07c9019fd | 55 | Forced1, |
Bethory | 0:6ad07c9019fd | 56 | Forced0 |
Bethory | 0:6ad07c9019fd | 57 | }; |
Bethory | 0:6ad07c9019fd | 58 | |
Bethory | 0:6ad07c9019fd | 59 | enum IrqType { |
Bethory | 0:6ad07c9019fd | 60 | RxIrq = 0, |
Bethory | 0:6ad07c9019fd | 61 | TxIrq, |
Bethory | 0:6ad07c9019fd | 62 | |
Bethory | 0:6ad07c9019fd | 63 | IrqCnt |
Bethory | 0:6ad07c9019fd | 64 | }; |
Bethory | 0:6ad07c9019fd | 65 | |
Bethory | 0:6ad07c9019fd | 66 | enum Flow { |
Bethory | 0:6ad07c9019fd | 67 | Disabled = 0, |
Bethory | 0:6ad07c9019fd | 68 | RTS, |
Bethory | 0:6ad07c9019fd | 69 | CTS, |
Bethory | 0:6ad07c9019fd | 70 | RTSCTS |
Bethory | 0:6ad07c9019fd | 71 | }; |
Bethory | 0:6ad07c9019fd | 72 | |
Bethory | 0:6ad07c9019fd | 73 | /** Set the transmission format used by the serial port |
Bethory | 0:6ad07c9019fd | 74 | * |
Bethory | 0:6ad07c9019fd | 75 | * @param bits The number of bits in a word (5-8; default = 8) |
Bethory | 0:6ad07c9019fd | 76 | * @param parity The parity used (SerialBase::None, SerialBase::Odd, SerialBase::Even, SerialBase::Forced1, SerialBase::Forced0; default = SerialBase::None) |
Bethory | 0:6ad07c9019fd | 77 | * @param stop_bits The number of stop bits (1 or 2; default = 1) |
Bethory | 0:6ad07c9019fd | 78 | */ |
Bethory | 0:6ad07c9019fd | 79 | void format(int bits=8, Parity parity=SerialBase::None, int stop_bits=1); |
Bethory | 0:6ad07c9019fd | 80 | |
Bethory | 0:6ad07c9019fd | 81 | /** Determine if there is a character available to read |
Bethory | 0:6ad07c9019fd | 82 | * |
Bethory | 0:6ad07c9019fd | 83 | * @returns |
Bethory | 0:6ad07c9019fd | 84 | * 1 if there is a character available to read, |
Bethory | 0:6ad07c9019fd | 85 | * 0 otherwise |
Bethory | 0:6ad07c9019fd | 86 | */ |
Bethory | 0:6ad07c9019fd | 87 | int readable(); |
Bethory | 0:6ad07c9019fd | 88 | |
Bethory | 0:6ad07c9019fd | 89 | /** Determine if there is space available to write a character |
Bethory | 0:6ad07c9019fd | 90 | * |
Bethory | 0:6ad07c9019fd | 91 | * @returns |
Bethory | 0:6ad07c9019fd | 92 | * 1 if there is space to write a character, |
Bethory | 0:6ad07c9019fd | 93 | * 0 otherwise |
Bethory | 0:6ad07c9019fd | 94 | */ |
Bethory | 0:6ad07c9019fd | 95 | int writeable(); |
Bethory | 0:6ad07c9019fd | 96 | |
Bethory | 0:6ad07c9019fd | 97 | /** Attach a function to call whenever a serial interrupt is generated |
Bethory | 0:6ad07c9019fd | 98 | * |
Bethory | 0:6ad07c9019fd | 99 | * @param func A pointer to a void function, or 0 to set as none |
Bethory | 0:6ad07c9019fd | 100 | * @param type Which serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty) |
Bethory | 0:6ad07c9019fd | 101 | */ |
Bethory | 0:6ad07c9019fd | 102 | void attach(Callback<void()> func, IrqType type=RxIrq); |
Bethory | 0:6ad07c9019fd | 103 | |
Bethory | 0:6ad07c9019fd | 104 | /** Attach a member function to call whenever a serial interrupt is generated |
Bethory | 0:6ad07c9019fd | 105 | * |
Bethory | 0:6ad07c9019fd | 106 | * @param obj pointer to the object to call the member function on |
Bethory | 0:6ad07c9019fd | 107 | * @param method pointer to the member function to be called |
Bethory | 0:6ad07c9019fd | 108 | * @param type Which serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty) |
Bethory | 0:6ad07c9019fd | 109 | * @deprecated |
Bethory | 0:6ad07c9019fd | 110 | * The attach function does not support cv-qualifiers. Replaced by |
Bethory | 0:6ad07c9019fd | 111 | * attach(callback(obj, method), type). |
Bethory | 0:6ad07c9019fd | 112 | */ |
Bethory | 0:6ad07c9019fd | 113 | template<typename T> |
Bethory | 0:6ad07c9019fd | 114 | MBED_DEPRECATED_SINCE("mbed-os-5.1", |
Bethory | 0:6ad07c9019fd | 115 | "The attach function does not support cv-qualifiers. Replaced by " |
Bethory | 0:6ad07c9019fd | 116 | "attach(callback(obj, method), type).") |
Bethory | 0:6ad07c9019fd | 117 | void attach(T *obj, void (T::*method)(), IrqType type=RxIrq) { |
Bethory | 0:6ad07c9019fd | 118 | attach(callback(obj, method), type); |
Bethory | 0:6ad07c9019fd | 119 | } |
Bethory | 0:6ad07c9019fd | 120 | |
Bethory | 0:6ad07c9019fd | 121 | /** Attach a member function to call whenever a serial interrupt is generated |
Bethory | 0:6ad07c9019fd | 122 | * |
Bethory | 0:6ad07c9019fd | 123 | * @param obj pointer to the object to call the member function on |
Bethory | 0:6ad07c9019fd | 124 | * @param method pointer to the member function to be called |
Bethory | 0:6ad07c9019fd | 125 | * @param type Which serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty) |
Bethory | 0:6ad07c9019fd | 126 | * @deprecated |
Bethory | 0:6ad07c9019fd | 127 | * The attach function does not support cv-qualifiers. Replaced by |
Bethory | 0:6ad07c9019fd | 128 | * attach(callback(obj, method), type). |
Bethory | 0:6ad07c9019fd | 129 | */ |
Bethory | 0:6ad07c9019fd | 130 | template<typename T> |
Bethory | 0:6ad07c9019fd | 131 | MBED_DEPRECATED_SINCE("mbed-os-5.1", |
Bethory | 0:6ad07c9019fd | 132 | "The attach function does not support cv-qualifiers. Replaced by " |
Bethory | 0:6ad07c9019fd | 133 | "attach(callback(obj, method), type).") |
Bethory | 0:6ad07c9019fd | 134 | void attach(T *obj, void (*method)(T*), IrqType type=RxIrq) { |
Bethory | 0:6ad07c9019fd | 135 | attach(callback(obj, method), type); |
Bethory | 0:6ad07c9019fd | 136 | } |
Bethory | 0:6ad07c9019fd | 137 | |
Bethory | 0:6ad07c9019fd | 138 | /** Generate a break condition on the serial line |
Bethory | 0:6ad07c9019fd | 139 | */ |
Bethory | 0:6ad07c9019fd | 140 | void send_break(); |
Bethory | 0:6ad07c9019fd | 141 | |
Bethory | 0:6ad07c9019fd | 142 | protected: |
Bethory | 0:6ad07c9019fd | 143 | |
Bethory | 0:6ad07c9019fd | 144 | /** Acquire exclusive access to this serial port |
Bethory | 0:6ad07c9019fd | 145 | */ |
Bethory | 0:6ad07c9019fd | 146 | virtual void lock(void); |
Bethory | 0:6ad07c9019fd | 147 | |
Bethory | 0:6ad07c9019fd | 148 | /** Release exclusive access to this serial port |
Bethory | 0:6ad07c9019fd | 149 | */ |
Bethory | 0:6ad07c9019fd | 150 | virtual void unlock(void); |
Bethory | 0:6ad07c9019fd | 151 | |
Bethory | 0:6ad07c9019fd | 152 | public: |
Bethory | 0:6ad07c9019fd | 153 | |
Bethory | 0:6ad07c9019fd | 154 | #if DEVICE_SERIAL_FC |
Bethory | 0:6ad07c9019fd | 155 | /** Set the flow control type on the serial port |
Bethory | 0:6ad07c9019fd | 156 | * |
Bethory | 0:6ad07c9019fd | 157 | * @param type the flow control type (Disabled, RTS, CTS, RTSCTS) |
Bethory | 0:6ad07c9019fd | 158 | * @param flow1 the first flow control pin (RTS for RTS or RTSCTS, CTS for CTS) |
Bethory | 0:6ad07c9019fd | 159 | * @param flow2 the second flow control pin (CTS for RTSCTS) |
Bethory | 0:6ad07c9019fd | 160 | */ |
Bethory | 0:6ad07c9019fd | 161 | void set_flow_control(Flow type, PinName flow1=NC, PinName flow2=NC); |
Bethory | 0:6ad07c9019fd | 162 | #endif |
Bethory | 0:6ad07c9019fd | 163 | |
Bethory | 0:6ad07c9019fd | 164 | static void _irq_handler(uint32_t id, SerialIrq irq_type); |
Bethory | 0:6ad07c9019fd | 165 | |
Bethory | 0:6ad07c9019fd | 166 | #if DEVICE_SERIAL_ASYNCH |
Bethory | 0:6ad07c9019fd | 167 | |
Bethory | 0:6ad07c9019fd | 168 | /** Begin asynchronous write using 8bit buffer. The completition invokes registered TX event callback |
Bethory | 0:6ad07c9019fd | 169 | * |
Bethory | 0:6ad07c9019fd | 170 | * This function locks the deep sleep until any event has occurred |
Bethory | 0:6ad07c9019fd | 171 | * |
Bethory | 0:6ad07c9019fd | 172 | * @param buffer The buffer where received data will be stored |
Bethory | 0:6ad07c9019fd | 173 | * @param length The buffer length in bytes |
Bethory | 0:6ad07c9019fd | 174 | * @param callback The event callback function |
Bethory | 0:6ad07c9019fd | 175 | * @param event The logical OR of TX events |
Bethory | 0:6ad07c9019fd | 176 | */ |
Bethory | 0:6ad07c9019fd | 177 | int write(const uint8_t *buffer, int length, const event_callback_t& callback, int event = SERIAL_EVENT_TX_COMPLETE); |
Bethory | 0:6ad07c9019fd | 178 | |
Bethory | 0:6ad07c9019fd | 179 | /** Begin asynchronous write using 16bit buffer. The completition invokes registered TX event callback |
Bethory | 0:6ad07c9019fd | 180 | * |
Bethory | 0:6ad07c9019fd | 181 | * This function locks the deep sleep until any event has occurred |
Bethory | 0:6ad07c9019fd | 182 | * |
Bethory | 0:6ad07c9019fd | 183 | * @param buffer The buffer where received data will be stored |
Bethory | 0:6ad07c9019fd | 184 | * @param length The buffer length in bytes |
Bethory | 0:6ad07c9019fd | 185 | * @param callback The event callback function |
Bethory | 0:6ad07c9019fd | 186 | * @param event The logical OR of TX events |
Bethory | 0:6ad07c9019fd | 187 | */ |
Bethory | 0:6ad07c9019fd | 188 | int write(const uint16_t *buffer, int length, const event_callback_t& callback, int event = SERIAL_EVENT_TX_COMPLETE); |
Bethory | 0:6ad07c9019fd | 189 | |
Bethory | 0:6ad07c9019fd | 190 | /** Abort the on-going write transfer |
Bethory | 0:6ad07c9019fd | 191 | */ |
Bethory | 0:6ad07c9019fd | 192 | void abort_write(); |
Bethory | 0:6ad07c9019fd | 193 | |
Bethory | 0:6ad07c9019fd | 194 | /** Begin asynchronous reading using 8bit buffer. The completition invokes registred RX event callback. |
Bethory | 0:6ad07c9019fd | 195 | * |
Bethory | 0:6ad07c9019fd | 196 | * This function locks the deep sleep until any event has occurred |
Bethory | 0:6ad07c9019fd | 197 | * |
Bethory | 0:6ad07c9019fd | 198 | * @param buffer The buffer where received data will be stored |
Bethory | 0:6ad07c9019fd | 199 | * @param length The buffer length in bytes |
Bethory | 0:6ad07c9019fd | 200 | * @param callback The event callback function |
Bethory | 0:6ad07c9019fd | 201 | * @param event The logical OR of RX events |
Bethory | 0:6ad07c9019fd | 202 | * @param char_match The matching character |
Bethory | 0:6ad07c9019fd | 203 | */ |
Bethory | 0:6ad07c9019fd | 204 | int read(uint8_t *buffer, int length, const event_callback_t& callback, int event = SERIAL_EVENT_RX_COMPLETE, unsigned char char_match = SERIAL_RESERVED_CHAR_MATCH); |
Bethory | 0:6ad07c9019fd | 205 | |
Bethory | 0:6ad07c9019fd | 206 | /** Begin asynchronous reading using 16bit buffer. The completition invokes registred RX event callback. |
Bethory | 0:6ad07c9019fd | 207 | * |
Bethory | 0:6ad07c9019fd | 208 | * This function locks the deep sleep until any event has occurred |
Bethory | 0:6ad07c9019fd | 209 | * |
Bethory | 0:6ad07c9019fd | 210 | * @param buffer The buffer where received data will be stored |
Bethory | 0:6ad07c9019fd | 211 | * @param length The buffer length in bytes |
Bethory | 0:6ad07c9019fd | 212 | * @param callback The event callback function |
Bethory | 0:6ad07c9019fd | 213 | * @param event The logical OR of RX events |
Bethory | 0:6ad07c9019fd | 214 | * @param char_match The matching character |
Bethory | 0:6ad07c9019fd | 215 | */ |
Bethory | 0:6ad07c9019fd | 216 | int read(uint16_t *buffer, int length, const event_callback_t& callback, int event = SERIAL_EVENT_RX_COMPLETE, unsigned char char_match = SERIAL_RESERVED_CHAR_MATCH); |
Bethory | 0:6ad07c9019fd | 217 | |
Bethory | 0:6ad07c9019fd | 218 | /** Abort the on-going read transfer |
Bethory | 0:6ad07c9019fd | 219 | */ |
Bethory | 0:6ad07c9019fd | 220 | void abort_read(); |
Bethory | 0:6ad07c9019fd | 221 | |
Bethory | 0:6ad07c9019fd | 222 | /** Configure DMA usage suggestion for non-blocking TX transfers |
Bethory | 0:6ad07c9019fd | 223 | * |
Bethory | 0:6ad07c9019fd | 224 | * @param usage The usage DMA hint for peripheral |
Bethory | 0:6ad07c9019fd | 225 | * @return Zero if the usage was set, -1 if a transaction is on-going |
Bethory | 0:6ad07c9019fd | 226 | */ |
Bethory | 0:6ad07c9019fd | 227 | int set_dma_usage_tx(DMAUsage usage); |
Bethory | 0:6ad07c9019fd | 228 | |
Bethory | 0:6ad07c9019fd | 229 | /** Configure DMA usage suggestion for non-blocking RX transfers |
Bethory | 0:6ad07c9019fd | 230 | * |
Bethory | 0:6ad07c9019fd | 231 | * @param usage The usage DMA hint for peripheral |
Bethory | 0:6ad07c9019fd | 232 | * @return Zero if the usage was set, -1 if a transaction is on-going |
Bethory | 0:6ad07c9019fd | 233 | */ |
Bethory | 0:6ad07c9019fd | 234 | int set_dma_usage_rx(DMAUsage usage); |
Bethory | 0:6ad07c9019fd | 235 | |
Bethory | 0:6ad07c9019fd | 236 | protected: |
Bethory | 0:6ad07c9019fd | 237 | void start_read(void *buffer, int buffer_size, char buffer_width, const event_callback_t& callback, int event, unsigned char char_match); |
Bethory | 0:6ad07c9019fd | 238 | void start_write(const void *buffer, int buffer_size, char buffer_width, const event_callback_t& callback, int event); |
Bethory | 0:6ad07c9019fd | 239 | void interrupt_handler_asynch(void); |
Bethory | 0:6ad07c9019fd | 240 | #endif |
Bethory | 0:6ad07c9019fd | 241 | |
Bethory | 0:6ad07c9019fd | 242 | protected: |
Bethory | 0:6ad07c9019fd | 243 | SerialBase(PinName tx, PinName rx, int baud); |
Bethory | 0:6ad07c9019fd | 244 | virtual ~SerialBase(); |
Bethory | 0:6ad07c9019fd | 245 | |
Bethory | 0:6ad07c9019fd | 246 | int _base_getc(); |
Bethory | 0:6ad07c9019fd | 247 | int _base_putc(int c); |
Bethory | 0:6ad07c9019fd | 248 | |
Bethory | 0:6ad07c9019fd | 249 | #if DEVICE_SERIAL_ASYNCH |
Bethory | 0:6ad07c9019fd | 250 | CThunk<SerialBase> _thunk_irq; |
Bethory | 0:6ad07c9019fd | 251 | DMAUsage _tx_usage; |
Bethory | 0:6ad07c9019fd | 252 | DMAUsage _rx_usage; |
Bethory | 0:6ad07c9019fd | 253 | event_callback_t _tx_callback; |
Bethory | 0:6ad07c9019fd | 254 | event_callback_t _rx_callback; |
Bethory | 0:6ad07c9019fd | 255 | #endif |
Bethory | 0:6ad07c9019fd | 256 | |
Bethory | 0:6ad07c9019fd | 257 | serial_t _serial; |
Bethory | 0:6ad07c9019fd | 258 | Callback<void()> _irq[IrqCnt]; |
Bethory | 0:6ad07c9019fd | 259 | int _baud; |
Bethory | 0:6ad07c9019fd | 260 | |
Bethory | 0:6ad07c9019fd | 261 | }; |
Bethory | 0:6ad07c9019fd | 262 | |
Bethory | 0:6ad07c9019fd | 263 | } // namespace mbed |
Bethory | 0:6ad07c9019fd | 264 | |
Bethory | 0:6ad07c9019fd | 265 | #endif |
Bethory | 0:6ad07c9019fd | 266 | |
Bethory | 0:6ad07c9019fd | 267 | #endif |