PX4 Firmware
PX4 Autopilot Software http://px4.io
icm20948_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 icm20948_i2c.cpp
36  *
37  * I2C interface for ICM20948
38  */
39 
40 #include <px4_platform_common/px4_config.h>
41 #include <drivers/device/i2c.h>
42 #include <drivers/drv_device.h>
43 
44 #include "icm20948.h"
45 
46 #ifdef USE_I2C
47 
48 device::Device *ICM20948_I2C_interface(int bus, uint32_t address);
49 
50 class ICM20948_I2C : public device::I2C
51 {
52 public:
53  ICM20948_I2C(int bus, uint32_t address);
54  ~ICM20948_I2C() override = default;
55 
56  int read(unsigned address, void *data, unsigned count) override;
57  int write(unsigned address, void *data, unsigned count) override;
58 
59 protected:
60  virtual int probe();
61 
62 private:
63 
64 };
65 
67 ICM20948_I2C_interface(int bus, uint32_t address)
68 {
69  return new ICM20948_I2C(bus, address);
70 }
71 
72 ICM20948_I2C::ICM20948_I2C(int bus, uint32_t address) :
73  I2C("ICM20948_I2C", nullptr, bus, address, 400000)
74 {
75  _device_id.devid_s.devtype = DRV_DEVTYPE_ICM20948;
76 }
77 
78 int
79 ICM20948_I2C::write(unsigned reg_speed, void *data, unsigned count)
80 {
81  uint8_t cmd[2] {};
82 
83  if (sizeof(cmd) < (count + 1)) {
84  return -EIO;
85  }
86 
87  cmd[0] = ICM20948_REG(reg_speed);
88  cmd[1] = *(uint8_t *)data;
89  return transfer(&cmd[0], count + 1, nullptr, 0);
90 }
91 
92 int
93 ICM20948_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 = ICM20948_REG(reg_speed);
103  return transfer(&cmd, 1, &((uint8_t *)data)[offset], count);
104 }
105 
106 int
108 {
109  uint8_t whoami = 0;
110  uint8_t register_select = REG_BANK(BANK0); // register bank containing WHOAMI for ICM20948
111 
112  // Try first for icm20948/6500
113  read(MPUREG_WHOAMI, &whoami, 1);
114 
115  /*
116  * If it's not an MPU it must be an ICM
117  * Make sure register bank 0 is selected - whoami is only present on bank 0, and that is
118  * not sure e.g. if the device has rebooted without repowering the sensor
119  */
120  write(ICMREG_20948_BANK_SEL, &register_select, 1);
121  read(ICMREG_20948_WHOAMI, &whoami, 1);
122 
123  if (whoami == ICM_WHOAMI_20948) {
124  return PX4_OK;
125  }
126 
127  return -ENODEV;
128 }
129 
130 #endif /* USE_I2C */
static struct vehicle_status_s status
Definition: Commander.cpp:138
#define ICM20948_REG(r)
Definition: icm20948.h:336
#define ICMREG_20948_WHOAMI
Definition: icm20948.h:210
#define MPUREG_WHOAMI
Definition: icm20948.h:52
static void read(bootloader_app_shared_t *pshared)
Generic device / sensor interface.
Report conversation within the mpu, including command byte and interrupt status.
Definition: icm20948.h:311
uint8_t * data
Definition: dataman.cpp:149
#define BANK0
Definition: icm20948.h:199
#define ICM_WHOAMI_20948
Definition: icm20948.h:169
static void write(bootloader_app_shared_t *pshared)
device::Device * ICM20948_I2C_interface(int bus, uint32_t address)
Fundamental base class for all physical drivers (I2C, SPI).
Definition: Device.hpp:65
#define REG_BANK(r)
Definition: icm20948.h:205
#define ICMREG_20948_BANK_SEL
Definition: icm20948.h:208
#define DRV_DEVTYPE_ICM20948
Definition: drv_sensor.h:64
Base class for devices connected via I2C.