test

Dependents:   robotic_fish_6

Committer:
juansal12
Date:
Fri Dec 03 23:00:34 2021 +0000
Revision:
0:c792b17d9f78
uploaded sofi code ;

Who changed what in which revision?

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