Merge pull request #11297 from SteveCEvans/baro_state
[betaflight.git] / src / main / drivers / accgyro_legacy / accgyro_bma280.c
blob5eab61d8ecf53a63335fc54cde96bacd5ec0da10
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 #ifdef USE_ACC_BMA280
28 #include "drivers/bus_i2c.h"
30 #include "drivers/sensor.h"
31 #include "drivers/accgyro/accgyro.h"
32 #include "accgyro_bma280.h"
34 // BMA280, default I2C address mode 0x18
35 #define BMA280_ADDRESS 0x18
36 #define BMA280_ACC_X_LSB 0x02
37 #define BMA280_PMU_BW 0x10
38 #define BMA280_PMU_RANGE 0x0F
40 static void bma280Init(accDev_t *acc)
42 i2cWrite(MPU_I2C_INSTANCE, BMA280_ADDRESS, BMA280_PMU_RANGE, 0x08); // +-8g range
43 i2cWrite(MPU_I2C_INSTANCE, BMA280_ADDRESS, BMA280_PMU_BW, 0x0E); // 500Hz BW
45 acc->acc_1G = 512 * 8;
48 static bool bma280Read(accDev_t *acc)
50 uint8_t buf[6];
52 if (!i2cRead(MPU_I2C_INSTANCE, BMA280_ADDRESS, BMA280_ACC_X_LSB, 6, buf)) {
53 return false;
56 // Data format is lsb<5:0><crap><new_data_bit> | msb<13:6>
57 acc->ADCRaw[0] = (int16_t)((buf[0] >> 2) + (buf[1] << 8));
58 acc->ADCRaw[1] = (int16_t)((buf[2] >> 2) + (buf[3] << 8));
59 acc->ADCRaw[2] = (int16_t)((buf[4] >> 2) + (buf[5] << 8));
61 return true;
64 bool bma280Detect(accDev_t *acc)
66 uint8_t sig = 0;
67 bool ack = i2cRead(MPU_I2C_INSTANCE, BMA280_ADDRESS, 0x00, 1, &sig);
69 if (!ack || sig != 0xFB)
70 return false;
72 acc->initFn = bma280Init;
73 acc->readFn = bma280Read;
74 return true;
76 #endif