PX4 Firmware
PX4 Autopilot Software http://px4.io
mpu9250_i2c.cpp
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (c) 2016-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 mpu9250_i2c.cpp
36  *
37  * I2C interface for MPU9250
38  */
39 
40 #include <drivers/device/i2c.h>
41 
42 #include "mpu9250.h"
43 
44 #ifdef USE_I2C
45 
46 device::Device *MPU9250_I2C_interface(int bus, uint32_t address);
47 
48 class MPU9250_I2C : public device::I2C
49 {
50 public:
51  MPU9250_I2C(int bus, uint32_t address);
52  ~MPU9250_I2C() override = default;
53 
54  int read(unsigned address, void *data, unsigned count) override;
55  int write(unsigned address, void *data, unsigned count) override;
56 
57 protected:
58  virtual int probe();
59 
60 private:
61 
62 };
63 
65 MPU9250_I2C_interface(int bus, uint32_t address)
66 {
67  return new MPU9250_I2C(bus, address);
68 }
69 
70 MPU9250_I2C::MPU9250_I2C(int bus, uint32_t address) :
71  I2C("MPU9250_I2C", nullptr, bus, address, 400000)
72 {
73  _device_id.devid_s.devtype = DRV_ACC_DEVTYPE_MPU9250;
74 }
75 
76 int
77 MPU9250_I2C::write(unsigned reg_speed, void *data, unsigned count)
78 {
79  uint8_t cmd[2] {};
80 
81  if (sizeof(cmd) < (count + 1)) {
82  return -EIO;
83  }
84 
85  cmd[0] = MPU9250_REG(reg_speed);
86  cmd[1] = *(uint8_t *)data;
87  return transfer(&cmd[0], count + 1, nullptr, 0);
88 }
89 
90 int
91 MPU9250_I2C::read(unsigned reg_speed, void *data, unsigned count)
92 {
93  /* We want to avoid copying the data of MPUReport: So if the caller
94  * supplies a buffer not MPUReport in size, it is assume to be a reg or
95  * reg 16 read
96  * Since MPUReport has a cmd at front, we must return the data
97  * after that. Foe anthing else we must return it
98  */
99  uint32_t offset = count < sizeof(MPUReport) ? 0 : offsetof(MPUReport, status);
100  uint8_t cmd = MPU9250_REG(reg_speed);
101  return transfer(&cmd, 1, &((uint8_t *)data)[offset], count);
102 }
103 
104 int
106 {
107  uint8_t whoami = 0;
108 
109  // Try first for mpu9250/6500
110  read(MPUREG_WHOAMI, &whoami, 1);
111 
112  if (whoami == MPU_WHOAMI_9250 || whoami == MPU_WHOAMI_6500) {
113  return PX4_OK;
114  }
115 
116  return -ENODEV;
117 }
118 
119 #endif /* USE_I2C */
static struct vehicle_status_s status
Definition: Commander.cpp:138
#define MPU_WHOAMI_9250
Definition: mpu9250.h:169
#define MPU9250_REG(r)
Definition: mpu9250.h:212
#define MPUREG_WHOAMI
Definition: icm20948.h:52
device::Device * MPU9250_I2C_interface(int bus, uint32_t address)
static void read(bootloader_app_shared_t *pshared)
Report conversation within the mpu, including command byte and interrupt status.
Definition: icm20948.h:311
uint8_t * data
Definition: dataman.cpp:149
#define MPU_WHOAMI_6500
Definition: mpu9250.h:170
static void write(bootloader_app_shared_t *pshared)
Fundamental base class for all physical drivers (I2C, SPI).
Definition: Device.hpp:65
#define DRV_ACC_DEVTYPE_MPU9250
Definition: drv_sensor.h:69
Base class for devices connected via I2C.