PX4 Firmware
PX4 Autopilot Software http://px4.io
qmc5883_i2c.cpp
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (c) 2013-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 QMC5883_I2C.cpp
36  *
37  * I2C interface for QMC5883
38  */
39 
40 /* XXX trim includes */
41 #include <px4_platform_common/px4_config.h>
42 
43 #include <sys/types.h>
44 #include <stdint.h>
45 #include <stdbool.h>
46 #include <string.h>
47 #include <assert.h>
48 #include <debug.h>
49 #include <errno.h>
50 #include <unistd.h>
51 
52 #include <arch/board/board.h>
53 
54 #include <drivers/device/i2c.h>
55 #include <drivers/drv_mag.h>
56 #include <drivers/drv_device.h>
57 
58 #include "qmc5883.h"
59 
60 #include "board_config.h"
61 
62 #define QMC5883L_ADDRESS 0x0D
63 
65 
66 class QMC5883_I2C : public device::I2C
67 {
68 public:
69  QMC5883_I2C(int bus);
70  virtual ~QMC5883_I2C() = default;
71 
72  virtual int read(unsigned address, void *data, unsigned count);
73  virtual int write(unsigned address, void *data, unsigned count);
74 
75  virtual int ioctl(unsigned operation, unsigned &arg);
76 
77 protected:
78  virtual int probe();
79 
80 };
81 
84 {
85  return new QMC5883_I2C(bus);
86 }
87 
89  I2C("QMC5883_I2C", nullptr, bus, QMC5883L_ADDRESS, 400000)
90 {
91  _device_id.devid_s.devtype = DRV_MAG_DEVTYPE_QMC5883;
92 }
93 
94 int
95 QMC5883_I2C::ioctl(unsigned operation, unsigned &arg)
96 {
97  int ret;
98 
99  switch (operation) {
100 
101  case MAGIOCGEXTERNAL:
102  return external();
103 
104  case DEVIOCGDEVICEID:
105  return CDev::ioctl(nullptr, operation, arg);
106 
107  default:
108  ret = -EINVAL;
109  }
110 
111  return ret;
112 }
113 
114 int
116 {
117  uint8_t data[2] = {0, 0};
118 
119  // must read registers 0x00 once or reset to read ID registers reliably
120  read(0x00, &data[0], 1);
121  read(0x00, &data[0], 1);
122  read(0x00, &data[0], 1);
123 
124  _retries = 10;
125 
126  bool read_valid = false;
127  bool id_valid = false;
128 
129  for (unsigned i = 0; i < _retries; i++) {
130  //attempt read
131  if (!read(ADDR_ID_A, &data[0], 1) &&
132  !read(ADDR_ID_B, &data[1], 1)) {
133  read_valid = true;
134  }
135 
136  if (read_valid && data[0] == ID_A_WHO_AM_I &&
137  data[1] == ID_B_WHO_AM_I) {
138  id_valid = true;
139  }
140 
141  if (read_valid && id_valid) {
142  return OK;
143  }
144 
145  // wait 100 usec
146  usleep(100);
147  }
148 
149  if (!read_valid) {
150  DEVICE_DEBUG("read_reg fail");
151  }
152 
153  if (!id_valid) {
154  DEVICE_DEBUG("ID byte mismatch (%02x,%02x)", data[0], data[1]);
155  }
156 
157  return -EIO;
158 }
159 
160 int
161 QMC5883_I2C::write(unsigned address, void *data, unsigned count)
162 {
163  uint8_t buf[32];
164 
165  if (sizeof(buf) < (count + 1)) {
166  return -EIO;
167  }
168 
169  buf[0] = address;
170  memcpy(&buf[1], data, count);
171 
172  return transfer(&buf[0], count + 1, nullptr, 0);
173 }
174 
175 int
176 QMC5883_I2C::read(unsigned address, void *data, unsigned count)
177 {
178  uint8_t cmd = address;
179  return transfer(&cmd, 1, (uint8_t *)data, count);
180 }
#define MAGIOCGEXTERNAL
determine if mag is external or onboard
Definition: drv_mag.h:88
#define ADDR_ID_B
Definition: hmc5883.h:45
virtual int ioctl(unsigned operation, unsigned &arg)
Definition: qmc5883_i2c.cpp:95
#define ADDR_ID_A
Definition: hmc5883.h:44
Shared defines for the qmc5883 driver.
#define ID_B_WHO_AM_I
Definition: hmc5883.h:49
Generic device / sensor interface.
virtual int read(unsigned address, void *data, unsigned count)
uint8_t * data
Definition: dataman.cpp:149
virtual int write(unsigned address, void *data, unsigned count)
QMC5883_I2C(int bus)
Definition: qmc5883_i2c.cpp:88
#define ID_A_WHO_AM_I
Definition: hmc5883.h:48
#define DRV_MAG_DEVTYPE_QMC5883
Definition: drv_sensor.h:62
device::Device * QMC5883_I2C_interface(int bus)
Definition: qmc5883_i2c.cpp:83
virtual ~QMC5883_I2C()=default
Fundamental base class for all physical drivers (I2C, SPI).
Definition: Device.hpp:65
#define QMC5883L_ADDRESS
Definition: qmc5883_i2c.cpp:62
#define OK
Definition: uavcan_main.cpp:71
virtual int probe()
#define DEVICE_DEBUG(FMT,...)
Definition: Device.hpp:52
Base class for devices connected via I2C.