001

Committer:
ganlikun
Date:
Sun Jun 12 14:02:44 2022 +0000
Revision:
0:13413ea9a877
00

Who changed what in which revision?

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