PX4 Firmware
PX4 Autopilot Software http://px4.io
MS5525_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 "MS5525.hpp"
35 
36 // Driver 'main' command.
37 extern "C" __EXPORT int ms5525_airspeed_main(int argc, char *argv[]);
38 
39 // Local functions in support of the shell command.
40 namespace ms5525_airspeed
41 {
42 MS5525 *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_ERR("already started");
82  goto fail;
83  }
84 
85  g_dev = new MS5525(i2c_bus, I2C_ADDRESS_1_MS5525DSO, PATH_MS5525);
86 
87  /* check if the MS4525DO was instantiated */
88  if (g_dev == nullptr) {
89  goto fail;
90  }
91 
92  /* try to initialize */
93  if (g_dev->init() != PX4_OK) {
94  goto fail;
95  }
96 
97  /* set the poll rate to default, starts automatic data collection */
98  fd = px4_open(PATH_MS5525, O_RDONLY);
99 
100  if (fd < 0) {
101  goto fail;
102  }
103 
105  goto fail;
106  }
107 
108  return PX4_OK;
109 
110 fail:
111 
112  if (g_dev != nullptr) {
113  delete g_dev;
114  g_dev = nullptr;
115  }
116 
117  return PX4_ERROR;
118 }
119 
120 // stop the driver
121 int stop()
122 {
123  if (g_dev != nullptr) {
124  delete g_dev;
125  g_dev = nullptr;
126 
127  } else {
128  PX4_ERR("driver not running");
129  return PX4_ERROR;
130  }
131 
132  return PX4_OK;
133 }
134 
135 // reset the driver
136 int reset()
137 {
138  int fd = px4_open(PATH_MS5525, O_RDONLY);
139 
140  if (fd < 0) {
141  PX4_ERR("failed ");
142  return PX4_ERROR;
143  }
144 
145  if (px4_ioctl(fd, SENSORIOCRESET, 0) < 0) {
146  PX4_ERR("driver reset failed");
147  return PX4_ERROR;
148  }
149 
151  PX4_ERR("driver poll restart failed");
152  return PX4_ERROR;
153  }
154 
155  return PX4_OK;
156 }
157 
158 } // namespace ms5525_airspeed
159 
160 
161 static void
163 {
164  PX4_INFO("usage: ms5525_airspeed command [options]");
165  PX4_INFO("options:");
166  PX4_INFO("\t-b --bus i2cbus (%d)", PX4_I2C_BUS_DEFAULT);
167  PX4_INFO("\t-a --all");
168  PX4_INFO("command:");
169  PX4_INFO("\tstart|stop|reset");
170 }
171 
172 int
173 ms5525_airspeed_main(int argc, char *argv[])
174 {
175  uint8_t i2c_bus = PX4_I2C_BUS_DEFAULT;
176 
177  int myoptind = 1;
178  int ch;
179  const char *myoptarg = nullptr;
180  bool start_all = false;
181 
182  while ((ch = px4_getopt(argc, argv, "ab:", &myoptind, &myoptarg)) != EOF) {
183  switch (ch) {
184  case 'b':
185  i2c_bus = atoi(myoptarg);
186  break;
187 
188  case 'a':
189  start_all = true;
190  break;
191 
192  default:
194  return 0;
195  }
196  }
197 
198  if (myoptind >= argc) {
200  return -1;
201  }
202 
203  /*
204  * Start/load the driver.
205  */
206  if (!strcmp(argv[myoptind], "start")) {
207  if (start_all) {
208  return ms5525_airspeed::start();
209 
210  } else {
211  return ms5525_airspeed::start_bus(i2c_bus);
212  }
213  }
214 
215  /*
216  * Stop the driver
217  */
218  if (!strcmp(argv[myoptind], "stop")) {
219  return ms5525_airspeed::stop();
220  }
221 
222  /*
223  * Reset the driver.
224  */
225  if (!strcmp(argv[myoptind], "reset")) {
226  return ms5525_airspeed::reset();
227  }
228 
230  return 0;
231 }
virtual int init()
Definition: airspeed.cpp:92
#define SENSOR_POLLRATE_DEFAULT
poll at driver normal rate
Definition: drv_sensor.h:136
Definition: I2C.hpp:51
static void ms5525_airspeed_usage()
#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.
int stop()
Stop the driver.
int fd
Definition: dataman.cpp:146
static const int i2c_bus_options[]
Definition: i2c.h:46
static constexpr uint8_t I2C_ADDRESS_1_MS5525DSO
Definition: MS5525.hpp:43
int px4_open(const char *path, int flags,...)
int start_bus(uint8_t i2c_bus)
Start the driver on a specific bus.
Definition: MS5525_main.cpp:76
#define SENSORIOCRESET
Reset the sensor to its default configuration.
Definition: drv_sensor.h:141
__EXPORT int ms5525_airspeed_main(int argc, char *argv[])
#define NUM_I2C_BUS_OPTIONS
Definition: i2c.h:61
static constexpr const char PATH_MS5525[]
Definition: MS5525.hpp:45
int start()
Attempt to start driver on all available I2C busses.
Definition: MS5525_main.cpp:58
int px4_ioctl(int fd, int cmd, unsigned long arg)