2 * This file is part of Cleanflight and Betaflight.
4 * Cleanflight and Betaflight are free software. You can redistribute
5 * this software and/or modify this software under the terms of the
6 * GNU General Public License as published by the Free Software
7 * Foundation, either version 3 of the License, or (at your option)
10 * Cleanflight and Betaflight are distributed in the hope that they
11 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
12 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * See the GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this software.
18 * If not, see <http://www.gnu.org/licenses/>.
26 #include "common/axis.h"
27 #include "common/maths.h"
29 #include "drivers/bus_spi.h"
30 #include "drivers/exti.h"
31 #include "drivers/io.h"
32 #include "drivers/sensor.h"
33 #include "drivers/time.h"
36 #include "accgyro_mpu.h"
37 #include "accgyro_mpu6500.h"
38 #include "accgyro_spi_mpu6500.h"
40 // 20 MHz max SPI frequency
41 #define MPU6500_MAX_SPI_CLK_HZ 20000000
43 #define BIT_SLEEP 0x40
45 static void mpu6500SpiInit(const extDevice_t
*dev
)
50 uint8_t mpu6500SpiDetect(const extDevice_t
*dev
)
54 const uint8_t whoAmI
= spiReadRegMsk(dev
, MPU_RA_WHO_AM_I
);
56 uint8_t mpuDetected
= MPU_NONE
;
58 case MPU6500_WHO_AM_I_CONST
:
59 mpuDetected
= MPU_65xx_SPI
;
61 case MPU9250_WHO_AM_I_CONST
:
62 case MPU9255_WHO_AM_I_CONST
:
63 mpuDetected
= MPU_9250_SPI
;
65 case ICM20601_WHO_AM_I_CONST
:
66 mpuDetected
= ICM_20601_SPI
;
68 case ICM20602_WHO_AM_I_CONST
:
69 mpuDetected
= ICM_20602_SPI
;
71 case ICM20608G_WHO_AM_I_CONST
:
72 mpuDetected
= ICM_20608_SPI
;
74 case ICM42605_WHO_AM_I_CONST
:
75 mpuDetected
= ICM_42605_SPI
;
77 case ICM42688P_WHO_AM_I_CONST
:
78 mpuDetected
= ICM_42688P_SPI
;
81 mpuDetected
= MPU_NONE
;
88 void mpu6500SpiAccInit(accDev_t
*acc
)
93 void mpu6500SpiGyroInit(gyroDev_t
*gyro
)
95 extDevice_t
*dev
= &gyro
->dev
;
97 mpu6500GyroInit(gyro
);
99 // Disable Primary I2C Interface
100 spiWriteReg(dev
, MPU_RA_USER_CTRL
, MPU6500_BIT_I2C_IF_DIS
);
103 spiSetClkDivisor(dev
, spiCalculateDivider(MPU6500_MAX_SPI_CLK_HZ
));
104 delayMicroseconds(1);
107 bool mpu6500SpiAccDetect(accDev_t
*acc
)
109 // MPU6500 is used as a equivalent of other accelerometers by some flight controllers
110 switch (acc
->mpuDetectionResult
.sensor
) {
121 acc
->initFn
= mpu6500SpiAccInit
;
122 acc
->readFn
= mpuAccReadSPI
;
127 bool mpu6500SpiGyroDetect(gyroDev_t
*gyro
)
129 // MPU6500 is used as a equivalent of other gyros by some flight controllers
130 switch (gyro
->mpuDetectionResult
.sensor
) {
135 gyro
->scale
= GYRO_SCALE_2000DPS
;
138 gyro
->scale
= (gyro
->gyro_high_fsr
? GYRO_SCALE_4000DPS
: GYRO_SCALE_2000DPS
);
144 gyro
->initFn
= mpu6500SpiGyroInit
;
145 gyro
->readFn
= mpuGyroReadSPI
;