PX4 Firmware
PX4 Autopilot Software http://px4.io
voxlpm_main.cpp
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (c) 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 <px4_platform_common/getopt.h>
35 #include <px4_platform_common/module.h>
36 
37 #include "voxlpm.hpp"
38 
39 enum VOXLPM_BUS {
43 };
44 
45 /**
46  * Local functions in support of the shell command.
47  */
48 namespace voxlpm
49 {
50 struct voxlpm_chan {
51  const char *devpath;
52  uint32_t device;
54 };
55 
57  enum VOXLPM_BUS busid;
58  uint8_t busnum;
59  bool external;
60  struct voxlpm_chan vbat;
61  struct voxlpm_chan vpwr;
62 } bus_options[] = {
63 #if defined(PX4_I2C_BUS_EXPANSION)
64  {
65  VOXLPM_BUS_I2C_EXTERNAL, PX4_I2C_BUS_EXPANSION, true,
66  {"/dev/voxlpm_vbat", VOXLPM_LTC2946_ADDR_VBATT, NULL },
67  {"/dev/voxlpm_p5vd", VOXLPM_LTC2946_ADDR_P5VD, NULL}
68  },
69 #endif
70 #if defined(PX4_I2C_BUS_EXPANSION1)
71  {
72  VOXLPM_BUS_I2C_EXTERNAL1, PX4_I2C_BUS_EXPANSION1, true,
73  {"/dev/voxlpm_vbat", VOXLPM_LTC2946_ADDR_VBATT, NULL },
74  {"/dev/voxlpm_p5vd", VOXLPM_LTC2946_ADDR_P5VD, NULL}
75  },
76 #endif
77 #if defined(PX4_I2C_BUS_EXPANSION2)
78  {
79  VOXLPM_BUS_I2C_EXTERNAL2, PX4_I2C_BUS_EXPANSION2, true,
80  {"/dev/voxlpm_vbat", VOXLPM_LTC2946_ADDR_VBATT, NULL },
81  {"/dev/voxlpm_p5vd", VOXLPM_LTC2946_ADDR_P5VD, NULL}
82  },
83 #endif
84 };
85 #define NUM_BUS_OPTIONS (sizeof(bus_options)/sizeof(bus_options[0]))
86 
87 
88 /**
89  * Start the driver.
90  */
91 bool
93 {
94  /* assume if we've got the battery channel we have the output channel as well */
95  if (bus.vbat.dev != nullptr) {
96  PX4_ERR("bus option already started");
97  exit(1);
98  }
99 
100  /* create the battery voltage / current channel */
101  bus.vbat.dev = new VOXLPM(bus.vbat.devpath, bus.busnum, bus.vbat.device, VOXLPM_CH_TYPE_VBATT);
102 
103  if (bus.vbat.dev == nullptr) {
104  return false;
105  }
106 
107  /* create the 5VDC output / compute current channel */
108  bus.vpwr.dev = new VOXLPM(bus.vpwr.devpath, bus.busnum, bus.vpwr.device, VOXLPM_CH_TYPE_P5VDC);
109 
110  if (bus.vbat.dev == nullptr) {
111  return false;
112  }
113 
114  if (bus.vbat.dev->init() != OK || bus.vpwr.dev->init() != OK) {
115  delete bus.vbat.dev;
116  bus.vbat.dev = nullptr;
117  delete bus.vpwr.dev;
118  bus.vpwr.dev = nullptr;
119  return false;
120  }
121 
122  return true;
123 }
124 
125 void
126 start(enum VOXLPM_BUS busid)
127 {
128  bool started = false;
129  uint8_t i;
130 
131  for (i = 0; i < NUM_BUS_OPTIONS; i++) {
132  if (bus_options[i].busid == busid) {
133  started = start_bus(bus_options[i]);
134  break; // one bus only...
135  }
136  }
137 
138  if (!started) {
139  PX4_WARN("bus option number is %d", i);
140  PX4_ERR("driver start failed");
141  exit(1);
142  }
143 
144  exit(0);
145 }
146 
147 void
149 {
150  uint8_t i;
151 
152  for (i = 0; i < NUM_BUS_OPTIONS; i++) {
153  if (bus_options[i].vbat.dev) {
154  bus_options[i].vbat.dev->print_info();
155  }
156 
157  if (bus_options[i].vpwr.dev) {
158  bus_options[i].vpwr.dev->print_info();
159  }
160  }
161 
162  exit(0);
163 }
164 
165 void
167 {
168  PRINT_MODULE_USAGE_NAME_SIMPLE("voxlpm", "command");
169 
170  PRINT_MODULE_USAGE_COMMAND_DESCR("start", "start monitoring");
171  PRINT_MODULE_USAGE_COMMAND_DESCR("info", "display info");;
172  PRINT_MODULE_USAGE_COMMAND_DESCR("-X", "PX4_I2C_BUS_EXPANSION");
173  PRINT_MODULE_USAGE_COMMAND_DESCR("-T", "PX4_I2C_BUS_EXPANSION1");
174  PRINT_MODULE_USAGE_COMMAND_DESCR("-R", "PX4_I2C_BUS_EXPANSION2 (default)");
175 }
176 
177 } // namespace
178 
179 extern "C" __EXPORT int voxlpm_main(int argc, char *argv[])
180 {
181  int myoptind = 1;
182  int ch;
183  const char *myoptarg = nullptr;
185 
186  while ((ch = px4_getopt(argc, argv, "XTR", &myoptind, &myoptarg)) != EOF) {
187  switch (ch) {
188  case 'X':
189  busid = VOXLPM_BUS_I2C_EXTERNAL;
190  break;
191 
192  case 'T':
193  busid = VOXLPM_BUS_I2C_EXTERNAL1;
194  break;
195 
196  case 'R':
197  busid = VOXLPM_BUS_I2C_EXTERNAL2;
198  break;
199 
200  default:
201  voxlpm::usage();
202  return 0;
203  }
204  }
205 
206  if (myoptind >= argc) {
207  voxlpm::usage();
208  return -1;
209  }
210 
211  const char *verb = argv[myoptind];
212 
213  if (!strcmp(verb, "start")) {
214  voxlpm::start(busid);
215  }
216 
217  if (!strcmp(verb, "info")) {
218  voxlpm::info();
219  }
220 
221  voxlpm::usage();
222  return -1;
223 }
__EXPORT int voxlpm_main(int argc, char *argv[])
Definition: I2C.hpp:51
Shared defines for the voxlpm (QTY2 LTC2946) driver.
#define NUM_BUS_OPTIONS
Definition: voxlpm_main.cpp:85
void info()
Print a little info about the driver.
Local functions in support of the shell command.
Definition: voxlpm_main.cpp:48
struct voxlpm_chan vbat
Definition: voxlpm_main.cpp:60
#define VOXLPM_LTC2946_ADDR_P5VD
Definition: voxlpm.hpp:78
#define VOXLPM_LTC2946_ADDR_VBATT
Definition: voxlpm.hpp:77
struct voxlpm::voxlpm_bus_option bus_options[]
virtual int init()
Definition: voxlpm.cpp:73
#define OK
Definition: uavcan_main.cpp:71
void start(enum VOXLPM_BUS busid)
bool start_bus(struct voxlpm_bus_option &bus)
Start the driver.
Definition: voxlpm_main.cpp:92
struct voxlpm_chan vpwr
Definition: voxlpm_main.cpp:61
const char * devpath
Definition: voxlpm_main.cpp:51
VOXLPM_BUS
Definition: voxlpm_main.cpp:39
void usage()
Prints info about the driver argument usage.