PX4 Firmware
PX4 Autopilot Software http://px4.io
lps22hb_main.cpp
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (c) 2018 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 "LPS22HB.hpp"
35 
36 extern "C" __EXPORT int lps22hb_main(int argc, char *argv[]);
37 
43 };
44 
45 /**
46  * Local functions in support of the shell command.
47  */
48 namespace lps22hb
49 {
50 
53  const char *devpath;
55  uint8_t busnum;
57 } bus_options[] = {
58  { LPS22HB_BUS_I2C_EXTERNAL, "/dev/lps22hb_ext", &LPS22HB_I2C_interface, PX4_I2C_BUS_EXPANSION, NULL },
59 #ifdef PX4_I2C_BUS_EXPANSION1
60  { LPS22HB_BUS_I2C_EXTERNAL, "/dev/lps22hb_ext1", &LPS22HB_I2C_interface, PX4_I2C_BUS_EXPANSION1, NULL },
61 #endif
62 #ifdef PX4_I2C_BUS_EXPANSION2
63  { LPS22HB_BUS_I2C_EXTERNAL, "/dev/lps22hb_ext2", &LPS22HB_I2C_interface, PX4_I2C_BUS_EXPANSION2, NULL },
64 #endif
65 #ifdef PX4_I2C_BUS_ONBOARD
66  { LPS22HB_BUS_I2C_INTERNAL, "/dev/lps22hb_int", &LPS22HB_I2C_interface, PX4_I2C_BUS_ONBOARD, NULL },
67 #endif
68 #ifdef PX4_SPIDEV_LPS22HB
69  { LPS22HB_BUS_SPI, "/dev/lps22hb_spi", &LPS22HB_SPI_interface, PX4_SPI_BUS_SENSOR4, NULL },
70 #endif
71 };
72 #define NUM_BUS_OPTIONS (sizeof(bus_options)/sizeof(bus_options[0]))
73 
74 int start(enum LPS22HB_BUS busid);
75 bool start_bus(struct lps22hb_bus_option &bus);
76 struct lps22hb_bus_option &find_bus(enum LPS22HB_BUS busid);
77 int reset(enum LPS22HB_BUS busid);
78 int info();
79 void usage();
80 
81 /**
82  * start driver for a specific bus option
83  */
84 bool
86 {
87  PX4_INFO("starting %s", bus.devpath);
88 
89  if (bus.dev != nullptr) {
90  PX4_WARN("bus option already started");
91  return false;
92  }
93 
94  device::Device *interface = bus.interface_constructor(bus.busnum);
95 
96  if (interface->init() != OK) {
97  delete interface;
98  PX4_WARN("no device on bus %u", (unsigned)bus.busid);
99  return false;
100  }
101 
102  bus.dev = new LPS22HB(interface, bus.devpath);
103 
104  if (bus.dev != nullptr && OK != bus.dev->init()) {
105  PX4_WARN("init failed");
106  delete bus.dev;
107  bus.dev = nullptr;
108  return false;
109  }
110 
111  int fd = px4_open(bus.devpath, O_RDONLY);
112 
113  /* set the poll rate to default, starts automatic data collection */
114  if (fd == -1) {
115  PX4_ERR("can't open baro device");
116  return false;
117  }
118 
120  px4_close(fd);
121  PX4_ERR("failed setting default poll rate");
122  return false;
123  }
124 
125  px4_close(fd);
126 
127  return true;
128 }
129 
130 
131 /**
132  * Start the driver.
133  *
134  * This function call only returns once the driver
135  * is either successfully up and running or failed to start.
136  */
137 int
139 {
140  bool started = false;
141 
142  for (unsigned i = 0; i < NUM_BUS_OPTIONS; i++) {
143  if (busid == LPS22HB_BUS_ALL && bus_options[i].dev != NULL) {
144  // this device is already started
145  continue;
146  }
147 
148  if (busid != LPS22HB_BUS_ALL && bus_options[i].busid != busid) {
149  // not the one that is asked for
150  continue;
151  }
152 
153  started |= start_bus(bus_options[i]);
154  }
155 
156  if (!started) {
157  return PX4_ERROR;
158  }
159 
160  return PX4_OK;
161 }
162 
163 /**
164  * find a bus structure for a busid
165  */
167 {
168  for (unsigned i = 0; i < NUM_BUS_OPTIONS; i++) {
169  if ((busid == LPS22HB_BUS_ALL ||
170  busid == bus_options[i].busid) && bus_options[i].dev != NULL) {
171  return bus_options[i];
172  }
173  }
174 
175  errx(1, "bus %u not started", (unsigned)busid);
176 }
177 
178 /**
179  * Reset the driver.
180  */
181 int
183 {
184  struct lps22hb_bus_option &bus = find_bus(busid);
185  const char *path = bus.devpath;
186 
187  int fd = open(path, O_RDONLY);
188 
189  if (fd < 0) {
190  PX4_ERR("failed");
191  return PX4_ERROR;
192  }
193 
194  if (px4_ioctl(fd, SENSORIOCRESET, 0) < 0) {
195  PX4_ERR("driver reset failed");
196  return PX4_ERROR;
197  }
198 
200  PX4_ERR("driver poll restart failed");
201  return PX4_ERROR;
202  }
203 
204  return 0;
205 }
206 
207 /**
208  * Print a little info about the driver.
209  */
210 int
212 {
213  for (uint8_t i = 0; i < NUM_BUS_OPTIONS; i++) {
214  struct lps22hb_bus_option &bus = bus_options[i];
215 
216  if (bus.dev != nullptr) {
217  warnx("%s", bus.devpath);
218  bus.dev->print_info();
219  }
220  }
221 
222  return 0;
223 }
224 
225 void
227 {
228  PX4_INFO("missing command: try 'start', 'info', 'reset'");
229  PX4_INFO("options:");
230  PX4_INFO(" -X (external I2C bus)");
231  PX4_INFO(" -I (internal I2C bus)");
232  PX4_INFO(" -S (external SPI bus)");
233 }
234 
235 } // namespace
236 
237 int
238 lps22hb_main(int argc, char *argv[])
239 {
240  int myoptind = 1;
241  int ch;
242  const char *myoptarg = nullptr;
243 
245 
246  while ((ch = px4_getopt(argc, argv, "IXS", &myoptind, &myoptarg)) != EOF) {
247  switch (ch) {
248 #if (PX4_I2C_BUS_ONBOARD)
249 
250  case 'I':
251  busid = LPS22HB_BUS_I2C_INTERNAL;
252  break;
253 #endif /* PX4_I2C_BUS_ONBOARD */
254 
255  case 'X':
256  busid = LPS22HB_BUS_I2C_EXTERNAL;
257  break;
258 
259  case 'S':
260  busid = LPS22HB_BUS_SPI;
261  break;
262 
263  default:
264  lps22hb::usage();
265  return 0;
266  }
267  }
268 
269  if (myoptind >= argc) {
270  lps22hb::usage();
271  return -1;
272  }
273 
274  const char *verb = argv[myoptind];
275 
276  /*
277  * Start/load the driver.
278  */
279  if (!strcmp(verb, "start")) {
280  return lps22hb::start(busid);
281  }
282 
283  /*
284  * Reset the driver.
285  */
286  if (!strcmp(verb, "reset")) {
287  return lps22hb::reset(busid);
288  }
289 
290  /*
291  * Print driver information.
292  */
293  if (!strcmp(verb, "info")) {
294  return lps22hb::info();
295  }
296 
297  PX4_WARN("unrecognised command, try 'start', 'reset' or 'info'");
298  return 0;
299 }
struct lps22hb::lps22hb_bus_option bus_options[]
#define SENSOR_POLLRATE_DEFAULT
poll at driver normal rate
Definition: drv_sensor.h:136
Definition: I2C.hpp:51
int info()
Print a little info about the driver.
int reset(enum LPS22HB_BUS busid)
Reset the driver.
int start(enum LPS22HB_BUS busid)
Start the driver.
LPS22HB_constructor interface_constructor
void usage()
Prints info about the driver argument usage.
struct lps22hb_bus_option & find_bus(enum LPS22HB_BUS busid)
find a bus structure for a busid
#define SENSORIOCSPOLLRATE
Set the driver polling rate to (arg) Hz, or one of the SENSOR_POLLRATE constants. ...
Definition: drv_sensor.h:134
device::Device * LPS22HB_I2C_interface(int bus)
Definition: LPS22HB_I2C.cpp:61
#define warnx(...)
Definition: err.h:95
device::Device * LPS22HB_SPI_interface(int bus)
bool start_bus(struct lps22hb_bus_option &bus)
start driver for a specific bus option
#define NUM_BUS_OPTIONS
device::Device *(* LPS22HB_constructor)(int)
Definition: LPS22HB.hpp:82
int fd
Definition: dataman.cpp:146
__EXPORT int lps22hb_main(int argc, char *argv[])
int px4_open(const char *path, int flags,...)
LPS22HB_BUS
#define SENSORIOCRESET
Reset the sensor to its default configuration.
Definition: drv_sensor.h:141
Fundamental base class for all physical drivers (I2C, SPI).
Definition: Device.hpp:65
#define errx(eval,...)
Definition: err.h:89
#define OK
Definition: uavcan_main.cpp:71
Local functions in support of the shell command.
int px4_close(int fd)
int px4_ioctl(int fd, int cmd, unsigned long arg)