Merge pull request #11297 from SteveCEvans/baro_state
[betaflight.git] / src / main / drivers / accgyro / accgyro_spi_mpu6500.c
blob573b9f1db34afe8ad88b4c9a8df79a97fff62dd7
1 /*
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)
8 * any later version.
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/>.
21 #include <stdbool.h>
22 #include <stdint.h>
24 #include "platform.h"
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"
35 #include "accgyro.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)
55 mpu6500SpiInit(dev);
57 const uint8_t whoAmI = spiReadRegMsk(dev, MPU_RA_WHO_AM_I);
59 uint8_t mpuDetected = MPU_NONE;
60 switch (whoAmI) {
61 case MPU6500_WHO_AM_I_CONST:
62 mpuDetected = MPU_65xx_SPI;
63 break;
64 case MPU9250_WHO_AM_I_CONST:
65 case MPU9255_WHO_AM_I_CONST:
66 mpuDetected = MPU_9250_SPI;
67 break;
68 case ICM20601_WHO_AM_I_CONST:
69 mpuDetected = ICM_20601_SPI;
70 break;
71 case ICM20602_WHO_AM_I_CONST:
72 mpuDetected = ICM_20602_SPI;
73 break;
74 case ICM20608G_WHO_AM_I_CONST:
75 mpuDetected = ICM_20608_SPI;
76 break;
77 case ICM42605_WHO_AM_I_CONST:
78 mpuDetected = ICM_42605_SPI;
79 break;
80 case ICM42688P_WHO_AM_I_CONST:
81 mpuDetected = ICM_42688P_SPI;
82 break;
83 default:
84 mpuDetected = MPU_NONE;
86 return mpuDetected;
89 void mpu6500SpiAccInit(accDev_t *acc)
91 mpu6500AccInit(acc);
94 void mpu6500SpiGyroInit(gyroDev_t *gyro)
96 spiSetClkDivisor(&gyro->dev, spiCalculateDivider(MPU6500_MAX_SPI_INIT_CLK_HZ));
97 delayMicroseconds(1);
99 mpu6500GyroInit(gyro);
101 // Disable Primary I2C Interface
102 spiWriteReg(&gyro->dev, MPU_RA_USER_CTRL, MPU6500_BIT_I2C_IF_DIS);
103 delay(100);
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) {
113 case MPU_65xx_SPI:
114 case MPU_9250_SPI:
115 case ICM_20608_SPI:
116 case ICM_20602_SPI:
117 case ICM_20601_SPI:
118 break;
119 default:
120 return false;
123 acc->initFn = mpu6500SpiAccInit;
124 acc->readFn = mpuAccReadSPI;
126 return true;
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) {
133 case MPU_65xx_SPI:
134 case MPU_9250_SPI:
135 case ICM_20608_SPI:
136 case ICM_20602_SPI:
137 gyro->scale = GYRO_SCALE_2000DPS;
138 break;
139 case ICM_20601_SPI:
140 gyro->scale = (gyro->gyro_high_fsr ? GYRO_SCALE_4000DPS : GYRO_SCALE_2000DPS);
141 break;
142 default:
143 return false;
146 gyro->initFn = mpu6500SpiGyroInit;
147 gyro->readFn = mpuGyroReadSPI;
149 return true;