Merge pull request #11297 from SteveCEvans/baro_state
[betaflight.git] / src / main / drivers / accgyro / accgyro_mpu3050.c
bloba52b1a7984abcc27853687049b06f989bf4c03a6
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 // NOTE: This gyro is considered obsolete and may be removed in the future.
23 #include <stdbool.h>
24 #include <stdint.h>
26 #include "platform.h"
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"
39 #include "accgyro.h"
40 #include "accgyro_mpu.h"
41 #include "accgyro_mpu3050.h"
43 // MPU3050, Standard address 0x68
44 #define MPU3050_ADDRESS 0x68
46 // Bits
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);
63 if (!ack) {
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)
75 uint8_t data[6];
77 const bool ack = busReadRegisterBuffer(&gyro->bus, MPU3050_GYRO_OUT, data, 6);
78 if (!ack) {
79 return false;
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]);
86 return true;
89 static bool mpu3050ReadTemperature(gyroDev_t *gyro, int16_t *tempData)
91 uint8_t buf[2];
92 if (!busReadRegisterBuffer(&gyro->bus, MPU3050_TEMP_OUT, buf, 2)) {
93 return false;
96 *tempData = 35 + ((int32_t)(buf[0] << 8 | buf[1]) + 13200) / 280;
98 return true;
101 bool mpu3050Detect(gyroDev_t *gyro)
103 if (gyro->mpuDetectionResult.sensor != MPU_3050) {
104 return false;
106 gyro->initFn = mpu3050Init;
107 gyro->readFn = mpu3050GyroRead;
108 gyro->temperatureFn = mpu3050ReadTemperature;
110 gyro->scale = GYRO_SCALE_2000DPS;
112 return true;
114 #endif