Update cloud build defines (#14080)
[betaflight.git] / src / main / drivers / accgyro / accgyro_spi_mpu6500.c
blob9691c77818d93675554190c331ff0abd3cc393c9
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 // 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)
47 UNUSED(dev);
50 uint8_t mpu6500SpiDetect(const extDevice_t *dev)
52 mpu6500SpiInit(dev);
54 const uint8_t whoAmI = spiReadRegMsk(dev, MPU_RA_WHO_AM_I);
56 uint8_t mpuDetected = MPU_NONE;
57 switch (whoAmI) {
58 case MPU6500_WHO_AM_I_CONST:
59 mpuDetected = MPU_65xx_SPI;
60 break;
61 case MPU9250_WHO_AM_I_CONST:
62 case MPU9255_WHO_AM_I_CONST:
63 mpuDetected = MPU_9250_SPI;
64 break;
65 case ICM20601_WHO_AM_I_CONST:
66 mpuDetected = ICM_20601_SPI;
67 break;
68 case ICM20602_WHO_AM_I_CONST:
69 mpuDetected = ICM_20602_SPI;
70 break;
71 case ICM20608G_WHO_AM_I_CONST:
72 mpuDetected = ICM_20608_SPI;
73 break;
74 case ICM42605_WHO_AM_I_CONST:
75 mpuDetected = ICM_42605_SPI;
76 break;
77 case ICM42688P_WHO_AM_I_CONST:
78 mpuDetected = ICM_42688P_SPI;
79 break;
80 default:
81 mpuDetected = MPU_NONE;
82 return mpuDetected;
85 return mpuDetected;
88 void mpu6500SpiAccInit(accDev_t *acc)
90 mpu6500AccInit(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);
101 delay(100);
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) {
111 case MPU_65xx_SPI:
112 case MPU_9250_SPI:
113 case ICM_20608_SPI:
114 case ICM_20602_SPI:
115 case ICM_20601_SPI:
116 break;
117 default:
118 return false;
121 acc->initFn = mpu6500SpiAccInit;
122 acc->readFn = mpuAccReadSPI;
124 return true;
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) {
131 case MPU_65xx_SPI:
132 case MPU_9250_SPI:
133 case ICM_20608_SPI:
134 case ICM_20602_SPI:
135 gyro->scale = GYRO_SCALE_2000DPS;
136 break;
137 case ICM_20601_SPI:
138 gyro->scale = (gyro->gyro_high_fsr ? GYRO_SCALE_4000DPS : GYRO_SCALE_2000DPS);
139 break;
140 default:
141 return false;
144 gyro->initFn = mpu6500SpiGyroInit;
145 gyro->readFn = mpuGyroReadSPI;
147 return true;