Update cloud build defines (#14080)
[betaflight.git] / src / main / drivers / accgyro / accgyro_spi_mpu6000.c
blobb94293758d78a53a8e0b4ff70919b094ebff7031
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/>.
22 * Authors:
23 * Dominic Clifton - Cleanflight implementation
24 * John Ihlein - Initial FF32 code
27 #include <stdbool.h>
28 #include <stdint.h>
30 #include "platform.h"
32 #if defined(USE_GYRO_SPI_MPU6000) || defined(USE_ACC_SPI_MPU6000)
34 #include "common/axis.h"
35 #include "common/maths.h"
37 #include "drivers/accgyro/accgyro.h"
38 #include "drivers/accgyro/accgyro_mpu.h"
39 #include "drivers/accgyro/accgyro_spi_mpu6000.h"
40 #include "drivers/bus_spi.h"
41 #include "drivers/exti.h"
42 #include "drivers/io.h"
43 #include "drivers/time.h"
44 #include "drivers/sensor.h"
45 #include "drivers/system.h"
47 static void mpu6000AccAndGyroInit(gyroDev_t *gyro);
49 // 20 MHz max SPI frequency
50 #define MPU6000_MAX_SPI_CLK_HZ 20000000
52 #define MPU6000_SHORT_THRESHOLD 82 // Any interrupt interval less than this will be recognised as the short interval of ~79us
54 // Bits
55 #define BIT_SLEEP 0x40
56 #define BIT_H_RESET 0x80
57 #define BITS_CLKSEL 0x07
58 #define MPU_CLK_SEL_PLLGYROX 0x01
59 #define MPU_CLK_SEL_PLLGYROZ 0x03
60 #define MPU_EXT_SYNC_GYROX 0x02
61 #define BITS_FS_250DPS 0x00
62 #define BITS_FS_500DPS 0x08
63 #define BITS_FS_1000DPS 0x10
64 #define BITS_FS_2000DPS 0x18
65 #define BITS_FS_2G 0x00
66 #define BITS_FS_4G 0x08
67 #define BITS_FS_8G 0x10
68 #define BITS_FS_16G 0x18
69 #define BITS_FS_MASK 0x18
70 #define BITS_DLPF_CFG_256HZ 0x00
71 #define BITS_DLPF_CFG_188HZ 0x01
72 #define BITS_DLPF_CFG_98HZ 0x02
73 #define BITS_DLPF_CFG_42HZ 0x03
74 #define BITS_DLPF_CFG_20HZ 0x04
75 #define BITS_DLPF_CFG_10HZ 0x05
76 #define BITS_DLPF_CFG_5HZ 0x06
77 #define BITS_DLPF_CFG_2100HZ_NOLPF 0x07
78 #define BITS_DLPF_CFG_MASK 0x07
79 #define BIT_INT_ANYRD_2CLEAR 0x10
80 #define BIT_RAW_RDY_EN 0x01
81 #define BIT_I2C_IF_DIS 0x10
82 #define BIT_INT_STATUS_DATA 0x01
83 #define BIT_GYRO 0x04
84 #define BIT_ACC 0x02
85 #define BIT_TEMP 0x01
87 // Product ID Description for MPU6000
88 // high 4 bits low 4 bits
89 // Product Name Product Revision
90 #define MPU6000ES_REV_C4 0x14
91 #define MPU6000ES_REV_C5 0x15
92 #define MPU6000ES_REV_D6 0x16
93 #define MPU6000ES_REV_D7 0x17
94 #define MPU6000ES_REV_D8 0x18
95 #define MPU6000_REV_C4 0x54
96 #define MPU6000_REV_C5 0x55
97 #define MPU6000_REV_D6 0x56
98 #define MPU6000_REV_D7 0x57
99 #define MPU6000_REV_D8 0x58
100 #define MPU6000_REV_D9 0x59
101 #define MPU6000_REV_D10 0x5A
103 void mpu6000SpiGyroInit(gyroDev_t *gyro)
105 extDevice_t *dev = &gyro->dev;
107 mpuGyroInit(gyro);
109 mpu6000AccAndGyroInit(gyro);
111 // Accel and Gyro DLPF Setting
112 spiWriteReg(dev, MPU6000_CONFIG, mpuGyroDLPF(gyro));
113 delayMicroseconds(1);
115 spiSetClkDivisor(dev, spiCalculateDivider(MPU6000_MAX_SPI_CLK_HZ));
117 mpuGyroRead(gyro);
119 if (((int8_t)gyro->gyroADCRaw[1]) == -1 && ((int8_t)gyro->gyroADCRaw[0]) == -1) {
120 failureMode(FAILURE_GYRO_INIT_FAILED);
124 void mpu6000SpiAccInit(accDev_t *acc)
126 acc->acc_1G = 512 * 4;
129 uint8_t mpu6000SpiDetect(const extDevice_t *dev)
131 // reset the device configuration
132 spiWriteReg(dev, MPU_RA_PWR_MGMT_1, BIT_H_RESET);
133 delay(100); // datasheet specifies a 100ms delay after reset
134 // reset the device signal paths
135 spiWriteReg(dev, MPU_RA_SIGNAL_PATH_RESET, BIT_GYRO | BIT_ACC | BIT_TEMP);
136 delay(100); // datasheet specifies a 100ms delay after signal path reset
138 const uint8_t whoAmI = spiReadRegMsk(dev, MPU_RA_WHO_AM_I);
139 delayMicroseconds(1); // Ensure CS high time is met which is violated on H7 without this delay
140 uint8_t detectedSensor = MPU_NONE;
142 if (whoAmI == MPU6000_WHO_AM_I_CONST) {
143 const uint8_t productID = spiReadRegMsk(dev, MPU_RA_PRODUCT_ID);
145 /* look for a product ID we recognise */
147 // verify product revision
148 switch (productID) {
149 case MPU6000ES_REV_C4:
150 case MPU6000ES_REV_C5:
151 case MPU6000_REV_C4:
152 case MPU6000_REV_C5:
153 case MPU6000ES_REV_D6:
154 case MPU6000ES_REV_D7:
155 case MPU6000ES_REV_D8:
156 case MPU6000_REV_D6:
157 case MPU6000_REV_D7:
158 case MPU6000_REV_D8:
159 case MPU6000_REV_D9:
160 case MPU6000_REV_D10:
161 detectedSensor = MPU_60x0_SPI;
165 return detectedSensor;
168 static void mpu6000AccAndGyroInit(gyroDev_t *gyro)
170 extDevice_t *dev = &gyro->dev;
172 // Device was already reset during detection so proceed with configuration
174 // Clock Source PPL with Z axis gyro reference
175 spiWriteReg(dev, MPU_RA_PWR_MGMT_1, MPU_CLK_SEL_PLLGYROZ);
176 delayMicroseconds(15);
178 // Disable Primary I2C Interface
179 spiWriteReg(dev, MPU_RA_USER_CTRL, BIT_I2C_IF_DIS);
180 delayMicroseconds(15);
182 spiWriteReg(dev, MPU_RA_PWR_MGMT_2, 0x00);
183 delayMicroseconds(15);
185 // Accel Sample Rate 1kHz
186 // Gyroscope Output Rate = 1kHz when the DLPF is enabled
187 spiWriteReg(dev, MPU_RA_SMPLRT_DIV, gyro->mpuDividerDrops);
188 delayMicroseconds(15);
190 // Gyro +/- 2000 DPS Full Scale
191 spiWriteReg(dev, MPU_RA_GYRO_CONFIG, INV_FSR_2000DPS << 3);
192 delayMicroseconds(15);
194 // Accel +/- 16 G Full Scale
195 spiWriteReg(dev, MPU_RA_ACCEL_CONFIG, INV_FSR_16G << 3);
196 delayMicroseconds(15);
198 spiWriteReg(dev, MPU_RA_INT_PIN_CFG, 0 << 7 | 0 << 6 | 0 << 5 | 1 << 4 | 0 << 3 | 0 << 2 | 0 << 1 | 0 << 0); // INT_ANYRD_2CLEAR
199 delayMicroseconds(15);
201 spiWriteReg(dev, MPU_RA_INT_ENABLE, MPU_RF_DATA_RDY_EN);
202 delayMicroseconds(15);
205 bool mpu6000SpiAccDetect(accDev_t *acc)
207 if (acc->mpuDetectionResult.sensor != MPU_60x0_SPI) {
208 return false;
211 acc->initFn = mpu6000SpiAccInit;
212 acc->readFn = mpuAccReadSPI;
214 return true;
217 bool mpu6000SpiGyroDetect(gyroDev_t *gyro)
219 if (gyro->mpuDetectionResult.sensor != MPU_60x0_SPI) {
220 return false;
223 gyro->initFn = mpu6000SpiGyroInit;
224 gyro->readFn = mpuGyroReadSPI;
225 gyro->scale = GYRO_SCALE_2000DPS;
226 gyro->gyroShortPeriod = clockMicrosToCycles(MPU6000_SHORT_THRESHOLD);
228 return true;
231 #endif