PX4 Firmware
PX4 Autopilot Software http://px4.io
MPU6000_SPI.cpp
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (c) 2012-2016 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 mpu6000_spi.cpp
36  *
37  * Driver for the Invensense MPU6000 connected via SPI.
38  *
39  * @author Andrew Tridgell
40  * @author Pat Hickey
41  * @author David sidrane
42  */
43 
44 #include <drivers/device/spi.h>
45 
46 #include "MPU6000.hpp"
47 
48 #define DIR_READ 0x80
49 #define DIR_WRITE 0x00
50 
51 #if defined(PX4_SPIDEV_MPU) || defined(PX4_SPIDEV_ICM_20602) || defined(PX4_SPIDEV_ICM_20689)
52 
53 /* The MPU6000 can only handle high SPI bus speeds of 20Mhz on the sensor and
54 * interrupt status registers. All other registers have a maximum 1MHz
55 * SPI speed
56 *
57 * The ICM parts are not rated as high.
58 *
59 * The Actual Value will be rounded down by the spi driver.
60 * 168 Mhz CPU 180 Mhz CPU
61 * Selected ------------actual---------------
62 * 20 Mhz 10.5 Mhz 11.250 Mhz
63 * 10 Mhz 5.250 Mhz 5.625 Mhz
64 * 8 Mhz 5.250 Mhz 5.625 Mhz
65 * 1 Mhz 0.703125 Mhz 0.65625 Mhz
66 *
67 */
68 #define MPU6000_LOW_SPI_BUS_SPEED 1000*1000
69 #define MPU6000_HIGH_SPI_BUS_SPEED 20*1000*1000
70 #define ICM20608_HIGH_SPI_BUS_SPEED 8*1000*1000
71 #define ICM20689_HIGH_SPI_BUS_SPEED 8*1000*1000
72 #define ICM20602_HIGH_SPI_BUS_SPEED 10*1000*1000
73 #define UNKNOWN_HIGH_SPI_BUS_SPEED 8*1000*1000 // Use the minimum
74 
75 
76 device::Device *MPU6000_SPI_interface(int bus, int device_type, bool external_bus);
77 
78 
79 class MPU6000_SPI : public device::SPI
80 {
81 public:
82  MPU6000_SPI(int bus, uint32_t device, int device_type);
83  ~MPU6000_SPI() override = default;
84 
85  int read(unsigned address, void *data, unsigned count) override;
86  int write(unsigned address, void *data, unsigned count) override;
87 
88 protected:
89  int probe() override;
90 
91 private:
92 
93  int _device_type;
94  /* Helper to set the desired speed and isolate the register on return */
95 
96  int _max_frequency;
97  void set_bus_frequency(unsigned &reg_speed_reg_out);
98 };
99 
101 MPU6000_SPI_interface(int bus, int device_type, bool external_bus)
102 {
103  int cs = SPIDEV_NONE(0);
104  device::Device *interface = nullptr;
105 
106  if (external_bus) {
107 
108 #if defined(PX4_SPI_BUS_EXT) || defined(PX4_SPI_BUS_EXTERNAL)
109 
110  switch (device_type) {
111 
113 # if defined(PX4_SPIDEV_EXT_MPU)
114  cs = PX4_SPIDEV_EXT_MPU;
115 # endif
116  break;
117 
119 # if defined(PX4_SPIDEV_ICM_20602_EXT)
120  cs = PX4_SPIDEV_ICM_20602_EXT;
121 # endif
122  break;
123 
125 # if defined(PX4_SPIDEV_EXT_ICM)
126  cs = PX4_SPIDEV_EXT_ICM;
127 # elif defined(PX4_SPIDEV_ICM_20608_EXT)
128  cs = PX4_SPIDEV_ICM_20608_EXT;
129 # endif
130  break;
131 
133 # if defined(PX4_SPIDEV_ICM_20689_EXT)
134  cs = PX4_SPIDEV_ICM_20689_EXT;
135 # endif
136  break;
137 
138  default:
139  break;
140  }
141 
142 #endif
143 
144  } else {
145 
146  switch (device_type) {
147 
149 #if defined(PX4_SPIDEV_MPU)
150  cs = PX4_SPIDEV_MPU;
151 #endif
152  break;
153 
155 #if defined(PX4_SPIDEV_ICM_20602)
156  cs = PX4_SPIDEV_ICM_20602;
157 #endif
158  break;
159 
161 #if defined(PX4_SPIDEV_ICM_20608)
162  cs = PX4_SPIDEV_ICM_20608;
163 #endif
164  break;
165 
167 # if defined(PX4_SPIDEV_ICM_20689)
168  cs = PX4_SPIDEV_ICM_20689;
169 # endif
170 
171  default:
172  break;
173  }
174  }
175 
176  if (cs != SPIDEV_NONE(0)) {
177  interface = new MPU6000_SPI(bus, cs, device_type);
178  }
179 
180  return interface;
181 }
182 
183 MPU6000_SPI::MPU6000_SPI(int bus, uint32_t device, int device_type) :
184  SPI("MPU6000", nullptr, bus, device, SPIDEV_MODE3, MPU6000_LOW_SPI_BUS_SPEED),
185  _device_type(device_type)
186 {
187 }
188 
189 void
190 MPU6000_SPI::set_bus_frequency(unsigned &reg_speed)
191 {
192  /* Set the desired speed */
193  set_frequency(MPU6000_IS_HIGH_SPEED(reg_speed) ? _max_frequency : MPU6000_LOW_SPI_BUS_SPEED);
194 
195  /* Isolate the register on return */
196  reg_speed = MPU6000_REG(reg_speed);
197 }
198 
199 int
200 MPU6000_SPI::write(unsigned reg_speed, void *data, unsigned count)
201 {
202  uint8_t cmd[MPU_MAX_WRITE_BUFFER_SIZE];
203 
204  if (sizeof(cmd) < (count + 1)) {
205  return -EIO;
206  }
207 
208  /* Set the desired speed and isolate the register */
209  set_bus_frequency(reg_speed);
210 
211  cmd[0] = reg_speed | DIR_WRITE;
212  cmd[1] = *(uint8_t *)data;
213 
214  return transfer(&cmd[0], &cmd[0], count + 1);
215 }
216 
217 int
218 MPU6000_SPI::read(unsigned reg_speed, void *data, unsigned count)
219 {
220  /* We want to avoid copying the data of MPUReport: So if the caller
221  * supplies a buffer not MPUReport in size, it is assume to be a reg or reg 16 read
222  * and we need to provied the buffer large enough for the callers data
223  * and our command.
224  */
225  uint8_t cmd[3] = {0, 0, 0};
226 
227  uint8_t *pbuff = count < sizeof(MPUReport) ? cmd : (uint8_t *) data ;
228 
229  if (count < sizeof(MPUReport)) {
230  /* add command */
231  count++;
232  }
233 
234  set_bus_frequency(reg_speed);
235 
236  /* Set command */
237  pbuff[0] = reg_speed | DIR_READ ;
238 
239  /* Transfer the command and get the data */
240  int ret = transfer(pbuff, pbuff, count);
241 
242  if (ret == OK && pbuff == &cmd[0]) {
243 
244  /* Adjust the count back */
245  count--;
246 
247  /* Return the data */
248  memcpy(data, &cmd[1], count);
249 
250  }
251 
252  return ret == OK ? count : ret;
253 }
254 
255 int
257 {
258  uint8_t whoami = 0;
259  uint8_t expected = MPU_WHOAMI_6000;
260  _max_frequency = UNKNOWN_HIGH_SPI_BUS_SPEED;
261 
262  switch (_device_type) {
263 
264  default:
266  _max_frequency = MPU6000_HIGH_SPI_BUS_SPEED;
267  break;
268 
270  expected = ICM_WHOAMI_20602;
271  _max_frequency = ICM20602_HIGH_SPI_BUS_SPEED;
272  break;
273 
275  expected = ICM_WHOAMI_20608;
276  _max_frequency = ICM20608_HIGH_SPI_BUS_SPEED;
277  break;
278 
280  expected = ICM_WHOAMI_20689;
281  _max_frequency = ICM20689_HIGH_SPI_BUS_SPEED;
282  break;
283  }
284 
285  return (read(MPUREG_WHOAMI, &whoami, 1) > 0 && (whoami == expected)) ? 0 : -EIO;
286 }
287 
288 #endif // PX4_SPIDEV_MPU
#define MPU6000_REG(r)
Definition: MPU6000.hpp:260
device::Device * MPU6000_SPI_interface(int bus, int device_type, bool external_bus)
#define MPU_MAX_WRITE_BUFFER_SIZE
Definition: MPU6000.hpp:249
#define DIR_WRITE
Definition: MPU6000_SPI.cpp:49
Namespace encapsulating all device framework classes, functions and data.
Definition: CDev.cpp:47
#define MPU6000_IS_HIGH_SPEED(r)
Definition: MPU6000.hpp:259
#define MPUREG_WHOAMI
Definition: icm20948.h:52
static void read(bootloader_app_shared_t *pshared)
Report conversation within the mpu, including command byte and interrupt status.
Definition: icm20948.h:311
#define MPU_WHOAMI_6000
Definition: MPU6000.hpp:166
uint8_t * data
Definition: dataman.cpp:149
#define ICM_WHOAMI_20689
Definition: MPU6000.hpp:169
#define ICM_WHOAMI_20602
Definition: MPU6000.hpp:167
static void write(bootloader_app_shared_t *pshared)
#define DIR_READ
Definition: MPU6000_SPI.cpp:48
Fundamental base class for all physical drivers (I2C, SPI).
Definition: Device.hpp:65
#define OK
Definition: uavcan_main.cpp:71
#define ICM_WHOAMI_20608
Definition: MPU6000.hpp:168