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