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.
29 * @brief Inter-Integrated Circuit
31 * An i2c context represents a master on an i2c bus and that context can
32 * communicate to multiple i2c slaves by configuring the address.
33 * @htmlinclude i2c.txt
35 * @snippet i2c_HMC5883L.c Interesting
51 * Opaque pointer definition to the internal struct _i2c
53 typedef struct _i2c* mraa_i2c_context;
56 * Initialise i2c context, using board defintions
58 * @param bus i2c bus to use
59 * @return i2c context or NULL
61 mraa_i2c_context mraa_i2c_init(int bus);
64 * Initialise i2c context, passing in the i2c bus to use.
66 * @param bus The i2c bus to use i.e. /dev/i2c-2 would be "2"
67 * @return i2c context or NULL
69 mraa_i2c_context mraa_i2c_init_raw(unsigned int bus);
72 * Sets the frequency of the i2c context. Most platforms do not support this.
74 * @param dev The i2c context
75 * @param mode The bus mode
76 * @return Result of operation
78 mraa_result_t mraa_i2c_frequency(mraa_i2c_context dev, mraa_i2c_mode_t mode);
81 * Simple bulk read from an i2c context, this will always begin with the i2c
84 * @param dev The i2c context
85 * @param data pointer to the byte array to read data in to
86 * @param length max number of bytes to read
87 * @return length of the read in bytes or 0
89 int mraa_i2c_read(mraa_i2c_context dev, uint8_t* data, int length);
92 * Simple read for a single byte from the i2c context, this will always begin
93 * with the i2c offset 0x0
95 * @param dev The i2c context
96 * @return The result of the read or -1 if failed
98 uint8_t mraa_i2c_read_byte(mraa_i2c_context dev);
101 * Read a single byte from i2c context, from designated register
103 * @param dev The i2c context
104 * @param command The register
105 * @return The result of the read or -1 if failed
107 uint8_t mraa_i2c_read_byte_data(mraa_i2c_context dev, const uint8_t command);
110 * Read a single word from i2c context, from designated register
112 * @param dev The i2c context
113 * @param command The register
114 * @return The result of the read or -1 if failed
116 uint16_t mraa_i2c_read_word_data(mraa_i2c_context dev, const uint8_t command);
119 * Bulk read from i2c context, starting from designated register
121 * @param dev The i2c context
122 * @param command The register
123 * @param data pointer to the byte array to read data in to
124 * @param length max number of bytes to read
125 * @return The length in bytes passed to the function or 0
127 int mraa_i2c_read_bytes_data(mraa_i2c_context dev, uint8_t command, uint8_t* data, int length);
130 * Write length bytes to the bus, the first byte in the array is the
131 * command/register to write
133 * @param dev The i2c context
134 * @param data pointer to the byte array to be written
135 * @param length the number of bytes to transmit
136 * @return Result of operation
138 mraa_result_t mraa_i2c_write(mraa_i2c_context dev, const uint8_t* data, int length);
141 * Write a single byte to an i2c context, always at offset 0x0
143 * @param dev The i2c context
144 * @param data The byte to write
145 * @return Result of operation
147 mraa_result_t mraa_i2c_write_byte(mraa_i2c_context dev, const uint8_t data);
150 * Write a single byte to an i2c context
152 * @param dev The i2c context
153 * @param data The byte to write
154 * @param command The register
155 * @return Result of operation
157 mraa_result_t mraa_i2c_write_byte_data(mraa_i2c_context dev, const uint8_t data, const uint8_t command);
160 * Write a single word to an i2c context
162 * @param dev The i2c context
163 * @param data The word to write
164 * @param command The register
165 * @return Result of operation
167 mraa_result_t mraa_i2c_write_word_data(mraa_i2c_context dev, const uint16_t data, const uint8_t command);
170 * Sets the i2c context address.
172 * @param dev The i2c context
173 * @param address The address to set for the slave (ignoring the least
174 * signifcant bit). If set to 0, the slave will only respond to the
175 * general call address.
176 * @return Result of operation
178 mraa_result_t mraa_i2c_address(mraa_i2c_context dev, uint8_t address);
181 * De-inits an mraa_i2c_context device
183 * @param dev The i2c context
184 * @return Result of operation
186 mraa_result_t mraa_i2c_stop(mraa_i2c_context dev);