Entrega 3er corte - sistemas embebidos

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?

UserRevisionLine numberNew contents of line
Bethory 0:6ad07c9019fd 1
Bethory 0:6ad07c9019fd 2 /** \addtogroup hal */
Bethory 0:6ad07c9019fd 3 /** @{*/
Bethory 0:6ad07c9019fd 4 /* mbed Microcontroller Library
Bethory 0:6ad07c9019fd 5 * Copyright (c) 2006-2013 ARM Limited
Bethory 0:6ad07c9019fd 6 *
Bethory 0:6ad07c9019fd 7 * Licensed under the Apache License, Version 2.0 (the "License");
Bethory 0:6ad07c9019fd 8 * you may not use this file except in compliance with the License.
Bethory 0:6ad07c9019fd 9 * You may obtain a copy of the License at
Bethory 0:6ad07c9019fd 10 *
Bethory 0:6ad07c9019fd 11 * http://www.apache.org/licenses/LICENSE-2.0
Bethory 0:6ad07c9019fd 12 *
Bethory 0:6ad07c9019fd 13 * Unless required by applicable law or agreed to in writing, software
Bethory 0:6ad07c9019fd 14 * distributed under the License is distributed on an "AS IS" BASIS,
Bethory 0:6ad07c9019fd 15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Bethory 0:6ad07c9019fd 16 * See the License for the specific language governing permissions and
Bethory 0:6ad07c9019fd 17 * limitations under the License.
Bethory 0:6ad07c9019fd 18 */
Bethory 0:6ad07c9019fd 19 #ifndef MBED_SERIAL_API_H
Bethory 0:6ad07c9019fd 20 #define MBED_SERIAL_API_H
Bethory 0:6ad07c9019fd 21
Bethory 0:6ad07c9019fd 22 #include "device.h"
Bethory 0:6ad07c9019fd 23 #include "hal/buffer.h"
Bethory 0:6ad07c9019fd 24 #include "hal/dma_api.h"
Bethory 0:6ad07c9019fd 25
Bethory 0:6ad07c9019fd 26 #if DEVICE_SERIAL
Bethory 0:6ad07c9019fd 27
Bethory 0:6ad07c9019fd 28 #define SERIAL_EVENT_TX_SHIFT (2)
Bethory 0:6ad07c9019fd 29 #define SERIAL_EVENT_RX_SHIFT (8)
Bethory 0:6ad07c9019fd 30
Bethory 0:6ad07c9019fd 31 #define SERIAL_EVENT_TX_MASK (0x00FC)
Bethory 0:6ad07c9019fd 32 #define SERIAL_EVENT_RX_MASK (0x3F00)
Bethory 0:6ad07c9019fd 33
Bethory 0:6ad07c9019fd 34 #define SERIAL_EVENT_ERROR (1 << 1)
Bethory 0:6ad07c9019fd 35
Bethory 0:6ad07c9019fd 36 /**
Bethory 0:6ad07c9019fd 37 * @defgroup SerialTXEvents Serial TX Events Macros
Bethory 0:6ad07c9019fd 38 *
Bethory 0:6ad07c9019fd 39 * @{
Bethory 0:6ad07c9019fd 40 */
Bethory 0:6ad07c9019fd 41 #define SERIAL_EVENT_TX_COMPLETE (1 << (SERIAL_EVENT_TX_SHIFT + 0))
Bethory 0:6ad07c9019fd 42 #define SERIAL_EVENT_TX_ALL (SERIAL_EVENT_TX_COMPLETE)
Bethory 0:6ad07c9019fd 43 /**@}*/
Bethory 0:6ad07c9019fd 44
Bethory 0:6ad07c9019fd 45 /**
Bethory 0:6ad07c9019fd 46 * @defgroup SerialRXEvents Serial RX Events Macros
Bethory 0:6ad07c9019fd 47 *
Bethory 0:6ad07c9019fd 48 * @{
Bethory 0:6ad07c9019fd 49 */
Bethory 0:6ad07c9019fd 50 #define SERIAL_EVENT_RX_COMPLETE (1 << (SERIAL_EVENT_RX_SHIFT + 0))
Bethory 0:6ad07c9019fd 51 #define SERIAL_EVENT_RX_OVERRUN_ERROR (1 << (SERIAL_EVENT_RX_SHIFT + 1))
Bethory 0:6ad07c9019fd 52 #define SERIAL_EVENT_RX_FRAMING_ERROR (1 << (SERIAL_EVENT_RX_SHIFT + 2))
Bethory 0:6ad07c9019fd 53 #define SERIAL_EVENT_RX_PARITY_ERROR (1 << (SERIAL_EVENT_RX_SHIFT + 3))
Bethory 0:6ad07c9019fd 54 #define SERIAL_EVENT_RX_OVERFLOW (1 << (SERIAL_EVENT_RX_SHIFT + 4))
Bethory 0:6ad07c9019fd 55 #define SERIAL_EVENT_RX_CHARACTER_MATCH (1 << (SERIAL_EVENT_RX_SHIFT + 5))
Bethory 0:6ad07c9019fd 56 #define SERIAL_EVENT_RX_ALL (SERIAL_EVENT_RX_OVERFLOW | SERIAL_EVENT_RX_PARITY_ERROR | \
Bethory 0:6ad07c9019fd 57 SERIAL_EVENT_RX_FRAMING_ERROR | SERIAL_EVENT_RX_OVERRUN_ERROR | \
Bethory 0:6ad07c9019fd 58 SERIAL_EVENT_RX_COMPLETE | SERIAL_EVENT_RX_CHARACTER_MATCH)
Bethory 0:6ad07c9019fd 59 /**@}*/
Bethory 0:6ad07c9019fd 60
Bethory 0:6ad07c9019fd 61 #define SERIAL_RESERVED_CHAR_MATCH (255)
Bethory 0:6ad07c9019fd 62
Bethory 0:6ad07c9019fd 63 typedef enum {
Bethory 0:6ad07c9019fd 64 ParityNone = 0,
Bethory 0:6ad07c9019fd 65 ParityOdd = 1,
Bethory 0:6ad07c9019fd 66 ParityEven = 2,
Bethory 0:6ad07c9019fd 67 ParityForced1 = 3,
Bethory 0:6ad07c9019fd 68 ParityForced0 = 4
Bethory 0:6ad07c9019fd 69 } SerialParity;
Bethory 0:6ad07c9019fd 70
Bethory 0:6ad07c9019fd 71 typedef enum {
Bethory 0:6ad07c9019fd 72 RxIrq,
Bethory 0:6ad07c9019fd 73 TxIrq
Bethory 0:6ad07c9019fd 74 } SerialIrq;
Bethory 0:6ad07c9019fd 75
Bethory 0:6ad07c9019fd 76 typedef enum {
Bethory 0:6ad07c9019fd 77 FlowControlNone,
Bethory 0:6ad07c9019fd 78 FlowControlRTS,
Bethory 0:6ad07c9019fd 79 FlowControlCTS,
Bethory 0:6ad07c9019fd 80 FlowControlRTSCTS
Bethory 0:6ad07c9019fd 81 } FlowControl;
Bethory 0:6ad07c9019fd 82
Bethory 0:6ad07c9019fd 83 typedef void (*uart_irq_handler)(uint32_t id, SerialIrq event);
Bethory 0:6ad07c9019fd 84
Bethory 0:6ad07c9019fd 85 #if DEVICE_SERIAL_ASYNCH
Bethory 0:6ad07c9019fd 86 /** Asynch serial HAL structure
Bethory 0:6ad07c9019fd 87 */
Bethory 0:6ad07c9019fd 88 typedef struct {
Bethory 0:6ad07c9019fd 89 struct serial_s serial; /**< Target specific serial structure */
Bethory 0:6ad07c9019fd 90 struct buffer_s tx_buff; /**< TX buffer */
Bethory 0:6ad07c9019fd 91 struct buffer_s rx_buff; /**< RX buffer */
Bethory 0:6ad07c9019fd 92 uint8_t char_match; /**< Character to be matched */
Bethory 0:6ad07c9019fd 93 uint8_t char_found; /**< State of the matched character */
Bethory 0:6ad07c9019fd 94 } serial_t;
Bethory 0:6ad07c9019fd 95
Bethory 0:6ad07c9019fd 96 #else
Bethory 0:6ad07c9019fd 97 /** Non-asynch serial HAL structure
Bethory 0:6ad07c9019fd 98 */
Bethory 0:6ad07c9019fd 99 typedef struct serial_s serial_t;
Bethory 0:6ad07c9019fd 100
Bethory 0:6ad07c9019fd 101 #endif
Bethory 0:6ad07c9019fd 102
Bethory 0:6ad07c9019fd 103 #ifdef __cplusplus
Bethory 0:6ad07c9019fd 104 extern "C" {
Bethory 0:6ad07c9019fd 105 #endif
Bethory 0:6ad07c9019fd 106
Bethory 0:6ad07c9019fd 107 /**
Bethory 0:6ad07c9019fd 108 * \defgroup hal_GeneralSerial Serial Configuration Functions
Bethory 0:6ad07c9019fd 109 * @{
Bethory 0:6ad07c9019fd 110 */
Bethory 0:6ad07c9019fd 111
Bethory 0:6ad07c9019fd 112 /** Initialize the serial peripheral. It sets the default parameters for serial
Bethory 0:6ad07c9019fd 113 * peripheral, and configures its specifieds pins.
Bethory 0:6ad07c9019fd 114 *
Bethory 0:6ad07c9019fd 115 * @param obj The serial object
Bethory 0:6ad07c9019fd 116 * @param tx The TX pin name
Bethory 0:6ad07c9019fd 117 * @param rx The RX pin name
Bethory 0:6ad07c9019fd 118 */
Bethory 0:6ad07c9019fd 119 void serial_init(serial_t *obj, PinName tx, PinName rx);
Bethory 0:6ad07c9019fd 120
Bethory 0:6ad07c9019fd 121 /** Release the serial peripheral, not currently invoked. It requires further
Bethory 0:6ad07c9019fd 122 * resource management.
Bethory 0:6ad07c9019fd 123 *
Bethory 0:6ad07c9019fd 124 * @param obj The serial object
Bethory 0:6ad07c9019fd 125 */
Bethory 0:6ad07c9019fd 126 void serial_free(serial_t *obj);
Bethory 0:6ad07c9019fd 127
Bethory 0:6ad07c9019fd 128 /** Configure the baud rate
Bethory 0:6ad07c9019fd 129 *
Bethory 0:6ad07c9019fd 130 * @param obj The serial object
Bethory 0:6ad07c9019fd 131 * @param baudrate The baud rate to be configured
Bethory 0:6ad07c9019fd 132 */
Bethory 0:6ad07c9019fd 133 void serial_baud(serial_t *obj, int baudrate);
Bethory 0:6ad07c9019fd 134
Bethory 0:6ad07c9019fd 135 /** Configure the format. Set the number of bits, parity and the number of stop bits
Bethory 0:6ad07c9019fd 136 *
Bethory 0:6ad07c9019fd 137 * @param obj The serial object
Bethory 0:6ad07c9019fd 138 * @param data_bits The number of data bits
Bethory 0:6ad07c9019fd 139 * @param parity The parity
Bethory 0:6ad07c9019fd 140 * @param stop_bits The number of stop bits
Bethory 0:6ad07c9019fd 141 */
Bethory 0:6ad07c9019fd 142 void serial_format(serial_t *obj, int data_bits, SerialParity parity, int stop_bits);
Bethory 0:6ad07c9019fd 143
Bethory 0:6ad07c9019fd 144 /** The serial interrupt handler registration
Bethory 0:6ad07c9019fd 145 *
Bethory 0:6ad07c9019fd 146 * @param obj The serial object
Bethory 0:6ad07c9019fd 147 * @param handler The interrupt handler which will be invoked when the interrupt fires
Bethory 0:6ad07c9019fd 148 * @param id The SerialBase object
Bethory 0:6ad07c9019fd 149 */
Bethory 0:6ad07c9019fd 150 void serial_irq_handler(serial_t *obj, uart_irq_handler handler, uint32_t id);
Bethory 0:6ad07c9019fd 151
Bethory 0:6ad07c9019fd 152 /** Configure serial interrupt. This function is used for word-approach
Bethory 0:6ad07c9019fd 153 *
Bethory 0:6ad07c9019fd 154 * @param obj The serial object
Bethory 0:6ad07c9019fd 155 * @param irq The serial IRQ type (RX or TX)
Bethory 0:6ad07c9019fd 156 * @param enable Set to non-zero to enable events, or zero to disable them
Bethory 0:6ad07c9019fd 157 */
Bethory 0:6ad07c9019fd 158 void serial_irq_set(serial_t *obj, SerialIrq irq, uint32_t enable);
Bethory 0:6ad07c9019fd 159
Bethory 0:6ad07c9019fd 160 /** Get character. This is a blocking call, waiting for a character
Bethory 0:6ad07c9019fd 161 *
Bethory 0:6ad07c9019fd 162 * @param obj The serial object
Bethory 0:6ad07c9019fd 163 */
Bethory 0:6ad07c9019fd 164 int serial_getc(serial_t *obj);
Bethory 0:6ad07c9019fd 165
Bethory 0:6ad07c9019fd 166 /** Send a character. This is a blocking call, waiting for a peripheral to be available
Bethory 0:6ad07c9019fd 167 * for writing
Bethory 0:6ad07c9019fd 168 *
Bethory 0:6ad07c9019fd 169 * @param obj The serial object
Bethory 0:6ad07c9019fd 170 * @param c The character to be sent
Bethory 0:6ad07c9019fd 171 */
Bethory 0:6ad07c9019fd 172 void serial_putc(serial_t *obj, int c);
Bethory 0:6ad07c9019fd 173
Bethory 0:6ad07c9019fd 174 /** Check if the serial peripheral is readable
Bethory 0:6ad07c9019fd 175 *
Bethory 0:6ad07c9019fd 176 * @param obj The serial object
Bethory 0:6ad07c9019fd 177 * @return Non-zero value if a character can be read, 0 if nothing to read
Bethory 0:6ad07c9019fd 178 */
Bethory 0:6ad07c9019fd 179 int serial_readable(serial_t *obj);
Bethory 0:6ad07c9019fd 180
Bethory 0:6ad07c9019fd 181 /** Check if the serial peripheral is writable
Bethory 0:6ad07c9019fd 182 *
Bethory 0:6ad07c9019fd 183 * @param obj The serial object
Bethory 0:6ad07c9019fd 184 * @return Non-zero value if a character can be written, 0 otherwise.
Bethory 0:6ad07c9019fd 185 */
Bethory 0:6ad07c9019fd 186 int serial_writable(serial_t *obj);
Bethory 0:6ad07c9019fd 187
Bethory 0:6ad07c9019fd 188 /** Clear the serial peripheral
Bethory 0:6ad07c9019fd 189 *
Bethory 0:6ad07c9019fd 190 * @param obj The serial object
Bethory 0:6ad07c9019fd 191 */
Bethory 0:6ad07c9019fd 192 void serial_clear(serial_t *obj);
Bethory 0:6ad07c9019fd 193
Bethory 0:6ad07c9019fd 194 /** Set the break
Bethory 0:6ad07c9019fd 195 *
Bethory 0:6ad07c9019fd 196 * @param obj The serial object
Bethory 0:6ad07c9019fd 197 */
Bethory 0:6ad07c9019fd 198 void serial_break_set(serial_t *obj);
Bethory 0:6ad07c9019fd 199
Bethory 0:6ad07c9019fd 200 /** Clear the break
Bethory 0:6ad07c9019fd 201 *
Bethory 0:6ad07c9019fd 202 * @param obj The serial object
Bethory 0:6ad07c9019fd 203 */
Bethory 0:6ad07c9019fd 204 void serial_break_clear(serial_t *obj);
Bethory 0:6ad07c9019fd 205
Bethory 0:6ad07c9019fd 206 /** Configure the TX pin for UART function.
Bethory 0:6ad07c9019fd 207 *
Bethory 0:6ad07c9019fd 208 * @param tx The pin name used for TX
Bethory 0:6ad07c9019fd 209 */
Bethory 0:6ad07c9019fd 210 void serial_pinout_tx(PinName tx);
Bethory 0:6ad07c9019fd 211
Bethory 0:6ad07c9019fd 212 /** Configure the serial for the flow control. It sets flow control in the hardware
Bethory 0:6ad07c9019fd 213 * if a serial peripheral supports it, otherwise software emulation is used.
Bethory 0:6ad07c9019fd 214 *
Bethory 0:6ad07c9019fd 215 * @param obj The serial object
Bethory 0:6ad07c9019fd 216 * @param type The type of the flow control. Look at the available FlowControl types.
Bethory 0:6ad07c9019fd 217 * @param rxflow The TX pin name
Bethory 0:6ad07c9019fd 218 * @param txflow The RX pin name
Bethory 0:6ad07c9019fd 219 */
Bethory 0:6ad07c9019fd 220 void serial_set_flow_control(serial_t *obj, FlowControl type, PinName rxflow, PinName txflow);
Bethory 0:6ad07c9019fd 221
Bethory 0:6ad07c9019fd 222 #if DEVICE_SERIAL_ASYNCH
Bethory 0:6ad07c9019fd 223
Bethory 0:6ad07c9019fd 224 /**@}*/
Bethory 0:6ad07c9019fd 225
Bethory 0:6ad07c9019fd 226 /**
Bethory 0:6ad07c9019fd 227 * \defgroup hal_AsynchSerial Asynchronous Serial Hardware Abstraction Layer
Bethory 0:6ad07c9019fd 228 * @{
Bethory 0:6ad07c9019fd 229 */
Bethory 0:6ad07c9019fd 230
Bethory 0:6ad07c9019fd 231 /** Begin asynchronous TX transfer. The used buffer is specified in the serial object,
Bethory 0:6ad07c9019fd 232 * tx_buff
Bethory 0:6ad07c9019fd 233 *
Bethory 0:6ad07c9019fd 234 * @param obj The serial object
Bethory 0:6ad07c9019fd 235 * @param tx The transmit buffer
Bethory 0:6ad07c9019fd 236 * @param tx_length The number of bytes to transmit
Bethory 0:6ad07c9019fd 237 * @param tx_width Deprecated argument
Bethory 0:6ad07c9019fd 238 * @param handler The serial handler
Bethory 0:6ad07c9019fd 239 * @param event The logical OR of events to be registered
Bethory 0:6ad07c9019fd 240 * @param hint A suggestion for how to use DMA with this transfer
Bethory 0:6ad07c9019fd 241 * @return Returns number of data transfered, otherwise returns 0
Bethory 0:6ad07c9019fd 242 */
Bethory 0:6ad07c9019fd 243 int serial_tx_asynch(serial_t *obj, const void *tx, size_t tx_length, uint8_t tx_width, uint32_t handler, uint32_t event, DMAUsage hint);
Bethory 0:6ad07c9019fd 244
Bethory 0:6ad07c9019fd 245 /** Begin asynchronous RX transfer (enable interrupt for data collecting)
Bethory 0:6ad07c9019fd 246 * The used buffer is specified in the serial object - rx_buff
Bethory 0:6ad07c9019fd 247 *
Bethory 0:6ad07c9019fd 248 * @param obj The serial object
Bethory 0:6ad07c9019fd 249 * @param rx The receive buffer
Bethory 0:6ad07c9019fd 250 * @param rx_length The number of bytes to receive
Bethory 0:6ad07c9019fd 251 * @param rx_width Deprecated argument
Bethory 0:6ad07c9019fd 252 * @param handler The serial handler
Bethory 0:6ad07c9019fd 253 * @param event The logical OR of events to be registered
Bethory 0:6ad07c9019fd 254 * @param handler The serial handler
Bethory 0:6ad07c9019fd 255 * @param char_match A character in range 0-254 to be matched
Bethory 0:6ad07c9019fd 256 * @param hint A suggestion for how to use DMA with this transfer
Bethory 0:6ad07c9019fd 257 */
Bethory 0:6ad07c9019fd 258 void serial_rx_asynch(serial_t *obj, void *rx, size_t rx_length, uint8_t rx_width, uint32_t handler, uint32_t event, uint8_t char_match, DMAUsage hint);
Bethory 0:6ad07c9019fd 259
Bethory 0:6ad07c9019fd 260 /** Attempts to determine if the serial peripheral is already in use for TX
Bethory 0:6ad07c9019fd 261 *
Bethory 0:6ad07c9019fd 262 * @param obj The serial object
Bethory 0:6ad07c9019fd 263 * @return Non-zero if the RX transaction is ongoing, 0 otherwise
Bethory 0:6ad07c9019fd 264 */
Bethory 0:6ad07c9019fd 265 uint8_t serial_tx_active(serial_t *obj);
Bethory 0:6ad07c9019fd 266
Bethory 0:6ad07c9019fd 267 /** Attempts to determine if the serial peripheral is already in use for RX
Bethory 0:6ad07c9019fd 268 *
Bethory 0:6ad07c9019fd 269 * @param obj The serial object
Bethory 0:6ad07c9019fd 270 * @return Non-zero if the RX transaction is ongoing, 0 otherwise
Bethory 0:6ad07c9019fd 271 */
Bethory 0:6ad07c9019fd 272 uint8_t serial_rx_active(serial_t *obj);
Bethory 0:6ad07c9019fd 273
Bethory 0:6ad07c9019fd 274 /** The asynchronous TX and RX handler.
Bethory 0:6ad07c9019fd 275 *
Bethory 0:6ad07c9019fd 276 * @param obj The serial object
Bethory 0:6ad07c9019fd 277 * @return Returns event flags if an RX transfer termination condition was met; otherwise returns 0
Bethory 0:6ad07c9019fd 278 */
Bethory 0:6ad07c9019fd 279 int serial_irq_handler_asynch(serial_t *obj);
Bethory 0:6ad07c9019fd 280
Bethory 0:6ad07c9019fd 281 /** Abort the ongoing TX transaction. It disables the enabled interupt for TX and
Bethory 0:6ad07c9019fd 282 * flushes the TX hardware buffer if TX FIFO is used
Bethory 0:6ad07c9019fd 283 *
Bethory 0:6ad07c9019fd 284 * @param obj The serial object
Bethory 0:6ad07c9019fd 285 */
Bethory 0:6ad07c9019fd 286 void serial_tx_abort_asynch(serial_t *obj);
Bethory 0:6ad07c9019fd 287
Bethory 0:6ad07c9019fd 288 /** Abort the ongoing RX transaction. It disables the enabled interrupt for RX and
Bethory 0:6ad07c9019fd 289 * flushes the RX hardware buffer if RX FIFO is used
Bethory 0:6ad07c9019fd 290 *
Bethory 0:6ad07c9019fd 291 * @param obj The serial object
Bethory 0:6ad07c9019fd 292 */
Bethory 0:6ad07c9019fd 293 void serial_rx_abort_asynch(serial_t *obj);
Bethory 0:6ad07c9019fd 294
Bethory 0:6ad07c9019fd 295 /**@}*/
Bethory 0:6ad07c9019fd 296
Bethory 0:6ad07c9019fd 297 #endif
Bethory 0:6ad07c9019fd 298
Bethory 0:6ad07c9019fd 299 #ifdef __cplusplus
Bethory 0:6ad07c9019fd 300 }
Bethory 0:6ad07c9019fd 301 #endif
Bethory 0:6ad07c9019fd 302
Bethory 0:6ad07c9019fd 303 #endif
Bethory 0:6ad07c9019fd 304
Bethory 0:6ad07c9019fd 305 #endif
Bethory 0:6ad07c9019fd 306
Bethory 0:6ad07c9019fd 307 /** @}*/