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