mbed library for NZ32-SC151

Committer:
modtronix
Date:
Fri Jul 24 21:01:44 2015 +1000
Revision:
1:71204b8406f2
Current mbed v103 (594)

Who changed what in which revision?

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