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