UVic Assistive Technology Lab / Mbed 2 deprecated DSLR_Camera_Gimbal

Dependencies:   mbed ros_lib_kinetic

Committer:
group-UVic-Assistive-Technolog
Date:
Wed Jan 31 05:24:12 2018 +0000
Revision:
0:3a767f41cf04
Initial commit

Who changed what in which revision?

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