i2c: flip the msg flags for mraa_i2c_read_bytes_data
[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  * Code is modified from the RoadNarrows-robotics i2c library (distributed under
6  * the MIT license, license is included verbatim under src/i2c/LICENSE)
7  *
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:
15  *
16  * The above copyright notice and this permission notice shall be
17  * included in all copies or substantial portions of the Software.
18  *
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.
26  */
27
28 #include "i2c.h"
29 #include "mraa_internal.h"
30
31 #include <stdlib.h>
32 #include <unistd.h>
33 #include <fcntl.h>
34 #include <inttypes.h>
35 #include <sys/types.h>
36 #include <sys/errno.h>
37 #include <sys/ioctl.h>
38 #include "linux/i2c-dev.h"
39
40 typedef union i2c_smbus_data_union
41 {
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
46 } i2c_smbus_data_t;
47
48 typedef struct i2c_smbus_ioctl_data_struct
49 {
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;
55
56 int
57 mraa_i2c_smbus_access(int fh, uint8_t read_write, uint8_t command, int size,
58         i2c_smbus_data_t *data)
59 {
60   i2c_smbus_ioctl_data_t args;
61
62   args.read_write = read_write;
63   args.command = command;
64   args.size = size;
65   args.data = data;
66
67   return ioctl(fh, I2C_SMBUS, &args);
68 }
69
70 mraa_i2c_context
71 mraa_i2c_init(int bus)
72 {
73     if (plat == NULL) {
74         syslog(LOG_ERR, "i2c: Platform Not Initialised");
75         return NULL;
76     }
77     if (plat->i2c_bus_count == 0) {
78         syslog(LOG_ERR, "No i2c buses defined in platform");
79         return NULL;
80     }
81     if (bus >= plat->i2c_bus_count) {
82         syslog(LOG_ERR, "Above i2c bus count");
83         return NULL;
84     }
85
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;
89     }
90
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");
95             return NULL;
96         }
97     }
98
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");
103             return NULL;
104         }
105     }
106
107     return mraa_i2c_init_raw((unsigned int) plat->i2c_bus[bus].bus_id);
108 }
109
110 mraa_i2c_context
111 mraa_i2c_init_raw(unsigned int bus)
112 {
113     if (advance_func->i2c_init_pre != NULL) {
114         if (advance_func->i2c_init_pre(bus) != MRAA_SUCCESS)
115             return NULL;
116     }
117     mraa_i2c_context dev = (mraa_i2c_context) malloc(sizeof(struct _i2c));
118     if (dev == NULL) {
119         syslog(LOG_CRIT, "i2c: Failed to allocate memory for context");
120         return NULL;
121     }
122
123     char filepath[32];
124     dev->busnum = bus;
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);
128     }
129
130     if (ioctl(dev->fh, I2C_FUNCS, &dev->funcs) < 0) {
131         syslog(LOG_CRIT, "i2c: Failed to get I2C_FUNC map from device");
132         dev->funcs = 0;
133     }
134
135     if (advance_func->i2c_init_post != NULL) {
136         mraa_result_t ret = advance_func->i2c_init_post(dev);
137         if (ret != MRAA_SUCCESS) {
138             free(dev);
139             return NULL;
140         }
141     }
142     return dev;
143 }
144
145 mraa_result_t
146 mraa_i2c_frequency(mraa_i2c_context dev, mraa_i2c_mode_t mode)
147 {
148     if (advance_func->i2c_set_frequency_replace != NULL) {
149         return advance_func->i2c_set_frequency_replace(dev, mode);
150     }
151     return MRAA_ERROR_FEATURE_NOT_SUPPORTED;
152 }
153
154 int
155 mraa_i2c_read(mraa_i2c_context dev, uint8_t* data, int length)
156 {
157     // this is the read(3) syscall not mraa_i2c_read()
158     if (read(dev->fh, data, length) == length) {
159         return length;
160     }
161     return 0;
162 }
163
164 uint8_t
165 mraa_i2c_read_byte(mraa_i2c_context dev)
166 {
167     i2c_smbus_data_t d;
168
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");
172         return 0;
173     }
174     return 0x0FF & d.byte;
175 }
176
177 uint8_t
178 mraa_i2c_read_byte_data(mraa_i2c_context dev, uint8_t command)
179 {
180     i2c_smbus_data_t d;
181
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");
185         return 0;
186     }
187     return 0x0FF & d.byte;
188 }
189
190 uint16_t
191 mraa_i2c_read_word_data(mraa_i2c_context dev, uint8_t command)
192 {
193     i2c_smbus_data_t d;
194
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");
198         return 0;
199     }
200     return 0xFFFF & d.word;
201 }
202
203 int
204 mraa_i2c_read_bytes_data(mraa_i2c_context dev, uint8_t command, uint8_t* data, int length)
205 {
206     struct i2c_rdwr_ioctl_data d;
207     struct i2c_msg m[2];
208
209     m[0].addr = dev->addr;
210     m[0].flags = 0x00;
211     m[0].len = 1;
212     m[0].buf = &command;
213     m[1].addr = dev->addr;
214     m[1].flags = I2C_M_RD;
215     m[1].len = length;
216     m[1].buf = data;
217
218     d.msgs = m;
219     d.nmsgs = 2;
220
221     return ioctl(dev->fh, I2C_RDWR, &d) < 0 ? -1 : length;
222 }
223
224 mraa_result_t
225 mraa_i2c_write(mraa_i2c_context dev, const uint8_t* data, int length)
226 {
227     i2c_smbus_data_t d;
228     int i;
229     uint8_t command = data[0];
230
231     data = &data[1];
232     length = length-1;
233     if (length > I2C_SMBUS_I2C_BLOCK_MAX) {
234         length = I2C_SMBUS_I2C_BLOCK_MAX;
235     }
236
237     for (i=1; i<=length; i++) {
238         d.block[i] = data[i-1];
239     }
240     d.block[0] = length;
241
242     return mraa_i2c_smbus_access(dev->fh, I2C_SMBUS_WRITE, command,
243             I2C_SMBUS_I2C_BLOCK_DATA, &d);
244 }
245
246 mraa_result_t
247 mraa_i2c_write_byte(mraa_i2c_context dev, const uint8_t data)
248 {
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;
253     }
254     return MRAA_SUCCESS;
255 }
256
257 mraa_result_t
258 mraa_i2c_write_byte_data(mraa_i2c_context dev, const uint8_t data,
259         const uint8_t command)
260 {
261     i2c_smbus_data_t d;
262
263     d.byte = data;
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;
268     }
269     return MRAA_SUCCESS;
270 }
271
272 mraa_result_t
273 mraa_i2c_write_word_data(mraa_i2c_context dev, const uint16_t data,
274         const uint8_t command)
275 {
276     i2c_smbus_data_t d;
277
278     d.word = data;
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;
283     }
284     return MRAA_SUCCESS;
285 }
286
287 mraa_result_t
288 mraa_i2c_address(mraa_i2c_context dev, uint8_t addr)
289 {
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;
294     }
295     return MRAA_SUCCESS;
296 }
297
298 mraa_result_t
299 mraa_i2c_stop(mraa_i2c_context dev)
300 {
301     free(dev);
302     return MRAA_SUCCESS;
303 }