Merge pull request #11297 from SteveCEvans/baro_state
[betaflight.git] / src / main / drivers / accgyro / accgyro_mpu6500.c
blob72c3dfb809a32b1abb6d728dc757c4d45c2d65f2
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>
23 #include <stdlib.h>
25 #include "platform.h"
27 #include "common/axis.h"
28 #include "common/maths.h"
30 #include "drivers/exti.h"
31 #include "drivers/sensor.h"
32 #include "drivers/time.h"
34 #include "accgyro.h"
35 #include "accgyro_mpu.h"
36 #include "accgyro_mpu6500.h"
38 void mpu6500AccInit(accDev_t *acc)
40 acc->acc_1G = 512 * 4;
43 bool mpu6500AccDetect(accDev_t *acc)
45 if (acc->mpuDetectionResult.sensor != MPU_65xx_I2C) {
46 return false;
49 acc->initFn = mpu6500AccInit;
50 acc->readFn = mpuAccRead;
52 return true;
55 void mpu6500GyroInit(gyroDev_t *gyro)
57 mpuGyroInit(gyro);
59 int gyro_range = INV_FSR_2000DPS;
60 int accel_range = INV_FSR_16G;
62 if (gyro->mpuDetectionResult.sensor == ICM_20601_SPI) {
63 gyro_range = gyro->gyro_high_fsr ? ICM_HIGH_RANGE_FSR_4000DPS : ICM_HIGH_RANGE_FSR_2000DPS;
64 accel_range = ICM_HIGH_RANGE_FSR_16G;
67 busWriteRegister(&gyro->dev, MPU_RA_PWR_MGMT_1, MPU6500_BIT_RESET);
68 delay(100);
69 busWriteRegister(&gyro->dev, MPU_RA_SIGNAL_PATH_RESET, 0x07);
70 delay(100);
71 busWriteRegister(&gyro->dev, MPU_RA_PWR_MGMT_1, 0);
72 delay(100);
73 busWriteRegister(&gyro->dev, MPU_RA_PWR_MGMT_1, INV_CLK_PLL);
74 delay(15);
75 busWriteRegister(&gyro->dev, MPU_RA_GYRO_CONFIG, gyro_range << 3);
76 delay(15);
77 busWriteRegister(&gyro->dev, MPU_RA_ACCEL_CONFIG, accel_range << 3);
78 delay(15);
79 busWriteRegister(&gyro->dev, MPU_RA_CONFIG, mpuGyroDLPF(gyro));
80 delay(15);
81 busWriteRegister(&gyro->dev, MPU_RA_SMPLRT_DIV, gyro->mpuDividerDrops); // Get Divider Drops
82 delay(100);
84 // Data ready interrupt configuration
85 #ifdef USE_MPU9250_MAG
86 busWriteRegister(&gyro->dev, MPU_RA_INT_PIN_CFG, MPU6500_BIT_INT_ANYRD_2CLEAR | MPU6500_BIT_BYPASS_EN); // INT_ANYRD_2CLEAR, BYPASS_EN
87 #else
88 busWriteRegister(&gyro->dev, MPU_RA_INT_PIN_CFG, MPU6500_BIT_INT_ANYRD_2CLEAR); // INT_ANYRD_2CLEAR
89 #endif
90 delay(15);
92 #ifdef USE_MPU_DATA_READY_SIGNAL
93 busWriteRegister(&gyro->dev, MPU_RA_INT_ENABLE, MPU6500_BIT_RAW_RDY_EN); // RAW_RDY_EN interrupt enable
94 #endif
95 delay(15);
98 bool mpu6500GyroDetect(gyroDev_t *gyro)
100 if (gyro->mpuDetectionResult.sensor != MPU_65xx_I2C) {
101 return false;
104 gyro->initFn = mpu6500GyroInit;
105 gyro->readFn = mpuGyroRead;
107 gyro->scale = GYRO_SCALE_2000DPS;
109 return true;