mbed-os

Fork of mbed-os by erkin yucel

Committer:
xuaner
Date:
Thu Jul 20 14:26:57 2017 +0000
Revision:
1:3deb71413561
Parent:
0:f269e3021894
mbed_os

Who changed what in which revision?

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