]> git.donarmstrong.com Git - qmk_firmware.git/blob - tmk_core/tool/mbed/mbed-sdk/libraries/mbed/api/I2C.h
Merge commit '4d116a04e94cf0d19317d5b44e4fa9f34a3e5594'
[qmk_firmware.git] / tmk_core / tool / mbed / mbed-sdk / libraries / mbed / api / I2C.h
1 /* mbed Microcontroller Library
2  * Copyright (c) 2006-2013 ARM Limited
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #ifndef MBED_I2C_H
17 #define MBED_I2C_H
18
19 #include "platform.h"
20
21 #if DEVICE_I2C
22
23 #include "i2c_api.h"
24
25 namespace mbed {
26
27 /** An I2C Master, used for communicating with I2C slave devices
28  *
29  * Example:
30  * @code
31  * // Read from I2C slave at address 0x62
32  *
33  * #include "mbed.h"
34  *
35  * I2C i2c(p28, p27);
36  *
37  * int main() {
38  *     int address = 0x62;
39  *     char data[2];
40  *     i2c.read(address, data, 2);
41  * }
42  * @endcode
43  */
44 class I2C {
45
46 public:
47     enum RxStatus {
48         NoData,
49         MasterGeneralCall,
50         MasterWrite,
51         MasterRead
52     };
53
54     enum Acknowledge {
55         NoACK = 0,
56         ACK   = 1
57     };
58
59     /** Create an I2C Master interface, connected to the specified pins
60      *
61      *  @param sda I2C data line pin
62      *  @param scl I2C clock line pin
63      */
64     I2C(PinName sda, PinName scl);
65
66     /** Set the frequency of the I2C interface
67      *
68      *  @param hz The bus frequency in hertz
69      */
70     void frequency(int hz);
71
72     /** Read from an I2C slave
73      *
74      * Performs a complete read transaction. The bottom bit of
75      * the address is forced to 1 to indicate a read.
76      *
77      *  @param address 8-bit I2C slave address [ addr | 1 ]
78      *  @param data Pointer to the byte-array to read data in to
79      *  @param length Number of bytes to read
80      *  @param repeated Repeated start, true - don't send stop at end
81      *
82      *  @returns
83      *       0 on success (ack),
84      *   non-0 on failure (nack)
85      */
86     int read(int address, char *data, int length, bool repeated = false);
87
88     /** Read a single byte from the I2C bus
89      *
90      *  @param ack indicates if the byte is to be acknowledged (1 = acknowledge)
91      *
92      *  @returns
93      *    the byte read
94      */
95     int read(int ack);
96
97     /** Write to an I2C slave
98      *
99      * Performs a complete write transaction. The bottom bit of
100      * the address is forced to 0 to indicate a write.
101      *
102      *  @param address 8-bit I2C slave address [ addr | 0 ]
103      *  @param data Pointer to the byte-array data to send
104      *  @param length Number of bytes to send
105      *  @param repeated Repeated start, true - do not send stop at end
106      *
107      *  @returns
108      *       0 on success (ack),
109      *   non-0 on failure (nack)
110      */
111     int write(int address, const char *data, int length, bool repeated = false);
112
113     /** Write single byte out on the I2C bus
114      *
115      *  @param data data to write out on bus
116      *
117      *  @returns
118      *    '1' if an ACK was received,
119      *    '0' otherwise
120      */
121     int write(int data);
122
123     /** Creates a start condition on the I2C bus
124      */
125
126     void start(void);
127
128     /** Creates a stop condition on the I2C bus
129      */
130     void stop(void);
131
132 protected:
133     void aquire();
134
135     i2c_t _i2c;
136     static I2C  *_owner;
137     int         _hz;
138 };
139
140 } // namespace mbed
141
142 #endif
143
144 #endif