PX4 Firmware
PX4 Autopilot Software http://px4.io
ADIS16477.cpp
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (c) 2018-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 "ADIS16477.hpp"
35 
36 #define DIR_READ 0x00
37 #define DIR_WRITE 0x80
38 
39 // ADIS16477 registers
40 static constexpr uint8_t DIAG_STAT = 0x02; // Output, system error flags
41 static constexpr uint8_t FILT_CTRL = 0x5C;
42 static constexpr uint8_t MSC_CTRL = 0x60;
43 static constexpr uint8_t DEC_RATE = 0x64;
44 static constexpr uint8_t GLOB_CMD = 0x68;
45 static constexpr uint8_t PROD_ID = 0x72;
46 
47 static constexpr uint16_t PROD_ID_ADIS16477 = 0x405D; // ADIS16477 Identification, device number
48 
49 // Stall time between SPI transfers
50 static constexpr uint8_t T_STALL = 16;
51 
52 static constexpr uint32_t ADIS16477_DEFAULT_RATE = 1000;
53 
54 using namespace time_literals;
55 
56 ADIS16477::ADIS16477(int bus, uint32_t device, enum Rotation rotation) :
57  SPI("ADIS16477", nullptr, bus, device, SPIDEV_MODE3, 1000000),
58  ScheduledWorkItem(MODULE_NAME, px4::device_bus_to_wq(get_device_id())),
59  _px4_accel(get_device_id(), ORB_PRIO_MAX, rotation),
60  _px4_gyro(get_device_id(), ORB_PRIO_MAX, rotation),
61  _sample_perf(perf_alloc(PC_ELAPSED, "adis16477: read")),
62  _bad_transfers(perf_alloc(PC_COUNT, "adis16477: bad transfers"))
63 {
64 #ifdef GPIO_SPI1_RESET_ADIS16477
65  // Configure hardware reset line
66  px4_arch_configgpio(GPIO_SPI1_RESET_ADIS16477);
67 #endif // GPIO_SPI1_RESET_ADIS16477
68 
71  _px4_accel.set_scale(1.25f * CONSTANTS_ONE_G / 1000.0f); // accel 1.25 mg/LSB
72 
75  _px4_gyro.set_scale(math::radians(0.025f)); // gyro 0.025 °/sec/LSB
76 }
77 
79 {
80  // make sure we are truly inactive
81  stop();
82 
83  // delete the perf counters
86 }
87 
88 int
90 {
91  // do SPI init (and probe) first
92  if (SPI::init() != OK) {
93  // if probe/setup failed, bail now
94  PX4_DEBUG("SPI setup failed");
95  return PX4_ERROR;
96  }
97 
98  start();
99 
100  return PX4_OK;
101 }
102 
104 {
105 #ifdef GPIO_SPI1_RESET_ADIS16477
106  // Hardware reset
107  px4_arch_gpiowrite(GPIO_SPI1_RESET_ADIS16477, 0);
108 
109  // The RST line must be in a low state for at least 10 μs to ensure a proper reset initiation and recovery.
110  usleep(10_us);
111 
112  px4_arch_gpiowrite(GPIO_SPI1_RESET_ADIS16477, 1);
113 #else
114  // Software reset (global command bit 7)
115  uint8_t value[2] {};
116  value[0] = (1 << 7);
117  write_reg16(GLOB_CMD, (uint16_t)value[0]);
118 #endif // GPIO_SPI1_RESET_ADIS16477
119 
120  // Reset recovery time
121  usleep(193_ms);
122 
123 
124  // Miscellaneous Control Register (MSC_CTRL)
125  static constexpr uint16_t MSC_CTRL_DEFAULT = 0x00C1;
126  usleep(100);
127  // verify
128  const uint16_t msc_ctrl = read_reg16(MSC_CTRL);
129 
130  if (msc_ctrl != MSC_CTRL_DEFAULT) {
131  PX4_ERR("invalid setup, MSC_CTRL=%#X", msc_ctrl);
132  return PX4_ERROR;
133  }
134 
135 
136  // Bartlett Window FIR Filter
137  static constexpr uint16_t FILT_CTRL_SETUP = 0x0004; // (disabled: 0x0000, 2 taps: 0x0001, 16 taps: 0x0004)
138  write_reg16(FILT_CTRL, FILT_CTRL_SETUP);
139  usleep(100);
140  // verify
141  const uint16_t filt_ctrl = read_reg16(FILT_CTRL);
142 
143  if (filt_ctrl != FILT_CTRL_SETUP) {
144  PX4_ERR("invalid setup, FILT_CTRL=%#X", filt_ctrl);
145  return PX4_ERROR;
146  }
147 
148 
149  // Decimation Filter
150  // set for 1000 samples per second
151  static constexpr uint16_t DEC_RATE_SETUP = 0x0001;
152  write_reg16(DEC_RATE, DEC_RATE_SETUP);
153  usleep(100);
154  // verify
155  const uint16_t dec_rate = read_reg16(DEC_RATE);
156 
157  if (dec_rate != DEC_RATE_SETUP) {
158  PX4_ERR("invalid setup, DEC_RATE=%#X", dec_rate);
159  return PX4_ERROR;
160  }
161 
162  return OK;
163 }
164 
165 int
167 {
168  reset();
169 
170  // read product id (5 attempts)
171  for (int i = 0; i < 5; i++) {
172  uint16_t product_id = read_reg16(PROD_ID);
173 
174  if (product_id == PROD_ID_ADIS16477) {
175  PX4_DEBUG("PRODUCT: %X", product_id);
176 
177  if (self_test_memory() && self_test_sensor()) {
178  return PX4_OK;
179 
180  } else {
181  PX4_ERR("probe attempt %d: self test failed, resetting", i);
182  reset();
183  }
184 
185  } else {
186  PX4_ERR("probe attempt %d: read product id failed, resetting", i);
187  reset();
188  }
189  }
190 
191  return -EIO;
192 }
193 
194 bool
196 {
197  DEVICE_DEBUG("self test memory");
198 
199  // self test (global command bit 4)
200  uint8_t value[2] {};
201  value[0] = (1 << 4);
202  write_reg16(GLOB_CMD, (uint16_t)value[0]);
203  usleep(32_ms); // Flash Memory Test Time
204 
205  // read DIAG_STAT to check result
206  uint16_t diag_stat = read_reg16(DIAG_STAT);
207 
208  if (diag_stat != 0) {
209  PX4_ERR("DIAG_STAT: %#X", diag_stat);
210  return false;
211  }
212 
213  return true;
214 }
215 
216 bool
218 {
219  PX4_DEBUG("self test sensor");
220 
221  // self test (global command bit 2)
222  uint8_t value[2] {};
223  value[0] = (1 << 2);
224  write_reg16(GLOB_CMD, (uint16_t)value[0]);
225  usleep(14_ms); // Self Test Time
226 
227  // read DIAG_STAT to check result
228  uint16_t diag_stat = read_reg16(DIAG_STAT);
229 
230  if (diag_stat != 0) {
231  PX4_ERR("DIAG_STAT: %#X", diag_stat);
232  return false;
233  }
234 
235  return true;
236 }
237 
238 uint16_t
240 {
241  uint16_t cmd[1] {};
242 
243  cmd[0] = ((reg | DIR_READ) << 8) & 0xff00;
244  transferhword(cmd, nullptr, 1);
245  up_udelay(T_STALL);
246  transferhword(nullptr, cmd, 1);
247  up_udelay(T_STALL);
248 
249  return cmd[0];
250 }
251 
252 void
253 ADIS16477::write_reg(uint8_t reg, uint8_t val)
254 {
255  uint8_t cmd[2] {};
256  cmd[0] = reg | 0x8;
257  cmd[1] = val;
258  transfer(cmd, cmd, sizeof(cmd));
259 }
260 
261 void
262 ADIS16477::write_reg16(uint8_t reg, uint16_t value)
263 {
264  uint16_t cmd[2] {};
265 
266  cmd[0] = ((reg | DIR_WRITE) << 8) | (0x00ff & value);
267  cmd[1] = (((reg + 0x1) | DIR_WRITE) << 8) | ((0xff00 & value) >> 8);
268 
269  transferhword(cmd, nullptr, 1);
270  up_udelay(T_STALL);
271  transferhword(cmd + 1, nullptr, 1);
272  up_udelay(T_STALL);
273 }
274 
275 void
277 {
278 #ifdef GPIO_SPI1_DRDY1_ADIS16477
279  // Setup data ready on rising edge
280  px4_arch_gpiosetevent(GPIO_SPI1_DRDY1_ADIS16477, true, false, true, &ADIS16477::data_ready_interrupt, this);
281 #else
282  // Make sure we are stopped first
283  stop();
284 
285  // start polling at the specified rate
286  ScheduleOnInterval((1_s / ADIS16477_DEFAULT_RATE), 10000);
287 #endif
288 }
289 
290 void
292 {
293 #ifdef GPIO_SPI1_DRDY1_ADIS16477
294  // Disable data ready callback
295  px4_arch_gpiosetevent(GPIO_SPI1_DRDY1_ADIS16477, false, false, false, nullptr, nullptr);
296 #else
297  ScheduleClear();
298 #endif
299 }
300 
301 int
302 ADIS16477::data_ready_interrupt(int irq, void *context, void *arg)
303 {
304  ADIS16477 *dev = static_cast<ADIS16477 *>(arg);
305 
306  // make another measurement
307  dev->ScheduleNow();
308 
309  return PX4_OK;
310 }
311 
312 void
314 {
315  // make another measurement
316  measure();
317 }
318 
319 int
321 {
323 
324  // Fetch the full set of measurements from the ADIS16477 in one pass (burst read).
325  ADISReport adis_report{};
326  adis_report.cmd = ((GLOB_CMD | DIR_READ) << 8) & 0xff00;
327 
328  // ADIS16477 burst report should be 176 bits
329  static_assert(sizeof(adis_report) == (176 / 8), "ADIS16477 report not 176 bits");
330 
331  const hrt_abstime timestamp_sample = hrt_absolute_time();
332 
333  if (OK != transferhword((uint16_t *)&adis_report, ((uint16_t *)&adis_report), sizeof(adis_report) / sizeof(uint16_t))) {
336  return -EIO;
337  }
338 
339  // Calculate checksum and compare
340 
341  // Checksum = DIAG_STAT, Bits[15:8] + DIAG_STAT, Bits[7:0] +
342  // X_GYRO_OUT, Bits[15:8] + X_GYRO_OUT, Bits[7:0] +
343  // Y_GYRO_OUT, Bits[15:8] + Y_GYRO_OUT, Bits[7:0] +
344  // Z_GYRO_OUT, Bits[15:8] + Z_GYRO_OUT, Bits[7:0] +
345  // X_ACCL_OUT, Bits[15:8] + X_ACCL_OUT, Bits[7:0] +
346  // Y_ACCL_OUT, Bits[15:8] + Y_ACCL_OUT, Bits[7:0] +
347  // Z_ACCL_OUT, Bits[15:8] + Z_ACCL_OUT, Bits[7:0] +
348  // TEMP_OUT, Bits[15:8] + TEMP_OUT, Bits[7:0] +
349  // DATA_CNTR, Bits[15:8] + DATA_CNTR, Bits[7:0]
350  uint8_t *checksum_helper = (uint8_t *)&adis_report.diag_stat;
351 
352  uint8_t checksum = 0;
353 
354  for (int i = 0; i < 18; i++) {
355  checksum += checksum_helper[i];
356  }
357 
358  if (adis_report.checksum != checksum) {
359  PX4_DEBUG("adis_report.checksum: %X vs calculated: %X", adis_report.checksum, checksum);
360 
363 
364  return -EIO;
365  }
366 
367  // Check all Status/Error Flag Indicators (DIAG_STAT)
368  if (adis_report.diag_stat != 0) {
371 
372  return -EIO;
373  }
374 
375  // temperature 1 LSB = 0.1°C
376  const float temperature = adis_report.temp * 0.1f;
377  _px4_accel.set_temperature(temperature);
378  _px4_gyro.set_temperature(temperature);
379 
380  _px4_accel.update(timestamp_sample, adis_report.accel_x, adis_report.accel_y, adis_report.accel_z);
381  _px4_gyro.update(timestamp_sample, adis_report.gyro_x, adis_report.gyro_y, adis_report.gyro_z);
382 
384 
385  return OK;
386 }
387 
388 void
390 {
393 
396 }
static constexpr uint16_t PROD_ID_ADIS16477
Definition: ADIS16477.cpp:47
measure the time elapsed performing an event
Definition: perf_counter.h:56
void set_sample_rate(unsigned rate)
static constexpr uint8_t MSC_CTRL
Definition: ADIS16477.cpp:42
void print_status()
static constexpr uint8_t T_STALL
Definition: ADIS16477.cpp:50
#define DIR_WRITE
Definition: ADIS16477.cpp:37
void update(hrt_abstime timestamp, float x, float y, float z)
static constexpr uint8_t DEC_RATE
Definition: ADIS16477.cpp:43
int reset()
Reset chip.
Definition: ADIS16477.cpp:103
perf_counter_t _sample_perf
Definition: ADIS16477.hpp:68
void print_info()
Definition: ADIS16477.cpp:389
void write_reg16(uint8_t reg, uint16_t value)
Definition: ADIS16477.cpp:262
bool self_test_sensor()
Definition: ADIS16477.cpp:217
Namespace encapsulating all device framework classes, functions and data.
Definition: CDev.cpp:47
count the number of times an event occurs
Definition: perf_counter.h:55
void set_device_type(uint8_t devtype)
static constexpr float CONSTANTS_ONE_G
Definition: geo.h:51
#define DRV_ACC_DEVTYPE_ADIS16477
Definition: drv_sensor.h:110
ADIS16477(int bus, uint32_t device, enum Rotation rotation=ROTATION_NONE)
Definition: ADIS16477.cpp:56
void update(hrt_abstime timestamp, float x, float y, float z)
void write_reg(uint8_t reg, uint8_t value)
Definition: ADIS16477.cpp:253
void perf_count(perf_counter_t handle)
Count a performance event.
static constexpr uint8_t FILT_CTRL
Definition: ADIS16477.cpp:41
void perf_free(perf_counter_t handle)
Free a counter.
void init()
Activates/configures the hardware registers.
void set_temperature(float temperature)
static constexpr uint8_t DIAG_STAT
Definition: ADIS16477.cpp:40
void set_sample_rate(unsigned rate)
#define perf_alloc(a, b)
Definition: px4io.h:59
Rotation
Enum for board and external compass rotations.
Definition: rotation.h:51
constexpr T radians(T degrees)
Definition: Limits.hpp:85
Vector< float, 6 > f(float t, const Matrix< float, 6, 1 > &, const Matrix< float, 3, 1 > &)
Definition: integration.cpp:8
uint16_t read_reg16(uint8_t reg)
Definition: ADIS16477.cpp:239
static int data_ready_interrupt(int irq, void *context, void *arg)
Definition: ADIS16477.cpp:302
void perf_end(perf_counter_t handle)
End a performance event.
PX4Accelerometer _px4_accel
Definition: ADIS16477.hpp:65
static constexpr uint8_t GLOB_CMD
Definition: ADIS16477.cpp:44
void start()
Start automatic measurement.
Definition: ADIS16477.cpp:276
__BEGIN_DECLS typedef uint64_t hrt_abstime
Absolute time, in microsecond units.
Definition: drv_hrt.h:58
void set_scale(float scale)
#define DIR_READ
Definition: ADIS16477.cpp:36
int measure()
Fetch measurements from the sensor and update the report buffers.
Definition: ADIS16477.cpp:320
static constexpr uint32_t ADIS16477_DEFAULT_RATE
Definition: ADIS16477.cpp:52
void set_temperature(float temperature)
void Run() override
Definition: ADIS16477.cpp:313
void stop()
Stop automatic measurement.
Definition: ADIS16477.cpp:291
void perf_print_counter(perf_counter_t handle)
Print one performance counter to stdout.
perf_counter_t _bad_transfers
Definition: ADIS16477.hpp:69
#define DRV_GYR_DEVTYPE_ADIS16477
Definition: drv_sensor.h:111
PX4Gyroscope _px4_gyro
Definition: ADIS16477.hpp:66
Definition: bst.cpp:62
virtual ~ADIS16477()
Definition: ADIS16477.cpp:78
bool self_test_memory()
Definition: ADIS16477.cpp:195
#define OK
Definition: uavcan_main.cpp:71
static constexpr uint8_t PROD_ID
Definition: ADIS16477.cpp:45
virtual int probe()
Definition: ADIS16477.cpp:166
void set_scale(float scale)
void set_device_type(uint8_t devtype)
void perf_begin(perf_counter_t handle)
Begin a performance event.
#define DEVICE_DEBUG(FMT,...)
Definition: Device.hpp:52
__EXPORT hrt_abstime hrt_absolute_time(void)
Get absolute time in [us] (does not wrap).
virtual int init()
Definition: ADIS16477.cpp:89