I2C_EEPROM

Committer:
jhon309
Date:
Thu Aug 13 00:23:16 2015 +0000
Revision:
0:ac8863619623
I2C

Who changed what in which revision?

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