Removed excess trailing spaces before new lines on licenses.
[betaflight.git] / src / main / drivers / accgyro / accgyro_mma845x.c
blob35021beff69f030d969c430606c4029ab34eefef
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 #include "drivers/io.h"
27 #include "drivers/bus_i2c.h"
29 #include "drivers/sensor.h"
30 #include "accgyro.h"
31 #include "accgyro_mma845x.h"
33 #ifndef MMA8452_I2C_INSTANCE
34 #define MMA8452_I2C_INSTANCE I2CDEV_1
35 #endif
37 // MMA8452QT, Standard address 0x1C
38 // ACC_INT2 routed to PA5
40 #define MMA8452_ADDRESS 0x1C
42 #define MMA8452_DEVICE_SIGNATURE 0x2A
43 #define MMA8451_DEVICE_SIGNATURE 0x1A
45 #define MMA8452_STATUS 0x00
46 #define MMA8452_OUT_X_MSB 0x01
47 #define MMA8452_WHO_AM_I 0x0D
48 #define MMA8452_XYZ_DATA_CFG 0x0E
49 #define MMA8452_HP_FILTER_CUTOFF 0x0F
50 #define MMA8452_CTRL_REG1 0x2A
51 #define MMA8452_CTRL_REG2 0x2B
52 #define MMA8452_CTRL_REG3 0x2C
53 #define MMA8452_CTRL_REG4 0x2D
54 #define MMA8452_CTRL_REG5 0x2E
56 #define MMA8452_FS_RANGE_8G 0x02
57 #define MMA8452_FS_RANGE_4G 0x01
58 #define MMA8452_FS_RANGE_2G 0x00
60 #define MMA8452_HPF_CUTOFF_LV1 0x00
61 #define MMA8452_HPF_CUTOFF_LV2 0x01
62 #define MMA8452_HPF_CUTOFF_LV3 0x02
63 #define MMA8452_HPF_CUTOFF_LV4 0x03
65 #define MMA8452_CTRL_REG2_B7_ST 0x80
66 #define MMA8452_CTRL_REG2_B6_RST 0x40
67 #define MMA8452_CTRL_REG2_B4_SMODS1 0x10
68 #define MMA8452_CTRL_REG2_B3_SMODS0 0x08
69 #define MMA8452_CTRL_REG2_B2_SLPE 0x04
70 #define MMA8452_CTRL_REG2_B1_MODS1 0x02
71 #define MMA8452_CTRL_REG2_B0_MODS0 0x01
73 #define MMA8452_CTRL_REG2_MODS_LP 0x03
74 #define MMA8452_CTRL_REG2_MODS_HR 0x02
75 #define MMA8452_CTRL_REG2_MODS_LNLP 0x01
76 #define MMA8452_CTRL_REG2_MODS_NOR 0x00
78 #define MMA8452_CTRL_REG3_IPOL 0x02
79 #define MMA8452_CTRL_REG4_INT_EN_DRDY 0x01
81 #define MMA8452_CTRL_REG1_LNOISE 0x04
82 #define MMA8452_CTRL_REG1_ACTIVE 0x01
84 static uint8_t device_id;
86 static inline void mma8451ConfigureInterrupt(void)
88 #ifdef MMA8451_INT_PIN
89 IOInit(IOGetByTag(IO_TAG(MMA8451_INT_PIN)), OWNER_MPU_EXTI, 0);
90 // TODO - maybe pullup / pulldown ?
91 IOConfigGPIO(IOGetByTag(IO_TAG(MMA8451_INT_PIN)), IOCFG_IN_FLOATING);
92 #endif
94 i2cWrite(MPU_I2C_INSTANCE, MMA8452_ADDRESS, MMA8452_CTRL_REG3, MMA8452_CTRL_REG3_IPOL); // Interrupt polarity (active HIGH)
95 i2cWrite(MPU_I2C_INSTANCE, MMA8452_ADDRESS, MMA8452_CTRL_REG4, MMA8452_CTRL_REG4_INT_EN_DRDY); // Enable DRDY interrupt (unused by this driver)
96 i2cWrite(MPU_I2C_INSTANCE, MMA8452_ADDRESS, MMA8452_CTRL_REG5, 0); // DRDY routed to INT2
99 static void mma8452Init(accDev_t *acc)
102 i2cWrite(MPU_I2C_INSTANCE, MMA8452_ADDRESS, MMA8452_CTRL_REG1, 0); // Put device in standby to configure stuff
103 i2cWrite(MPU_I2C_INSTANCE, MMA8452_ADDRESS, MMA8452_XYZ_DATA_CFG, MMA8452_FS_RANGE_8G);
104 i2cWrite(MPU_I2C_INSTANCE, MMA8452_ADDRESS, MMA8452_HP_FILTER_CUTOFF, MMA8452_HPF_CUTOFF_LV4);
105 i2cWrite(MPU_I2C_INSTANCE, MMA8452_ADDRESS, MMA8452_CTRL_REG2, MMA8452_CTRL_REG2_MODS_HR | MMA8452_CTRL_REG2_MODS_HR << 3); // High resolution measurement in both sleep and active modes
107 mma8451ConfigureInterrupt();
109 i2cWrite(MPU_I2C_INSTANCE, MMA8452_ADDRESS, MMA8452_CTRL_REG1, MMA8452_CTRL_REG1_LNOISE | MMA8452_CTRL_REG1_ACTIVE); // Turn on measurements, low noise at max scale mode, Data Rate 800Hz. LNoise mode makes range +-4G.
111 acc->acc_1G = 256;
114 static bool mma8452Read(accDev_t *acc)
116 uint8_t buf[6];
118 if (!i2cRead(MPU_I2C_INSTANCE, MMA8452_ADDRESS, MMA8452_OUT_X_MSB, 6, buf)) {
119 return false;
122 acc->ADCRaw[0] = ((int16_t)((buf[0] << 8) | buf[1]) >> 2) / 4;
123 acc->ADCRaw[1] = ((int16_t)((buf[2] << 8) | buf[3]) >> 2) / 4;
124 acc->ADCRaw[2] = ((int16_t)((buf[4] << 8) | buf[5]) >> 2) / 4;
126 return true;
129 bool mma8452Detect(accDev_t *acc)
131 uint8_t sig = 0;
132 bool ack = i2cRead(MPU_I2C_INSTANCE, MMA8452_ADDRESS, MMA8452_WHO_AM_I, 1, &sig);
134 if (!ack || (sig != MMA8452_DEVICE_SIGNATURE && sig != MMA8451_DEVICE_SIGNATURE))
135 return false;
137 acc->initFn = mma8452Init;
138 acc->readFn = mma8452Read;
139 device_id = sig;
140 return true;