PX4 Firmware
PX4 Autopilot Software http://px4.io
AK8963_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 AK8963
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 "mpu9250.h"
45 #include "MPU9250_mag.h"
46 
47 #ifdef USE_I2C
48 
50 
51 class AK8963_I2C : public device::I2C
52 {
53 public:
54  AK8963_I2C(int bus);
55  ~AK8963_I2C() override = default;
56 
57  int read(unsigned address, void *data, unsigned count) override;
58  int write(unsigned address, void *data, unsigned count) override;
59 
60 protected:
61  int probe() override;
62 
63 };
64 
66 AK8963_I2C_interface(int bus)
67 {
68  return new AK8963_I2C(bus);
69 }
70 
71 AK8963_I2C::AK8963_I2C(int bus) : I2C("AK8963_I2C", nullptr, bus, AK8963_I2C_ADDR, 400000)
72 {
73  _device_id.devid_s.devtype = DRV_MAG_DEVTYPE_MPU9250;
74 }
75 
76 int
77 AK8963_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 AK8963_I2C::read(unsigned reg_speed, void *data, unsigned count)
92 {
93  uint8_t cmd = MPU9250_REG(reg_speed);
94  return transfer(&cmd, 1, (uint8_t *)data, count);
95 }
96 
97 int
99 {
100  uint8_t whoami = 0;
101  uint8_t expected = AK8963_DEVICE_ID;
102 
103  if (PX4_OK != read(AK8963REG_WIA, &whoami, 1)) {
104  return -EIO;
105  }
106 
107  if (whoami != expected) {
108  return -EIO;
109  }
110 
111  return OK;
112 }
113 
114 #endif
#define AK8963_DEVICE_ID
Definition: MPU9250_mag.h:45
device::Device * AK8963_I2C_interface(int bus)
#define MPU9250_REG(r)
Definition: mpu9250.h:212
#define AK8963REG_WIA
Definition: MPU9250_mag.h:47
#define AK8963_I2C_ADDR
Definition: MPU9250_mag.h:44
static void read(bootloader_app_shared_t *pshared)
Generic device / sensor interface.
uint8_t * data
Definition: dataman.cpp:149
static void write(bootloader_app_shared_t *pshared)
#define DRV_MAG_DEVTYPE_MPU9250
Definition: drv_sensor.h:58
Fundamental base class for all physical drivers (I2C, SPI).
Definition: Device.hpp:65
#define OK
Definition: uavcan_main.cpp:71
Base class for devices connected via I2C.