.

Dependents:   RTC

Committer:
jhon309
Date:
Thu Aug 13 00:20:09 2015 +0000
Revision:
0:88e313c910d0
RTC Example

Who changed what in which revision?

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