PX4 Firmware
PX4 Autopilot Software http://px4.io
hmc5883_spi.cpp
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (c) 2013-2015 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 HMC5883_SPI.cpp
36  *
37  * SPI interface for HMC5983
38  */
39 
40 #include <px4_platform_common/px4_config.h>
41 #include <drivers/device/spi.h>
42 #include <drivers/drv_mag.h>
43 #include <drivers/drv_device.h>
44 
45 #include "hmc5883.h"
46 
47 #ifdef PX4_SPIDEV_HMC
48 
49 /* SPI protocol address bits */
50 #define DIR_READ (1<<7)
51 #define DIR_WRITE (0<<7)
52 #define ADDR_INCREMENT (1<<6)
53 
54 #define HMC_MAX_SEND_LEN 4
55 #define HMC_MAX_RCV_LEN 8
56 
58 
59 class HMC5883_SPI : public device::SPI
60 {
61 public:
62  HMC5883_SPI(int bus, uint32_t device);
63  virtual ~HMC5883_SPI() = default;
64 
65  virtual int init();
66  virtual int read(unsigned address, void *data, unsigned count);
67  virtual int write(unsigned address, void *data, unsigned count);
68 
69  virtual int ioctl(unsigned operation, unsigned &arg);
70 
71 };
72 
74 HMC5883_SPI_interface(int bus)
75 {
76  return new HMC5883_SPI(bus, PX4_SPIDEV_HMC);
77 }
78 
79 HMC5883_SPI::HMC5883_SPI(int bus, uint32_t device) :
80  SPI("HMC5883_SPI", nullptr, bus, device, SPIDEV_MODE3, 11 * 1000 * 1000 /* will be rounded to 10.4 MHz */)
81 {
82  _device_id.devid_s.devtype = DRV_MAG_DEVTYPE_HMC5883;
83 }
84 
85 int
87 {
88  int ret;
89 
90  ret = SPI::init();
91 
92  if (ret != OK) {
93  DEVICE_DEBUG("SPI init failed");
94  return -EIO;
95  }
96 
97  // read WHO_AM_I value
98  uint8_t data[3] = {0, 0, 0};
99 
100  if (read(ADDR_ID_A, &data[0], 1) ||
101  read(ADDR_ID_B, &data[1], 1) ||
102  read(ADDR_ID_C, &data[2], 1)) {
103  DEVICE_DEBUG("read_reg fail");
104  }
105 
106  if ((data[0] != ID_A_WHO_AM_I) ||
107  (data[1] != ID_B_WHO_AM_I) ||
108  (data[2] != ID_C_WHO_AM_I)) {
109  DEVICE_DEBUG("ID byte mismatch (%02x,%02x,%02x)", data[0], data[1], data[2]);
110  return -EIO;
111  }
112 
113  return OK;
114 }
115 
116 int
117 HMC5883_SPI::ioctl(unsigned operation, unsigned &arg)
118 {
119  int ret;
120 
121  switch (operation) {
122 
123  case MAGIOCGEXTERNAL:
124  /*
125  * Even if this sensor is on the external SPI
126  * bus it is still internal to the autopilot
127  * assembly, so always return 0 for internal.
128  */
129  return 0;
130 
131  case DEVIOCGDEVICEID:
132  return CDev::ioctl(nullptr, operation, arg);
133 
134  default: {
135  ret = -EINVAL;
136  }
137  }
138 
139  return ret;
140 }
141 
142 int
143 HMC5883_SPI::write(unsigned address, void *data, unsigned count)
144 {
145  uint8_t buf[32];
146 
147  if (sizeof(buf) < (count + 1)) {
148  return -EIO;
149  }
150 
151  buf[0] = address | DIR_WRITE;
152  memcpy(&buf[1], data, count);
153 
154  return transfer(&buf[0], &buf[0], count + 1);
155 }
156 
157 int
158 HMC5883_SPI::read(unsigned address, void *data, unsigned count)
159 {
160  uint8_t buf[32];
161 
162  if (sizeof(buf) < (count + 1)) {
163  return -EIO;
164  }
165 
166  buf[0] = address | DIR_READ | ADDR_INCREMENT;
167 
168  int ret = transfer(&buf[0], &buf[0], count + 1);
169  memcpy(data, &buf[1], count);
170  return ret;
171 }
172 
173 #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
#define ID_C_WHO_AM_I
Definition: hmc5883.h:50
#define ADDR_ID_A
Definition: hmc5883.h:44
#define ADDR_INCREMENT
Definition: ms5611_spi.cpp:45
#define DRV_MAG_DEVTYPE_HMC5883
Sensor type definitions.
Definition: drv_sensor.h:55
Namespace encapsulating all device framework classes, functions and data.
Definition: CDev.cpp:47
#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.
void init()
Activates/configures the hardware registers.
#define ADDR_ID_C
Definition: hmc5883.h:46
device::Device * HMC5883_SPI_interface(int bus)
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
Shared defines for the hmc5883 driver.
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