mbed library sources. Supersedes mbed-src.

Fork of mbed-dev by mbed official

Committer:
bogdanm
Date:
Thu Oct 01 15:25:22 2015 +0300
Revision:
0:9b334a45a8ff
Initial commit on mbed-dev

Replaces mbed-src (now inactive)

Who changed what in which revision?

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