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-2015 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_I2C_API_H
jhon309 0:ac8863619623 17 #define MBED_I2C_API_H
jhon309 0:ac8863619623 18
jhon309 0:ac8863619623 19 #include "device.h"
jhon309 0:ac8863619623 20 #include "buffer.h"
jhon309 0:ac8863619623 21
jhon309 0:ac8863619623 22 #if DEVICE_I2C
jhon309 0:ac8863619623 23
jhon309 0:ac8863619623 24 /**
jhon309 0:ac8863619623 25 * @defgroup I2CEvents I2C Events Macros
jhon309 0:ac8863619623 26 *
jhon309 0:ac8863619623 27 * @{
jhon309 0:ac8863619623 28 */
jhon309 0:ac8863619623 29 #define I2C_EVENT_ERROR (1 << 1)
jhon309 0:ac8863619623 30 #define I2C_EVENT_ERROR_NO_SLAVE (1 << 2)
jhon309 0:ac8863619623 31 #define I2C_EVENT_TRANSFER_COMPLETE (1 << 3)
jhon309 0:ac8863619623 32 #define I2C_EVENT_TRANSFER_EARLY_NACK (1 << 4)
jhon309 0:ac8863619623 33 #define I2C_EVENT_ALL (I2C_EVENT_ERROR | I2C_EVENT_TRANSFER_COMPLETE | I2C_EVENT_ERROR_NO_SLAVE | I2C_EVENT_TRANSFER_EARLY_NACK)
jhon309 0:ac8863619623 34
jhon309 0:ac8863619623 35 /**@}*/
jhon309 0:ac8863619623 36
jhon309 0:ac8863619623 37 #if DEVICE_I2C_ASYNCH
jhon309 0:ac8863619623 38 /** Asynch i2c hal structure
jhon309 0:ac8863619623 39 */
jhon309 0:ac8863619623 40 typedef struct {
jhon309 0:ac8863619623 41 struct i2c_s i2c; /**< Target specific i2c structure */
jhon309 0:ac8863619623 42 struct buffer_s tx_buff; /**< Tx buffer */
jhon309 0:ac8863619623 43 struct buffer_s rx_buff; /**< Rx buffer */
jhon309 0:ac8863619623 44 } i2c_t;
jhon309 0:ac8863619623 45
jhon309 0:ac8863619623 46 #else
jhon309 0:ac8863619623 47 /** Non-asynch i2c hal structure
jhon309 0:ac8863619623 48 */
jhon309 0:ac8863619623 49 typedef struct i2c_s i2c_t;
jhon309 0:ac8863619623 50
jhon309 0:ac8863619623 51 #endif
jhon309 0:ac8863619623 52
jhon309 0:ac8863619623 53 enum {
jhon309 0:ac8863619623 54 I2C_ERROR_NO_SLAVE = -1,
jhon309 0:ac8863619623 55 I2C_ERROR_BUS_BUSY = -2
jhon309 0:ac8863619623 56 };
jhon309 0:ac8863619623 57
jhon309 0:ac8863619623 58 #ifdef __cplusplus
jhon309 0:ac8863619623 59 extern "C" {
jhon309 0:ac8863619623 60 #endif
jhon309 0:ac8863619623 61
jhon309 0:ac8863619623 62 /**
jhon309 0:ac8863619623 63 * \defgroup GeneralI2C I2C Configuration Functions
jhon309 0:ac8863619623 64 * @{
jhon309 0:ac8863619623 65 */
jhon309 0:ac8863619623 66
jhon309 0:ac8863619623 67 /** Initialize the I2C peripheral. It sets the default parameters for I2C
jhon309 0:ac8863619623 68 * peripheral, and configure its specifieds pins.
jhon309 0:ac8863619623 69 * @param obj The i2c object
jhon309 0:ac8863619623 70 * @param sda The sda pin
jhon309 0:ac8863619623 71 * @param scl The scl pin
jhon309 0:ac8863619623 72 */
jhon309 0:ac8863619623 73 void i2c_init(i2c_t *obj, PinName sda, PinName scl);
jhon309 0:ac8863619623 74
jhon309 0:ac8863619623 75 /** Configure the I2C frequency.
jhon309 0:ac8863619623 76 * @param obj The i2c object
jhon309 0:ac8863619623 77 * @param hz Frequency in Hz
jhon309 0:ac8863619623 78 */
jhon309 0:ac8863619623 79 void i2c_frequency(i2c_t *obj, int hz);
jhon309 0:ac8863619623 80
jhon309 0:ac8863619623 81 /** Send START command.
jhon309 0:ac8863619623 82 * @param obj The i2c object
jhon309 0:ac8863619623 83 */
jhon309 0:ac8863619623 84 int i2c_start(i2c_t *obj);
jhon309 0:ac8863619623 85
jhon309 0:ac8863619623 86 /** Send STOP command.
jhon309 0:ac8863619623 87 * @param obj The i2c object
jhon309 0:ac8863619623 88 */
jhon309 0:ac8863619623 89 int i2c_stop(i2c_t *obj);
jhon309 0:ac8863619623 90
jhon309 0:ac8863619623 91 /** Blocking reading data.
jhon309 0:ac8863619623 92 * @param obj The i2c object
jhon309 0:ac8863619623 93 * @param address 7-bit address (last bit is 1)
jhon309 0:ac8863619623 94 * @param data The buffer for receiving
jhon309 0:ac8863619623 95 * @param length Number of bytes to read
jhon309 0:ac8863619623 96 * @param stop Stop to be generated after the transfer is done
jhon309 0:ac8863619623 97 * @return Number of read bytes
jhon309 0:ac8863619623 98 */
jhon309 0:ac8863619623 99 int i2c_read(i2c_t *obj, int address, char *data, int length, int stop);
jhon309 0:ac8863619623 100
jhon309 0:ac8863619623 101 /** Blocking sending data.
jhon309 0:ac8863619623 102 * @param obj The i2c object
jhon309 0:ac8863619623 103 * @param address 7-bit address (last bit is 0)
jhon309 0:ac8863619623 104 * @param data The buffer for sending
jhon309 0:ac8863619623 105 * @param length Number of bytes to wrte
jhon309 0:ac8863619623 106 * @param stop Stop to be generated after the transfer is done
jhon309 0:ac8863619623 107 * @return Number of written bytes
jhon309 0:ac8863619623 108 */
jhon309 0:ac8863619623 109 int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop);
jhon309 0:ac8863619623 110
jhon309 0:ac8863619623 111 /** Reset I2C peripheral. TODO: The action here. Most of the implementation sends stop().
jhon309 0:ac8863619623 112 * @param obj The i2c object
jhon309 0:ac8863619623 113 */
jhon309 0:ac8863619623 114 void i2c_reset(i2c_t *obj);
jhon309 0:ac8863619623 115
jhon309 0:ac8863619623 116 /** Read one byte.
jhon309 0:ac8863619623 117 * @param obj The i2c object
jhon309 0:ac8863619623 118 * @param last Acknoledge
jhon309 0:ac8863619623 119 * @return The read byte
jhon309 0:ac8863619623 120 */
jhon309 0:ac8863619623 121 int i2c_byte_read(i2c_t *obj, int last);
jhon309 0:ac8863619623 122
jhon309 0:ac8863619623 123 /** Write one byte.
jhon309 0:ac8863619623 124 * @param obj The i2c object
jhon309 0:ac8863619623 125 * @param data Byte to be written
jhon309 0:ac8863619623 126 * @return 1 if NAK was received, 0 if ACK was received, 2 for timeout.
jhon309 0:ac8863619623 127 */
jhon309 0:ac8863619623 128 int i2c_byte_write(i2c_t *obj, int data);
jhon309 0:ac8863619623 129
jhon309 0:ac8863619623 130 /**@}*/
jhon309 0:ac8863619623 131
jhon309 0:ac8863619623 132 #if DEVICE_I2CSLAVE
jhon309 0:ac8863619623 133
jhon309 0:ac8863619623 134 /**
jhon309 0:ac8863619623 135 * \defgroup SynchI2C Synchronous I2C Hardware Abstraction Layer for slave
jhon309 0:ac8863619623 136 * @{
jhon309 0:ac8863619623 137 */
jhon309 0:ac8863619623 138
jhon309 0:ac8863619623 139 /** Configure I2C as slave or master.
jhon309 0:ac8863619623 140 * @param obj The I2C object
jhon309 0:ac8863619623 141 * @return non-zero if a value is available
jhon309 0:ac8863619623 142 */
jhon309 0:ac8863619623 143 void i2c_slave_mode(i2c_t *obj, int enable_slave);
jhon309 0:ac8863619623 144
jhon309 0:ac8863619623 145 /** Check to see if the I2C slave has been addressed.
jhon309 0:ac8863619623 146 * @param obj The I2C object
jhon309 0:ac8863619623 147 * @return The status - 1 - read addresses, 2 - write to all slaves,
jhon309 0:ac8863619623 148 * 3 write addressed, 0 - the slave has not been addressed
jhon309 0:ac8863619623 149 */
jhon309 0:ac8863619623 150 int i2c_slave_receive(i2c_t *obj);
jhon309 0:ac8863619623 151
jhon309 0:ac8863619623 152 /** Configure I2C as slave or master.
jhon309 0:ac8863619623 153 * @param obj The I2C object
jhon309 0:ac8863619623 154 * @return non-zero if a value is available
jhon309 0:ac8863619623 155 */
jhon309 0:ac8863619623 156 int i2c_slave_read(i2c_t *obj, char *data, int length);
jhon309 0:ac8863619623 157
jhon309 0:ac8863619623 158 /** Configure I2C as slave or master.
jhon309 0:ac8863619623 159 * @param obj The I2C object
jhon309 0:ac8863619623 160 * @return non-zero if a value is available
jhon309 0:ac8863619623 161 */
jhon309 0:ac8863619623 162 int i2c_slave_write(i2c_t *obj, const char *data, int length);
jhon309 0:ac8863619623 163
jhon309 0:ac8863619623 164 /** Configure I2C address.
jhon309 0:ac8863619623 165 * @param obj The I2C object
jhon309 0:ac8863619623 166 * @param idx Currently not used
jhon309 0:ac8863619623 167 * @param address The address to be set
jhon309 0:ac8863619623 168 * @param mask Currently not used
jhon309 0:ac8863619623 169 */
jhon309 0:ac8863619623 170 void i2c_slave_address(i2c_t *obj, int idx, uint32_t address, uint32_t mask);
jhon309 0:ac8863619623 171
jhon309 0:ac8863619623 172 #endif
jhon309 0:ac8863619623 173
jhon309 0:ac8863619623 174 /**@}*/
jhon309 0:ac8863619623 175
jhon309 0:ac8863619623 176 #if DEVICE_I2C_ASYNCH
jhon309 0:ac8863619623 177
jhon309 0:ac8863619623 178 /**
jhon309 0:ac8863619623 179 * \defgroup AsynchI2C Asynchronous I2C Hardware Abstraction Layer
jhon309 0:ac8863619623 180 * @{
jhon309 0:ac8863619623 181 */
jhon309 0:ac8863619623 182
jhon309 0:ac8863619623 183 /** Start i2c asynchronous transfer.
jhon309 0:ac8863619623 184 * @param obj The I2C object
jhon309 0:ac8863619623 185 * @param tx The buffer to send
jhon309 0:ac8863619623 186 * @param tx_length The number of words to transmit
jhon309 0:ac8863619623 187 * @param rx The buffer to receive
jhon309 0:ac8863619623 188 * @param rx_length The number of words to receive
jhon309 0:ac8863619623 189 * @param address The address to be set - 7bit or 9 bit
jhon309 0:ac8863619623 190 * @param stop If true, stop will be generated after the transfer is done
jhon309 0:ac8863619623 191 * @param handler The I2C IRQ handler to be set
jhon309 0:ac8863619623 192 * @param hint DMA hint usage
jhon309 0:ac8863619623 193 */
jhon309 0:ac8863619623 194 void i2c_transfer_asynch(i2c_t *obj, const void *tx, size_t tx_length, void *rx, size_t rx_length, uint32_t address, uint32_t stop, uint32_t handler, uint32_t event, DMAUsage hint);
jhon309 0:ac8863619623 195
jhon309 0:ac8863619623 196 /** The asynchronous IRQ handler
jhon309 0:ac8863619623 197 * @param obj The I2C object which holds the transfer information
jhon309 0:ac8863619623 198 * @return event flags if a transfer termination condition was met or 0 otherwise.
jhon309 0:ac8863619623 199 */
jhon309 0:ac8863619623 200 uint32_t i2c_irq_handler_asynch(i2c_t *obj);
jhon309 0:ac8863619623 201
jhon309 0:ac8863619623 202 /** Attempts to determine if I2C peripheral is already in use.
jhon309 0:ac8863619623 203 * @param obj The I2C object
jhon309 0:ac8863619623 204 * @return non-zero if the I2C module is active or zero if it is not
jhon309 0:ac8863619623 205 */
jhon309 0:ac8863619623 206 uint8_t i2c_active(i2c_t *obj);
jhon309 0:ac8863619623 207
jhon309 0:ac8863619623 208 /** Abort ongoing asynchronous transaction.
jhon309 0:ac8863619623 209 * @param obj The I2C object
jhon309 0:ac8863619623 210 */
jhon309 0:ac8863619623 211 void i2c_abort_asynch(i2c_t *obj);
jhon309 0:ac8863619623 212
jhon309 0:ac8863619623 213 #endif
jhon309 0:ac8863619623 214
jhon309 0:ac8863619623 215 /**@}*/
jhon309 0:ac8863619623 216
jhon309 0:ac8863619623 217 #ifdef __cplusplus
jhon309 0:ac8863619623 218 }
jhon309 0:ac8863619623 219 #endif
jhon309 0:ac8863619623 220
jhon309 0:ac8863619623 221 #endif
jhon309 0:ac8863619623 222
jhon309 0:ac8863619623 223 #endif