PX4 Firmware
PX4 Autopilot Software http://px4.io
MPU6000_I2C.cpp
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (c) 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_i2c.cpp
36  *
37  * I2C interface for MPU6000 /MPU6050
38  */
39 
40 #include <drivers/device/i2c.h>
41 
42 #include "MPU6000.hpp"
43 
44 #ifdef USE_I2C
45 
46 device::Device *MPU6000_I2C_interface(int bus, int device_type, bool external_bus);
47 
48 class MPU6000_I2C : public device::I2C
49 {
50 public:
51  MPU6000_I2C(int bus, int device_type);
52  ~MPU6000_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  int probe() override;
59 
60 private:
61  int _device_type;
62 
63 };
64 
65 
67 MPU6000_I2C_interface(int bus, int device_type, bool external_bus)
68 {
69  return new MPU6000_I2C(bus, device_type);
70 }
71 
72 MPU6000_I2C::MPU6000_I2C(int bus, int device_type) :
73  I2C("MPU6000_I2C", nullptr, bus, PX4_I2C_MPU6050_ADDR, 400000),
74  _device_type(device_type)
75 {
76 }
77 
78 int
79 MPU6000_I2C::write(unsigned reg_speed, void *data, unsigned count)
80 {
81  uint8_t cmd[MPU_MAX_WRITE_BUFFER_SIZE];
82 
83  if (sizeof(cmd) < (count + 1)) {
84  return -EIO;
85  }
86 
87  cmd[0] = MPU6000_REG(reg_speed);
88  cmd[1] = *(uint8_t *)data;
89  return transfer(&cmd[0], count + 1, nullptr, 0);
90 }
91 
92 int
93 MPU6000_I2C::read(unsigned reg_speed, void *data, unsigned count)
94 {
95  /* We want to avoid copying the data of MPUReport: So if the caller
96  * supplies a buffer not MPUReport in size, it is assume to be a reg or
97  * reg 16 read
98  * Since MPUReport has a cmd at front, we must return the data
99  * after that. Foe anthing else we must return it
100  */
101  uint32_t offset = count < sizeof(MPUReport) ? 0 : offsetof(MPUReport, status);
102  uint8_t cmd = MPU6000_REG(reg_speed);
103  int ret = transfer(&cmd, 1, &((uint8_t *)data)[offset], count);
104  return ret == OK ? count : ret;
105 }
106 
107 int
109 {
110  uint8_t whoami = 0;
112  return (read(MPUREG_WHOAMI, &whoami, 1) > 0 && (whoami == expected)) ? 0 : -EIO;
113 
114 }
115 #endif /* USE_I2C */
#define MPU6000_REG(r)
Definition: MPU6000.hpp:260
static struct vehicle_status_s status
Definition: Commander.cpp:138
#define MPU_MAX_WRITE_BUFFER_SIZE
Definition: MPU6000.hpp:249
int _device_type
Definition: MPU6000.hpp:334
#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
device::Device * MPU6000_I2C_interface(int bus, int device_type, bool external_bus)
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 ICM_WHOAMI_20608
Definition: MPU6000.hpp:168
Base class for devices connected via I2C.