PX4 Firmware
PX4 Autopilot Software http://px4.io
ms5611_main.cpp
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (c) 2012-2015 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 /**
35  * @file ms5611.cpp
36  * Driver for the MS5611 and MS5607 barometric pressure sensor connected via I2C or SPI.
37  */
38 
39 #include "MS5611.hpp"
40 #include "ms5611.h"
41 
42 enum MS5611_BUS {
48 };
49 
50 /*
51  * Driver 'main' command.
52  */
53 extern "C" __EXPORT int ms5611_main(int argc, char *argv[]);
54 
55 /**
56  * Local functions in support of the shell command.
57  */
58 namespace ms5611
59 {
60 
61 /*
62  list of supported bus configurations
63  */
66  const char *devpath;
68  uint8_t busnum;
70 } bus_options[] = {
71 #if defined(PX4_SPIDEV_EXT_BARO) && defined(PX4_SPI_BUS_EXT)
72  { MS5611_BUS_SPI_EXTERNAL, "/dev/ms5611_spi_ext", &MS5611_spi_interface, PX4_SPI_BUS_EXT, NULL },
73 #endif
74 #ifdef PX4_SPIDEV_BARO
75  { MS5611_BUS_SPI_INTERNAL, "/dev/ms5611_spi_int", &MS5611_spi_interface, PX4_SPI_BUS_BARO, NULL },
76 #endif
77 #ifdef PX4_I2C_BUS_ONBOARD
78  { MS5611_BUS_I2C_INTERNAL, "/dev/ms5611_int", &MS5611_i2c_interface, PX4_I2C_BUS_ONBOARD, NULL },
79 #endif
80 #ifdef PX4_I2C_BUS_EXPANSION
81  { MS5611_BUS_I2C_EXTERNAL, "/dev/ms5611_ext", &MS5611_i2c_interface, PX4_I2C_BUS_EXPANSION, NULL },
82 #endif
83 #ifdef PX4_I2C_BUS_EXPANSION1
84  { MS5611_BUS_I2C_EXTERNAL, "/dev/ms5611_ext1", &MS5611_i2c_interface, PX4_I2C_BUS_EXPANSION1, NULL },
85 #endif
86 #ifdef PX4_I2C_BUS_EXPANSION2
87  { MS5611_BUS_I2C_EXTERNAL, "/dev/ms5611_ext2", &MS5611_i2c_interface, PX4_I2C_BUS_EXPANSION2, NULL },
88 #endif
89 };
90 #define NUM_BUS_OPTIONS (sizeof(bus_options)/sizeof(bus_options[0]))
91 
92 /**
93  * Start the driver.
94  */
95 static bool
96 start_bus(struct ms5611_bus_option &bus, enum MS56XX_DEVICE_TYPES device_type)
97 {
98  if (bus.dev != nullptr) {
99  PX4_ERR("bus option already started");
100  return false;
101  }
102 
103  prom_u prom_buf;
104  device::Device *interface = bus.interface_constructor(prom_buf, bus.busnum);
105 
106  if (interface->init() != OK) {
107  delete interface;
108  PX4_WARN("no device on bus %u", (unsigned)bus.busid);
109  return false;
110  }
111 
112  bus.dev = new MS5611(interface, prom_buf, bus.devpath, device_type);
113 
114  if (bus.dev != nullptr && OK != bus.dev->init()) {
115  delete bus.dev;
116  bus.dev = NULL;
117  return false;
118  }
119 
120  int fd = px4_open(bus.devpath, O_RDONLY);
121 
122  /* set the poll rate to default, starts automatic data collection */
123  if (fd == -1) {
124  PX4_ERR("can't open baro device");
125  return false;
126  }
127 
128  if (ioctl(fd, SENSORIOCSPOLLRATE, SENSOR_POLLRATE_DEFAULT) < 0) {
129  px4_close(fd);
130  PX4_ERR("failed setting default poll rate");
131  return false;
132  }
133 
134  px4_close(fd);
135  return true;
136 }
137 
138 /**
139  * Start the driver.
140  *
141  * This function call only returns once the driver
142  * is either successfully up and running or failed to start.
143  */
144 static int
145 start(enum MS5611_BUS busid, enum MS56XX_DEVICE_TYPES device_type)
146 {
147  uint8_t i;
148  bool started = false;
149 
150  for (i = 0; i < NUM_BUS_OPTIONS; i++) {
151  if (busid == MS5611_BUS_ALL && bus_options[i].dev != NULL) {
152  // this device is already started
153  continue;
154  }
155 
156  if (busid != MS5611_BUS_ALL && bus_options[i].busid != busid) {
157  // not the one that is asked for
158  continue;
159  }
160 
161  started = started | start_bus(bus_options[i], device_type);
162  }
163 
164  if (!started) {
165  return PX4_ERROR;
166  }
167 
168  // one or more drivers started OK
169  return PX4_OK;
170 }
171 
172 static int
174 {
175  for (uint8_t i = 0; i < NUM_BUS_OPTIONS; i++) {
176  struct ms5611_bus_option &bus = bus_options[i];
177 
178  if (bus.dev != nullptr) {
179  PX4_INFO("%s", bus.devpath);
180  bus.dev->print_info();
181  }
182  }
183 
184  return 0;
185 }
186 
187 static int
189 {
190  PX4_INFO("missing command: try 'start', 'info',");
191  PX4_INFO("options:");
192  PX4_INFO(" -X (external I2C bus)");
193  PX4_INFO(" -I (intternal I2C bus)");
194  PX4_INFO(" -S (external SPI bus)");
195  PX4_INFO(" -s (internal SPI bus)");
196  PX4_INFO(" -T 5611|5607 (default 5611)");
197  PX4_INFO(" -T 0 (autodetect version)");
198 
199  return 0;
200 }
201 
202 } // namespace
203 
204 int
205 ms5611_main(int argc, char *argv[])
206 {
208  enum MS56XX_DEVICE_TYPES device_type = MS5611_DEVICE;
209  int ch;
210  int myoptind = 1;
211  const char *myoptarg = NULL;
212 
213  /* jump over start/off/etc and look at options first */
214  while ((ch = px4_getopt(argc, argv, "T:XISs", &myoptind, &myoptarg)) != EOF) {
215  switch (ch) {
216  case 'X':
217  busid = MS5611_BUS_I2C_EXTERNAL;
218  break;
219 
220  case 'I':
221  busid = MS5611_BUS_I2C_INTERNAL;
222  break;
223 
224  case 'S':
225  busid = MS5611_BUS_SPI_EXTERNAL;
226  break;
227 
228  case 's':
229  busid = MS5611_BUS_SPI_INTERNAL;
230  break;
231 
232  case 'T': {
233  int val = atoi(myoptarg);
234 
235  if (val == 5611) {
236  device_type = MS5611_DEVICE;
237  break;
238 
239  } else if (val == 5607) {
240  device_type = MS5607_DEVICE;
241  break;
242 
243  } else if (val == 0) {
244  device_type = MS56XX_DEVICE;
245  break;
246  }
247  }
248 
249  /* FALLTHROUGH */
250 
251  default:
252  return ms5611::usage();
253  }
254  }
255 
256  if (myoptind >= argc) {
257  return ms5611::usage();
258  }
259 
260  const char *verb = argv[myoptind];
261 
262  /*
263  * Start/load the driver.
264  */
265  if (!strcmp(verb, "start")) {
266  return ms5611::start(busid, device_type);
267  }
268 
269  /*
270  * Print driver information.
271  */
272  if (!strcmp(verb, "info")) {
273  return ms5611::info();
274  }
275 
276  return ms5611::usage();
277 }
device::Device *(* MS5611_constructor)(ms5611::prom_u &prom_buf, uint8_t busnum)
Definition: ms5611.h:100
struct ms5611::ms5611_bus_option bus_options[]
#define NUM_BUS_OPTIONS
Definition: ms5611_main.cpp:90
#define SENSOR_POLLRATE_DEFAULT
poll at driver normal rate
Definition: drv_sensor.h:136
__EXPORT int ms5611_main(int argc, char *argv[])
Definition: I2C.hpp:51
MS5611_BUS
Definition: ms5611_main.cpp:42
#define SENSORIOCSPOLLRATE
Set the driver polling rate to (arg) Hz, or one of the SENSOR_POLLRATE constants. ...
Definition: drv_sensor.h:134
MS5611_constructor interface_constructor
Definition: ms5611_main.cpp:67
Shared defines for the ms5611 driver.
static int start(enum MS5611_BUS busid, enum MS56XX_DEVICE_TYPES device_type)
Start the driver.
device::Device * MS5611_i2c_interface(ms5611::prom_u &prom_buf, uint8_t busnum)
Definition: ms5611_i2c.cpp:90
static int usage()
static int info()
int fd
Definition: dataman.cpp:146
int px4_open(const char *path, int flags,...)
Grody hack for crc4()
Definition: ms5611.h:86
Local functions in support of the shell command.
Definition: ms5611.cpp:601
device::Device * MS5611_spi_interface(ms5611::prom_u &prom_buf, uint8_t busnum)
Fundamental base class for all physical drivers (I2C, SPI).
Definition: Device.hpp:65
#define OK
Definition: uavcan_main.cpp:71
MS56XX_DEVICE_TYPES
Definition: MS5611.hpp:50
static bool start_bus(struct ms5611_bus_option &bus, enum MS56XX_DEVICE_TYPES device_type)
Start the driver.
Definition: ms5611_main.cpp:96
void print_info()
Diagnostics - print some basic information about the driver.
Definition: ms5611.cpp:585
int px4_close(int fd)