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