PX4 Firmware
PX4 Autopilot Software http://px4.io
mag_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 mag_i2c.cpp
36  *
37  * I2C interface for AK09916
38  */
39 
40 #include "icm20948.h"
41 #include "ICM20948_mag.h"
42 
43 #include <drivers/device/i2c.h>
44 
45 #ifdef USE_I2C
46 
48 
49 class AK09916_I2C : public device::I2C
50 {
51 public:
52  AK09916_I2C(int bus);
53  ~AK09916_I2C() override = default;
54 
55  int read(unsigned address, void *data, unsigned count) override;
56  int write(unsigned address, void *data, unsigned count) override;
57 
58 protected:
59  int probe() override;
60 
61 };
62 
64 AK09916_I2C_interface(int bus)
65 {
66  return new AK09916_I2C(bus);
67 }
68 
69 AK09916_I2C::AK09916_I2C(int bus) :
70  I2C("AK09916_I2C", nullptr, bus, AK09916_I2C_ADDR, 400000)
71 {
72  _device_id.devid_s.devtype = DRV_DEVTYPE_ICM20948;
73 }
74 
75 int
76 AK09916_I2C::write(unsigned reg_speed, void *data, unsigned count)
77 {
78  uint8_t cmd[2] {};
79 
80  if (sizeof(cmd) < (count + 1)) {
81  return -EIO;
82  }
83 
84  cmd[0] = ICM20948_REG(reg_speed);
85  cmd[1] = *(uint8_t *)data;
86  return transfer(&cmd[0], count + 1, nullptr, 0);
87 }
88 
89 int
90 AK09916_I2C::read(unsigned reg_speed, void *data, unsigned count)
91 {
92  uint8_t cmd = ICM20948_REG(reg_speed);
93  return transfer(&cmd, 1, (uint8_t *)data, count);
94 }
95 
96 int
98 {
99  uint8_t whoami = 0;
100  uint8_t expected = AK09916_DEVICE_ID;
101 
102  if (PX4_OK != read(AK09916REG_WIA, &whoami, 1)) {
103  return -EIO;
104  }
105 
106  if (whoami != expected) {
107  return -EIO;
108  }
109 
110  return OK;
111 }
112 
113 #endif // USE_I2C
#define ICM20948_REG(r)
Definition: icm20948.h:336
#define AK09916REG_WIA
Definition: ICM20948_mag.h:48
static void read(bootloader_app_shared_t *pshared)
device::Device * AK09916_I2C_interface(int bus)
uint8_t * data
Definition: dataman.cpp:149
#define AK09916_I2C_ADDR
Definition: ICM20948_mag.h:45
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 AK09916_DEVICE_ID
Definition: ICM20948_mag.h:46
#define DRV_DEVTYPE_ICM20948
Definition: drv_sensor.h:64
int read(unsigned address, void *data, unsigned count) override
Base class for devices connected via I2C.