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