i2c: use default i2c bus in board config if invalid
[contrib/mraa.git] / src / i2c / i2c.c
1 /*
2  * Author: Brendan Le Foll <brendan.le.foll@intel.com>
3  * Copyright (c) 2014 Intel Corporation.
4  *
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:
12  *
13  * The above copyright notice and this permission notice shall be
14  * included in all copies or substantial portions of the Software.
15  *
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.
23  */
24
25 #include "i2c.h"
26 #include "smbus.h"
27 #include "mraa_internal.h"
28
29 mraa_i2c_context
30 mraa_i2c_init(int bus)
31 {
32     int checked_pin = mraa_setup_i2c(&bus);
33     if (checked_pin < 0) {
34         switch(checked_pin) {
35             case MRAA_NO_SUCH_IO:
36                 syslog(LOG_ERR, "i2c: No i2c on board");
37                 return NULL;
38             case MRAA_IO_SETUP_FAILURE:
39                 syslog(LOG_ERR, "i2c: Failed to set-up i2c multiplexer");
40                 return NULL;
41             case MRAA_PLATFORM_NO_INIT:
42                 syslog(LOG_ERR, "i2c: Platform Not Initialised");
43                 return NULL;
44             default: return NULL;
45         }
46     }
47     return mraa_i2c_init_raw((unsigned int) checked_pin);
48 }
49
50 mraa_i2c_context
51 mraa_i2c_init_raw(unsigned int bus)
52 {
53     if (advance_func->i2c_init_pre != NULL) {
54         if (advance_func->i2c_init_pre(bus) != MRAA_SUCCESS)
55             return NULL;
56     }
57     mraa_i2c_context dev = (mraa_i2c_context) malloc(sizeof(struct _i2c));
58     if (dev == NULL) {
59         syslog(LOG_CRIT, "i2c: Failed to allocate memory for context");
60         return NULL;
61     }
62
63     char filepath[32];
64     snprintf(filepath, 32, "/dev/i2c-%u", bus);
65     if ((dev->fh = open(filepath, O_RDWR)) < 1) {
66         syslog(LOG_ERR, "i2c: Failed to open requested i2c port %s", filepath);
67     }
68
69     if (advance_func->i2c_init_post != NULL) {
70         mraa_result_t ret = advance_func->i2c_init_post(dev);
71         if (ret != MRAA_SUCCESS) {
72             free(dev);
73             return NULL;
74         }
75     }
76     return dev;
77 }
78
79 mraa_result_t
80 mraa_i2c_frequency(mraa_i2c_context dev, int hz)
81 {
82     dev->hz = hz;
83
84     return MRAA_ERROR_FEATURE_NOT_SUPPORTED;
85 }
86
87 int
88 mraa_i2c_read(mraa_i2c_context dev, uint8_t* data, int length)
89 {
90     // this is the read(3) syscall not mraa_i2c_read()
91     if (read(dev->fh, data, length) == length) {
92         return length;
93     }
94     return 0;
95 }
96
97 uint8_t
98 mraa_i2c_read_byte(mraa_i2c_context dev)
99 {
100     uint8_t byte = i2c_smbus_read_byte(dev->fh);
101     return byte;
102 }
103
104 mraa_result_t
105 mraa_i2c_write(mraa_i2c_context dev, const uint8_t* data, int length)
106 {
107     if (i2c_smbus_write_i2c_block_data(dev->fh, data[0], length-1, (uint8_t*) data+1) < 0) {
108         syslog(LOG_ERR, "i2c: Failed to write");
109         return MRAA_ERROR_INVALID_HANDLE;
110     }
111     return MRAA_SUCCESS;
112 }
113
114 mraa_result_t
115 mraa_i2c_write_byte(mraa_i2c_context dev, const uint8_t data)
116 {
117     if (i2c_smbus_write_byte(dev->fh, data) < 0) {
118         syslog(LOG_ERR, "i2c: Failed to write");
119         return MRAA_ERROR_INVALID_HANDLE;
120     }
121     return MRAA_SUCCESS;
122 }
123
124 mraa_result_t
125 mraa_i2c_address(mraa_i2c_context dev, int addr)
126 {
127     dev->addr = addr;
128     if (ioctl(dev->fh, I2C_SLAVE_FORCE, addr) < 0) {
129         syslog(LOG_ERR, "i2c: Failed to set slave address %d", addr);
130         return MRAA_ERROR_INVALID_HANDLE;
131     }
132     return MRAA_SUCCESS;
133 }
134
135 mraa_result_t
136 mraa_i2c_stop(mraa_i2c_context dev)
137 {
138     free(dev);
139     return MRAA_SUCCESS;
140 }