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/>.
21 // NOTE: This gyro is considered obsolete and may be removed in the future.
28 #ifdef USE_GYRO_MPU3050
30 #include "common/maths.h"
31 #include "common/utils.h"
33 #include "drivers/bus_i2c.h"
34 #include "drivers/exti.h"
35 #include "drivers/sensor.h"
36 #include "drivers/system.h"
37 #include "drivers/time.h"
40 #include "accgyro_mpu.h"
41 #include "accgyro_mpu3050.h"
43 // MPU3050, Standard address 0x68
44 #define MPU3050_ADDRESS 0x68
47 #define MPU3050_FS_SEL_2000DPS 0x18
48 #define MPU3050_DLPF_10HZ 0x05
49 #define MPU3050_DLPF_20HZ 0x04
50 #define MPU3050_DLPF_42HZ 0x03
51 #define MPU3050_DLPF_98HZ 0x02
52 #define MPU3050_DLPF_188HZ 0x01
53 #define MPU3050_DLPF_256HZ 0x00
55 #define MPU3050_USER_RESET 0x01
56 #define MPU3050_CLK_SEL_PLL_GX 0x01
58 static void mpu3050Init(gyroDev_t
*gyro
)
60 delay(25); // datasheet page 13 says 20ms. other stuff could have been running meanwhile. but we'll be safe
62 const bool ack
= busWriteRegister(&gyro
->bus
, MPU3050_SMPLRT_DIV
, 0);
64 failureMode(FAILURE_ACC_INIT
);
67 busWriteRegister(&gyro
->bus
, MPU3050_DLPF_FS_SYNC
, MPU3050_FS_SEL_2000DPS
| MPU3050_DLPF_256HZ
);
68 busWriteRegister(&gyro
->bus
, MPU3050_INT_CFG
, 0);
69 busWriteRegister(&gyro
->bus
, MPU3050_USER_CTRL
, MPU3050_USER_RESET
);
70 busWriteRegister(&gyro
->bus
, MPU3050_PWR_MGM
, MPU3050_CLK_SEL_PLL_GX
);
73 static bool mpu3050GyroRead(gyroDev_t
*gyro
)
77 const bool ack
= busReadRegisterBuffer(&gyro
->bus
, MPU3050_GYRO_OUT
, data
, 6);
82 gyro
->gyroADCRaw
[X
] = (int16_t)((data
[0] << 8) | data
[1]);
83 gyro
->gyroADCRaw
[Y
] = (int16_t)((data
[2] << 8) | data
[3]);
84 gyro
->gyroADCRaw
[Z
] = (int16_t)((data
[4] << 8) | data
[5]);
89 static bool mpu3050ReadTemperature(gyroDev_t
*gyro
, int16_t *tempData
)
92 if (!busReadRegisterBuffer(&gyro
->bus
, MPU3050_TEMP_OUT
, buf
, 2)) {
96 *tempData
= 35 + ((int32_t)(buf
[0] << 8 | buf
[1]) + 13200) / 280;
101 bool mpu3050Detect(gyroDev_t
*gyro
)
103 if (gyro
->mpuDetectionResult
.sensor
!= MPU_3050
) {
106 gyro
->initFn
= mpu3050Init
;
107 gyro
->readFn
= mpu3050GyroRead
;
108 gyro
->temperatureFn
= mpu3050ReadTemperature
;
110 gyro
->scale
= GYRO_SCALE_2000DPS
;