gpio: gpio in/out both work
[contrib/mraa.git] / api / i2c.h
1 /*
2  * Author: Brendan Le Foll
3  *
4  * Copyright © 2014 Intel Corporation
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining
7  * a copy of this software and associated documentation files (the
8  * "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish,
10  * distribute, sublicense, and/or sell copies of the Software, and to
11  * permit persons to whom the Software is furnished to do so, subject to
12  * the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be
15  * included in all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24  */
25
26 #pragma once
27
28 #include <stdio.h>
29 #include <fcntl.h>
30
31 #include "smbus.hpp"
32
33 namespace maa {
34
35 /** An I2C Master, used for communicating with I2C slave devices
36  *
37  * Example:
38  * @code
39  * // Read from I2C slave at address 0x62
40  *
41  * #include "maa.h"
42  *
43  * I2C i2c(p28, p27);
44  *
45  * int main() {
46  *     int address = 0x62;
47  *     char data[2];
48  *     i2c.read(address, data, 2);
49  * }
50  * @endcode
51  */
52 class I2C {
53
54 public:
55     enum RxStatus {
56         NoData,
57         MasterGeneralCall,
58         MasterWrite,
59         MasterRead
60     };
61
62     enum Acknowledge {
63         NoACK = 0,
64         ACK   = 1
65     };
66
67     /** Create an I2C Master interface, connected to the specified pins
68      *
69      *  @param sda I2C data line pin
70      *  @param scl I2C clock line pin
71      */
72     I2C(unsigned int sda, unsigned int scl);
73
74     /** Set the frequency of the I2C interface
75      *
76      *  @param hz The bus frequency in hertz
77      */
78     void frequency(int hz);
79
80     /** Read from an I2C slave
81      *
82      * Performs a complete read transaction. The bottom bit of
83      * the address is forced to 1 to indicate a read.
84      *
85      *  @param address 8-bit I2C slave address [ addr | 1 ]
86      *  @param data Pointer to the byte-array to read data in to
87      *  @param length Number of bytes to read
88      *  @param repeated Repeated start, true - don't send stop at end
89      *
90      *  @returns
91      *       0 on success (ack),
92      *   non-0 on failure (nack)
93      */
94     int read(int address, char *data, int length, bool repeated = false);
95
96     /** Read a single byte from the I2C bus
97      *
98      *  @param ack indicates if the byte is to be acknowledged (1 = acknowledge)
99      *
100      *  @returns
101      *    the byte read
102      */
103     int read(int ack);
104
105     /** Write to an I2C slave
106      *
107      * Performs a complete write transaction. The bottom bit of
108      * the address is forced to 0 to indicate a write.
109      *
110      *  @param address 8-bit I2C slave address [ addr | 0 ]
111      *  @param data Pointer to the byte-array data to send
112      *  @param length Number of bytes to send
113      *  @param repeated Repeated start, true - do not send stop at end
114      *
115      *  @returns
116      *       0 on success (ack),
117      *   non-0 on failure (nack)
118      */
119     int write(int address, const char *data, int length, bool repeated = false);
120
121     /** Write single byte out on the I2C bus
122      *  @param data data to write out on bus
123      *
124      *  @returns
125      *    '1' if an ACK was received,
126      *    '0' otherwise
127      */
128     int write(int data);
129
130     /** Creates a start condition on the I2C bus
131      */
132     void start(void);
133
134     /** Creates a stop condition on the I2C bus
135      */
136     void stop(void);
137
138 protected:
139     void aquire();
140     int _hz;
141     int i2c_handle;
142 };
143 }