this hurts

Dependencies:   FFT

Committer:
annieluo2
Date:
Wed Dec 02 18:02:03 2020 +0000
Revision:
0:d6c9b09b4042
boo

Who changed what in which revision?

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