mbed library sources. Supersedes mbed-src.

Dependents:   Nucleo_Hello_Encoder BLE_iBeaconScan AM1805_DEMO DISCO-F429ZI_ExportTemplate1 ... more

Committer:
AnnaBridge
Date:
Wed Feb 20 22:31:08 2019 +0000
Revision:
189:f392fc9709a3
Parent:
188:bcfe06ba3d64
mbed library release version 165

Who changed what in which revision?

UserRevisionLine numberNew contents of line
<> 149:156823d33999 1 /* mbed Microcontroller Library
<> 149:156823d33999 2 * Copyright (c) 2006-2015 ARM Limited
AnnaBridge 189:f392fc9709a3 3 * SPDX-License-Identifier: Apache-2.0
<> 149:156823d33999 4 *
<> 149:156823d33999 5 * Licensed under the Apache License, Version 2.0 (the "License");
<> 149:156823d33999 6 * you may not use this file except in compliance with the License.
<> 149:156823d33999 7 * You may obtain a copy of the License at
<> 149:156823d33999 8 *
<> 149:156823d33999 9 * http://www.apache.org/licenses/LICENSE-2.0
<> 149:156823d33999 10 *
<> 149:156823d33999 11 * Unless required by applicable law or agreed to in writing, software
<> 149:156823d33999 12 * distributed under the License is distributed on an "AS IS" BASIS,
<> 149:156823d33999 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<> 149:156823d33999 14 * See the License for the specific language governing permissions and
<> 149:156823d33999 15 * limitations under the License.
<> 149:156823d33999 16 */
<> 149:156823d33999 17 #ifndef MBED_I2C_H
<> 149:156823d33999 18 #define MBED_I2C_H
<> 149:156823d33999 19
<> 149:156823d33999 20 #include "platform/platform.h"
AnnaBridge 189:f392fc9709a3 21 #include "hal/gpio_api.h"
<> 149:156823d33999 22
AnnaBridge 189:f392fc9709a3 23 #if DEVICE_I2C || defined(DOXYGEN_ONLY)
<> 149:156823d33999 24
<> 149:156823d33999 25 #include "hal/i2c_api.h"
<> 149:156823d33999 26 #include "platform/SingletonPtr.h"
<> 149:156823d33999 27 #include "platform/PlatformMutex.h"
AnnaBridge 168:9672193075cf 28 #include "platform/NonCopyable.h"
<> 149:156823d33999 29
<> 149:156823d33999 30 #if DEVICE_I2C_ASYNCH
<> 149:156823d33999 31 #include "platform/CThunk.h"
<> 149:156823d33999 32 #include "hal/dma_api.h"
<> 149:156823d33999 33 #include "platform/FunctionPointer.h"
<> 149:156823d33999 34 #endif
<> 149:156823d33999 35
<> 149:156823d33999 36 namespace mbed {
<> 149:156823d33999 37 /** \addtogroup drivers */
<> 149:156823d33999 38
<> 149:156823d33999 39 /** An I2C Master, used for communicating with I2C slave devices
<> 149:156823d33999 40 *
AnnaBridge 167:e84263d55307 41 * @note Synchronization level: Thread safe
<> 149:156823d33999 42 *
<> 149:156823d33999 43 * Example:
<> 149:156823d33999 44 * @code
AnnaBridge 189:f392fc9709a3 45 * Read temperature from LM75BD
<> 149:156823d33999 46 * #include "mbed.h"
AnnaBridge 189:f392fc9709a3 47 * I2C i2c(I2C_SDA , I2C_SCL);
AnnaBridge 189:f392fc9709a3 48 * const int addr7bit = 0x48; // 7-bit I2C address
AnnaBridge 189:f392fc9709a3 49 * const int addr8bit = 0x48 << 1; // 8-bit I2C address, 0x90
<> 149:156823d33999 50 *
<> 149:156823d33999 51 * int main() {
AnnaBridge 189:f392fc9709a3 52 * char cmd[2];
AnnaBridge 189:f392fc9709a3 53 * while (1) {
AnnaBridge 189:f392fc9709a3 54 * cmd[0] = 0x01;
AnnaBridge 189:f392fc9709a3 55 * cmd[1] = 0x00;
AnnaBridge 189:f392fc9709a3 56 *
AnnaBridge 189:f392fc9709a3 57 * // read and write takes the 8-bit version of the address.
AnnaBridge 189:f392fc9709a3 58 * // set up configuration register (at 0x01)
AnnaBridge 189:f392fc9709a3 59 * i2c.write(addr8bit, cmd, 2);
AnnaBridge 189:f392fc9709a3 60 *
AnnaBridge 189:f392fc9709a3 61 * wait(0.5);
AnnaBridge 189:f392fc9709a3 62 *
AnnaBridge 189:f392fc9709a3 63 * // read temperature register
AnnaBridge 189:f392fc9709a3 64 * cmd[0] = 0x00;
AnnaBridge 189:f392fc9709a3 65 * i2c.write(addr8bit, cmd, 1);
AnnaBridge 189:f392fc9709a3 66 * i2c.read( addr8bit, cmd, 2);
AnnaBridge 189:f392fc9709a3 67 *
AnnaBridge 189:f392fc9709a3 68 * float tmp = (float((cmd[0]<<8)|cmd[1]) / 256.0);
AnnaBridge 189:f392fc9709a3 69 * printf("Temp = %.2f\n", tmp);
AnnaBridge 189:f392fc9709a3 70 * }
<> 149:156823d33999 71 * }
<> 149:156823d33999 72 * @endcode
AnnaBridge 167:e84263d55307 73 * @ingroup drivers
<> 149:156823d33999 74 */
AnnaBridge 168:9672193075cf 75 class I2C : private NonCopyable<I2C> {
<> 149:156823d33999 76
<> 149:156823d33999 77 public:
<> 149:156823d33999 78 enum RxStatus {
<> 149:156823d33999 79 NoData,
<> 149:156823d33999 80 MasterGeneralCall,
<> 149:156823d33999 81 MasterWrite,
<> 149:156823d33999 82 MasterRead
<> 149:156823d33999 83 };
<> 149:156823d33999 84
<> 149:156823d33999 85 enum Acknowledge {
<> 149:156823d33999 86 NoACK = 0,
<> 149:156823d33999 87 ACK = 1
<> 149:156823d33999 88 };
<> 149:156823d33999 89
<> 149:156823d33999 90 /** Create an I2C Master interface, connected to the specified pins
<> 149:156823d33999 91 *
<> 149:156823d33999 92 * @param sda I2C data line pin
<> 149:156823d33999 93 * @param scl I2C clock line pin
<> 149:156823d33999 94 */
<> 149:156823d33999 95 I2C(PinName sda, PinName scl);
<> 149:156823d33999 96
<> 149:156823d33999 97 /** Set the frequency of the I2C interface
<> 149:156823d33999 98 *
<> 149:156823d33999 99 * @param hz The bus frequency in hertz
<> 149:156823d33999 100 */
<> 149:156823d33999 101 void frequency(int hz);
<> 149:156823d33999 102
<> 149:156823d33999 103 /** Read from an I2C slave
<> 149:156823d33999 104 *
<> 149:156823d33999 105 * Performs a complete read transaction. The bottom bit of
<> 149:156823d33999 106 * the address is forced to 1 to indicate a read.
<> 149:156823d33999 107 *
<> 149:156823d33999 108 * @param address 8-bit I2C slave address [ addr | 1 ]
<> 149:156823d33999 109 * @param data Pointer to the byte-array to read data in to
<> 149:156823d33999 110 * @param length Number of bytes to read
<> 149:156823d33999 111 * @param repeated Repeated start, true - don't send stop at end
AnnaBridge 189:f392fc9709a3 112 * default value is false.
<> 149:156823d33999 113 *
<> 149:156823d33999 114 * @returns
<> 149:156823d33999 115 * 0 on success (ack),
AnnaBridge 189:f392fc9709a3 116 * nonzero on failure (nack)
<> 149:156823d33999 117 */
<> 149:156823d33999 118 int read(int address, char *data, int length, bool repeated = false);
<> 149:156823d33999 119
<> 149:156823d33999 120 /** Read a single byte from the I2C bus
<> 149:156823d33999 121 *
<> 149:156823d33999 122 * @param ack indicates if the byte is to be acknowledged (1 = acknowledge)
<> 149:156823d33999 123 *
<> 149:156823d33999 124 * @returns
<> 149:156823d33999 125 * the byte read
<> 149:156823d33999 126 */
<> 149:156823d33999 127 int read(int ack);
<> 149:156823d33999 128
<> 149:156823d33999 129 /** Write to an I2C slave
<> 149:156823d33999 130 *
<> 149:156823d33999 131 * Performs a complete write transaction. The bottom bit of
<> 149:156823d33999 132 * the address is forced to 0 to indicate a write.
<> 149:156823d33999 133 *
<> 149:156823d33999 134 * @param address 8-bit I2C slave address [ addr | 0 ]
<> 149:156823d33999 135 * @param data Pointer to the byte-array data to send
<> 149:156823d33999 136 * @param length Number of bytes to send
<> 149:156823d33999 137 * @param repeated Repeated start, true - do not send stop at end
AnnaBridge 189:f392fc9709a3 138 * default value is false.
<> 149:156823d33999 139 *
<> 149:156823d33999 140 * @returns
<> 161:2cc1468da177 141 * 0 on success (ack),
AnnaBridge 189:f392fc9709a3 142 * nonzero on failure (nack)
<> 149:156823d33999 143 */
<> 149:156823d33999 144 int write(int address, const char *data, int length, bool repeated = false);
<> 149:156823d33999 145
<> 149:156823d33999 146 /** Write single byte out on the I2C bus
<> 149:156823d33999 147 *
<> 149:156823d33999 148 * @param data data to write out on bus
<> 149:156823d33999 149 *
<> 149:156823d33999 150 * @returns
<> 153:fa9ff456f731 151 * '0' - NAK was received
<> 153:fa9ff456f731 152 * '1' - ACK was received,
<> 153:fa9ff456f731 153 * '2' - timeout
<> 149:156823d33999 154 */
<> 149:156823d33999 155 int write(int data);
<> 149:156823d33999 156
<> 149:156823d33999 157 /** Creates a start condition on the I2C bus
<> 149:156823d33999 158 */
<> 149:156823d33999 159 void start(void);
<> 149:156823d33999 160
<> 149:156823d33999 161 /** Creates a stop condition on the I2C bus
<> 149:156823d33999 162 */
<> 149:156823d33999 163 void stop(void);
<> 149:156823d33999 164
<> 149:156823d33999 165 /** Acquire exclusive access to this I2C bus
<> 149:156823d33999 166 */
<> 149:156823d33999 167 virtual void lock(void);
<> 149:156823d33999 168
<> 149:156823d33999 169 /** Release exclusive access to this I2C bus
<> 149:156823d33999 170 */
<> 149:156823d33999 171 virtual void unlock(void);
<> 149:156823d33999 172
AnnaBridge 187:0387e8f68319 173 virtual ~I2C()
AnnaBridge 187:0387e8f68319 174 {
<> 149:156823d33999 175 // Do nothing
<> 149:156823d33999 176 }
<> 149:156823d33999 177
<> 149:156823d33999 178 #if DEVICE_I2C_ASYNCH
<> 149:156823d33999 179
AnnaBridge 189:f392fc9709a3 180 /** Start nonblocking I2C transfer.
<> 149:156823d33999 181 *
AnnaBridge 184:08ed48f1de7f 182 * This function locks the deep sleep until any event has occurred
AnnaBridge 187:0387e8f68319 183 *
AnnaBridge 184:08ed48f1de7f 184 * @param address 8/10 bit I2C slave address
AnnaBridge 188:bcfe06ba3d64 185 * @param tx_buffer The TX buffer with data to be transferred
<> 149:156823d33999 186 * @param tx_length The length of TX buffer in bytes
AnnaBridge 189:f392fc9709a3 187 * @param rx_buffer The RX buffer, which is used for received data
<> 149:156823d33999 188 * @param rx_length The length of RX buffer in bytes
<> 149:156823d33999 189 * @param event The logical OR of events to modify
<> 149:156823d33999 190 * @param callback The event callback function
<> 149:156823d33999 191 * @param repeated Repeated start, true - do not send stop at end
AnnaBridge 189:f392fc9709a3 192 * default value is false.
AnnaBridge 189:f392fc9709a3 193 *
AnnaBridge 189:f392fc9709a3 194 * @returns Zero if the transfer has started, or -1 if I2C peripheral is busy
<> 149:156823d33999 195 */
AnnaBridge 187:0387e8f68319 196 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);
<> 149:156823d33999 197
AnnaBridge 189:f392fc9709a3 198 /** Abort the ongoing I2C transfer
<> 149:156823d33999 199 */
<> 149:156823d33999 200 void abort_transfer();
Anna Bridge 180:96ed750bd169 201
AnnaBridge 189:f392fc9709a3 202 #if !defined(DOXYGEN_ONLY)
AnnaBridge 187:0387e8f68319 203 protected:
Anna Bridge 180:96ed750bd169 204 /** Lock deep sleep only if it is not yet locked */
Anna Bridge 180:96ed750bd169 205 void lock_deep_sleep();
Anna Bridge 180:96ed750bd169 206
Anna Bridge 180:96ed750bd169 207 /** Unlock deep sleep only if it has been locked */
Anna Bridge 180:96ed750bd169 208 void unlock_deep_sleep();
Anna Bridge 180:96ed750bd169 209
<> 149:156823d33999 210 void irq_handler_asynch(void);
<> 149:156823d33999 211 event_callback_t _callback;
<> 149:156823d33999 212 CThunk<I2C> _irq;
<> 149:156823d33999 213 DMAUsage _usage;
Anna Bridge 180:96ed750bd169 214 bool _deep_sleep_locked;
<> 149:156823d33999 215 #endif
AnnaBridge 189:f392fc9709a3 216 #endif
<> 149:156823d33999 217
AnnaBridge 189:f392fc9709a3 218 #if !defined(DOXYGEN_ONLY)
<> 149:156823d33999 219 protected:
<> 149:156823d33999 220 void aquire();
<> 149:156823d33999 221
<> 149:156823d33999 222 i2c_t _i2c;
<> 149:156823d33999 223 static I2C *_owner;
AnnaBridge 189:f392fc9709a3 224 int _hz;
<> 149:156823d33999 225 static SingletonPtr<PlatformMutex> _mutex;
AnnaBridge 189:f392fc9709a3 226 PinName _sda;
AnnaBridge 189:f392fc9709a3 227 PinName _scl;
AnnaBridge 189:f392fc9709a3 228
AnnaBridge 189:f392fc9709a3 229 private:
AnnaBridge 189:f392fc9709a3 230 /** Recover I2C bus, when stuck with SDA low
AnnaBridge 189:f392fc9709a3 231 * @note : Initialization of I2C bus is required after this API.
AnnaBridge 189:f392fc9709a3 232 *
AnnaBridge 189:f392fc9709a3 233 * @param sda I2C data line pin
AnnaBridge 189:f392fc9709a3 234 * @param scl I2C clock line pin
AnnaBridge 189:f392fc9709a3 235 *
AnnaBridge 189:f392fc9709a3 236 * @returns
AnnaBridge 189:f392fc9709a3 237 * '0' - Successfully recovered
AnnaBridge 189:f392fc9709a3 238 * 'I2C_ERROR_BUS_BUSY' - In case of failure
AnnaBridge 189:f392fc9709a3 239 *
AnnaBridge 189:f392fc9709a3 240 */
AnnaBridge 189:f392fc9709a3 241 int recover(PinName sda, PinName scl);
AnnaBridge 189:f392fc9709a3 242 #endif
<> 149:156823d33999 243 };
<> 149:156823d33999 244
<> 149:156823d33999 245 } // namespace mbed
<> 149:156823d33999 246
<> 149:156823d33999 247 #endif
<> 149:156823d33999 248
<> 149:156823d33999 249 #endif