PX4 Firmware
PX4 Autopilot Software http://px4.io
SDP3X_main.cpp
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (c) 2017 PX4 Development Team. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in
13  * the documentation and/or other materials provided with the
14  * distribution.
15  * 3. Neither the name PX4 nor the names of its contributors may be
16  * used to endorse or promote products derived from this software
17  * without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
26  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  *
32  ****************************************************************************/
33 
34 #include "SDP3X.hpp"
35 
36 // Driver 'main' command.
37 extern "C" __EXPORT int sdp3x_airspeed_main(int argc, char *argv[]);
38 
39 // Local functions in support of the shell command.
40 namespace sdp3x_airspeed
41 {
42 SDP3X *g_dev = nullptr;
43 
44 int start();
45 int start_bus(uint8_t i2c_bus);
46 int stop();
47 int reset();
48 
49 /**
50  * Attempt to start driver on all available I2C busses.
51  *
52  * This function will return as soon as the first sensor
53  * is detected on one of the available busses or if no
54  * sensors are detected.
55  *
56  */
57 int
59 {
60  for (unsigned i = 0; i < NUM_I2C_BUS_OPTIONS; i++) {
61  if (start_bus(i2c_bus_options[i]) == PX4_OK) {
62  return PX4_OK;
63  }
64  }
65 
66  return PX4_ERROR;
67 }
68 
69 /**
70  * Start the driver on a specific bus.
71  *
72  * This function call only returns once the driver is up and running
73  * or failed to detect the sensor.
74  */
75 int
76 start_bus(uint8_t i2c_bus)
77 {
78  int fd = -1;
79 
80  if (g_dev != nullptr) {
81  PX4_WARN("already started");
82  return PX4_ERROR;
83  }
84 
85  g_dev = new SDP3X(i2c_bus, I2C_ADDRESS_1_SDP3X, PATH_SDP3X);
86 
87  /* check if the SDP3XDSO was instantiated */
88  if (g_dev == nullptr) {
89  goto fail;
90  }
91 
92  /* try the next SDP3XDSO if init fails */
93  if (g_dev->init() != PX4_OK) {
94  delete g_dev;
95 
96  g_dev = new SDP3X(i2c_bus, I2C_ADDRESS_2_SDP3X, PATH_SDP3X);
97 
98  /* check if the SDP3XDSO was instantiated */
99  if (g_dev == nullptr) {
100  PX4_ERR("SDP3X was not instantiated (RAM)");
101  goto fail;
102  }
103 
104  /* both versions failed if the init for the SDP3XDSO fails, give up */
105  if (g_dev->init() != PX4_OK) {
106  goto fail;
107  }
108  }
109 
110  /* set the poll rate to default, starts automatic data collection */
111  fd = px4_open(PATH_SDP3X, O_RDONLY);
112 
113  if (fd < 0) {
114  goto fail;
115  }
116 
118  goto fail;
119  }
120 
121  return PX4_OK;
122 
123 fail:
124 
125  if (g_dev != nullptr) {
126  delete g_dev;
127  g_dev = nullptr;
128  }
129 
130  return PX4_ERROR;
131 }
132 
133 // stop the driver
134 int stop()
135 {
136  if (g_dev != nullptr) {
137  delete g_dev;
138  g_dev = nullptr;
139 
140  } else {
141  PX4_ERR("driver not running");
142  return PX4_ERROR;
143  }
144 
145  return PX4_OK;
146 }
147 
148 // reset the driver
149 int reset()
150 {
151  int fd = px4_open(PATH_SDP3X, O_RDONLY);
152 
153  if (fd < 0) {
154  PX4_ERR("failed ");
155  return PX4_ERROR;
156  }
157 
158  if (px4_ioctl(fd, SENSORIOCRESET, 0) < 0) {
159  PX4_ERR("driver reset failed");
160  return PX4_ERROR;
161  }
162 
164  PX4_ERR("driver poll restart failed");
165  return PX4_ERROR;
166  }
167 
168  return PX4_OK;
169 }
170 
171 } // namespace sdp3x_airspeed
172 
173 
174 static void
176 {
177  PX4_INFO("usage: sdp3x_airspeed command [options]");
178  PX4_INFO("options:");
179  PX4_INFO("\t-b --bus i2cbus (%d)", PX4_I2C_BUS_DEFAULT);
180  PX4_INFO("\t-a --all");
181  PX4_INFO("command:");
182  PX4_INFO("\tstart|stop|reset");
183 }
184 
185 int
186 sdp3x_airspeed_main(int argc, char *argv[])
187 {
188  uint8_t i2c_bus = PX4_I2C_BUS_DEFAULT;
189 
190  int myoptind = 1;
191  int ch;
192  const char *myoptarg = nullptr;
193  bool start_all = false;
194 
195  while ((ch = px4_getopt(argc, argv, "ab:", &myoptind, &myoptarg)) != EOF) {
196  switch (ch) {
197  case 'b':
198  i2c_bus = atoi(myoptarg);
199  break;
200 
201  case 'a':
202  start_all = true;
203  break;
204 
205  default:
207  return 0;
208  }
209  }
210 
211  if (myoptind >= argc) {
213  return -1;
214  }
215 
216 
217  /*
218  * Start/load the driver.
219  */
220  if (!strcmp(argv[myoptind], "start")) {
221  if (start_all) {
222  return sdp3x_airspeed::start();
223 
224  } else {
225  return sdp3x_airspeed::start_bus(i2c_bus);
226  }
227  }
228 
229  /*
230  * Stop the driver
231  */
232  if (!strcmp(argv[myoptind], "stop")) {
233  return sdp3x_airspeed::stop();
234  }
235 
236  /*
237  * Reset the driver.
238  */
239  if (!strcmp(argv[myoptind], "reset")) {
240  return sdp3x_airspeed::reset();
241  }
242 
244  return 0;
245 }
#define I2C_ADDRESS_2_SDP3X
Definition: SDP3X.hpp:51
int start_bus(uint8_t i2c_bus)
Start the driver on a specific bus.
Definition: SDP3X_main.cpp:76
virtual int init()
Definition: airspeed.cpp:92
#define SENSOR_POLLRATE_DEFAULT
poll at driver normal rate
Definition: drv_sensor.h:136
#define I2C_ADDRESS_1_SDP3X
Definition: SDP3X.hpp:50
Definition: I2C.hpp:51
int start()
Attempt to start driver on all available I2C busses.
Definition: SDP3X_main.cpp:58
#define SENSORIOCSPOLLRATE
Set the driver polling rate to (arg) Hz, or one of the SENSOR_POLLRATE constants. ...
Definition: drv_sensor.h:134
static constexpr uint8_t PX4_I2C_BUS_DEFAULT
Definition: airspeed.h:48
int reset()
Reset the driver.
Definition: SDP3X_main.cpp:149
#define PATH_SDP3X
Definition: SDP3X.hpp:63
Definition: SDP3X.hpp:70
Driver for Sensirion SDP3X Differential Pressure Sensor.
__EXPORT int sdp3x_airspeed_main(int argc, char *argv[])
Definition: SDP3X_main.cpp:186
int fd
Definition: dataman.cpp:146
static const int i2c_bus_options[]
Definition: i2c.h:46
int stop()
Stop the driver.
Definition: SDP3X_main.cpp:134
int px4_open(const char *path, int flags,...)
#define SENSORIOCRESET
Reset the sensor to its default configuration.
Definition: drv_sensor.h:141
static void sdp3x_airspeed_usage()
Definition: SDP3X_main.cpp:175
#define NUM_I2C_BUS_OPTIONS
Definition: i2c.h:61
int px4_ioctl(int fd, int cmd, unsigned long arg)