From fb3d60850ea0b9c4ca59158b78fc90c37a2b291f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Roberto=20de=20Souza?= Date: Thu, 4 May 2017 13:12:06 -0700 Subject: [PATCH] aerofc_adc: Add support to use others I2C besides PX4_I2C_BUS_EXPANSION --- src/drivers/aerofc_adc/aerofc_adc.cpp | 86 +++++++++++++++++++++++---- 1 file changed, 74 insertions(+), 12 deletions(-) diff --git a/src/drivers/aerofc_adc/aerofc_adc.cpp b/src/drivers/aerofc_adc/aerofc_adc.cpp index 196f66e6d6..0790d178e6 100644 --- a/src/drivers/aerofc_adc/aerofc_adc.cpp +++ b/src/drivers/aerofc_adc/aerofc_adc.cpp @@ -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 "); + 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; }