mraa-i2c: Updated to use new sub-platform API.
Signed-off-by: Henry Bruce <henry.bruce@intel.com> Signed-off-by: Brendan Le Foll <brendan.le.foll@intel.com>
This commit is contained in:
committed by
Brendan Le Foll
parent
6284806b96
commit
2101e491ce
@@ -58,8 +58,6 @@ print_command_error()
|
||||
print_help();
|
||||
}
|
||||
|
||||
|
||||
|
||||
void
|
||||
print_bus(mraa_board_t* board)
|
||||
{
|
||||
@@ -67,24 +65,24 @@ print_bus(mraa_board_t* board)
|
||||
for (i = 0; i < board->i2c_bus_count; ++i) {
|
||||
char* busType;
|
||||
switch (board->platform_type) {
|
||||
case MRAA_INTEL_GALILEO_GEN1:
|
||||
case MRAA_INTEL_GALILEO_GEN2:
|
||||
case MRAA_INTEL_EDISON_FAB_C:
|
||||
case MRAA_INTEL_DE3815:
|
||||
case MRAA_INTEL_MINNOWBOARD_MAX:
|
||||
case MRAA_RASPBERRY_PI:
|
||||
case MRAA_BEAGLEBONE:
|
||||
case MRAA_BANANA:
|
||||
bus = i;
|
||||
busType = "stdapi";
|
||||
break;
|
||||
case MRAA_FTDI_FT4222:
|
||||
busType = "ft4222";
|
||||
bus = mraa_use_sub_platform(i);
|
||||
break;
|
||||
default:
|
||||
busType = "unknown";
|
||||
break;
|
||||
case MRAA_INTEL_GALILEO_GEN1:
|
||||
case MRAA_INTEL_GALILEO_GEN2:
|
||||
case MRAA_INTEL_EDISON_FAB_C:
|
||||
case MRAA_INTEL_DE3815:
|
||||
case MRAA_INTEL_MINNOWBOARD_MAX:
|
||||
case MRAA_RASPBERRY_PI:
|
||||
case MRAA_BEAGLEBONE:
|
||||
case MRAA_BANANA:
|
||||
bus = i;
|
||||
busType = "linux";
|
||||
break;
|
||||
case MRAA_FTDI_FT4222:
|
||||
busType = "ft4222";
|
||||
bus = mraa_get_sub_platform_id(i);
|
||||
break;
|
||||
default:
|
||||
busType = "unknown";
|
||||
break;
|
||||
}
|
||||
int id = board->i2c_bus[bus].bus_id;
|
||||
fprintf(stdout, "Bus %3d: id=%02d type=%s ", bus, id, busType);
|
||||
@@ -97,102 +95,84 @@ print_bus(mraa_board_t* board)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
print_busses()
|
||||
{
|
||||
print_bus(plat);
|
||||
if (plat->sub_platform != NULL)
|
||||
print_bus(plat->sub_platform);
|
||||
print_bus(plat);
|
||||
if (mraa_has_sub_platform())
|
||||
print_bus(plat->sub_platform);
|
||||
}
|
||||
|
||||
|
||||
|
||||
mraa_result_t
|
||||
i2c_get(int bus, uint8_t device_address, uint8_t register_address, uint8_t* data)
|
||||
{
|
||||
mraa_result_t status = MRAA_SUCCESS;
|
||||
mraa_i2c_context i2c = mraa_i2c_init(bus);
|
||||
if (i2c == NULL) {
|
||||
status = MRAA_ERROR_NO_RESOURCES;
|
||||
// fprintf(stdout, "Could not initialize i2c\n");
|
||||
goto i2c_get_exit;
|
||||
return MRAA_ERROR_NO_RESOURCES;
|
||||
}
|
||||
status = mraa_i2c_address(i2c, device_address);
|
||||
if (status != MRAA_SUCCESS) {
|
||||
// fprintf(stdout, "Could not set i2c device address\n");
|
||||
goto i2c_get_exit;
|
||||
}
|
||||
status = mraa_i2c_write_byte(i2c, register_address);
|
||||
if (status != MRAA_SUCCESS) {
|
||||
// fprintf(stdout, "Could not set i2c register address. Status = %d\n", status);
|
||||
goto i2c_get_exit;
|
||||
}
|
||||
status = mraa_i2c_read(i2c, data, 1) == 1 ? MRAA_SUCCESS : MRAA_ERROR_UNSPECIFIED;
|
||||
if (status != MRAA_SUCCESS) {
|
||||
// fprintf(stdout, "i2c read failed\n");
|
||||
goto i2c_get_exit;
|
||||
goto i2c_get_exit;
|
||||
}
|
||||
i2c_get_exit:
|
||||
if (i2c != NULL)
|
||||
mraa_i2c_stop(i2c);
|
||||
return status;
|
||||
}
|
||||
|
||||
|
||||
mraa_result_t
|
||||
i2c_set(int bus, uint8_t device_address, uint8_t register_address, uint8_t data)
|
||||
{
|
||||
mraa_result_t status = MRAA_SUCCESS;
|
||||
mraa_i2c_context i2c = mraa_i2c_init(bus);
|
||||
if (i2c == NULL) {
|
||||
status = MRAA_ERROR_NO_RESOURCES;
|
||||
fprintf(stdout, "Could not initialize i2c\n");
|
||||
goto i2c_set_exit;
|
||||
return MRAA_ERROR_NO_RESOURCES;
|
||||
}
|
||||
status = mraa_i2c_address(i2c, device_address);
|
||||
if (status != MRAA_SUCCESS) {
|
||||
fprintf(stdout, "Could not set i2c device address\n");
|
||||
fprintf(stderr, "Could not set i2c device address\n");
|
||||
goto i2c_set_exit;
|
||||
}
|
||||
status = mraa_i2c_write_byte_data(i2c, data, register_address);
|
||||
if (status != MRAA_SUCCESS) {
|
||||
fprintf(stdout, "Could not write to i2c register. Status = %d\n", status);
|
||||
fprintf(stderr, "Could not write to i2c register. Status = %d\n", status);
|
||||
goto i2c_set_exit;
|
||||
}
|
||||
/*
|
||||
status = mraa_i2c_write_byte(i2c, data);
|
||||
if (status != MRAA_SUCCESS) {
|
||||
fprintf(stdout, "Could not set value. Status = %d\n", status);
|
||||
goto i2c_set_exit;
|
||||
}
|
||||
*/
|
||||
i2c_set_exit:
|
||||
if (i2c != NULL)
|
||||
mraa_i2c_stop(i2c);
|
||||
return status;
|
||||
}
|
||||
|
||||
|
||||
void i2c_detect_devices(int bus)
|
||||
void
|
||||
i2c_detect_devices(int bus)
|
||||
{
|
||||
mraa_result_t status = MRAA_SUCCESS;
|
||||
mraa_i2c_context i2c = mraa_i2c_init(bus);
|
||||
if (i2c == NULL) {
|
||||
return;
|
||||
}
|
||||
int addr;
|
||||
for (addr=0x0; addr < 0x80; ++addr) {
|
||||
uint8_t value;
|
||||
if ((addr) % 16 == 0)
|
||||
printf("%02x: ", addr);
|
||||
if (i2c_get(bus, addr, 0, &value) == MRAA_SUCCESS)
|
||||
printf("%02x ", addr);
|
||||
else
|
||||
printf("-- ", addr);
|
||||
if ((addr + 1) % 16 == 0)
|
||||
printf("\n");
|
||||
}
|
||||
for (addr = 0x0; addr < 0x80; ++addr) {
|
||||
uint8_t value;
|
||||
if ((addr) % 16 == 0)
|
||||
printf("%02x: ", addr);
|
||||
if (i2c_get(bus, addr, 0, &value) == MRAA_SUCCESS)
|
||||
printf("%02x ", addr);
|
||||
else
|
||||
printf("-- ", addr);
|
||||
if ((addr + 1) % 16 == 0)
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int
|
||||
process_command(int argc, char** argv)
|
||||
{
|
||||
@@ -201,15 +181,15 @@ process_command(int argc, char** argv)
|
||||
return 0;
|
||||
} else if (strcmp(argv[1], "version") == 0) {
|
||||
print_version();
|
||||
return 0;
|
||||
return 0;
|
||||
} else if (strcmp(argv[1], "list") == 0) {
|
||||
print_busses();
|
||||
return 0;
|
||||
return 0;
|
||||
} else if (strcmp(argv[1], "detect") == 0) {
|
||||
if (argc == 3) {
|
||||
int bus = strtol(argv[2], NULL, 0);
|
||||
i2c_detect_devices(bus);
|
||||
return 0;
|
||||
return 0;
|
||||
} else {
|
||||
print_command_error();
|
||||
return 1;
|
||||
@@ -238,7 +218,8 @@ process_command(int argc, char** argv)
|
||||
uint8_t device_address = strtol(argv[3], NULL, 0);
|
||||
uint8_t register_address = strtol(argv[4], NULL, 0);
|
||||
uint8_t value = strtol(argv[5], NULL, 0);
|
||||
fprintf(stdout, "Device %02X, Register = %02X, Value = %02X\n", device_address, register_address, value);
|
||||
fprintf(stdout, "Device %02X, Register = %02X, Value = %02X\n", device_address,
|
||||
register_address, value);
|
||||
if (i2c_set(bus, device_address, register_address, value) != MRAA_SUCCESS) {
|
||||
fprintf(stdout, "i2c set failed\n");
|
||||
return 0;
|
||||
@@ -252,7 +233,6 @@ process_command(int argc, char** argv)
|
||||
print_command_error();
|
||||
return 1;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void
|
||||
@@ -260,27 +240,25 @@ run_interactive_mode()
|
||||
{
|
||||
char command[80];
|
||||
while (1) {
|
||||
int i, argc = 1;
|
||||
char **argv;
|
||||
char *arg;
|
||||
int i, argc = 1;
|
||||
char* argv[32];
|
||||
char* arg;
|
||||
argv[0] = "mraa-i2c";
|
||||
fprintf(stdout, "Command: ");
|
||||
fgets(command, 80, stdin);
|
||||
command[strlen(command) - 1] = 0;
|
||||
if (strcmp(command, "q") == 0)
|
||||
return;
|
||||
char *str = strtok(command, " ");
|
||||
char* str = strtok(command, " ");
|
||||
while (str != NULL) {
|
||||
// fprintf(stdout, "%s\n", str);
|
||||
arg = malloc(strlen(str) + 1);
|
||||
argv[argc++] = strcpy(arg, str);
|
||||
str = strtok(NULL, " ");
|
||||
}
|
||||
process_command(argc, argv);
|
||||
for (i=1; i<argc; ++i)
|
||||
process_command(argc, argv);
|
||||
for (i = 1; i < argc; ++i)
|
||||
free(argv[i]);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int
|
||||
@@ -292,6 +270,4 @@ main(int argc, char** argv)
|
||||
return 0;
|
||||
} else
|
||||
return process_command(argc, argv);
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user