examples: Created mraa-i2c tool for i2c layer testing.
[contrib/mraa.git] / examples / mraa-i2c.c
1 /*
2  * Author: Henry Bruce <henry.bruce@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 <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <unistd.h>
29 #include "mraa/i2c.h"
30
31 #include "mraa_internal_types.h"
32
33 extern mraa_board_t* plat;
34
35 void
36 print_version()
37 {
38     fprintf(stdout, "Version %s on %s", mraa_get_version(), mraa_get_platform_name());
39     if (plat != NULL && plat->sub_platform != NULL)
40         fprintf(stdout, " with %s", plat->sub_platform->platform_name);
41     fprintf(stdout, "\n");
42 }
43
44 void
45 print_help()
46 {
47     fprintf(stdout, "version                   Get mraa version and board name\n");
48     fprintf(stdout, "list                      List available busses\n");
49     fprintf(stdout, "detect bus                List detected devices on specified bus\n");
50     fprintf(stdout, "get bus device reg        Get value from specified device register\n");
51     fprintf(stdout, "set bus device reg value  Set specified device register to value\n");
52 }
53
54 void
55 print_command_error()
56 {
57     fprintf(stdout, "Invalid command, options are:\n");
58     print_help();
59 }
60
61
62
63 void
64 print_bus(mraa_board_t* board)
65 {
66     int bus_index;
67     int bus;
68     for (bus = 0; bus < board->i2c_bus_count; ++bus) {
69         char* busType;
70         switch (mraa_get_platform_type()) {
71         case MRAA_INTEL_GALILEO_GEN1:
72         case MRAA_INTEL_GALILEO_GEN2:
73         case MRAA_INTEL_EDISON_FAB_C:
74         case MRAA_INTEL_DE3815:
75         case MRAA_INTEL_MINNOWBOARD_MAX:
76         case MRAA_RASPBERRY_PI:
77         case MRAA_BEAGLEBONE:
78         case MRAA_BANANA:
79             bus_index = bus;
80             busType = "stdapi";
81             break;
82         case MRAA_FTDI_FT4222:
83             busType = "ft4222";
84             bus_index = MRAA_USE_SUB_PLATFORM(bus);
85             break;
86         default:
87             busType = "unknown";
88             break;
89         }
90         fprintf(stdout, "Bus %2d: id=%02d type=%s ", bus_index, plat->i2c_bus[bus].bus_id, busType);
91         if (bus == plat->def_i2c_bus)
92             fprintf(stdout, " default", bus);
93         fprintf(stdout, "\n", bus);
94     }
95 }
96
97
98 void
99 print_busses()
100 {
101    print_bus(plat);
102    if (plat->sub_platform != NULL)
103       print_bus(plat->sub_platform);
104 }
105
106
107
108 mraa_result_t
109 i2c_get(int bus, uint8_t device_address, uint8_t register_address, uint8_t* data)
110 {
111     mraa_result_t status = MRAA_SUCCESS;
112     mraa_i2c_context i2c = mraa_i2c_init(bus);
113     if (i2c == NULL) {
114         status = MRAA_ERROR_NO_RESOURCES;
115         // fprintf(stdout, "Could not initialize i2c\n");
116         goto i2c_get_exit;
117     }
118     status = mraa_i2c_address(i2c, device_address);
119     if (status != MRAA_SUCCESS) {
120         // fprintf(stdout, "Could not set i2c device address\n");
121         goto i2c_get_exit;
122     }
123     status = mraa_i2c_write_byte(i2c, register_address);
124     if (status != MRAA_SUCCESS) {
125         // fprintf(stdout, "Could not set i2c register address. Status = %d\n", status);
126         goto i2c_get_exit;
127     }
128     status = mraa_i2c_read(i2c, data, 1) == 1 ? MRAA_SUCCESS : MRAA_ERROR_UNSPECIFIED;
129     if (status != MRAA_SUCCESS) {
130         // fprintf(stdout, "i2c read failed\n");
131        goto i2c_get_exit;
132     }
133 i2c_get_exit:
134     if (i2c != NULL)
135     mraa_i2c_stop(i2c);
136     return status;
137 }
138
139
140 mraa_result_t
141 i2c_set(int bus, uint8_t device_address, uint8_t register_address, uint8_t data)
142 {
143     mraa_result_t status = MRAA_SUCCESS;
144     mraa_i2c_context i2c = mraa_i2c_init(bus);
145     if (i2c == NULL) {
146         status = MRAA_ERROR_NO_RESOURCES;
147         fprintf(stdout, "Could not initialize i2c\n");
148         goto i2c_set_exit;
149     }
150     status = mraa_i2c_address(i2c, device_address);
151     if (status != MRAA_SUCCESS) {
152         fprintf(stdout, "Could not set i2c device address\n");
153         goto i2c_set_exit;
154     }
155     status = mraa_i2c_write_byte_data(i2c, data, register_address);
156     if (status != MRAA_SUCCESS) {
157         fprintf(stdout, "Could not write to i2c register. Status = %d\n", status);
158         goto i2c_set_exit;
159     }
160 /*
161     status = mraa_i2c_write_byte(i2c, data);
162     if (status != MRAA_SUCCESS) {
163         fprintf(stdout, "Could not set value. Status = %d\n", status);
164         goto i2c_set_exit;
165   }
166 */
167 i2c_set_exit:
168     if (i2c != NULL)
169     mraa_i2c_stop(i2c);
170     return status;
171 }
172
173
174 void i2c_detect_devices(int bus)
175 {
176     mraa_result_t status = MRAA_SUCCESS;
177     mraa_i2c_context i2c = mraa_i2c_init(bus);
178     int addr;
179         for (addr=0x0; addr < 0x80; ++addr) {
180                 uint8_t value;
181                 if ((addr) % 16 == 0)
182                    printf("%02x: ", addr);
183                 if (i2c_get(bus, addr, 0, &value) == MRAA_SUCCESS)
184                         printf("%02x ", addr);
185                 else
186                         printf("-- ", addr);
187                 if ((addr + 1) % 16 == 0)
188                    printf("\n");
189         }
190 }
191
192
193
194 int
195 main(int argc, char** argv)
196 {
197     mraa_set_log_level(7);
198     if (argc == 1) {
199         print_command_error();
200     }
201
202     if (argc > 1) {
203         if (strcmp(argv[1], "help") == 0) {
204             print_help();
205         } else if (strcmp(argv[1], "version") == 0) {
206             print_version();
207         } else if (strcmp(argv[1], "list") == 0) {
208             print_busses();
209         } else if (strcmp(argv[1], "detect") == 0) {
210             if (argc == 3) {
211                 int bus = strtol(argv[2], NULL, 0);
212                 i2c_detect_devices(bus);
213             } else {
214                 print_command_error();
215             }
216         } else if ((strcmp(argv[1], "get") == 0)) {
217             if (argc == 5) {
218                 int bus = strtol(argv[2], NULL, 0);
219                 uint8_t device_address = strtol(argv[3], NULL, 0);
220                 uint8_t register_address = strtol(argv[4], NULL, 0);
221                 // fprintf(stdout, "Device %02X, Register = %02X\n", device_address, register_address);
222                 uint8_t data;
223                 if (i2c_get(bus, device_address, register_address, &data) == MRAA_SUCCESS) {
224                     fprintf(stdout, "Register %#02X = %#02X\n", register_address, data);
225                 } else {
226                     fprintf(stdout, "i2c get failed\n");
227                 }
228             } else {
229                 print_command_error();
230             }
231         } else if ((strcmp(argv[1], "set") == 0)) {
232             if (argc == 6) {
233                 int bus = strtol(argv[2], NULL, 0);
234                 uint8_t device_address = strtol(argv[3], NULL, 0);
235                 uint8_t register_address = strtol(argv[4], NULL, 0);
236                 uint8_t value = strtol(argv[5], NULL, 0);
237                 fprintf(stdout, "Device %02X, Register = %02X, Value = %02X\n", device_address, register_address, value);
238                 if (i2c_set(bus, device_address, register_address, value) != MRAA_SUCCESS) {
239                     fprintf(stdout, "i2c set failed\n");
240                 }
241             } else {
242                 print_command_error();
243             }
244         }
245
246     }
247     return 0;
248 }
249