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