Fork of the official mbed C/C SDK provides the software platform and libraries to build your applications for RenBED.

Dependents:   1-RenBuggyTimed RenBED_RGB RenBED_RGB_PWM RenBED_RGB

Fork of mbed by mbed official

Committer:
elijahorr
Date:
Thu Apr 14 07:28:54 2016 +0000
Revision:
121:672067c3ada4
Parent:
102:da0ca467f8b5
.

Who changed what in which revision?

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