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");
98 pos = plat->i2c_bus[bus].scl;
99 if (plat->pins[pos].i2c.mux_total > 0)
100 if (mraa_setup_mux_mapped(plat->pins[pos].i2c) != MRAA_SUCCESS) {
101 syslog(LOG_ERR, "i2c: Failed to set-up i2c scl multiplexer");
105 return mraa_i2c_init_raw((unsigned int) plat->i2c_bus[bus].bus_id);
109 mraa_i2c_init_raw(unsigned int bus)
111 if (advance_func->i2c_init_pre != NULL) {
112 if (advance_func->i2c_init_pre(bus) != MRAA_SUCCESS)
115 mraa_i2c_context dev = (mraa_i2c_context) malloc(sizeof(struct _i2c));
117 syslog(LOG_CRIT, "i2c: Failed to allocate memory for context");
123 snprintf(filepath, 32, "/dev/i2c-%u", bus);
124 if ((dev->fh = open(filepath, O_RDWR)) < 1) {
125 syslog(LOG_ERR, "i2c: Failed to open requested i2c port %s", filepath);
128 if (advance_func->i2c_init_post != NULL) {
129 mraa_result_t ret = advance_func->i2c_init_post(dev);
130 if (ret != MRAA_SUCCESS) {
139 mraa_i2c_frequency(mraa_i2c_context dev, mraa_i2c_mode_t mode)
141 return MRAA_ERROR_FEATURE_NOT_SUPPORTED;
145 mraa_i2c_read(mraa_i2c_context dev, uint8_t* data, int length)
147 // this is the read(3) syscall not mraa_i2c_read()
148 if (read(dev->fh, data, length) == length) {
155 mraa_i2c_read_byte(mraa_i2c_context dev)
159 if (mraa_i2c_smbus_access(dev->fh, I2C_SMBUS_READ, I2C_NOCMD,
160 I2C_SMBUS_BYTE, &d) < 0) {
161 syslog(LOG_ERR, "i2c: Failed to write");
164 return 0x0FF & d.byte;
168 mraa_i2c_read_byte_data(mraa_i2c_context dev, uint8_t command)
172 if (mraa_i2c_smbus_access(dev->fh, I2C_SMBUS_READ, command,
173 I2C_SMBUS_BYTE_DATA, &d) < 0) {
174 syslog(LOG_ERR, "i2c: Failed to write");
177 return 0x0FF & d.byte;
181 mraa_i2c_read_word_data(mraa_i2c_context dev, uint8_t command)
185 if (mraa_i2c_smbus_access(dev->fh, I2C_SMBUS_READ, command,
186 I2C_SMBUS_WORD_DATA, &d) < 0) {
187 syslog(LOG_ERR, "i2c: Failed to write");
190 return 0xFFFF & d.word;
194 mraa_i2c_write(mraa_i2c_context dev, const uint8_t* data, int length)
198 uint8_t command = data[0];
202 if (length > I2C_SMBUS_I2C_BLOCK_MAX) {
203 length = I2C_SMBUS_I2C_BLOCK_MAX;
206 for (i=1; i<=length; i++) {
207 d.block[i] = data[i-1];
211 return mraa_i2c_smbus_access(dev->fh, I2C_SMBUS_WRITE, command,
212 I2C_SMBUS_I2C_BLOCK_DATA, &d);
216 mraa_i2c_write_byte(mraa_i2c_context dev, const uint8_t data)
218 if (mraa_i2c_smbus_access(dev->fh, I2C_SMBUS_WRITE, data,
219 I2C_SMBUS_BYTE, NULL) < 0) {
220 syslog(LOG_ERR, "i2c: Failed to write");
221 return MRAA_ERROR_INVALID_HANDLE;
227 mraa_i2c_write_byte_data(mraa_i2c_context dev, const uint8_t data,
228 const uint8_t command)
233 if (mraa_i2c_smbus_access(dev->fh, I2C_SMBUS_WRITE, command,
234 I2C_SMBUS_BYTE_DATA, &d) < 0) {
235 syslog(LOG_ERR, "i2c: Failed to write");
236 return MRAA_ERROR_INVALID_HANDLE;
242 mraa_i2c_write_word_data(mraa_i2c_context dev, const uint16_t data,
243 const uint8_t command)
248 if (mraa_i2c_smbus_access(dev->fh, I2C_SMBUS_WRITE, command,
249 I2C_SMBUS_WORD_DATA, &d) < 0) {
250 syslog(LOG_ERR, "i2c: Failed to write");
251 return MRAA_ERROR_INVALID_HANDLE;
257 mraa_i2c_address(mraa_i2c_context dev, uint8_t addr)
259 dev->addr = (int) addr;
260 if (ioctl(dev->fh, I2C_SLAVE_FORCE, addr) < 0) {
261 syslog(LOG_ERR, "i2c: Failed to set slave address %d", addr);
262 return MRAA_ERROR_INVALID_HANDLE;
268 mraa_i2c_stop(mraa_i2c_context dev)