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.
27 #include "mraa_internal.h"
30 mraa_i2c_init(int bus)
32 int checked_pin = mraa_setup_i2c(bus);
33 if (checked_pin < 0) {
36 fprintf(stderr, "No i2c on board\n");
39 fprintf(stderr, "Failed to set-up i2c multiplexer!\n");
42 fprintf(stderr, "Platform Not Initialised\n");
47 return mraa_i2c_init_raw((unsigned int) checked_pin);
51 mraa_i2c_init_raw(unsigned int bus)
53 if (advance_func->i2c_init_pre != NULL) {
54 if (advance_func->i2c_init_pre(bus) != MRAA_SUCCESS)
57 mraa_i2c_context dev = (mraa_i2c_context) malloc(sizeof(struct _i2c));
62 snprintf(filepath, 32, "/dev/i2c-%u", bus);
63 if ((dev->fh = open(filepath, O_RDWR)) < 1) {
64 fprintf(stderr, "Failed to open requested i2c port %s\n", filepath);
67 if (advance_func->i2c_init_post != NULL) {
68 mraa_result_t ret = advance_func->i2c_init_post(dev);
69 if (ret != MRAA_SUCCESS) {
78 mraa_i2c_frequency(mraa_i2c_context dev, int hz)
86 mraa_i2c_read(mraa_i2c_context dev, uint8_t* data, int length)
88 // this is the read(3) syscall not mraa_i2c_read()
89 if (read(dev->fh, data, length) == length) {
96 mraa_i2c_read_byte(mraa_i2c_context dev)
98 uint8_t byte = i2c_smbus_read_byte(dev->fh);
106 mraa_i2c_write(mraa_i2c_context dev, const uint8_t* data, int length)
108 if (i2c_smbus_write_i2c_block_data(dev->fh, data[0], length-1, (uint8_t*) data+1) < 0) {
109 fprintf(stderr, "Failed to write to i2c\n");
110 return MRAA_ERROR_INVALID_HANDLE;
116 mraa_i2c_write_byte(mraa_i2c_context dev, const uint8_t data)
118 if (i2c_smbus_write_byte(dev->fh, data) < 0) {
119 fprintf(stderr, "Failed to write to i2c\n");
120 return MRAA_ERROR_INVALID_HANDLE;
126 mraa_i2c_address(mraa_i2c_context dev, int addr)
129 if (ioctl(dev->fh, I2C_SLAVE_FORCE, addr) < 0) {
130 fprintf(stderr, "Failed to set slave address %d\n", addr);
131 return MRAA_ERROR_INVALID_HANDLE;
137 mraa_i2c_stop(mraa_i2c_context dev)