,,

Fork of Application by Daniel Sygut

Committer:
Zaitsev
Date:
Thu Feb 15 14:29:23 2018 +0000
Revision:
15:2a20c3d2616e
Parent:
10:41552d038a69
j

Who changed what in which revision?

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