2 * Author: Brendan Le Foll <brendan.le.foll@intel.com>
3 * Copyright (c) 2014 Intel Corporation.
5 * Code is modified from the RoadNarrows-robotics i2c library (distributed under
6 * the MIT license, license is included verbatim under src/i2c/LICENSE)
8 * Permission is hereby granted, free of charge, to any person obtaining
9 * a copy of this software and associated documentation files (the
10 * "Software"), to deal in the Software without restriction, including
11 * without limitation the rights to use, copy, modify, merge, publish,
12 * distribute, sublicense, and/or sell copies of the Software, and to
13 * permit persons to whom the Software is furnished to do so, subject to
14 * the following conditions:
16 * The above copyright notice and this permission notice shall be
17 * included in all copies or substantial portions of the Software.
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
23 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
24 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
25 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 #include "mraa_internal.h"
35 #include <sys/types.h>
36 #include <sys/errno.h>
37 #include <sys/ioctl.h>
38 #include "linux/i2c-dev.h"
40 typedef union i2c_smbus_data_union
42 uint8_t byte; ///< data byte
43 unsigned short word; ///< data short word
44 uint8_t block[I2C_SMBUS_BLOCK_MAX + 2];
45 ///< block[0] is used for length and one more for PEC
48 typedef struct i2c_smbus_ioctl_data_struct
50 uint8_t read_write; ///< operation direction
51 uint8_t command; ///< ioctl command
52 int size; ///< data size
53 i2c_smbus_data_t *data; ///< data
54 } i2c_smbus_ioctl_data_t;
57 mraa_i2c_smbus_access(int fh, uint8_t read_write, uint8_t command, int size,
58 i2c_smbus_data_t *data)
60 i2c_smbus_ioctl_data_t args;
62 args.read_write = read_write;
63 args.command = command;
67 return ioctl(fh, I2C_SMBUS, &args);
71 mraa_i2c_init(int bus)
74 syslog(LOG_ERR, "i2c: Platform Not Initialised");
77 if (plat->i2c_bus_count == 0) {
78 syslog(LOG_ERR, "No i2c buses defined in platform");
81 if (bus >= plat->i2c_bus_count) {
82 syslog(LOG_ERR, "Above i2c bus count");
86 if (plat->i2c_bus[bus].bus_id == -1) {
87 syslog(LOG_ERR, "Invalid i2c bus, moving to default i2c bus");
88 bus = plat->def_i2c_bus;
91 int pos = plat->i2c_bus[bus].sda;
92 if (plat->pins[pos].i2c.mux_total > 0) {
93 if (mraa_setup_mux_mapped(plat->pins[pos].i2c) != MRAA_SUCCESS) {
94 syslog(LOG_ERR, "i2c: Failed to set-up i2c sda multiplexer");
99 pos = plat->i2c_bus[bus].scl;
100 if (plat->pins[pos].i2c.mux_total > 0) {
101 if (mraa_setup_mux_mapped(plat->pins[pos].i2c) != MRAA_SUCCESS) {
102 syslog(LOG_ERR, "i2c: Failed to set-up i2c scl multiplexer");
107 return mraa_i2c_init_raw((unsigned int) plat->i2c_bus[bus].bus_id);
111 mraa_i2c_init_raw(unsigned int bus)
113 if (advance_func->i2c_init_pre != NULL) {
114 if (advance_func->i2c_init_pre(bus) != MRAA_SUCCESS)
117 mraa_i2c_context dev = (mraa_i2c_context) malloc(sizeof(struct _i2c));
119 syslog(LOG_CRIT, "i2c: Failed to allocate memory for context");
125 snprintf(filepath, 32, "/dev/i2c-%u", bus);
126 if ((dev->fh = open(filepath, O_RDWR)) < 1) {
127 syslog(LOG_ERR, "i2c: Failed to open requested i2c port %s", filepath);
130 if (ioctl(dev->fh, I2C_FUNCS, &dev->funcs) < 0) {
131 syslog(LOG_CRIT, "i2c: Failed to get I2C_FUNC map from device");
135 if (advance_func->i2c_init_post != NULL) {
136 mraa_result_t ret = advance_func->i2c_init_post(dev);
137 if (ret != MRAA_SUCCESS) {
146 mraa_i2c_frequency(mraa_i2c_context dev, mraa_i2c_mode_t mode)
148 if (advance_func->i2c_set_frequency_replace != NULL) {
149 return advance_func->i2c_set_frequency_replace(dev, mode);
151 return MRAA_ERROR_FEATURE_NOT_SUPPORTED;
155 mraa_i2c_read(mraa_i2c_context dev, uint8_t* data, int length)
157 // this is the read(3) syscall not mraa_i2c_read()
158 if (read(dev->fh, data, length) == length) {
165 mraa_i2c_read_byte(mraa_i2c_context dev)
169 if (mraa_i2c_smbus_access(dev->fh, I2C_SMBUS_READ, I2C_NOCMD,
170 I2C_SMBUS_BYTE, &d) < 0) {
171 syslog(LOG_ERR, "i2c: Failed to write");
174 return 0x0FF & d.byte;
178 mraa_i2c_read_byte_data(mraa_i2c_context dev, uint8_t command)
182 if (mraa_i2c_smbus_access(dev->fh, I2C_SMBUS_READ, command,
183 I2C_SMBUS_BYTE_DATA, &d) < 0) {
184 syslog(LOG_ERR, "i2c: Failed to write");
187 return 0x0FF & d.byte;
191 mraa_i2c_read_word_data(mraa_i2c_context dev, uint8_t command)
195 if (mraa_i2c_smbus_access(dev->fh, I2C_SMBUS_READ, command,
196 I2C_SMBUS_WORD_DATA, &d) < 0) {
197 syslog(LOG_ERR, "i2c: Failed to write");
200 return 0xFFFF & d.word;
204 mraa_i2c_read_bytes_data(mraa_i2c_context dev, uint8_t command, uint8_t* data, int length)
206 struct i2c_rdwr_ioctl_data d;
209 m[0].addr = dev->addr;
210 m[0].flags = I2C_M_RD;
213 m[1].addr = dev->addr;
221 return ioctl(dev->fh, I2C_RDWR, &d) < 0 ? -1 : length;
225 mraa_i2c_write(mraa_i2c_context dev, const uint8_t* data, int length)
229 uint8_t command = data[0];
233 if (length > I2C_SMBUS_I2C_BLOCK_MAX) {
234 length = I2C_SMBUS_I2C_BLOCK_MAX;
237 for (i=1; i<=length; i++) {
238 d.block[i] = data[i-1];
242 return mraa_i2c_smbus_access(dev->fh, I2C_SMBUS_WRITE, command,
243 I2C_SMBUS_I2C_BLOCK_DATA, &d);
247 mraa_i2c_write_byte(mraa_i2c_context dev, const uint8_t data)
249 if (mraa_i2c_smbus_access(dev->fh, I2C_SMBUS_WRITE, data,
250 I2C_SMBUS_BYTE, NULL) < 0) {
251 syslog(LOG_ERR, "i2c: Failed to write");
252 return MRAA_ERROR_INVALID_HANDLE;
258 mraa_i2c_write_byte_data(mraa_i2c_context dev, const uint8_t data,
259 const uint8_t command)
264 if (mraa_i2c_smbus_access(dev->fh, I2C_SMBUS_WRITE, command,
265 I2C_SMBUS_BYTE_DATA, &d) < 0) {
266 syslog(LOG_ERR, "i2c: Failed to write");
267 return MRAA_ERROR_INVALID_HANDLE;
273 mraa_i2c_write_word_data(mraa_i2c_context dev, const uint16_t data,
274 const uint8_t command)
279 if (mraa_i2c_smbus_access(dev->fh, I2C_SMBUS_WRITE, command,
280 I2C_SMBUS_WORD_DATA, &d) < 0) {
281 syslog(LOG_ERR, "i2c: Failed to write");
282 return MRAA_ERROR_INVALID_HANDLE;
288 mraa_i2c_address(mraa_i2c_context dev, uint8_t addr)
290 dev->addr = (int) addr;
291 if (ioctl(dev->fh, I2C_SLAVE_FORCE, addr) < 0) {
292 syslog(LOG_ERR, "i2c: Failed to set slave address %d", addr);
293 return MRAA_ERROR_INVALID_HANDLE;
299 mraa_i2c_stop(mraa_i2c_context dev)