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