mbed library sources. Supersedes mbed-src.
Dependents: Nucleo_Hello_Encoder BLE_iBeaconScan AM1805_DEMO DISCO-F429ZI_ExportTemplate1 ... more
cmsis/BUILD/mbed/drivers/I2C.h@189:f392fc9709a3, 2019-02-20 (annotated)
- Committer:
- AnnaBridge
- Date:
- Wed Feb 20 22:31:08 2019 +0000
- Revision:
- 189:f392fc9709a3
mbed library release version 165
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
AnnaBridge | 189:f392fc9709a3 | 1 | /* mbed Microcontroller Library |
AnnaBridge | 189:f392fc9709a3 | 2 | * Copyright (c) 2006-2015 ARM Limited |
AnnaBridge | 189:f392fc9709a3 | 3 | * SPDX-License-Identifier: Apache-2.0 |
AnnaBridge | 189:f392fc9709a3 | 4 | * |
AnnaBridge | 189:f392fc9709a3 | 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
AnnaBridge | 189:f392fc9709a3 | 6 | * you may not use this file except in compliance with the License. |
AnnaBridge | 189:f392fc9709a3 | 7 | * You may obtain a copy of the License at |
AnnaBridge | 189:f392fc9709a3 | 8 | * |
AnnaBridge | 189:f392fc9709a3 | 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
AnnaBridge | 189:f392fc9709a3 | 10 | * |
AnnaBridge | 189:f392fc9709a3 | 11 | * Unless required by applicable law or agreed to in writing, software |
AnnaBridge | 189:f392fc9709a3 | 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
AnnaBridge | 189:f392fc9709a3 | 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
AnnaBridge | 189:f392fc9709a3 | 14 | * See the License for the specific language governing permissions and |
AnnaBridge | 189:f392fc9709a3 | 15 | * limitations under the License. |
AnnaBridge | 189:f392fc9709a3 | 16 | */ |
AnnaBridge | 189:f392fc9709a3 | 17 | #ifndef MBED_I2C_H |
AnnaBridge | 189:f392fc9709a3 | 18 | #define MBED_I2C_H |
AnnaBridge | 189:f392fc9709a3 | 19 | |
AnnaBridge | 189:f392fc9709a3 | 20 | #include "platform/platform.h" |
AnnaBridge | 189:f392fc9709a3 | 21 | #include "hal/gpio_api.h" |
AnnaBridge | 189:f392fc9709a3 | 22 | |
AnnaBridge | 189:f392fc9709a3 | 23 | #if DEVICE_I2C || defined(DOXYGEN_ONLY) |
AnnaBridge | 189:f392fc9709a3 | 24 | |
AnnaBridge | 189:f392fc9709a3 | 25 | #include "hal/i2c_api.h" |
AnnaBridge | 189:f392fc9709a3 | 26 | #include "platform/SingletonPtr.h" |
AnnaBridge | 189:f392fc9709a3 | 27 | #include "platform/PlatformMutex.h" |
AnnaBridge | 189:f392fc9709a3 | 28 | #include "platform/NonCopyable.h" |
AnnaBridge | 189:f392fc9709a3 | 29 | |
AnnaBridge | 189:f392fc9709a3 | 30 | #if DEVICE_I2C_ASYNCH |
AnnaBridge | 189:f392fc9709a3 | 31 | #include "platform/CThunk.h" |
AnnaBridge | 189:f392fc9709a3 | 32 | #include "hal/dma_api.h" |
AnnaBridge | 189:f392fc9709a3 | 33 | #include "platform/FunctionPointer.h" |
AnnaBridge | 189:f392fc9709a3 | 34 | #endif |
AnnaBridge | 189:f392fc9709a3 | 35 | |
AnnaBridge | 189:f392fc9709a3 | 36 | namespace mbed { |
AnnaBridge | 189:f392fc9709a3 | 37 | /** \addtogroup drivers */ |
AnnaBridge | 189:f392fc9709a3 | 38 | |
AnnaBridge | 189:f392fc9709a3 | 39 | /** An I2C Master, used for communicating with I2C slave devices |
AnnaBridge | 189:f392fc9709a3 | 40 | * |
AnnaBridge | 189:f392fc9709a3 | 41 | * @note Synchronization level: Thread safe |
AnnaBridge | 189:f392fc9709a3 | 42 | * |
AnnaBridge | 189:f392fc9709a3 | 43 | * Example: |
AnnaBridge | 189:f392fc9709a3 | 44 | * @code |
AnnaBridge | 189:f392fc9709a3 | 45 | * Read temperature from LM75BD |
AnnaBridge | 189:f392fc9709a3 | 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 |
AnnaBridge | 189:f392fc9709a3 | 50 | * |
AnnaBridge | 189:f392fc9709a3 | 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 | * } |
AnnaBridge | 189:f392fc9709a3 | 71 | * } |
AnnaBridge | 189:f392fc9709a3 | 72 | * @endcode |
AnnaBridge | 189:f392fc9709a3 | 73 | * @ingroup drivers |
AnnaBridge | 189:f392fc9709a3 | 74 | */ |
AnnaBridge | 189:f392fc9709a3 | 75 | class I2C : private NonCopyable<I2C> { |
AnnaBridge | 189:f392fc9709a3 | 76 | |
AnnaBridge | 189:f392fc9709a3 | 77 | public: |
AnnaBridge | 189:f392fc9709a3 | 78 | enum RxStatus { |
AnnaBridge | 189:f392fc9709a3 | 79 | NoData, |
AnnaBridge | 189:f392fc9709a3 | 80 | MasterGeneralCall, |
AnnaBridge | 189:f392fc9709a3 | 81 | MasterWrite, |
AnnaBridge | 189:f392fc9709a3 | 82 | MasterRead |
AnnaBridge | 189:f392fc9709a3 | 83 | }; |
AnnaBridge | 189:f392fc9709a3 | 84 | |
AnnaBridge | 189:f392fc9709a3 | 85 | enum Acknowledge { |
AnnaBridge | 189:f392fc9709a3 | 86 | NoACK = 0, |
AnnaBridge | 189:f392fc9709a3 | 87 | ACK = 1 |
AnnaBridge | 189:f392fc9709a3 | 88 | }; |
AnnaBridge | 189:f392fc9709a3 | 89 | |
AnnaBridge | 189:f392fc9709a3 | 90 | /** Create an I2C Master interface, connected to the specified pins |
AnnaBridge | 189:f392fc9709a3 | 91 | * |
AnnaBridge | 189:f392fc9709a3 | 92 | * @param sda I2C data line pin |
AnnaBridge | 189:f392fc9709a3 | 93 | * @param scl I2C clock line pin |
AnnaBridge | 189:f392fc9709a3 | 94 | */ |
AnnaBridge | 189:f392fc9709a3 | 95 | I2C(PinName sda, PinName scl); |
AnnaBridge | 189:f392fc9709a3 | 96 | |
AnnaBridge | 189:f392fc9709a3 | 97 | /** Set the frequency of the I2C interface |
AnnaBridge | 189:f392fc9709a3 | 98 | * |
AnnaBridge | 189:f392fc9709a3 | 99 | * @param hz The bus frequency in hertz |
AnnaBridge | 189:f392fc9709a3 | 100 | */ |
AnnaBridge | 189:f392fc9709a3 | 101 | void frequency(int hz); |
AnnaBridge | 189:f392fc9709a3 | 102 | |
AnnaBridge | 189:f392fc9709a3 | 103 | /** Read from an I2C slave |
AnnaBridge | 189:f392fc9709a3 | 104 | * |
AnnaBridge | 189:f392fc9709a3 | 105 | * Performs a complete read transaction. The bottom bit of |
AnnaBridge | 189:f392fc9709a3 | 106 | * the address is forced to 1 to indicate a read. |
AnnaBridge | 189:f392fc9709a3 | 107 | * |
AnnaBridge | 189:f392fc9709a3 | 108 | * @param address 8-bit I2C slave address [ addr | 1 ] |
AnnaBridge | 189:f392fc9709a3 | 109 | * @param data Pointer to the byte-array to read data in to |
AnnaBridge | 189:f392fc9709a3 | 110 | * @param length Number of bytes to read |
AnnaBridge | 189:f392fc9709a3 | 111 | * @param repeated Repeated start, true - don't send stop at end |
AnnaBridge | 189:f392fc9709a3 | 112 | * default value is false. |
AnnaBridge | 189:f392fc9709a3 | 113 | * |
AnnaBridge | 189:f392fc9709a3 | 114 | * @returns |
AnnaBridge | 189:f392fc9709a3 | 115 | * 0 on success (ack), |
AnnaBridge | 189:f392fc9709a3 | 116 | * nonzero on failure (nack) |
AnnaBridge | 189:f392fc9709a3 | 117 | */ |
AnnaBridge | 189:f392fc9709a3 | 118 | int read(int address, char *data, int length, bool repeated = false); |
AnnaBridge | 189:f392fc9709a3 | 119 | |
AnnaBridge | 189:f392fc9709a3 | 120 | /** Read a single byte from the I2C bus |
AnnaBridge | 189:f392fc9709a3 | 121 | * |
AnnaBridge | 189:f392fc9709a3 | 122 | * @param ack indicates if the byte is to be acknowledged (1 = acknowledge) |
AnnaBridge | 189:f392fc9709a3 | 123 | * |
AnnaBridge | 189:f392fc9709a3 | 124 | * @returns |
AnnaBridge | 189:f392fc9709a3 | 125 | * the byte read |
AnnaBridge | 189:f392fc9709a3 | 126 | */ |
AnnaBridge | 189:f392fc9709a3 | 127 | int read(int ack); |
AnnaBridge | 189:f392fc9709a3 | 128 | |
AnnaBridge | 189:f392fc9709a3 | 129 | /** Write to an I2C slave |
AnnaBridge | 189:f392fc9709a3 | 130 | * |
AnnaBridge | 189:f392fc9709a3 | 131 | * Performs a complete write transaction. The bottom bit of |
AnnaBridge | 189:f392fc9709a3 | 132 | * the address is forced to 0 to indicate a write. |
AnnaBridge | 189:f392fc9709a3 | 133 | * |
AnnaBridge | 189:f392fc9709a3 | 134 | * @param address 8-bit I2C slave address [ addr | 0 ] |
AnnaBridge | 189:f392fc9709a3 | 135 | * @param data Pointer to the byte-array data to send |
AnnaBridge | 189:f392fc9709a3 | 136 | * @param length Number of bytes to send |
AnnaBridge | 189:f392fc9709a3 | 137 | * @param repeated Repeated start, true - do not send stop at end |
AnnaBridge | 189:f392fc9709a3 | 138 | * default value is false. |
AnnaBridge | 189:f392fc9709a3 | 139 | * |
AnnaBridge | 189:f392fc9709a3 | 140 | * @returns |
AnnaBridge | 189:f392fc9709a3 | 141 | * 0 on success (ack), |
AnnaBridge | 189:f392fc9709a3 | 142 | * nonzero on failure (nack) |
AnnaBridge | 189:f392fc9709a3 | 143 | */ |
AnnaBridge | 189:f392fc9709a3 | 144 | int write(int address, const char *data, int length, bool repeated = false); |
AnnaBridge | 189:f392fc9709a3 | 145 | |
AnnaBridge | 189:f392fc9709a3 | 146 | /** Write single byte out on the I2C bus |
AnnaBridge | 189:f392fc9709a3 | 147 | * |
AnnaBridge | 189:f392fc9709a3 | 148 | * @param data data to write out on bus |
AnnaBridge | 189:f392fc9709a3 | 149 | * |
AnnaBridge | 189:f392fc9709a3 | 150 | * @returns |
AnnaBridge | 189:f392fc9709a3 | 151 | * '0' - NAK was received |
AnnaBridge | 189:f392fc9709a3 | 152 | * '1' - ACK was received, |
AnnaBridge | 189:f392fc9709a3 | 153 | * '2' - timeout |
AnnaBridge | 189:f392fc9709a3 | 154 | */ |
AnnaBridge | 189:f392fc9709a3 | 155 | int write(int data); |
AnnaBridge | 189:f392fc9709a3 | 156 | |
AnnaBridge | 189:f392fc9709a3 | 157 | /** Creates a start condition on the I2C bus |
AnnaBridge | 189:f392fc9709a3 | 158 | */ |
AnnaBridge | 189:f392fc9709a3 | 159 | void start(void); |
AnnaBridge | 189:f392fc9709a3 | 160 | |
AnnaBridge | 189:f392fc9709a3 | 161 | /** Creates a stop condition on the I2C bus |
AnnaBridge | 189:f392fc9709a3 | 162 | */ |
AnnaBridge | 189:f392fc9709a3 | 163 | void stop(void); |
AnnaBridge | 189:f392fc9709a3 | 164 | |
AnnaBridge | 189:f392fc9709a3 | 165 | /** Acquire exclusive access to this I2C bus |
AnnaBridge | 189:f392fc9709a3 | 166 | */ |
AnnaBridge | 189:f392fc9709a3 | 167 | virtual void lock(void); |
AnnaBridge | 189:f392fc9709a3 | 168 | |
AnnaBridge | 189:f392fc9709a3 | 169 | /** Release exclusive access to this I2C bus |
AnnaBridge | 189:f392fc9709a3 | 170 | */ |
AnnaBridge | 189:f392fc9709a3 | 171 | virtual void unlock(void); |
AnnaBridge | 189:f392fc9709a3 | 172 | |
AnnaBridge | 189:f392fc9709a3 | 173 | virtual ~I2C() |
AnnaBridge | 189:f392fc9709a3 | 174 | { |
AnnaBridge | 189:f392fc9709a3 | 175 | // Do nothing |
AnnaBridge | 189:f392fc9709a3 | 176 | } |
AnnaBridge | 189:f392fc9709a3 | 177 | |
AnnaBridge | 189:f392fc9709a3 | 178 | #if DEVICE_I2C_ASYNCH |
AnnaBridge | 189:f392fc9709a3 | 179 | |
AnnaBridge | 189:f392fc9709a3 | 180 | /** Start nonblocking I2C transfer. |
AnnaBridge | 189:f392fc9709a3 | 181 | * |
AnnaBridge | 189:f392fc9709a3 | 182 | * This function locks the deep sleep until any event has occurred |
AnnaBridge | 189:f392fc9709a3 | 183 | * |
AnnaBridge | 189:f392fc9709a3 | 184 | * @param address 8/10 bit I2C slave address |
AnnaBridge | 189:f392fc9709a3 | 185 | * @param tx_buffer The TX buffer with data to be transferred |
AnnaBridge | 189:f392fc9709a3 | 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 |
AnnaBridge | 189:f392fc9709a3 | 188 | * @param rx_length The length of RX buffer in bytes |
AnnaBridge | 189:f392fc9709a3 | 189 | * @param event The logical OR of events to modify |
AnnaBridge | 189:f392fc9709a3 | 190 | * @param callback The event callback function |
AnnaBridge | 189:f392fc9709a3 | 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 |
AnnaBridge | 189:f392fc9709a3 | 195 | */ |
AnnaBridge | 189:f392fc9709a3 | 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); |
AnnaBridge | 189:f392fc9709a3 | 197 | |
AnnaBridge | 189:f392fc9709a3 | 198 | /** Abort the ongoing I2C transfer |
AnnaBridge | 189:f392fc9709a3 | 199 | */ |
AnnaBridge | 189:f392fc9709a3 | 200 | void abort_transfer(); |
AnnaBridge | 189:f392fc9709a3 | 201 | |
AnnaBridge | 189:f392fc9709a3 | 202 | #if !defined(DOXYGEN_ONLY) |
AnnaBridge | 189:f392fc9709a3 | 203 | protected: |
AnnaBridge | 189:f392fc9709a3 | 204 | /** Lock deep sleep only if it is not yet locked */ |
AnnaBridge | 189:f392fc9709a3 | 205 | void lock_deep_sleep(); |
AnnaBridge | 189:f392fc9709a3 | 206 | |
AnnaBridge | 189:f392fc9709a3 | 207 | /** Unlock deep sleep only if it has been locked */ |
AnnaBridge | 189:f392fc9709a3 | 208 | void unlock_deep_sleep(); |
AnnaBridge | 189:f392fc9709a3 | 209 | |
AnnaBridge | 189:f392fc9709a3 | 210 | void irq_handler_asynch(void); |
AnnaBridge | 189:f392fc9709a3 | 211 | event_callback_t _callback; |
AnnaBridge | 189:f392fc9709a3 | 212 | CThunk<I2C> _irq; |
AnnaBridge | 189:f392fc9709a3 | 213 | DMAUsage _usage; |
AnnaBridge | 189:f392fc9709a3 | 214 | bool _deep_sleep_locked; |
AnnaBridge | 189:f392fc9709a3 | 215 | #endif |
AnnaBridge | 189:f392fc9709a3 | 216 | #endif |
AnnaBridge | 189:f392fc9709a3 | 217 | |
AnnaBridge | 189:f392fc9709a3 | 218 | #if !defined(DOXYGEN_ONLY) |
AnnaBridge | 189:f392fc9709a3 | 219 | protected: |
AnnaBridge | 189:f392fc9709a3 | 220 | void aquire(); |
AnnaBridge | 189:f392fc9709a3 | 221 | |
AnnaBridge | 189:f392fc9709a3 | 222 | i2c_t _i2c; |
AnnaBridge | 189:f392fc9709a3 | 223 | static I2C *_owner; |
AnnaBridge | 189:f392fc9709a3 | 224 | int _hz; |
AnnaBridge | 189:f392fc9709a3 | 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 |
AnnaBridge | 189:f392fc9709a3 | 243 | }; |
AnnaBridge | 189:f392fc9709a3 | 244 | |
AnnaBridge | 189:f392fc9709a3 | 245 | } // namespace mbed |
AnnaBridge | 189:f392fc9709a3 | 246 | |
AnnaBridge | 189:f392fc9709a3 | 247 | #endif |
AnnaBridge | 189:f392fc9709a3 | 248 | |
AnnaBridge | 189:f392fc9709a3 | 249 | #endif |