PX4 Firmware
PX4 Autopilot Software http://px4.io
lsm303agr_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 "LSM303AGR.hpp"
35 
36 #include <px4_platform_common/px4_config.h>
37 #include <px4_platform_common/defines.h>
38 #include <px4_platform_common/getopt.h>
39 
40 #define LSM303AGR_DEVICE_PATH_MAG "/dev/lsm303agr_mag"
41 
42 extern "C" { __EXPORT int lsm303agr_main(int argc, char *argv[]); }
43 
44 /**
45  * Local functions in support of the shell command.
46  */
47 namespace lsm303agr
48 {
49 
51 
52 void start(enum Rotation rotation);
53 void info();
54 void usage();
55 
56 /**
57  * Start the driver.
58  *
59  * This function call only returns once the driver is
60  * up and running or failed to detect the sensor.
61  */
62 void
63 start(enum Rotation rotation)
64 {
65  int fd_mag = -1;
66 
67  if (g_dev != nullptr) {
68  errx(0, "already started");
69  }
70 
71  /* create the driver */
72 #if defined(PX4_SPIDEV_LSM303A_M) && defined(PX4_SPIDEV_LSM303A_X)
73  g_dev = new LSM303AGR(PX4_SPI_BUS_SENSOR5, LSM303AGR_DEVICE_PATH_MAG, PX4_SPIDEV_LSM303A_M, rotation);
74 #else
75  errx(0, "External SPI not available");
76 #endif
77 
78  if (g_dev == nullptr) {
79  PX4_ERR("alloc failed");
80  goto fail;
81  }
82 
83  if (OK != g_dev->init()) {
84  goto fail;
85  }
86 
87  fd_mag = px4_open(LSM303AGR_DEVICE_PATH_MAG, O_RDONLY);
88 
89  /* don't fail if open cannot be opened */
90  if (0 <= fd_mag) {
92  goto fail;
93  }
94  }
95 
96  px4_close(fd_mag);
97 
98  exit(0);
99 fail:
100 
101  if (g_dev != nullptr) {
102  delete g_dev;
103  g_dev = nullptr;
104  }
105 
106  errx(1, "driver start failed");
107 }
108 
109 /**
110  * Print a little info about the driver.
111  */
112 void
114 {
115  if (g_dev == nullptr) {
116  errx(1, "driver not running\n");
117  }
118 
119  printf("state @ %p\n", g_dev);
120  g_dev->print_info();
121 
122  exit(0);
123 }
124 
125 void
127 {
128  PX4_INFO("missing command: try 'start', 'info', 'reset'");
129  PX4_INFO("options:");
130  PX4_INFO(" -X (external bus)");
131  PX4_INFO(" -R rotation");
132 }
133 
134 } // namespace
135 
136 int
137 lsm303agr_main(int argc, char *argv[])
138 {
139  int myoptind = 1;
140  int ch;
141  const char *myoptarg = nullptr;
142 
143  enum Rotation rotation = ROTATION_NONE;
144 
145  /* jump over start/off/etc and look at options first */
146  while ((ch = px4_getopt(argc, argv, "XR:a:", &myoptind, &myoptarg)) != EOF) {
147  switch (ch) {
148  case 'R':
149  rotation = (enum Rotation)atoi(myoptarg);
150  break;
151 
152  default:
154  exit(0);
155  }
156  }
157 
158  if (myoptind >= argc) {
160  exit(0);
161  }
162 
163  const char *verb = argv[myoptind];
164 
165  /*
166  * Start/load the driver.
167 
168  */
169  if (!strcmp(verb, "start")) {
170  lsm303agr::start(rotation);
171  }
172 
173  /*
174  * Print driver information.
175  */
176  if (!strcmp(verb, "info")) {
177  lsm303agr::info();
178  }
179 
180  errx(1, "unrecognized command, try 'start', 'info'");
181 }
#define SENSOR_POLLRATE_DEFAULT
poll at driver normal rate
Definition: drv_sensor.h:136
Definition: I2C.hpp:51
virtual int init()
Definition: LSM303AGR.cpp:100
#define SENSORIOCSPOLLRATE
Set the driver polling rate to (arg) Hz, or one of the SENSOR_POLLRATE constants. ...
Definition: drv_sensor.h:134
void info()
Print a little info about the driver.
Rotation
Enum for board and external compass rotations.
Definition: rotation.h:51
__EXPORT int lsm303agr_main(int argc, char *argv[])
LSM303AGR * g_dev
Local functions in support of the shell command.
int px4_open(const char *path, int flags,...)
#define errx(eval,...)
Definition: err.h:89
void print_info()
Diagnostics - print some basic information about the driver.
Definition: LSM303AGR.cpp:479
#define OK
Definition: uavcan_main.cpp:71
#define LSM303AGR_DEVICE_PATH_MAG
void start(enum Rotation rotation)
Start the driver.
void usage()
Prints info about the driver argument usage.
int px4_close(int fd)
int px4_ioctl(int fd, int cmd, unsigned long arg)