PX4 Firmware
PX4 Autopilot Software http://px4.io
fxos8701cq_main.cpp
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (c) 2017-2019 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 fxos8701cq.cpp
36  * Driver for the NXP FXOS8701CQ 6-axis sensor with integrated linear accelerometer and
37  * magnetometer connected via SPI.
38  */
39 
40 #include "FXOS8701CQ.hpp"
41 
42 #include <px4_platform_common/getopt.h>
43 
44 /**
45  * Local functions in support of the shell command.
46  */
47 namespace fxos8701cq
48 {
49 
50 FXOS8701CQ *g_dev{nullptr};
51 
52 int start(bool external_bus, enum Rotation rotation);
53 int info();
54 int stop();
55 int regdump();
56 int usage();
57 int test_error();
58 
59 /**
60  * Start the driver.
61  *
62  * This function call only returns once the driver is
63  * up and running or failed to detect the sensor.
64  */
65 int
66 start(bool external_bus, enum Rotation rotation)
67 {
68  if (g_dev != nullptr) {
69  PX4_INFO("already started");
70  return 0;
71  }
72 
73  /* create the driver */
74  if (external_bus) {
75 #if defined(PX4_SPI_BUS_EXT) && defined(PX4_SPIDEV_EXT_ACCEL_MAG)
76  g_dev = new FXOS8701CQ(PX4_SPI_BUS_EXT, PX4_SPIDEV_EXT_ACCEL_MAG, rotation);
77 #else
78  PX4_ERR("External SPI not available");
79  return 0;
80 #endif
81 
82  } else {
83  g_dev = new FXOS8701CQ(PX4_SPI_BUS_SENSORS, PX4_SPIDEV_ACCEL_MAG, rotation);
84  }
85 
86  if (g_dev == nullptr) {
87  PX4_ERR("failed instantiating FXOS8701C obj");
88  goto fail;
89  }
90 
91  if (OK != g_dev->init()) {
92  goto fail;
93  }
94 
95  return PX4_OK;
96 fail:
97 
98  if (g_dev != nullptr) {
99  delete g_dev;
100  g_dev = nullptr;
101  }
102 
103  PX4_ERR("driver start failed");
104  return PX4_ERROR;
105 }
106 
107 /**
108  * Print a little info about the driver.
109  */
110 int
112 {
113  if (g_dev == nullptr) {
114  PX4_ERR("driver not running\n");
115  return 1;
116  }
117 
118  g_dev->print_info();
119 
120  return 0;
121 }
122 
123 int
125 {
126  if (g_dev == nullptr) {
127  PX4_ERR("driver not running\n");
128  return 1;
129  }
130 
131  delete g_dev;
132  g_dev = nullptr;
133 
134  return 0;
135 }
136 
137 /**
138  * dump registers from device
139  */
140 int
142 {
143  if (g_dev == nullptr) {
144  PX4_ERR("driver not running\n");
145  return 1;
146  }
147 
148  printf("regdump @ %p\n", g_dev);
150 
151  return 0;
152 }
153 
154 /**
155  * trigger an error
156  */
157 int
159 {
160  if (g_dev == nullptr) {
161  PX4_ERR("driver not running\n");
162  return 1;
163  }
164 
165  g_dev->test_error();
166 
167  return 0;
168 }
169 
170 int
172 {
173  PX4_INFO("missing command: try 'start', 'info', 'stop', 'testerror' or 'regdump'");
174  PX4_INFO("options:");
175  PX4_INFO(" -X (external bus)");
176  PX4_INFO(" -R rotation");
177 
178  return 0;
179 }
180 
181 } // namespace
182 
183 extern "C" { __EXPORT int fxos8701cq_main(int argc, char *argv[]); }
184 
185 int fxos8701cq_main(int argc, char *argv[])
186 {
187  bool external_bus = false;
188  int ch;
189  enum Rotation rotation = ROTATION_NONE;
190 
191  int myoptind = 1;
192  const char *myoptarg = NULL;
193 
194  while ((ch = px4_getopt(argc, argv, "XR:a:", &myoptind, &myoptarg)) != EOF) {
195  switch (ch) {
196  case 'X':
197  external_bus = true;
198  break;
199 
200  case 'R':
201  rotation = (enum Rotation)atoi(myoptarg);
202  break;
203 
204  default:
206  exit(0);
207  }
208  }
209 
210  const char *verb = argv[myoptind];
211 
212  if (!strcmp(verb, "start")) {
213  return fxos8701cq::start(external_bus, rotation);
214 
215  } else if (!strcmp(verb, "stop")) {
216  return fxos8701cq::stop();
217 
218  } else if (!strcmp(verb, "info")) {
219  return fxos8701cq::info();
220 
221  } else if (!strcmp(verb, "regdump")) {
222  return fxos8701cq::regdump();
223 
224  } else if (!strcmp(verb, "testerror")) {
225  return fxos8701cq::test_error();
226  }
227 
228  PX4_ERR("unrecognized command, try 'start', 'stop', 'info', 'testerror' or 'regdump'");
229  return PX4_ERROR;
230 }
Local functions in support of the shell command.
virtual int init()
Definition: FXOS8701CQ.cpp:94
FXOS8701CQ * g_dev
Definition: I2C.hpp:51
void print_registers()
Definition: FXOS8701CQ.cpp:435
int stop()
Stop the driver.
__EXPORT int fxos8701cq_main(int argc, char *argv[])
int info()
Print a little info about the driver.
int start(bool external_bus, enum Rotation rotation)
Start the driver.
int test_error()
trigger an error
int usage()
Prints info about the driver argument usage.
int regdump()
dump registers from device
Rotation
Enum for board and external compass rotations.
Definition: rotation.h:51
Driver for the NXP FXOS8701CQ 6-axis sensor with integrated linear accelerometer and magnetometer con...
#define OK
Definition: uavcan_main.cpp:71
void test_error()
Definition: FXOS8701CQ.cpp:460
void print_info()
Definition: FXOS8701CQ.cpp:410