2 * Author: Brendan Le Foll <brendan.le.foll@intel.com>
3 * Copyright (c) 2014 Intel Corporation.
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 * @brief API to Inter-Integrated Circuit
35 * An I2c object represents an i2c master and can talk multiple i2c slaves by
36 * selecting the correct address
37 * @htmlinclude i2c.txt
39 * @snippet I2c-compass.cpp Interesting
44 * Instantiates an i2c bus. Multiple instances of the same bus can
45 * exist and the bus is not guarranteed to be on the correct address
48 * @param bus The i2c bus to use
49 * @param raw Whether to disable pinmapper for your board
51 I2c(int bus, bool raw=false) {
53 m_i2c = mraa_i2c_init_raw(bus);
56 m_i2c = mraa_i2c_init(bus);
59 throw std::invalid_argument("Invalid i2c bus");
64 * Closes the I2c Bus used. This does not guarrantee the bus will not
65 * be usable by anyone else or communicates this disconnect to any
73 * Sets the i2c Frequency for communication. Your board may not support
74 * the set frequency. Anyone can change this at any time and this will
75 * affect every slave on the bus
77 * @param mode Frequency to set the bus to
78 * @return Result of operation
80 mraa_result_t frequency(mraa_i2c_mode_t mode) {
81 return mraa_i2c_frequency(m_i2c, mode);
85 * Set the slave to talk to, typically called before every read/write
88 * @param address Communicate to the i2c slave on this address
89 * @return Result of operation
91 mraa_result_t address(uint8_t address) {
92 return mraa_i2c_address(m_i2c, address);
96 * Read exactly one byte from the bus
98 * @return char read from the bus
101 return (uint8_t) mraa_i2c_read_byte(m_i2c);
105 * Read length bytes from the bus into *data pointer
107 * @param data Data to read into
108 * @param length Size of read in bytes to make
109 * @return length of read, should match length
111 int read(uint8_t *data, int length) {
112 return mraa_i2c_read(m_i2c, data, length);
116 * Read byte from an i2c register
118 * @param reg Register to read from
119 * @return char read from register
121 uint8_t readReg(uint8_t reg) {
122 return mraa_i2c_read_byte_data(m_i2c, reg);
126 * Read word from an i2c register
128 * @param reg Register to read from
129 * @return char read from register
131 uint16_t readWordReg(uint8_t reg) {
132 return mraa_i2c_read_word_data(m_i2c, reg);
136 * Read length bytes from the bus into *data pointer starting from
139 * @param reg Register to read from
140 * @param data pointer to the byte array to read data in to
141 * @param length max number of bytes to read
142 * @return length passed to the function or 0
144 int readBytesReg(uint8_t reg, uint8_t* data, int length) {
145 return mraa_i2c_read_bytes_data(m_i2c, reg, data, length);
149 * Write a byte on the bus
151 * @param data The byte to send on the bus
152 * @return Result of operation
154 mraa_result_t writeByte(uint8_t data) {
155 return mraa_i2c_write_byte(m_i2c, data);
159 * Write length bytes to the bus, the first byte in the array is the
160 * command/register to write
162 * @param data Buffer to send on the bus, first byte is i2c command
163 * @param length Size of buffer to send
164 * @return Result of operation
166 mraa_result_t write(const uint8_t* data, int length) {
167 return mraa_i2c_write(m_i2c, data, length);
171 * Write a byte to an i2c register
173 * @param reg Register to write to
174 * @param data Value to write to register
175 * @return Result of operation
177 mraa_result_t writeReg(uint8_t reg, uint8_t data) {
178 return mraa_i2c_write_byte_data(m_i2c, data, reg);
182 * Write a word to an i2c register
184 * @param reg Register to write to
185 * @param data Value to write to register
186 * @return Result of operation
188 mraa_result_t writeWordReg(uint8_t reg, uint16_t data) {
189 return mraa_i2c_write_word_data(m_i2c, data, reg);
192 mraa_i2c_context m_i2c;