inport from local

Dependents:   Hobbyking_Cheetah_0511

Committer:
NYX
Date:
Mon Mar 16 06:35:48 2020 +0000
Revision:
0:85b3fd62ea1a
reinport to mbed;

Who changed what in which revision?

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