From fa35731e02ff54857dc3b0dfdf09765047494446 Mon Sep 17 00:00:00 2001
From: DanielePettenuzzo <daniele@px4.io>
Date: Sat, 16 Jun 2018 16:21:02 +0200
Subject: [PATCH] ms4525_airspeed: add -a flag to scan all i2c busses during
 start

---
 .../ms4525/ms4525_airspeed.cpp                | 41 ++++++++++++++++++-
 1 file changed, 40 insertions(+), 1 deletion(-)

diff --git a/src/drivers/differential_pressure/ms4525/ms4525_airspeed.cpp b/src/drivers/differential_pressure/ms4525/ms4525_airspeed.cpp
index f0a966bbc8..b54f46edfc 100644
--- a/src/drivers/differential_pressure/ms4525/ms4525_airspeed.cpp
+++ b/src/drivers/differential_pressure/ms4525/ms4525_airspeed.cpp
@@ -81,6 +81,8 @@
 #define MEAS_DRIVER_FILTER_FREQ 1.2f
 #define CONVERSION_INTERVAL	(1000000 / MEAS_RATE)	/* microseconds */
 
+#define PX4_I2C_ALL 0xFF
+
 class MEASAirspeed : public Airspeed
 {
 public:
@@ -373,7 +375,22 @@ namespace meas_airspeed
 
 MEASAirspeed	*g_dev = nullptr;
 
+int bus_options[] = {
+#ifdef PX4_I2C_BUS_EXPANSION
+	PX4_I2C_BUS_EXPANSION,
+#endif
+#ifdef PX4_I2C_BUS_EXPANSION1
+	PX4_I2C_BUS_EXPANSION1,
+#endif
+#ifdef PX4_I2C_BUS_EXPANSION2
+	PX4_I2C_BUS_EXPANSION2,
+#endif
+};
+
+#define NUM_BUS_OPTIONS (sizeof(bus_options)/sizeof(bus_options[0]))
+
 int start(int i2c_bus);
+bool start_bus(int i2c_bus);
 int stop();
 int reset();
 
@@ -385,6 +402,23 @@ int reset();
  */
 int
 start(int i2c_bus)
+{
+	if (i2c_bus == PX4_I2C_ALL) {
+		for (unsigned i = 0; i < NUM_BUS_OPTIONS; i++) {
+			if (start_bus(bus_options[i]) == PX4_OK) {
+				return PX4_OK;
+			}
+		}
+
+		return PX4_ERROR;
+
+	} else {
+		return start_bus(i2c_bus);
+	}
+}
+
+bool
+start_bus(int i2c_bus)
 {
 	int fd;
 
@@ -483,6 +517,7 @@ meas_airspeed_usage()
 	PX4_INFO("usage: meas_airspeed command [options]");
 	PX4_INFO("options:");
 	PX4_INFO("\t-b --bus i2cbus (%d)", PX4_I2C_BUS_DEFAULT);
+	PX4_INFO("\t-a --all");
 	PX4_INFO("command:");
 	PX4_INFO("\tstart|stop|reset");
 }
@@ -496,12 +531,16 @@ ms4525_airspeed_main(int argc, char *argv[])
 	int ch;
 	const char *myoptarg = nullptr;
 
-	while ((ch = px4_getopt(argc, argv, "b:", &myoptind, &myoptarg)) != EOF) {
+	while ((ch = px4_getopt(argc, argv, "ab:", &myoptind, &myoptarg)) != EOF) {
 		switch (ch) {
 		case 'b':
 			i2c_bus = atoi(myoptarg);
 			break;
 
+		case 'a':
+			i2c_bus = PX4_I2C_ALL;
+			break;
+
 		default:
 			meas_airspeed_usage();
 			return 0;
-- 
GitLab