TUKS MCU Introductory course / TUKS-COURSE-TIMER
Committer:
elmot
Date:
Fri Feb 24 21:13:56 2017 +0000
Revision:
1:d0dfbce63a89
Ready-to-copy

Who changed what in which revision?

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