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