.

Dependents:   RTC

Committer:
jhon309
Date:
Thu Aug 13 00:20:09 2015 +0000
Revision:
0:88e313c910d0
RTC Example

Who changed what in which revision?

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