initial

Dependencies:   mbed

Committer:
yihui
Date:
Mon Jan 11 02:32:24 2016 +0000
Revision:
0:638edba3adf6
initial

Who changed what in which revision?

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