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