2 * This file is part of Cleanflight.
4 * Cleanflight is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * Cleanflight is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with Cleanflight. If not, see <http://www.gnu.org/licenses/>.
26 #include "build/debug.h"
28 #include "common/axis.h"
29 #include "common/maths.h"
30 #include "common/utils.h"
32 #include "drivers/bus.h"
33 #include "drivers/sensor.h"
34 #include "drivers/system.h"
35 #include "drivers/time.h"
37 #include "drivers/accgyro/accgyro.h"
38 #include "drivers/accgyro/accgyro_mpu.h"
40 #include "drivers/compass/compass.h"
41 #include "drivers/compass/compass_mpu9250.h"
43 #if defined(USE_MAG_MPU9250) && defined(USE_IMU_MPU9250)
45 // No separate hardware descriptor needed. Hardware descriptor initialization is handled by GYRO driver
47 // AK8963, mag sensor address
48 #define AK8963_MAG_I2C_ADDRESS 0x0C
49 #define AK8963_DEVICE_ID 0x48
52 #define AK8963_MAG_REG_WHO_AM_I 0x00
53 #define AK8963_MAG_REG_INFO 0x01
54 #define AK8963_MAG_REG_STATUS1 0x02
55 #define AK8963_MAG_REG_HXL 0x03
56 #define AK8963_MAG_REG_HXH 0x04
57 #define AK8963_MAG_REG_HYL 0x05
58 #define AK8963_MAG_REG_HYH 0x06
59 #define AK8963_MAG_REG_HZL 0x07
60 #define AK8963_MAG_REG_HZH 0x08
61 #define AK8963_MAG_REG_STATUS2 0x09
62 #define AK8963_MAG_REG_CNTL 0x0a
63 #define AK8963_MAG_REG_ASCT 0x0c // self test
64 #define AK8963_MAG_REG_ASAX 0x10 // Fuse ROM x-axis sensitivity adjustment value
65 #define AK8963_MAG_REG_ASAY 0x11 // Fuse ROM y-axis sensitivity adjustment value
66 #define AK8963_MAG_REG_ASAZ 0x12 // Fuse ROM z-axis sensitivity adjustment value
68 #define READ_FLAG 0x80
70 #define STATUS1_DATA_READY 0x01
71 #define STATUS1_DATA_OVERRUN 0x02
73 #define STATUS2_MAG_SENSOR_OVERFLOW 0x08
75 #define CNTL_MODE_POWER_DOWN 0x00
76 #define CNTL_MODE_ONCE 0x01
77 #define CNTL_MODE_CONT1 0x02
78 #define CNTL_MODE_CONT2 0x06
79 #define CNTL_MODE_SELF_TEST 0x08
80 #define CNTL_MODE_FUSE_ROM 0x0F
81 #define CNTL_BIT_14_BIT 0x00
82 #define CNTL_BIT_16_BIT 0x10
84 #define DETECTION_MAX_RETRY_COUNT 5
90 } mpu9250CompassReadState_e
;
93 mpu9250CompassReadState_e state
;
96 timeUs_t readStartedAt
; // time read was queued in micros.
97 } mpu9250CompassReadContext_s
;
99 static int16_t magGain
[3];
100 static mpu9250CompassReadContext_s ctx
;
101 static bool lastReadResult
= false;
102 static int16_t cachedMagData
[3];
104 static bool mpu9250SlaveI2CRead(magDev_t
* mag
, uint8_t addr
, uint8_t reg
, uint8_t *buf
, uint8_t len
)
106 // Setting up MPU9250's I2C master to read from AK8963 via internal I2C bus
107 busWrite(mag
->busDev
, MPU_RA_I2C_SLV0_ADDR
, addr
| READ_FLAG
); // set I2C slave address for read
108 busWrite(mag
->busDev
, MPU_RA_I2C_SLV0_REG
, reg
); // set I2C slave register
109 busWrite(mag
->busDev
, MPU_RA_I2C_SLV0_CTRL
, len
| 0x80); // read number of bytes
110 delay(10); // wait for transaction to complete
111 busReadBuf(mag
->busDev
, MPU_RA_EXT_SENS_DATA_00
, buf
, len
); // read I2C
115 static bool mpu9250SlaveI2CWrite(magDev_t
* mag
, uint8_t addr
, uint8_t reg
, uint8_t data
)
117 // Setting up MPU9250's I2C master to write to AK8963 via internal I2C bus
118 busWrite(mag
->busDev
, MPU_RA_I2C_SLV0_ADDR
, addr
); // set I2C slave address for write
119 busWrite(mag
->busDev
, MPU_RA_I2C_SLV0_REG
, reg
); // set I2C slave register
120 busWrite(mag
->busDev
, MPU_RA_I2C_SLV0_DO
, data
); // set I2C salve value
121 busWrite(mag
->busDev
, MPU_RA_I2C_SLV0_CTRL
, 0x81); // write 1 byte
125 static bool mpu9250SlaveI2CStartRead(magDev_t
* mag
, uint8_t addr
, uint8_t reg
, uint8_t len
)
133 busWrite(mag
->busDev
, MPU_RA_I2C_SLV0_ADDR
, addr
| READ_FLAG
); // set I2C slave address for read
134 busWrite(mag
->busDev
, MPU_RA_I2C_SLV0_REG
, reg
); // set I2C slave register
135 busWrite(mag
->busDev
, MPU_RA_I2C_SLV0_CTRL
, len
| 0x80); // read number of bytes
137 ctx
.readStartedAt
= micros();
142 static timeDelta_t
mpu9250SlaveI2CReadTimeRemaining(void)
148 timeDelta_t timeSinceStarted
= micros() - ctx
.readStartedAt
;
149 timeDelta_t timeRemaining
= 8000 - timeSinceStarted
;
151 if (timeRemaining
< 0) {
155 return timeRemaining
;
158 static bool mpu9250SlaveI2CCompleteRead(magDev_t
* mag
, uint8_t * buf
)
160 timeDelta_t timeRemaining
= mpu9250SlaveI2CReadTimeRemaining();
162 if (timeRemaining
> 0) {
163 delayMicroseconds(timeRemaining
);
167 busReadBuf(mag
->busDev
, MPU_RA_EXT_SENS_DATA_00
, buf
, ctx
.len
);
171 static bool mpu9250CompassInit(magDev_t
* mag
)
173 uint8_t calibration
[3];
176 // Do init at low speed
177 busSetSpeed(mag
->busDev
, BUS_SPEED_INITIALIZATION
);
179 mpu9250SlaveI2CWrite(mag
, AK8963_MAG_I2C_ADDRESS
, AK8963_MAG_REG_CNTL
, CNTL_MODE_POWER_DOWN
); // power down before entering fuse mode
182 mpu9250SlaveI2CWrite(mag
, AK8963_MAG_I2C_ADDRESS
, AK8963_MAG_REG_CNTL
, CNTL_MODE_FUSE_ROM
); // Enter Fuse ROM access mode
185 mpu9250SlaveI2CRead(mag
, AK8963_MAG_I2C_ADDRESS
, AK8963_MAG_REG_ASAX
, calibration
, sizeof(calibration
)); // Read the x-, y-, and z-axis calibration values
188 magGain
[X
] = calibration
[X
] + 128;
189 magGain
[Y
] = calibration
[Y
] + 128;
190 magGain
[Z
] = calibration
[Z
] + 128;
192 mpu9250SlaveI2CWrite(mag
, AK8963_MAG_I2C_ADDRESS
, AK8963_MAG_REG_CNTL
, CNTL_MODE_POWER_DOWN
); // power down after reading.
195 // Clear status registers
196 mpu9250SlaveI2CRead(mag
, AK8963_MAG_I2C_ADDRESS
, AK8963_MAG_REG_STATUS1
, &status
, 1);
197 mpu9250SlaveI2CRead(mag
, AK8963_MAG_I2C_ADDRESS
, AK8963_MAG_REG_STATUS2
, &status
, 1);
199 // Trigger first measurement
200 mpu9250SlaveI2CWrite(mag
, AK8963_MAG_I2C_ADDRESS
, AK8963_MAG_REG_CNTL
, CNTL_BIT_16_BIT
| CNTL_MODE_CONT1
);
202 busSetSpeed(mag
->busDev
, BUS_SPEED_FAST
);
206 static int16_t parseMag(uint8_t *raw
, int16_t gain
) {
207 int ret
= (int16_t)(raw
[1] << 8 | raw
[0]) * gain
/ 256;
208 return constrain(ret
, INT16_MIN
, INT16_MAX
);
211 static bool mpu9250CompassRead(magDev_t
* mag
)
216 // set magData to latest cached value
217 memcpy(&mag
->magADCRaw
, cachedMagData
, sizeof(cachedMagData
));
226 mpu9250SlaveI2CStartRead(mag
, AK8963_MAG_I2C_ADDRESS
, AK8963_MAG_REG_STATUS1
, 1);
227 ctx
.state
= WAITING_FOR_STATUS
;
228 return lastReadResult
;
230 case WAITING_FOR_STATUS
: {
231 uint32_t timeRemaining
= mpu9250SlaveI2CReadTimeRemaining();
233 return lastReadResult
;
236 ack
= mpu9250SlaveI2CCompleteRead(mag
, &buf
[0]);
237 uint8_t status
= buf
[0];
239 if (!ack
|| ((status
& STATUS1_DATA_READY
) == 0 && (status
& STATUS1_DATA_OVERRUN
) == 0)) {
240 // too early. queue the status read again
241 ctx
.state
= CHECK_STATUS
;
243 return lastReadResult
;
246 // read the 6 bytes of data and the status2 register
247 mpu9250SlaveI2CStartRead(mag
, AK8963_MAG_I2C_ADDRESS
, AK8963_MAG_REG_HXL
, 7);
248 ctx
.state
= WAITING_FOR_DATA
;
249 return lastReadResult
;
252 case WAITING_FOR_DATA
: {
253 uint32_t timeRemaining
= mpu9250SlaveI2CReadTimeRemaining();
255 return lastReadResult
;
258 ack
= mpu9250SlaveI2CCompleteRead(mag
, &buf
[0]);
264 uint8_t status2
= buf
[6];
265 if (!ack
|| (status2
& STATUS2_MAG_SENSOR_OVERFLOW
)) {
266 ctx
.state
= CHECK_STATUS
;
267 lastReadResult
= false;
268 return lastReadResult
;
271 mag
->magADCRaw
[X
] = -parseMag(buf
+ 0, magGain
[X
]);
272 mag
->magADCRaw
[Y
] = parseMag(buf
+ 2, magGain
[Y
]);
273 mag
->magADCRaw
[Z
] = -parseMag(buf
+ 4, magGain
[Z
]);
275 memcpy(cachedMagData
, &mag
->magADCRaw
, sizeof(cachedMagData
));
276 ctx
.state
= CHECK_STATUS
;
277 lastReadResult
= true;
279 return lastReadResult
;
282 bool mpu9250CompassDetect(magDev_t
* mag
)
284 // Compass on MPU9250 is only supported if MPU9250 is connected to SPI bus
285 // FIXME: We need to use gyro_to_use here, not mag_to_use
286 mag
->busDev
= busDeviceOpen(BUSTYPE_SPI
, DEVHW_MPU9250
, mag
->magSensorToUse
);
287 if (mag
->busDev
== NULL
) {
291 // Check if Gyro driver initialized the chip
292 mpuContextData_t
* ctx
= busDeviceGetScratchpadMemory(mag
->busDev
);
293 if (ctx
->chipMagicNumber
!= 0x9250) {
297 busSetSpeed(mag
->busDev
, BUS_SPEED_INITIALIZATION
);
298 for (int retryCount
= 0; retryCount
< DETECTION_MAX_RETRY_COUNT
; retryCount
++) {
302 // Initialize I2C master via SPI bus (MPU9250)
303 busWrite(mag
->busDev
, MPU_RA_INT_PIN_CFG
, 0x10); // INT_ANYRD_2CLEAR
306 busWrite(mag
->busDev
, MPU_RA_I2C_MST_CTRL
, 0x0D); // I2C multi-master / 400kHz
309 busWrite(mag
->busDev
, MPU_RA_USER_CTRL
, 0x30); // I2C master mode, SPI mode only
313 ack
= mpu9250SlaveI2CRead(mag
, AK8963_MAG_I2C_ADDRESS
, AK8963_MAG_REG_WHO_AM_I
, &sig
, 1);
314 if (ack
&& sig
== AK8963_DEVICE_ID
) { // 0x48 / 01001000 / 'H'
315 mag
->init
= mpu9250CompassInit
;
316 mag
->read
= mpu9250CompassRead
;
321 busSetSpeed(mag
->busDev
, BUS_SPEED_FAST
);