Erste version der Software für der Prototyp

Committer:
borlanic
Date:
Fri Mar 30 14:07:05 2018 +0000
Revision:
4:75df35ef4fb6
Parent:
0:380207fcb5c1
commentar

Who changed what in which revision?

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