mbed library sources. Supersedes mbed-src.

Fork of mbed-dev by mbed official

Committer:
AnnaBridge
Date:
Wed Jun 21 17:46:44 2017 +0100
Revision:
167:e84263d55307
Parent:
161:2cc1468da177
Child:
168:9672193075cf
This updates the lib to the mbed lib v 145

Who changed what in which revision?

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