nothing changed

Fork of mbed by gokmen ascioglu

Committer:
Jing_Qiu
Date:
Tue Mar 03 00:55:57 2015 +0000
Revision:
1:27d81db81dd7
Parent:
0:a8fa94490a0a
revise

Who changed what in which revision?

UserRevisionLine numberNew contents of line
gokmenascioglu 0:a8fa94490a0a 1 /* mbed Microcontroller Library - I2C
gokmenascioglu 0:a8fa94490a0a 2 * Copyright (c) 2007-2011 ARM Limited. All rights reserved.
gokmenascioglu 0:a8fa94490a0a 3 */
gokmenascioglu 0:a8fa94490a0a 4
gokmenascioglu 0:a8fa94490a0a 5 #ifndef MBED_I2C_H
gokmenascioglu 0:a8fa94490a0a 6 #define MBED_I2C_H
gokmenascioglu 0:a8fa94490a0a 7
gokmenascioglu 0:a8fa94490a0a 8 #include "device.h"
gokmenascioglu 0:a8fa94490a0a 9
gokmenascioglu 0:a8fa94490a0a 10 #if DEVICE_I2C
gokmenascioglu 0:a8fa94490a0a 11
gokmenascioglu 0:a8fa94490a0a 12 #include "platform.h"
gokmenascioglu 0:a8fa94490a0a 13 #include "PinNames.h"
gokmenascioglu 0:a8fa94490a0a 14 #include "PeripheralNames.h"
gokmenascioglu 0:a8fa94490a0a 15 #include "Base.h"
gokmenascioglu 0:a8fa94490a0a 16
gokmenascioglu 0:a8fa94490a0a 17 namespace mbed {
gokmenascioglu 0:a8fa94490a0a 18
gokmenascioglu 0:a8fa94490a0a 19 /* Class: I2C
gokmenascioglu 0:a8fa94490a0a 20 * An I2C Master, used for communicating with I2C slave devices
gokmenascioglu 0:a8fa94490a0a 21 *
gokmenascioglu 0:a8fa94490a0a 22 * Example:
gokmenascioglu 0:a8fa94490a0a 23 * > // Read from I2C slave at address 0x62
gokmenascioglu 0:a8fa94490a0a 24 * >
gokmenascioglu 0:a8fa94490a0a 25 * > #include "mbed.h"
gokmenascioglu 0:a8fa94490a0a 26 * >
gokmenascioglu 0:a8fa94490a0a 27 * > I2C i2c(p28, p27);
gokmenascioglu 0:a8fa94490a0a 28 * >
gokmenascioglu 0:a8fa94490a0a 29 * > int main() {
gokmenascioglu 0:a8fa94490a0a 30 * > int address = 0x62;
gokmenascioglu 0:a8fa94490a0a 31 * > char data[2];
gokmenascioglu 0:a8fa94490a0a 32 * > i2c.read(address, data, 2);
gokmenascioglu 0:a8fa94490a0a 33 * > }
gokmenascioglu 0:a8fa94490a0a 34 */
gokmenascioglu 0:a8fa94490a0a 35 class I2C : public Base {
gokmenascioglu 0:a8fa94490a0a 36
gokmenascioglu 0:a8fa94490a0a 37 public:
gokmenascioglu 0:a8fa94490a0a 38
gokmenascioglu 0:a8fa94490a0a 39 enum RxStatus {
gokmenascioglu 0:a8fa94490a0a 40 NoData
gokmenascioglu 0:a8fa94490a0a 41 , MasterGeneralCall
gokmenascioglu 0:a8fa94490a0a 42 , MasterWrite
gokmenascioglu 0:a8fa94490a0a 43 , MasterRead
gokmenascioglu 0:a8fa94490a0a 44 };
gokmenascioglu 0:a8fa94490a0a 45
gokmenascioglu 0:a8fa94490a0a 46 enum Acknowledge {
gokmenascioglu 0:a8fa94490a0a 47 NoACK = 0
gokmenascioglu 0:a8fa94490a0a 48 , ACK = 1
gokmenascioglu 0:a8fa94490a0a 49 };
gokmenascioglu 0:a8fa94490a0a 50
gokmenascioglu 0:a8fa94490a0a 51 /* Constructor: I2C
gokmenascioglu 0:a8fa94490a0a 52 * Create an I2C Master interface, connected to the specified pins
gokmenascioglu 0:a8fa94490a0a 53 *
gokmenascioglu 0:a8fa94490a0a 54 * Variables:
gokmenascioglu 0:a8fa94490a0a 55 * sda - I2C data line pin
gokmenascioglu 0:a8fa94490a0a 56 * scl - I2C clock line pin
gokmenascioglu 0:a8fa94490a0a 57 */
gokmenascioglu 0:a8fa94490a0a 58 I2C(PinName sda, PinName scl, const char *name = NULL);
gokmenascioglu 0:a8fa94490a0a 59
gokmenascioglu 0:a8fa94490a0a 60 /* Function: frequency
gokmenascioglu 0:a8fa94490a0a 61 * Set the frequency of the I2C interface
gokmenascioglu 0:a8fa94490a0a 62 *
gokmenascioglu 0:a8fa94490a0a 63 * Variables:
gokmenascioglu 0:a8fa94490a0a 64 * hz - The bus frequency in hertz
gokmenascioglu 0:a8fa94490a0a 65 */
gokmenascioglu 0:a8fa94490a0a 66 void frequency(int hz);
gokmenascioglu 0:a8fa94490a0a 67
gokmenascioglu 0:a8fa94490a0a 68 /* Function: read
gokmenascioglu 0:a8fa94490a0a 69 * Read from an I2C slave
gokmenascioglu 0:a8fa94490a0a 70 *
gokmenascioglu 0:a8fa94490a0a 71 * Performs a complete read transaction. The bottom bit of
gokmenascioglu 0:a8fa94490a0a 72 * the address is forced to 1 to indicate a read.
gokmenascioglu 0:a8fa94490a0a 73 *
gokmenascioglu 0:a8fa94490a0a 74 * Variables:
gokmenascioglu 0:a8fa94490a0a 75 * address - 8-bit I2C slave address [ addr | 1 ]
gokmenascioglu 0:a8fa94490a0a 76 * data - Pointer to the byte-array to read data in to
gokmenascioglu 0:a8fa94490a0a 77 * length - Number of bytes to read
gokmenascioglu 0:a8fa94490a0a 78 * repeated - Repeated start, true - don't send stop at end
gokmenascioglu 0:a8fa94490a0a 79 * returns - 0 on success (ack), or non-0 on failure (nack)
gokmenascioglu 0:a8fa94490a0a 80 */
gokmenascioglu 0:a8fa94490a0a 81 int read(int address, char *data, int length, bool repeated = false);
gokmenascioglu 0:a8fa94490a0a 82
gokmenascioglu 0:a8fa94490a0a 83 /* Function: read
gokmenascioglu 0:a8fa94490a0a 84 * Read a single byte from the I2C bus
gokmenascioglu 0:a8fa94490a0a 85 *
gokmenascioglu 0:a8fa94490a0a 86 * Variables:
gokmenascioglu 0:a8fa94490a0a 87 * ack - indicates if the byte is to be acknowledged (1 = acknowledge)
gokmenascioglu 0:a8fa94490a0a 88 * returns - the byte read
gokmenascioglu 0:a8fa94490a0a 89 */
gokmenascioglu 0:a8fa94490a0a 90 int read(int ack);
gokmenascioglu 0:a8fa94490a0a 91
gokmenascioglu 0:a8fa94490a0a 92 /* Function: write
gokmenascioglu 0:a8fa94490a0a 93 * Write to an I2C slave
gokmenascioglu 0:a8fa94490a0a 94 *
gokmenascioglu 0:a8fa94490a0a 95 * Performs a complete write transaction. The bottom bit of
gokmenascioglu 0:a8fa94490a0a 96 * the address is forced to 0 to indicate a write.
gokmenascioglu 0:a8fa94490a0a 97 *
gokmenascioglu 0:a8fa94490a0a 98 * Variables:
gokmenascioglu 0:a8fa94490a0a 99 * address - 8-bit I2C slave address [ addr | 0 ]
gokmenascioglu 0:a8fa94490a0a 100 * data - Pointer to the byte-array data to send
gokmenascioglu 0:a8fa94490a0a 101 * length - Number of bytes to send
gokmenascioglu 0:a8fa94490a0a 102 * repeated - Repeated start, true - do not send stop at end
gokmenascioglu 0:a8fa94490a0a 103 * returns - 0 on success (ack), or non-0 on failure (nack)
gokmenascioglu 0:a8fa94490a0a 104 */
gokmenascioglu 0:a8fa94490a0a 105 int write(int address, const char *data, int length, bool repeated = false);
gokmenascioglu 0:a8fa94490a0a 106
gokmenascioglu 0:a8fa94490a0a 107 /* Function: write
gokmenascioglu 0:a8fa94490a0a 108 * Write single byte out on the I2C bus
gokmenascioglu 0:a8fa94490a0a 109 *
gokmenascioglu 0:a8fa94490a0a 110 * Variables:
gokmenascioglu 0:a8fa94490a0a 111 * data - data to write out on bus
gokmenascioglu 0:a8fa94490a0a 112 * returns - a '1' if an ACK was received, a '0' otherwise
gokmenascioglu 0:a8fa94490a0a 113 */
gokmenascioglu 0:a8fa94490a0a 114 int write(int data);
gokmenascioglu 0:a8fa94490a0a 115
gokmenascioglu 0:a8fa94490a0a 116 /* Function: start
gokmenascioglu 0:a8fa94490a0a 117 * Creates a start condition on the I2C bus
gokmenascioglu 0:a8fa94490a0a 118 */
gokmenascioglu 0:a8fa94490a0a 119
gokmenascioglu 0:a8fa94490a0a 120 void start(void);
gokmenascioglu 0:a8fa94490a0a 121
gokmenascioglu 0:a8fa94490a0a 122 /* Function: stop
gokmenascioglu 0:a8fa94490a0a 123 * Creates a stop condition on the I2C bus
gokmenascioglu 0:a8fa94490a0a 124 */
gokmenascioglu 0:a8fa94490a0a 125 void stop(void);
gokmenascioglu 0:a8fa94490a0a 126
gokmenascioglu 0:a8fa94490a0a 127 protected:
gokmenascioglu 0:a8fa94490a0a 128
gokmenascioglu 0:a8fa94490a0a 129 void aquire();
gokmenascioglu 0:a8fa94490a0a 130
gokmenascioglu 0:a8fa94490a0a 131 I2CName _i2c;
gokmenascioglu 0:a8fa94490a0a 132 static I2C *_owner;
gokmenascioglu 0:a8fa94490a0a 133 int _hz;
gokmenascioglu 0:a8fa94490a0a 134
gokmenascioglu 0:a8fa94490a0a 135 };
gokmenascioglu 0:a8fa94490a0a 136
gokmenascioglu 0:a8fa94490a0a 137 } // namespace mbed
gokmenascioglu 0:a8fa94490a0a 138
gokmenascioglu 0:a8fa94490a0a 139 #endif
gokmenascioglu 0:a8fa94490a0a 140
gokmenascioglu 0:a8fa94490a0a 141 #endif
gokmenascioglu 0:a8fa94490a0a 142