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.
34 * @brief API to Inter-Integrated Circuit
36 * An I2c object represents an i2c master and can talk multiple i2c slaves by
37 * selecting the correct address
38 * @htmlinclude i2c.txt
40 * @snippet I2c-compass.cpp Interesting
46 * Instantiates an i2c bus. Multiple instances of the same bus can
47 * exist and the bus is not guarranteed to be on the correct address
50 * @param bus The i2c bus to use
51 * @param raw Whether to disable pinmapper for your board
53 I2c(int bus, bool raw = false)
56 m_i2c = mraa_i2c_init_raw(bus);
58 m_i2c = mraa_i2c_init(bus);
61 throw std::invalid_argument("Invalid i2c bus");
66 * Closes the I2c Bus used. This does not guarrantee the bus will not
67 * be usable by anyone else or communicates this disconnect to any
76 * Sets the i2c Frequency for communication. Your board may not support
77 * the set frequency. Anyone can change this at any time and this will
78 * affect every slave on the bus
80 * @param mode Frequency to set the bus to
81 * @return Result of operation
84 frequency(mraa_i2c_mode_t mode)
86 return mraa_i2c_frequency(m_i2c, mode);
90 * Set the slave to talk to, typically called before every read/write
93 * @param address Communicate to the i2c slave on this address
94 * @return Result of operation
97 address(uint8_t address)
99 return mraa_i2c_address(m_i2c, address);
103 * Read exactly one byte from the bus
105 * @return char read from the bus
110 return (uint8_t) mraa_i2c_read_byte(m_i2c);
114 * Read length bytes from the bus into *data pointer
116 * @param data Data to read into
117 * @param length Size of read in bytes to make
118 * @return length of read, should match length
121 read(uint8_t* data, int length)
123 return mraa_i2c_read(m_i2c, data, length);
127 * Read byte from an i2c register
129 * @param reg Register to read from
130 * @return char read from register
135 return mraa_i2c_read_byte_data(m_i2c, reg);
139 * Read word from an i2c register
141 * @param reg Register to read from
142 * @return char read from register
145 readWordReg(uint8_t reg)
147 return mraa_i2c_read_word_data(m_i2c, reg);
151 * Read length bytes from the bus into *data pointer starting from
154 * @param reg Register to read from
155 * @param data pointer to the byte array to read data in to
156 * @param length max number of bytes to read
157 * @return length passed to the function or 0
160 readBytesReg(uint8_t reg, uint8_t* data, int length)
162 return mraa_i2c_read_bytes_data(m_i2c, reg, data, length);
166 * Write a byte on the bus
168 * @param data The byte to send on the bus
169 * @return Result of operation
172 writeByte(uint8_t data)
174 return mraa_i2c_write_byte(m_i2c, data);
178 * Write length bytes to the bus, the first byte in the array is the
179 * command/register to write
181 * @param data Buffer to send on the bus, first byte is i2c command
182 * @param length Size of buffer to send
183 * @return Result of operation
186 write(const uint8_t* data, int length)
188 return mraa_i2c_write(m_i2c, data, length);
192 * Write a byte to an i2c register
194 * @param reg Register to write to
195 * @param data Value to write to register
196 * @return Result of operation
199 writeReg(uint8_t reg, uint8_t data)
201 return mraa_i2c_write_byte_data(m_i2c, data, reg);
205 * Write a word to an i2c register
207 * @param reg Register to write to
208 * @param data Value to write to register
209 * @return Result of operation
212 writeWordReg(uint8_t reg, uint16_t data)
214 return mraa_i2c_write_word_data(m_i2c, data, reg);
218 mraa_i2c_context m_i2c;