lol

Dependencies:   MMA8451Q

Fork of Application by Mateusz Kowalik

Committer:
Zaitsev
Date:
Tue Jan 10 20:42:26 2017 +0000
Revision:
10:41552d038a69
USB Serial bi-directional bridge

Who changed what in which revision?

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