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