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 // 1 MHz max SPI frequency for initialisation
41 #define MPU6500_MAX_SPI_INIT_CLK_HZ 1000000
42 // 20 MHz max SPI frequency
43 #define MPU6500_MAX_SPI_CLK_HZ 20000000
45 #define BIT_SLEEP 0x40
47 static void mpu6500SpiInit(const extDevice_t
*dev
)
50 spiSetClkDivisor(dev
, spiCalculateDivider(MPU6500_MAX_SPI_CLK_HZ
));
53 uint8_t mpu6500SpiDetect(const extDevice_t
*dev
)
57 const uint8_t whoAmI
= spiReadRegMsk(dev
, MPU_RA_WHO_AM_I
);
59 uint8_t mpuDetected
= MPU_NONE
;
61 case MPU6500_WHO_AM_I_CONST
:
62 mpuDetected
= MPU_65xx_SPI
;
64 case MPU9250_WHO_AM_I_CONST
:
65 case MPU9255_WHO_AM_I_CONST
:
66 mpuDetected
= MPU_9250_SPI
;
68 case ICM20601_WHO_AM_I_CONST
:
69 mpuDetected
= ICM_20601_SPI
;
71 case ICM20602_WHO_AM_I_CONST
:
72 mpuDetected
= ICM_20602_SPI
;
74 case ICM20608G_WHO_AM_I_CONST
:
75 mpuDetected
= ICM_20608_SPI
;
77 case ICM42605_WHO_AM_I_CONST
:
78 mpuDetected
= ICM_42605_SPI
;
80 case ICM42688P_WHO_AM_I_CONST
:
81 mpuDetected
= ICM_42688P_SPI
;
84 mpuDetected
= MPU_NONE
;
89 void mpu6500SpiAccInit(accDev_t
*acc
)
94 void mpu6500SpiGyroInit(gyroDev_t
*gyro
)
96 spiSetClkDivisor(&gyro
->dev
, spiCalculateDivider(MPU6500_MAX_SPI_INIT_CLK_HZ
));
99 mpu6500GyroInit(gyro
);
101 // Disable Primary I2C Interface
102 spiWriteReg(&gyro
->dev
, MPU_RA_USER_CTRL
, MPU6500_BIT_I2C_IF_DIS
);
105 spiSetClkDivisor(&gyro
->dev
, spiCalculateDivider(MPU6500_MAX_SPI_CLK_HZ
));
106 delayMicroseconds(1);
109 bool mpu6500SpiAccDetect(accDev_t
*acc
)
111 // MPU6500 is used as a equivalent of other accelerometers by some flight controllers
112 switch (acc
->mpuDetectionResult
.sensor
) {
123 acc
->initFn
= mpu6500SpiAccInit
;
124 acc
->readFn
= mpuAccReadSPI
;
129 bool mpu6500SpiGyroDetect(gyroDev_t
*gyro
)
131 // MPU6500 is used as a equivalent of other gyros by some flight controllers
132 switch (gyro
->mpuDetectionResult
.sensor
) {
137 gyro
->scale
= GYRO_SCALE_2000DPS
;
140 gyro
->scale
= (gyro
->gyro_high_fsr
? GYRO_SCALE_4000DPS
: GYRO_SCALE_2000DPS
);
146 gyro
->initFn
= mpu6500SpiGyroInit
;
147 gyro
->readFn
= mpuGyroReadSPI
;