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.
32 * @brief C++ API to Inter-Integrated Circuit
34 * This file defines the I2c C++ interface for libmraa
36 * @snippet I2c-compass.cpp Interesting
41 * Instantiates an i2c bus. Multiple instances of the same bus can
42 * exist and the bus is not guarranteed to be on the correct address
45 * @param bus The i2c bus to use
46 * @param raw Whether to disable pinmapper for your board
48 I2c(int bus, bool raw=false) {
50 m_i2c = mraa_i2c_init_raw(bus);
52 m_i2c = mraa_i2c_init(bus);
55 * Closes the I2c Bus used. This does not guarrantee the bus will not
56 * be usable by anyone else or communicates this disconnect to any
63 * Sets the i2c Frequency for communication. Your board may not support
64 * the set frequency. Anyone can change this at any time and this will
65 * affect every slave on the bus
67 * @param hz Frequency to set the bus to in hz
68 * @return Result of operation
70 mraa_result_t frequency(int hz) {
71 return mraa_i2c_frequency(m_i2c, hz);
74 * Set the slave to talk to, typically called before every read/write
77 * @param address Communicate to the i2c slave on this address
78 * @return Result of operation
80 mraa_result_t address(int address) {
81 return mraa_i2c_address(m_i2c, address);
84 * Read exactly one byte from the bus
86 * @return Char read from the bus
88 unsigned char readByte() {
89 return (unsigned char) mraa_i2c_read_byte(m_i2c);
92 * Read mutliple bytes from the bus
94 * @param data Buffer to write into
95 * @param length Size of read
96 * @return length of the read or 0 if failed
98 int read(unsigned char * data, int length) {
99 return mraa_i2c_read(m_i2c, data, length);
102 * Write one byte to the bus
104 * @param data Buffer to send on the bus
105 * @param length Size of buffer to send
106 * @return Result of operation
108 mraa_result_t write(const unsigned char* data, int length) {
109 return mraa_i2c_write(m_i2c, data, length);
113 * Write to an i2c register
115 * @param reg Register to write to
116 * @param data Value to write to register
117 * @return Result of operation
119 mraa_result_t writeReg(const unsigned char reg, const unsigned char data) {
120 const unsigned char buf[2] = {reg, data};
121 return mraa_i2c_write(m_i2c, buf, 2);
125 * Write multiple bytes to the bus
127 * @param data The byte to send on the bus
128 * @return Result of operation
130 mraa_result_t write(const unsigned char data) {
131 return mraa_i2c_write_byte(m_i2c, data);
134 mraa_i2c_context m_i2c;