aerofc_adc: Add support to use others I2C besides PX4_I2C_BUS_EXPANSION

This commit is contained in:
José Roberto de Souza 2017-05-04 13:12:06 -07:00 committed by Lorenz Meier
parent 18626661b6
commit fb3d60850e
1 changed files with 74 additions and 12 deletions

View File

@ -54,6 +54,27 @@
// 10Hz
#define CYCLE_TICKS_DELAY MSEC2TICK(100)
enum AEROFC_ADC_BUS {
AEROFC_ADC_BUS_ALL = 0,
AEROFC_ADC_BUS_I2C_INTERNAL,
AEROFC_ADC_BUS_I2C_EXTERNAL
};
static struct aerofc_adc_bus_option {
enum AEROFC_ADC_BUS busid;
uint8_t busnum;
} bus_options[] = {
#ifdef PX4_I2C_BUS_EXPANSION
{ AEROFC_ADC_BUS_I2C_EXTERNAL, PX4_I2C_BUS_EXPANSION },
#endif
#ifdef PX4_I2C_BUS_EXPANSION1
{ AEROFC_ADC_BUS_I2C_EXTERNAL, PX4_I2C_BUS_EXPANSION1 },
#endif
#ifdef PX4_I2C_BUS_ONBOARD
{ AEROFC_ADC_BUS_I2C_INTERNAL, PX4_I2C_BUS_ONBOARD },
#endif
};
extern "C" { __EXPORT int aerofc_adc_main(int argc, char *argv[]); }
class AEROFC_ADC : public device::I2C
@ -105,38 +126,80 @@ static void test()
exit(0);
}
static void help()
{
printf("missing command: try 'start' or 'test'\n");
printf("options:\n");
printf(" -I only internal I2C bus\n");
printf(" -X only external I2C bus\n");
}
int aerofc_adc_main(int argc, char *argv[])
{
if (argc < 2) {
warn("Missing action <start>");
int ch;
enum AEROFC_ADC_BUS busid = AEROFC_ADC_BUS_ALL;
while ((ch = getopt(argc, argv, "XI")) != EOF) {
switch (ch) {
case 'X':
busid = AEROFC_ADC_BUS_I2C_EXTERNAL;
break;
case 'I':
busid = AEROFC_ADC_BUS_I2C_INTERNAL;
break;
default:
help();
return -1;
}
}
if (optind >= argc) {
help();
return PX4_ERROR;
}
if (!strcmp(argv[1], "start")) {
const char *verb = argv[optind];
if (!strcmp(verb, "start")) {
if (instance) {
warn("AEROFC_ADC was already started");
return PX4_OK;
}
instance = new AEROFC_ADC(PX4_I2C_BUS_EXPANSION);
for (uint8_t i = 0; i < (sizeof(bus_options) / sizeof(bus_options[0])); i++) {
if (busid != AEROFC_ADC_BUS_ALL && busid != bus_options[i].busid) {
continue;
}
if (!instance) {
warn("No memory to instance AEROFC_ADC");
return PX4_ERROR;
}
instance = new AEROFC_ADC(bus_options[i].busnum);
if (instance->init() != PX4_OK) {
if (!instance) {
warn("No memory to instance AEROFC_ADC");
return PX4_ERROR;
}
if (instance->init() == PX4_OK) {
break;
}
warn("AEROFC_ADC not found on busnum=%u", bus_options[i].busnum);
delete instance;
instance = nullptr;
warn("AEROFC_ADC failed to init");
}
if (!instance) {
warn("AEROFC_ADC not found");
return PX4_ERROR;
}
} else if (!strcmp(argv[1], "test")) {
} else if (!strcmp(verb, "test")) {
test();
} else {
warn("Action not supported");
help();
return PX4_ERROR;
}
@ -199,7 +262,6 @@ int AEROFC_ADC::probe()
return PX4_OK;
error:
warn("AEROFC_ADC not found");
return -EIO;
}