SPKT

Dependencies:   F746_GUI SD_PlayerSkeleton F746_SAI_IO

Committer:
phungductung
Date:
Tue Jun 04 21:37:21 2019 +0000
Revision:
0:8ede47d38d10
SPKT

Who changed what in which revision?

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