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