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