7a0724b01a0017cd8cf7920daa2da1db9c334328
[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 -1:
36                 fprintf(stderr, "No i2c on board\n");
37                 return NULL;
38             case -2:
39                 fprintf(stderr, "Failed to set-up i2c multiplexer!\n");
40                 return NULL;
41             case -3:
42                 fprintf(stderr, "Platform Not Initialised\n");
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         return NULL;
60
61     char filepath[32];
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);
65     }
66
67     if (advance_func->i2c_init_post != NULL) {
68         mraa_result_t ret = advance_func->i2c_init_post(dev);
69         if (ret != MRAA_SUCCESS) {
70             free(dev);
71             return NULL;
72         }
73     }
74     return dev;
75 }
76
77 mraa_result_t
78 mraa_i2c_frequency(mraa_i2c_context dev, int hz)
79 {
80     dev->hz = hz;
81
82     return MRAA_SUCCESS;
83 }
84
85 int
86 mraa_i2c_read(mraa_i2c_context dev, uint8_t* data, int length)
87 {
88     // this is the read(3) syscall not mraa_i2c_read()
89     if (read(dev->fh, data, length) == length) {
90         return length;
91     }
92     return 0;
93 }
94
95 uint8_t
96 mraa_i2c_read_byte(mraa_i2c_context dev)
97 {
98     uint8_t byte = i2c_smbus_read_byte(dev->fh);
99     if (byte < 0) {
100         return -1;
101     }
102     return byte;
103 }
104
105 mraa_result_t
106 mraa_i2c_write(mraa_i2c_context dev, const uint8_t* data, int length)
107 {
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;
111     }
112     return MRAA_SUCCESS;
113 }
114
115 mraa_result_t
116 mraa_i2c_write_byte(mraa_i2c_context dev, const uint8_t data)
117 {
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;
121     }
122     return MRAA_SUCCESS;
123 }
124
125 mraa_result_t
126 mraa_i2c_address(mraa_i2c_context dev, int addr)
127 {
128     dev->addr = 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;
132     }
133     return MRAA_SUCCESS;
134 }
135
136 mraa_result_t
137 mraa_i2c_stop(mraa_i2c_context dev)
138 {
139     free(dev);
140     return MRAA_SUCCESS;
141 }