Includes library modifications to allow access to AIN_4 (AIN_0 / 5)

Committer:
bryantaylor
Date:
Tue Sep 20 21:26:12 2016 +0000
Revision:
0:eafc3fd41f75
hackathon

Who changed what in which revision?

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