2 * Author: Henry Bruce <henry.bruce@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.
31 #include "mraa_internal_types.h"
33 extern mraa_board_t* plat;
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");
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");
57 fprintf(stdout, "Invalid command, options are:\n");
64 print_bus(mraa_board_t* board)
68 for (bus = 0; bus < board->i2c_bus_count; ++bus) {
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:
82 case MRAA_FTDI_FT4222:
84 bus_index = MRAA_USE_SUB_PLATFORM(bus);
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);
102 if (plat->sub_platform != NULL)
103 print_bus(plat->sub_platform);
109 i2c_get(int bus, uint8_t device_address, uint8_t register_address, uint8_t* data)
111 mraa_result_t status = MRAA_SUCCESS;
112 mraa_i2c_context i2c = mraa_i2c_init(bus);
114 status = MRAA_ERROR_NO_RESOURCES;
115 // fprintf(stdout, "Could not initialize i2c\n");
118 status = mraa_i2c_address(i2c, device_address);
119 if (status != MRAA_SUCCESS) {
120 // fprintf(stdout, "Could not set i2c device address\n");
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);
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");
141 i2c_set(int bus, uint8_t device_address, uint8_t register_address, uint8_t data)
143 mraa_result_t status = MRAA_SUCCESS;
144 mraa_i2c_context i2c = mraa_i2c_init(bus);
146 status = MRAA_ERROR_NO_RESOURCES;
147 fprintf(stdout, "Could not initialize i2c\n");
150 status = mraa_i2c_address(i2c, device_address);
151 if (status != MRAA_SUCCESS) {
152 fprintf(stdout, "Could not set i2c device address\n");
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);
161 status = mraa_i2c_write_byte(i2c, data);
162 if (status != MRAA_SUCCESS) {
163 fprintf(stdout, "Could not set value. Status = %d\n", status);
174 void i2c_detect_devices(int bus)
176 mraa_result_t status = MRAA_SUCCESS;
177 mraa_i2c_context i2c = mraa_i2c_init(bus);
179 for (addr=0x0; addr < 0x80; ++addr) {
181 if ((addr) % 16 == 0)
182 printf("%02x: ", addr);
183 if (i2c_get(bus, addr, 0, &value) == MRAA_SUCCESS)
184 printf("%02x ", addr);
187 if ((addr + 1) % 16 == 0)
195 main(int argc, char** argv)
197 mraa_set_log_level(7);
199 print_command_error();
203 if (strcmp(argv[1], "help") == 0) {
205 } else if (strcmp(argv[1], "version") == 0) {
207 } else if (strcmp(argv[1], "list") == 0) {
209 } else if (strcmp(argv[1], "detect") == 0) {
211 int bus = strtol(argv[2], NULL, 0);
212 i2c_detect_devices(bus);
214 print_command_error();
216 } else if ((strcmp(argv[1], "get") == 0)) {
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);
223 if (i2c_get(bus, device_address, register_address, &data) == MRAA_SUCCESS) {
224 fprintf(stdout, "Register %#02X = %#02X\n", register_address, data);
226 fprintf(stdout, "i2c get failed\n");
229 print_command_error();
231 } else if ((strcmp(argv[1], "set") == 0)) {
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");
242 print_command_error();