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_H
jhon309 0:ac8863619623 17 #define MBED_I2C_H
jhon309 0:ac8863619623 18
jhon309 0:ac8863619623 19 #include "platform.h"
jhon309 0:ac8863619623 20
jhon309 0:ac8863619623 21 #if DEVICE_I2C
jhon309 0:ac8863619623 22
jhon309 0:ac8863619623 23 #include "i2c_api.h"
jhon309 0:ac8863619623 24
jhon309 0:ac8863619623 25 #if DEVICE_I2C_ASYNCH
jhon309 0:ac8863619623 26 #include "CThunk.h"
jhon309 0:ac8863619623 27 #include "dma_api.h"
jhon309 0:ac8863619623 28 #include "FunctionPointer.h"
jhon309 0:ac8863619623 29 #endif
jhon309 0:ac8863619623 30
jhon309 0:ac8863619623 31 namespace mbed {
jhon309 0:ac8863619623 32
jhon309 0:ac8863619623 33 /** An I2C Master, used for communicating with I2C slave devices
jhon309 0:ac8863619623 34 *
jhon309 0:ac8863619623 35 * Example:
jhon309 0:ac8863619623 36 * @code
jhon309 0:ac8863619623 37 * // Read from I2C slave at address 0x62
jhon309 0:ac8863619623 38 *
jhon309 0:ac8863619623 39 * #include "mbed.h"
jhon309 0:ac8863619623 40 *
jhon309 0:ac8863619623 41 * I2C i2c(p28, p27);
jhon309 0:ac8863619623 42 *
jhon309 0:ac8863619623 43 * int main() {
jhon309 0:ac8863619623 44 * int address = 0x62;
jhon309 0:ac8863619623 45 * char data[2];
jhon309 0:ac8863619623 46 * i2c.read(address, data, 2);
jhon309 0:ac8863619623 47 * }
jhon309 0:ac8863619623 48 * @endcode
jhon309 0:ac8863619623 49 */
jhon309 0:ac8863619623 50 class I2C {
jhon309 0:ac8863619623 51
jhon309 0:ac8863619623 52 public:
jhon309 0:ac8863619623 53 enum RxStatus {
jhon309 0:ac8863619623 54 NoData,
jhon309 0:ac8863619623 55 MasterGeneralCall,
jhon309 0:ac8863619623 56 MasterWrite,
jhon309 0:ac8863619623 57 MasterRead
jhon309 0:ac8863619623 58 };
jhon309 0:ac8863619623 59
jhon309 0:ac8863619623 60 enum Acknowledge {
jhon309 0:ac8863619623 61 NoACK = 0,
jhon309 0:ac8863619623 62 ACK = 1
jhon309 0:ac8863619623 63 };
jhon309 0:ac8863619623 64
jhon309 0:ac8863619623 65 /** Create an I2C Master interface, connected to the specified pins
jhon309 0:ac8863619623 66 *
jhon309 0:ac8863619623 67 * @param sda I2C data line pin
jhon309 0:ac8863619623 68 * @param scl I2C clock line pin
jhon309 0:ac8863619623 69 */
jhon309 0:ac8863619623 70 I2C(PinName sda, PinName scl);
jhon309 0:ac8863619623 71
jhon309 0:ac8863619623 72 /** Set the frequency of the I2C interface
jhon309 0:ac8863619623 73 *
jhon309 0:ac8863619623 74 * @param hz The bus frequency in hertz
jhon309 0:ac8863619623 75 */
jhon309 0:ac8863619623 76 void frequency(int hz);
jhon309 0:ac8863619623 77
jhon309 0:ac8863619623 78 /** Read from an I2C slave
jhon309 0:ac8863619623 79 *
jhon309 0:ac8863619623 80 * Performs a complete read transaction. The bottom bit of
jhon309 0:ac8863619623 81 * the address is forced to 1 to indicate a read.
jhon309 0:ac8863619623 82 *
jhon309 0:ac8863619623 83 * @param address 8-bit I2C slave address [ addr | 1 ]
jhon309 0:ac8863619623 84 * @param data Pointer to the byte-array to read data in to
jhon309 0:ac8863619623 85 * @param length Number of bytes to read
jhon309 0:ac8863619623 86 * @param repeated Repeated start, true - don't send stop at end
jhon309 0:ac8863619623 87 *
jhon309 0:ac8863619623 88 * @returns
jhon309 0:ac8863619623 89 * 0 on success (ack),
jhon309 0:ac8863619623 90 * non-0 on failure (nack)
jhon309 0:ac8863619623 91 */
jhon309 0:ac8863619623 92 int read(int address, char *data, int length, bool repeated = false);
jhon309 0:ac8863619623 93
jhon309 0:ac8863619623 94 /** Read a single byte from the I2C bus
jhon309 0:ac8863619623 95 *
jhon309 0:ac8863619623 96 * @param ack indicates if the byte is to be acknowledged (1 = acknowledge)
jhon309 0:ac8863619623 97 *
jhon309 0:ac8863619623 98 * @returns
jhon309 0:ac8863619623 99 * the byte read
jhon309 0:ac8863619623 100 */
jhon309 0:ac8863619623 101 int read(int ack);
jhon309 0:ac8863619623 102
jhon309 0:ac8863619623 103 /** Write to an I2C slave
jhon309 0:ac8863619623 104 *
jhon309 0:ac8863619623 105 * Performs a complete write transaction. The bottom bit of
jhon309 0:ac8863619623 106 * the address is forced to 0 to indicate a write.
jhon309 0:ac8863619623 107 *
jhon309 0:ac8863619623 108 * @param address 8-bit I2C slave address [ addr | 0 ]
jhon309 0:ac8863619623 109 * @param data Pointer to the byte-array data to send
jhon309 0:ac8863619623 110 * @param length Number of bytes to send
jhon309 0:ac8863619623 111 * @param repeated Repeated start, true - do not send stop at end
jhon309 0:ac8863619623 112 *
jhon309 0:ac8863619623 113 * @returns
jhon309 0:ac8863619623 114 * 0 on success (ack),
jhon309 0:ac8863619623 115 * non-0 on failure (nack)
jhon309 0:ac8863619623 116 */
jhon309 0:ac8863619623 117 int write(int address, const char *data, int length, bool repeated = false);
jhon309 0:ac8863619623 118
jhon309 0:ac8863619623 119 /** Write single byte out on the I2C bus
jhon309 0:ac8863619623 120 *
jhon309 0:ac8863619623 121 * @param data data to write out on bus
jhon309 0:ac8863619623 122 *
jhon309 0:ac8863619623 123 * @returns
jhon309 0:ac8863619623 124 * '1' if an ACK was received,
jhon309 0:ac8863619623 125 * '0' otherwise
jhon309 0:ac8863619623 126 */
jhon309 0:ac8863619623 127 int write(int data);
jhon309 0:ac8863619623 128
jhon309 0:ac8863619623 129 /** Creates a start condition on the I2C bus
jhon309 0:ac8863619623 130 */
jhon309 0:ac8863619623 131
jhon309 0:ac8863619623 132 void start(void);
jhon309 0:ac8863619623 133
jhon309 0:ac8863619623 134 /** Creates a stop condition on the I2C bus
jhon309 0:ac8863619623 135 */
jhon309 0:ac8863619623 136 void stop(void);
jhon309 0:ac8863619623 137
jhon309 0:ac8863619623 138 #if DEVICE_I2C_ASYNCH
jhon309 0:ac8863619623 139
jhon309 0:ac8863619623 140 /** Start non-blocking I2C transfer.
jhon309 0:ac8863619623 141 *
jhon309 0:ac8863619623 142 * @param address 8/10 bit I2c slave address
jhon309 0:ac8863619623 143 * @param tx_buffer The TX buffer with data to be transfered
jhon309 0:ac8863619623 144 * @param tx_length The length of TX buffer in bytes
jhon309 0:ac8863619623 145 * @param rx_buffer The RX buffer which is used for received data
jhon309 0:ac8863619623 146 * @param rx_length The length of RX buffer in bytes
jhon309 0:ac8863619623 147 * @param event The logical OR of events to modify
jhon309 0:ac8863619623 148 * @param callback The event callback function
jhon309 0:ac8863619623 149 * @param repeated Repeated start, true - do not send stop at end
jhon309 0:ac8863619623 150 * @return Zero if the transfer has started, or -1 if I2C peripheral is busy
jhon309 0:ac8863619623 151 */
jhon309 0:ac8863619623 152 int transfer(int address, const char *tx_buffer, int tx_length, char *rx_buffer, int rx_length, const event_callback_t& callback, int event = I2C_EVENT_TRANSFER_COMPLETE, bool repeated = false);
jhon309 0:ac8863619623 153
jhon309 0:ac8863619623 154 /** Abort the on-going I2C transfer
jhon309 0:ac8863619623 155 */
jhon309 0:ac8863619623 156 void abort_transfer();
jhon309 0:ac8863619623 157 protected:
jhon309 0:ac8863619623 158 void irq_handler_asynch(void);
jhon309 0:ac8863619623 159 event_callback_t _callback;
jhon309 0:ac8863619623 160 CThunk<I2C> _irq;
jhon309 0:ac8863619623 161 DMAUsage _usage;
jhon309 0:ac8863619623 162 #endif
jhon309 0:ac8863619623 163
jhon309 0:ac8863619623 164 protected:
jhon309 0:ac8863619623 165 void aquire();
jhon309 0:ac8863619623 166
jhon309 0:ac8863619623 167 i2c_t _i2c;
jhon309 0:ac8863619623 168 static I2C *_owner;
jhon309 0:ac8863619623 169 int _hz;
jhon309 0:ac8863619623 170 };
jhon309 0:ac8863619623 171
jhon309 0:ac8863619623 172 } // namespace mbed
jhon309 0:ac8863619623 173
jhon309 0:ac8863619623 174 #endif
jhon309 0:ac8863619623 175
jhon309 0:ac8863619623 176 #endif