2 * This file is part of Cleanflight.
4 * Cleanflight is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * Cleanflight is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with Cleanflight. If not, see <http://www.gnu.org/licenses/>.
23 #include "drivers/bus.h"
24 #include "drivers/sensor.h"
25 #include "drivers/system.h"
26 #include "drivers/time.h"
28 #include "drivers/accgyro/accgyro_mpu.h"
30 #include "drivers/compass/compass.h"
31 #include "drivers/compass/compass_ak8963.h"
32 #include "drivers/compass/compass_mpu925x_ak8963.h"
34 #if defined(USE_MAG_MPU925X_AK8963)
36 #define MPU925X_I2C_ADDRESS 0x68
37 #define AK8963_MAG_I2C_ADDRESS 0x0C
38 #define MPU9250_BIT_RESET 0x80
40 static bool mpu925xDeviceDetect(const extDevice_t
* dev
)
42 busWriteRegister(dev
, MPU_RA_PWR_MGMT_1
, MPU9250_BIT_RESET
);
44 switch (busReadRegister(dev
, MPU_RA_WHO_AM_I
)) {
45 case MPU9250_WHO_AM_I_CONST
:
46 case MPU9255_WHO_AM_I_CONST
:
53 bool mpu925Xak8963CompassDetect(magDev_t
* mag
)
55 extDevice_t
*dev
= &mag
->dev
;
56 busDeviceRegister(dev
);
57 if (!mpu925xDeviceDetect(dev
->bus
->busType_u
.mpuSlave
.master
)) {
60 // set bypass mode on mpu9250
61 busWriteRegister(dev
->bus
->busType_u
.mpuSlave
.master
, MPU_RA_INT_PIN_CFG
, 0x02);
63 // now we have ak8963 alike on the bus
64 dev
->busType_u
.i2c
.address
= AK8963_MAG_I2C_ADDRESS
;
65 busDeviceRegister(dev
);
66 if(!ak8963Detect(mag
)) {
67 // if ak8963 is not detected, reset the MPU to disable bypass mode
68 dev
->busType_u
.i2c
.address
= MPU925X_I2C_ADDRESS
;
69 busWriteRegister(dev
, MPU_RA_PWR_MGMT_1
, MPU9250_BIT_RESET
);