PX4 Firmware
PX4 Autopilot Software http://px4.io
qmc5883_spi.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_SPI.cpp
36  *
37  * SPI interface for HMC5983
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/spi.h>
55 #include <drivers/drv_mag.h>
56 #include <drivers/drv_device.h>
57 
58 #include "qmc5883.h"
59 #include <board_config.h>
60 
61 #ifdef PX4_SPIDEV_HMC
62 
63 /* SPI protocol address bits */
64 #define DIR_READ (1<<7)
65 #define DIR_WRITE (0<<7)
66 #define ADDR_INCREMENT (1<<6)
67 
68 #define HMC_MAX_SEND_LEN 4
69 #define HMC_MAX_RCV_LEN 8
70 
72 
73 class QMC5883_SPI : public device::SPI
74 {
75 public:
76  QMC5883_SPI(int bus, uint32_t device);
77  virtual ~QMC5883_SPI() = default;
78 
79  virtual int init();
80  virtual int read(unsigned address, void *data, unsigned count);
81  virtual int write(unsigned address, void *data, unsigned count);
82 
83  virtual int ioctl(unsigned operation, unsigned &arg);
84 
85 };
86 
88 QMC5883_SPI_interface(int bus)
89 {
90  return new QMC5883_SPI(bus, PX4_SPIDEV_HMC);
91 }
92 
93 QMC5883_SPI::QMC5883_SPI(int bus, uint32_t device) :
94  SPI("QMC5883_SPI", nullptr, bus, device, SPIDEV_MODE3, 11 * 1000 * 1000 /* will be rounded to 10.4 MHz */)
95 {
96  _device_id.devid_s.devtype = DRV_MAG_DEVTYPE_QMC5883;
97 }
98 
99 int
101 {
102  int ret;
103 
104  ret = SPI::init();
105 
106  if (ret != OK) {
107  DEVICE_DEBUG("SPI init failed");
108  return -EIO;
109  }
110 
111  // read WHO_AM_I value
112  uint8_t data[2] = {0, 0};
113 
114  if (read(ADDR_ID_A, &data[0], 1) ||
115  read(ADDR_ID_B, &data[1], 1)) {
116  DEVICE_DEBUG("read_reg fail");
117  }
118 
119  if ((data[0] != ID_A_WHO_AM_I) ||
120  (data[1] != ID_B_WHO_AM_I)) {
121  DEVICE_DEBUG("ID byte mismatch (%02x,%02x)", data[0], data[1]);
122  return -EIO;
123  }
124 
125  return OK;
126 }
127 
128 int
129 QMC5883_SPI::ioctl(unsigned operation, unsigned &arg)
130 {
131  int ret;
132 
133  switch (operation) {
134 
135  case MAGIOCGEXTERNAL:
136  /*
137  * Even if this sensor is on the external SPI
138  * bus it is still internal to the autopilot
139  * assembly, so always return 0 for internal.
140  */
141  return 0;
142 
143  case DEVIOCGDEVICEID:
144  return CDev::ioctl(nullptr, operation, arg);
145 
146  default: {
147  ret = -EINVAL;
148  }
149  }
150 
151  return ret;
152 }
153 
154 int
155 QMC5883_SPI::write(unsigned address, void *data, unsigned count)
156 {
157  uint8_t buf[32];
158 
159  if (sizeof(buf) < (count + 1)) {
160  return -EIO;
161  }
162 
163  buf[0] = address | DIR_WRITE;
164  memcpy(&buf[1], data, count);
165 
166  return transfer(&buf[0], &buf[0], count + 1);
167 }
168 
169 int
170 QMC5883_SPI::read(unsigned address, void *data, unsigned count)
171 {
172  uint8_t buf[32];
173 
174  if (sizeof(buf) < (count + 1)) {
175  return -EIO;
176  }
177 
178  buf[0] = address | DIR_READ | ADDR_INCREMENT;
179 
180  int ret = transfer(&buf[0], &buf[0], count + 1);
181  memcpy(data, &buf[1], count);
182  return ret;
183 }
184 
185 #endif /* PX4_SPIDEV_HMC */
#define MAGIOCGEXTERNAL
determine if mag is external or onboard
Definition: drv_mag.h:88
#define ADDR_ID_B
Definition: hmc5883.h:45
device::Device * QMC5883_SPI_interface(int bus)
#define ADDR_ID_A
Definition: hmc5883.h:44
#define ADDR_INCREMENT
Definition: ms5611_spi.cpp:45
Namespace encapsulating all device framework classes, functions and data.
Definition: CDev.cpp:47
Shared defines for the qmc5883 driver.
#define DIR_READ
Definition: bmp388_spi.cpp:46
#define ID_B_WHO_AM_I
Definition: hmc5883.h:49
static void read(bootloader_app_shared_t *pshared)
Generic device / sensor interface.
virtual int read(unsigned address, void *data, unsigned count)
void init()
Activates/configures the hardware registers.
uint8_t * data
Definition: dataman.cpp:149
#define ID_A_WHO_AM_I
Definition: hmc5883.h:48
#define DIR_WRITE
Definition: bmp388_spi.cpp:47
#define DRV_MAG_DEVTYPE_QMC5883
Definition: drv_sensor.h:62
static void write(bootloader_app_shared_t *pshared)
Fundamental base class for all physical drivers (I2C, SPI).
Definition: Device.hpp:65
#define OK
Definition: uavcan_main.cpp:71
#define DEVICE_DEBUG(FMT,...)
Definition: Device.hpp:52