The official Mbed 2 C/C++ SDK provides the software platform and libraries to build your applications.

Dependents:   hello SerialTestv11 SerialTestv12 Sierpinski ... more

mbed 2

This is the mbed 2 library. If you'd like to learn about Mbed OS please see the mbed-os docs.

Committer:
AnnaBridge
Date:
Wed Feb 20 20:53:29 2019 +0000
Revision:
172:65be27845400
Parent:
128:9bcdf88f62b0
mbed library release version 165

Who changed what in which revision?

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