Backup 1

Committer:
borlanic
Date:
Tue Apr 24 11:45:18 2018 +0000
Revision:
0:02dd72d1d465
BaBoRo_test2 - backup 1

Who changed what in which revision?

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