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();
|
print_help();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void
|
void
|
||||||
print_bus(mraa_board_t* board)
|
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) {
|
for (i = 0; i < board->i2c_bus_count; ++i) {
|
||||||
char* busType;
|
char* busType;
|
||||||
switch (board->platform_type) {
|
switch (board->platform_type) {
|
||||||
case MRAA_INTEL_GALILEO_GEN1:
|
case MRAA_INTEL_GALILEO_GEN1:
|
||||||
case MRAA_INTEL_GALILEO_GEN2:
|
case MRAA_INTEL_GALILEO_GEN2:
|
||||||
case MRAA_INTEL_EDISON_FAB_C:
|
case MRAA_INTEL_EDISON_FAB_C:
|
||||||
case MRAA_INTEL_DE3815:
|
case MRAA_INTEL_DE3815:
|
||||||
case MRAA_INTEL_MINNOWBOARD_MAX:
|
case MRAA_INTEL_MINNOWBOARD_MAX:
|
||||||
case MRAA_RASPBERRY_PI:
|
case MRAA_RASPBERRY_PI:
|
||||||
case MRAA_BEAGLEBONE:
|
case MRAA_BEAGLEBONE:
|
||||||
case MRAA_BANANA:
|
case MRAA_BANANA:
|
||||||
bus = i;
|
bus = i;
|
||||||
busType = "stdapi";
|
busType = "linux";
|
||||||
break;
|
break;
|
||||||
case MRAA_FTDI_FT4222:
|
case MRAA_FTDI_FT4222:
|
||||||
busType = "ft4222";
|
busType = "ft4222";
|
||||||
bus = mraa_use_sub_platform(i);
|
bus = mraa_get_sub_platform_id(i);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
busType = "unknown";
|
busType = "unknown";
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
int id = board->i2c_bus[bus].bus_id;
|
int id = board->i2c_bus[bus].bus_id;
|
||||||
fprintf(stdout, "Bus %3d: id=%02d type=%s ", bus, id, busType);
|
fprintf(stdout, "Bus %3d: id=%02d type=%s ", bus, id, busType);
|
||||||
@@ -97,102 +95,84 @@ print_bus(mraa_board_t* board)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void
|
void
|
||||||
print_busses()
|
print_busses()
|
||||||
{
|
{
|
||||||
print_bus(plat);
|
print_bus(plat);
|
||||||
if (plat->sub_platform != NULL)
|
if (mraa_has_sub_platform())
|
||||||
print_bus(plat->sub_platform);
|
print_bus(plat->sub_platform);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
mraa_result_t
|
mraa_result_t
|
||||||
i2c_get(int bus, uint8_t device_address, uint8_t register_address, uint8_t* data)
|
i2c_get(int bus, uint8_t device_address, uint8_t register_address, uint8_t* data)
|
||||||
{
|
{
|
||||||
mraa_result_t status = MRAA_SUCCESS;
|
mraa_result_t status = MRAA_SUCCESS;
|
||||||
mraa_i2c_context i2c = mraa_i2c_init(bus);
|
mraa_i2c_context i2c = mraa_i2c_init(bus);
|
||||||
if (i2c == NULL) {
|
if (i2c == NULL) {
|
||||||
status = MRAA_ERROR_NO_RESOURCES;
|
return MRAA_ERROR_NO_RESOURCES;
|
||||||
// fprintf(stdout, "Could not initialize i2c\n");
|
|
||||||
goto i2c_get_exit;
|
|
||||||
}
|
}
|
||||||
status = mraa_i2c_address(i2c, device_address);
|
status = mraa_i2c_address(i2c, device_address);
|
||||||
if (status != MRAA_SUCCESS) {
|
if (status != MRAA_SUCCESS) {
|
||||||
// fprintf(stdout, "Could not set i2c device address\n");
|
|
||||||
goto i2c_get_exit;
|
goto i2c_get_exit;
|
||||||
}
|
}
|
||||||
status = mraa_i2c_write_byte(i2c, register_address);
|
status = mraa_i2c_write_byte(i2c, register_address);
|
||||||
if (status != MRAA_SUCCESS) {
|
if (status != MRAA_SUCCESS) {
|
||||||
// fprintf(stdout, "Could not set i2c register address. Status = %d\n", status);
|
|
||||||
goto i2c_get_exit;
|
goto i2c_get_exit;
|
||||||
}
|
}
|
||||||
status = mraa_i2c_read(i2c, data, 1) == 1 ? MRAA_SUCCESS : MRAA_ERROR_UNSPECIFIED;
|
status = mraa_i2c_read(i2c, data, 1) == 1 ? MRAA_SUCCESS : MRAA_ERROR_UNSPECIFIED;
|
||||||
if (status != MRAA_SUCCESS) {
|
if (status != MRAA_SUCCESS) {
|
||||||
// fprintf(stdout, "i2c read failed\n");
|
goto i2c_get_exit;
|
||||||
goto i2c_get_exit;
|
|
||||||
}
|
}
|
||||||
i2c_get_exit:
|
i2c_get_exit:
|
||||||
if (i2c != NULL)
|
|
||||||
mraa_i2c_stop(i2c);
|
mraa_i2c_stop(i2c);
|
||||||
return status;
|
return status;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
mraa_result_t
|
mraa_result_t
|
||||||
i2c_set(int bus, uint8_t device_address, uint8_t register_address, uint8_t data)
|
i2c_set(int bus, uint8_t device_address, uint8_t register_address, uint8_t data)
|
||||||
{
|
{
|
||||||
mraa_result_t status = MRAA_SUCCESS;
|
mraa_result_t status = MRAA_SUCCESS;
|
||||||
mraa_i2c_context i2c = mraa_i2c_init(bus);
|
mraa_i2c_context i2c = mraa_i2c_init(bus);
|
||||||
if (i2c == NULL) {
|
if (i2c == NULL) {
|
||||||
status = MRAA_ERROR_NO_RESOURCES;
|
return MRAA_ERROR_NO_RESOURCES;
|
||||||
fprintf(stdout, "Could not initialize i2c\n");
|
|
||||||
goto i2c_set_exit;
|
|
||||||
}
|
}
|
||||||
status = mraa_i2c_address(i2c, device_address);
|
status = mraa_i2c_address(i2c, device_address);
|
||||||
if (status != MRAA_SUCCESS) {
|
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;
|
goto i2c_set_exit;
|
||||||
}
|
}
|
||||||
status = mraa_i2c_write_byte_data(i2c, data, register_address);
|
status = mraa_i2c_write_byte_data(i2c, data, register_address);
|
||||||
if (status != MRAA_SUCCESS) {
|
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;
|
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:
|
i2c_set_exit:
|
||||||
if (i2c != NULL)
|
|
||||||
mraa_i2c_stop(i2c);
|
mraa_i2c_stop(i2c);
|
||||||
return status;
|
return status;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void
|
||||||
void i2c_detect_devices(int bus)
|
i2c_detect_devices(int bus)
|
||||||
{
|
{
|
||||||
mraa_result_t status = MRAA_SUCCESS;
|
mraa_result_t status = MRAA_SUCCESS;
|
||||||
mraa_i2c_context i2c = mraa_i2c_init(bus);
|
mraa_i2c_context i2c = mraa_i2c_init(bus);
|
||||||
|
if (i2c == NULL) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
int addr;
|
int addr;
|
||||||
for (addr=0x0; addr < 0x80; ++addr) {
|
for (addr = 0x0; addr < 0x80; ++addr) {
|
||||||
uint8_t value;
|
uint8_t value;
|
||||||
if ((addr) % 16 == 0)
|
if ((addr) % 16 == 0)
|
||||||
printf("%02x: ", addr);
|
printf("%02x: ", addr);
|
||||||
if (i2c_get(bus, addr, 0, &value) == MRAA_SUCCESS)
|
if (i2c_get(bus, addr, 0, &value) == MRAA_SUCCESS)
|
||||||
printf("%02x ", addr);
|
printf("%02x ", addr);
|
||||||
else
|
else
|
||||||
printf("-- ", addr);
|
printf("-- ", addr);
|
||||||
if ((addr + 1) % 16 == 0)
|
if ((addr + 1) % 16 == 0)
|
||||||
printf("\n");
|
printf("\n");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int
|
int
|
||||||
process_command(int argc, char** argv)
|
process_command(int argc, char** argv)
|
||||||
{
|
{
|
||||||
@@ -204,7 +184,7 @@ process_command(int argc, char** argv)
|
|||||||
return 0;
|
return 0;
|
||||||
} else if (strcmp(argv[1], "list") == 0) {
|
} else if (strcmp(argv[1], "list") == 0) {
|
||||||
print_busses();
|
print_busses();
|
||||||
return 0;
|
return 0;
|
||||||
} else if (strcmp(argv[1], "detect") == 0) {
|
} else if (strcmp(argv[1], "detect") == 0) {
|
||||||
if (argc == 3) {
|
if (argc == 3) {
|
||||||
int bus = strtol(argv[2], NULL, 0);
|
int bus = strtol(argv[2], NULL, 0);
|
||||||
@@ -238,7 +218,8 @@ process_command(int argc, char** argv)
|
|||||||
uint8_t device_address = strtol(argv[3], NULL, 0);
|
uint8_t device_address = strtol(argv[3], NULL, 0);
|
||||||
uint8_t register_address = strtol(argv[4], NULL, 0);
|
uint8_t register_address = strtol(argv[4], NULL, 0);
|
||||||
uint8_t value = strtol(argv[5], 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) {
|
if (i2c_set(bus, device_address, register_address, value) != MRAA_SUCCESS) {
|
||||||
fprintf(stdout, "i2c set failed\n");
|
fprintf(stdout, "i2c set failed\n");
|
||||||
return 0;
|
return 0;
|
||||||
@@ -252,7 +233,6 @@ process_command(int argc, char** argv)
|
|||||||
print_command_error();
|
print_command_error();
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
@@ -261,26 +241,24 @@ run_interactive_mode()
|
|||||||
char command[80];
|
char command[80];
|
||||||
while (1) {
|
while (1) {
|
||||||
int i, argc = 1;
|
int i, argc = 1;
|
||||||
char **argv;
|
char* argv[32];
|
||||||
char *arg;
|
char* arg;
|
||||||
argv[0] = "mraa-i2c";
|
argv[0] = "mraa-i2c";
|
||||||
fprintf(stdout, "Command: ");
|
fprintf(stdout, "Command: ");
|
||||||
fgets(command, 80, stdin);
|
fgets(command, 80, stdin);
|
||||||
command[strlen(command) - 1] = 0;
|
command[strlen(command) - 1] = 0;
|
||||||
if (strcmp(command, "q") == 0)
|
if (strcmp(command, "q") == 0)
|
||||||
return;
|
return;
|
||||||
char *str = strtok(command, " ");
|
char* str = strtok(command, " ");
|
||||||
while (str != NULL) {
|
while (str != NULL) {
|
||||||
// fprintf(stdout, "%s\n", str);
|
|
||||||
arg = malloc(strlen(str) + 1);
|
arg = malloc(strlen(str) + 1);
|
||||||
argv[argc++] = strcpy(arg, str);
|
argv[argc++] = strcpy(arg, str);
|
||||||
str = strtok(NULL, " ");
|
str = strtok(NULL, " ");
|
||||||
}
|
}
|
||||||
process_command(argc, argv);
|
process_command(argc, argv);
|
||||||
for (i=1; i<argc; ++i)
|
for (i = 1; i < argc; ++i)
|
||||||
free(argv[i]);
|
free(argv[i]);
|
||||||
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
@@ -292,6 +270,4 @@ main(int argc, char** argv)
|
|||||||
return 0;
|
return 0;
|
||||||
} else
|
} else
|
||||||
return process_command(argc, argv);
|
return process_command(argc, argv);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user