PX4 Firmware
PX4 Autopilot Software http://px4.io
fxas21002c_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 #include "FXAS21002C.hpp"
35 
36 extern "C" { __EXPORT int fxas21002c_main(int argc, char *argv[]); }
37 
38 /**
39  * Local functions in support of the shell command.
40  */
41 namespace fxas21002c
42 {
43 
45 
46 void start(bool external_bus, enum Rotation rotation);
47 void info();
48 void regdump();
49 void usage();
50 void test_error();
51 
52 /**
53  * Start the driver.
54  *
55  * This function call only returns once the driver is
56  * up and running or failed to detect the sensor.
57  */
58 void
59 start(bool external_bus, enum Rotation rotation)
60 {
61  if (g_dev != nullptr) {
62  PX4_INFO("already started");
63  exit(0);
64  }
65 
66  /* create the driver */
67  if (external_bus) {
68 #if defined(PX4_SPI_BUS_EXT) && defined(PX4_SPIDEV_EXT_GYRO)
69  g_dev = new FXAS21002C(PX4_SPI_BUS_EXT, PX4_SPIDEV_EXT_GYRO, rotation);
70 #else
71  PX4_ERR("External SPI not available");
72  exit(0);
73 #endif
74 
75  } else {
76  g_dev = new FXAS21002C(PX4_SPI_BUS_SENSORS, PX4_SPIDEV_GYRO, rotation);
77  }
78 
79  if (g_dev == nullptr) {
80  PX4_ERR("failed instantiating FXAS21002C obj");
81  goto fail;
82  }
83 
84  if (OK != g_dev->init()) {
85  goto fail;
86  }
87 
88  exit(0);
89 fail:
90 
91  if (g_dev != nullptr) {
92  delete g_dev;
93  g_dev = nullptr;
94  }
95 
96  errx(1, "driver start failed");
97 }
98 
99 /**
100  * Print a little info about the driver.
101  */
102 void
104 {
105  if (g_dev == nullptr) {
106  PX4_ERR("driver not running\n");
107  exit(1);
108  }
109 
110  printf("state @ %p\n", g_dev);
111  g_dev->print_info();
112 
113  exit(0);
114 }
115 
116 /**
117  * dump registers from device
118  */
119 void
121 {
122  if (g_dev == nullptr) {
123  PX4_ERR("driver not running\n");
124  exit(1);
125  }
126 
127  printf("regdump @ %p\n", g_dev);
128  g_dev->print_registers();
129 
130  exit(0);
131 }
132 
133 /**
134  * trigger an error
135  */
136 void
138 {
139  if (g_dev == nullptr) {
140  PX4_ERR("driver not running\n");
141  exit(1);
142  }
143 
144  g_dev->test_error();
145 
146  exit(0);
147 }
148 
149 void
151 {
152  PX4_INFO("missing command: try 'start', 'info', 'testerror' or 'regdump'");
153  PX4_INFO("options:");
154  PX4_INFO(" -X (external bus)");
155  PX4_INFO(" -R rotation");
156 }
157 
158 } // namespace
159 
160 int
161 fxas21002c_main(int argc, char *argv[])
162 {
163  bool external_bus = false;
164  enum Rotation rotation = ROTATION_NONE;
165 
166  int ch = 0;
167  int myoptind = 1;
168  const char *myoptarg = NULL;
169 
170  while ((ch = px4_getopt(argc, argv, "XR:", &myoptind, &myoptarg)) != EOF) {
171  switch (ch) {
172  case 'X':
173  external_bus = true;
174  break;
175 
176  case 'R':
177  rotation = (enum Rotation)atoi(myoptarg);
178  break;
179 
180  default:
182  return 0;
183  }
184  }
185 
186  const char *verb = argv[myoptind];
187 
188  /*
189  * Start/load the driver.
190 
191  */
192  if (!strcmp(verb, "start")) {
193  fxas21002c::start(external_bus, rotation);
194  }
195 
196  /*
197  * Print driver information.
198  */
199  if (!strcmp(verb, "info")) {
201  }
202 
203  /*
204  * dump device registers
205  */
206  if (!strcmp(verb, "regdump")) {
208  }
209 
210  /*
211  * trigger an error
212  */
213  if (!strcmp(verb, "testerror")) {
215  }
216 
217  PX4_WARN("unrecognized command, try 'start', 'info', 'testerror' or 'regdump'");
218  return -1;
219 }
void regdump()
dump registers from device
void test_error()
deliberately trigger an error
Definition: FXAS21002C.cpp:654
void info()
Print a little info about the driver.
void print_registers()
dump register values
Definition: FXAS21002C.cpp:618
void usage()
Prints info about the driver argument usage.
Definition: I2C.hpp:51
__EXPORT int fxas21002c_main(int argc, char *argv[])
void test_error()
trigger an error
Rotation
Enum for board and external compass rotations.
Definition: rotation.h:51
Local functions in support of the shell command.
virtual int init()
Definition: FXAS21002C.cpp:214
void start(bool external_bus, enum Rotation rotation)
Start the driver.
Driver for the NXP FXAS21002C 3-Axis Digital Angular Rate Gyroscope connected via SPI...
#define errx(eval,...)
Definition: err.h:89
void print_info()
Diagnostics - print some basic information about the driver.
Definition: FXAS21002C.cpp:594
#define OK
Definition: uavcan_main.cpp:71
FXAS21002C * g_dev