mbed library sources. Supersedes mbed-src.

Dependents:   Nucleo_Hello_Encoder BLE_iBeaconScan AM1805_DEMO DISCO-F429ZI_ExportTemplate1 ... more

Committer:
AnnaBridge
Date:
Fri Feb 16 16:09:33 2018 +0000
Revision:
181:57724642e740
Parent:
180:96ed750bd169
Child:
184:08ed48f1de7f
mbed-dev library. Release version 159.

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"
AnnaBridge 168:9672193075cf 26 #include "platform/NonCopyable.h"
<> 149:156823d33999 27
<> 149:156823d33999 28 #if DEVICE_I2C_ASYNCH
<> 149:156823d33999 29 #include "platform/CThunk.h"
<> 149:156823d33999 30 #include "hal/dma_api.h"
<> 149:156823d33999 31 #include "platform/FunctionPointer.h"
<> 149:156823d33999 32 #endif
<> 149:156823d33999 33
<> 149:156823d33999 34 namespace mbed {
<> 149:156823d33999 35 /** \addtogroup drivers */
<> 149:156823d33999 36
<> 149:156823d33999 37 /** An I2C Master, used for communicating with I2C slave devices
<> 149:156823d33999 38 *
AnnaBridge 167:e84263d55307 39 * @note Synchronization level: Thread safe
<> 149:156823d33999 40 *
<> 149:156823d33999 41 * Example:
<> 149:156823d33999 42 * @code
<> 149:156823d33999 43 * // Read from I2C slave at address 0x62
<> 149:156823d33999 44 *
<> 149:156823d33999 45 * #include "mbed.h"
<> 149:156823d33999 46 *
<> 149:156823d33999 47 * I2C i2c(p28, p27);
<> 149:156823d33999 48 *
<> 149:156823d33999 49 * int main() {
<> 149:156823d33999 50 * int address = 0x62;
<> 149:156823d33999 51 * char data[2];
<> 149:156823d33999 52 * i2c.read(address, data, 2);
<> 149:156823d33999 53 * }
<> 149:156823d33999 54 * @endcode
AnnaBridge 167:e84263d55307 55 * @ingroup drivers
<> 149:156823d33999 56 */
AnnaBridge 168:9672193075cf 57 class I2C : private NonCopyable<I2C> {
<> 149:156823d33999 58
<> 149:156823d33999 59 public:
<> 149:156823d33999 60 enum RxStatus {
<> 149:156823d33999 61 NoData,
<> 149:156823d33999 62 MasterGeneralCall,
<> 149:156823d33999 63 MasterWrite,
<> 149:156823d33999 64 MasterRead
<> 149:156823d33999 65 };
<> 149:156823d33999 66
<> 149:156823d33999 67 enum Acknowledge {
<> 149:156823d33999 68 NoACK = 0,
<> 149:156823d33999 69 ACK = 1
<> 149:156823d33999 70 };
<> 149:156823d33999 71
<> 149:156823d33999 72 /** Create an I2C Master interface, connected to the specified pins
<> 149:156823d33999 73 *
<> 149:156823d33999 74 * @param sda I2C data line pin
<> 149:156823d33999 75 * @param scl I2C clock line pin
<> 149:156823d33999 76 */
<> 149:156823d33999 77 I2C(PinName sda, PinName scl);
<> 149:156823d33999 78
<> 149:156823d33999 79 /** Set the frequency of the I2C interface
<> 149:156823d33999 80 *
<> 149:156823d33999 81 * @param hz The bus frequency in hertz
<> 149:156823d33999 82 */
<> 149:156823d33999 83 void frequency(int hz);
<> 149:156823d33999 84
<> 149:156823d33999 85 /** Read from an I2C slave
<> 149:156823d33999 86 *
<> 149:156823d33999 87 * Performs a complete read transaction. The bottom bit of
<> 149:156823d33999 88 * the address is forced to 1 to indicate a read.
<> 149:156823d33999 89 *
<> 149:156823d33999 90 * @param address 8-bit I2C slave address [ addr | 1 ]
<> 149:156823d33999 91 * @param data Pointer to the byte-array to read data in to
<> 149:156823d33999 92 * @param length Number of bytes to read
<> 149:156823d33999 93 * @param repeated Repeated start, true - don't send stop at end
<> 149:156823d33999 94 *
<> 149:156823d33999 95 * @returns
<> 149:156823d33999 96 * 0 on success (ack),
<> 149:156823d33999 97 * non-0 on failure (nack)
<> 149:156823d33999 98 */
<> 149:156823d33999 99 int read(int address, char *data, int length, bool repeated = false);
<> 149:156823d33999 100
<> 149:156823d33999 101 /** Read a single byte from the I2C bus
<> 149:156823d33999 102 *
<> 149:156823d33999 103 * @param ack indicates if the byte is to be acknowledged (1 = acknowledge)
<> 149:156823d33999 104 *
<> 149:156823d33999 105 * @returns
<> 149:156823d33999 106 * the byte read
<> 149:156823d33999 107 */
<> 149:156823d33999 108 int read(int ack);
<> 149:156823d33999 109
<> 149:156823d33999 110 /** Write to an I2C slave
<> 149:156823d33999 111 *
<> 149:156823d33999 112 * Performs a complete write transaction. The bottom bit of
<> 149:156823d33999 113 * the address is forced to 0 to indicate a write.
<> 149:156823d33999 114 *
<> 149:156823d33999 115 * @param address 8-bit I2C slave address [ addr | 0 ]
<> 149:156823d33999 116 * @param data Pointer to the byte-array data to send
<> 149:156823d33999 117 * @param length Number of bytes to send
<> 149:156823d33999 118 * @param repeated Repeated start, true - do not send stop at end
<> 149:156823d33999 119 *
<> 149:156823d33999 120 * @returns
<> 161:2cc1468da177 121 * 0 on success (ack),
<> 161:2cc1468da177 122 * non-0 on failure (nack)
<> 149:156823d33999 123 */
<> 149:156823d33999 124 int write(int address, const char *data, int length, bool repeated = false);
<> 149:156823d33999 125
<> 149:156823d33999 126 /** Write single byte out on the I2C bus
<> 149:156823d33999 127 *
<> 149:156823d33999 128 * @param data data to write out on bus
<> 149:156823d33999 129 *
<> 149:156823d33999 130 * @returns
<> 153:fa9ff456f731 131 * '0' - NAK was received
<> 153:fa9ff456f731 132 * '1' - ACK was received,
<> 153:fa9ff456f731 133 * '2' - timeout
<> 149:156823d33999 134 */
<> 149:156823d33999 135 int write(int data);
<> 149:156823d33999 136
<> 149:156823d33999 137 /** Creates a start condition on the I2C bus
<> 149:156823d33999 138 */
<> 149:156823d33999 139
<> 149:156823d33999 140 void start(void);
<> 149:156823d33999 141
<> 149:156823d33999 142 /** Creates a stop condition on the I2C bus
<> 149:156823d33999 143 */
<> 149:156823d33999 144 void stop(void);
<> 149:156823d33999 145
<> 149:156823d33999 146 /** Acquire exclusive access to this I2C bus
<> 149:156823d33999 147 */
<> 149:156823d33999 148 virtual void lock(void);
<> 149:156823d33999 149
<> 149:156823d33999 150 /** Release exclusive access to this I2C bus
<> 149:156823d33999 151 */
<> 149:156823d33999 152 virtual void unlock(void);
<> 149:156823d33999 153
<> 149:156823d33999 154 virtual ~I2C() {
<> 149:156823d33999 155 // Do nothing
<> 149:156823d33999 156 }
<> 149:156823d33999 157
<> 149:156823d33999 158 #if DEVICE_I2C_ASYNCH
<> 149:156823d33999 159
<> 149:156823d33999 160 /** Start non-blocking I2C transfer.
<> 149:156823d33999 161 *
AnnaBridge 174:b96e65c34a4d 162 * This function locks the deep sleep until any event has occured
AnnaBridge 174:b96e65c34a4d 163 *
<> 149:156823d33999 164 * @param address 8/10 bit I2c slave address
<> 149:156823d33999 165 * @param tx_buffer The TX buffer with data to be transfered
<> 149:156823d33999 166 * @param tx_length The length of TX buffer in bytes
<> 149:156823d33999 167 * @param rx_buffer The RX buffer which is used for received data
<> 149:156823d33999 168 * @param rx_length The length of RX buffer in bytes
<> 149:156823d33999 169 * @param event The logical OR of events to modify
<> 149:156823d33999 170 * @param callback The event callback function
<> 149:156823d33999 171 * @param repeated Repeated start, true - do not send stop at end
<> 149:156823d33999 172 * @return Zero if the transfer has started, or -1 if I2C peripheral is busy
<> 149:156823d33999 173 */
<> 149:156823d33999 174 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 175
<> 149:156823d33999 176 /** Abort the on-going I2C transfer
<> 149:156823d33999 177 */
<> 149:156823d33999 178 void abort_transfer();
Anna Bridge 180:96ed750bd169 179
Anna Bridge 180:96ed750bd169 180 protected:
Anna Bridge 180:96ed750bd169 181 /** Lock deep sleep only if it is not yet locked */
Anna Bridge 180:96ed750bd169 182 void lock_deep_sleep();
Anna Bridge 180:96ed750bd169 183
Anna Bridge 180:96ed750bd169 184 /** Unlock deep sleep only if it has been locked */
Anna Bridge 180:96ed750bd169 185 void unlock_deep_sleep();
Anna Bridge 180:96ed750bd169 186
<> 149:156823d33999 187 void irq_handler_asynch(void);
<> 149:156823d33999 188 event_callback_t _callback;
<> 149:156823d33999 189 CThunk<I2C> _irq;
<> 149:156823d33999 190 DMAUsage _usage;
Anna Bridge 180:96ed750bd169 191 bool _deep_sleep_locked;
<> 149:156823d33999 192 #endif
<> 149:156823d33999 193
<> 149:156823d33999 194 protected:
<> 149:156823d33999 195 void aquire();
<> 149:156823d33999 196
<> 149:156823d33999 197 i2c_t _i2c;
<> 149:156823d33999 198 static I2C *_owner;
<> 149:156823d33999 199 int _hz;
<> 149:156823d33999 200 static SingletonPtr<PlatformMutex> _mutex;
<> 149:156823d33999 201 };
<> 149:156823d33999 202
<> 149:156823d33999 203 } // namespace mbed
<> 149:156823d33999 204
<> 149:156823d33999 205 #endif
<> 149:156823d33999 206
<> 149:156823d33999 207 #endif