PX4 Firmware
PX4 Autopilot Software http://px4.io
lis3mdl_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 lis3mdl_spi.cpp
36  *
37  * SPI interface for LIS3MDL
38  */
39 
40 #include <px4_platform_common/px4_config.h>
41 
42 #include <assert.h>
43 #include <debug.h>
44 #include <errno.h>
45 #include <stdint.h>
46 #include <stdbool.h>
47 #include <string.h>
48 #include <sys/types.h>
49 #include <unistd.h>
50 
51 #include <arch/board/board.h>
52 
53 #include <drivers/device/spi.h>
54 #include <drivers/drv_mag.h>
55 #include <drivers/drv_device.h>
56 
57 #include "board_config.h"
58 #include "lis3mdl.h"
59 
60 #ifdef PX4_SPIDEV_LIS
61 
62 /* SPI protocol address bits */
63 #define DIR_READ (1<<7)
64 #define DIR_WRITE (0<<7)
65 #define ADDR_INCREMENT (1<<6)
66 
67 class LIS3MDL_SPI : public device::SPI
68 {
69 public:
70  LIS3MDL_SPI(int bus, uint32_t device);
71  virtual ~LIS3MDL_SPI() = default;
72 
73  virtual int init();
74  virtual int ioctl(unsigned operation, unsigned &arg);
75  virtual int read(unsigned address, void *data, unsigned count);
76  virtual int write(unsigned address, void *data, unsigned count);
77 };
78 
80 LIS3MDL_SPI_interface(int bus);
81 
83 LIS3MDL_SPI_interface(int bus)
84 {
85  return new LIS3MDL_SPI(bus, PX4_SPIDEV_LIS);
86 }
87 
88 LIS3MDL_SPI::LIS3MDL_SPI(int bus, uint32_t device) :
89  SPI("LIS3MDL_SPI", nullptr, bus, device, SPIDEV_MODE3, 11 * 1000 * 1000 /* will be rounded to 10.4 MHz */)
90 {
91  _device_id.devid_s.devtype = DRV_MAG_DEVTYPE_LIS3MDL;
92 }
93 
94 int
96 {
97  int ret;
98 
99  ret = SPI::init();
100 
101  if (ret != OK) {
102  DEVICE_DEBUG("SPI init failed");
103  return -EIO;
104  }
105 
106  // read WHO_AM_I value
107  uint8_t data = 0;
108 
109  if (read(ADDR_WHO_AM_I, &data, 1)) {
110  DEVICE_DEBUG("LIS3MDL read_reg fail");
111  }
112 
113  if (data != ID_WHO_AM_I) {
114  DEVICE_DEBUG("LIS3MDL bad ID: %02x", data);
115  return -EIO;
116  }
117 
118  return OK;
119 }
120 
121 int
122 LIS3MDL_SPI::ioctl(unsigned operation, unsigned &arg)
123 {
124  int ret;
125 
126  switch (operation) {
127 
128  case MAGIOCGEXTERNAL:
129  /*
130  * Even if this sensor is on the external SPI
131  * bus it is still internal to the autopilot
132  * assembly, so always return 0 for internal.
133  */
134  return 0;
135 
136  case DEVIOCGDEVICEID:
137  return CDev::ioctl(nullptr, operation, arg);
138 
139  default: {
140  ret = -EINVAL;
141  }
142  }
143 
144  return ret;
145 }
146 
147 int
148 LIS3MDL_SPI::read(unsigned address, void *data, unsigned count)
149 {
150  uint8_t buf[32];
151 
152  if (sizeof(buf) < (count + 1)) {
153  return -EIO;
154  }
155 
156  buf[0] = address | DIR_READ | ADDR_INCREMENT;
157 
158  int ret = transfer(&buf[0], &buf[0], count + 1);
159  memcpy(data, &buf[1], count);
160  return ret;
161 }
162 
163 int
164 LIS3MDL_SPI::write(unsigned address, void *data, unsigned count)
165 {
166  uint8_t buf[32];
167 
168  if (sizeof(buf) < (count + 1)) {
169  return -EIO;
170  }
171 
172  buf[0] = address | DIR_WRITE;
173  memcpy(&buf[1], data, count);
174 
175  return transfer(&buf[0], &buf[0], count + 1);
176 }
177 
178 #endif /* PX4_SPIDEV_LIS */
#define MAGIOCGEXTERNAL
determine if mag is external or onboard
Definition: drv_mag.h:88
#define ADDR_WHO_AM_I
Definition: lps25h.cpp:52
#define ADDR_INCREMENT
Definition: ms5611_spi.cpp:45
Namespace encapsulating all device framework classes, functions and data.
Definition: CDev.cpp:47
#define DRV_MAG_DEVTYPE_LIS3MDL
Definition: drv_sensor.h:59
#define DIR_READ
Definition: bmp388_spi.cpp:46
static void read(bootloader_app_shared_t *pshared)
Generic device / sensor interface.
#define ID_WHO_AM_I
Definition: lps25h.h:59
void init()
Activates/configures the hardware registers.
uint8_t * data
Definition: dataman.cpp:149
Shared defines for the LIS3MDL driver.
device::Device * LIS3MDL_SPI_interface(int bus)
#define DIR_WRITE
Definition: bmp388_spi.cpp:47
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