test

Dependents:   robotic_fish_7

Committer:
juansal12
Date:
Tue Jan 14 19:14:29 2020 +0000
Revision:
0:44931ff4a3ed
Sofi 7 code;

Who changed what in which revision?

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