svn / mbed / trunk / I2C.h

Revision 29, 3.1 kB (checked in by emilmont, 6 months ago)

New Libraries 11.11

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