Merge pull request #11939 from blckmn/flash-fix
[betaflight.git] / src / main / drivers / accgyro / accgyro_mpu.c
blob2ded5b0d7ff720bb06776245a051b0b45a86ac02
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>
23 #include <stdlib.h>
24 #include <string.h>
26 #include "platform.h"
28 #include "build/atomic.h"
29 #include "build/build_config.h"
30 #include "build/debug.h"
32 #include "common/maths.h"
33 #include "common/utils.h"
35 #include "drivers/bus.h"
36 #include "drivers/bus_i2c.h"
37 #include "drivers/bus_spi.h"
38 #include "drivers/exti.h"
39 #include "drivers/io.h"
40 #include "drivers/nvic.h"
41 #include "drivers/sensor.h"
42 #include "drivers/system.h"
43 #include "drivers/time.h"
45 #include "drivers/accgyro/accgyro.h"
46 #include "drivers/accgyro/accgyro_mpu3050.h"
47 #include "drivers/accgyro/accgyro_mpu6050.h"
48 #include "drivers/accgyro/accgyro_mpu6500.h"
49 #include "drivers/accgyro/accgyro_spi_bmi160.h"
50 #include "drivers/accgyro/accgyro_spi_bmi270.h"
51 #include "drivers/accgyro/accgyro_spi_icm20649.h"
52 #include "drivers/accgyro/accgyro_spi_icm20689.h"
53 #include "drivers/accgyro/accgyro_spi_icm426xx.h"
54 #include "drivers/accgyro/accgyro_spi_lsm6dso.h"
55 #include "drivers/accgyro/accgyro_spi_mpu6000.h"
56 #include "drivers/accgyro/accgyro_spi_mpu6500.h"
57 #include "drivers/accgyro/accgyro_spi_mpu9250.h"
58 #include "drivers/accgyro/accgyro_spi_l3gd20.h"
59 #include "drivers/accgyro/accgyro_mpu.h"
61 #include "pg/pg.h"
62 #include "pg/gyrodev.h"
64 #ifndef MPU_ADDRESS
65 #define MPU_ADDRESS 0x68
66 #endif
68 // 1 MHz max SPI frequency during device detection
69 #define MPU_MAX_SPI_DETECT_CLK_HZ 1000000
71 #define MPU_INQUIRY_MASK 0x7E
73 // Allow 100ms before attempting to access SPI bus
74 #define GYRO_SPI_STARTUP_MS 100
76 // Need to see at least this many interrupts during initialisation to confirm EXTI connectivity
77 #define GYRO_EXTI_DETECT_THRESHOLD 1000
79 #ifdef USE_I2C_GYRO
80 static void mpu6050FindRevision(gyroDev_t *gyro)
82 // There is a map of revision contained in the android source tree which is quite comprehensive and may help to understand this code
83 // See https://android.googlesource.com/kernel/msm.git/+/eaf36994a3992b8f918c18e4f7411e8b2320a35f/drivers/misc/mpu6050/mldl_cfg.c
85 // determine product ID and revision
86 uint8_t readBuffer[6];
87 bool ack = busReadRegisterBuffer(&gyro->dev, MPU_RA_XA_OFFS_H, readBuffer, 6);
88 uint8_t revision = ((readBuffer[5] & 0x01) << 2) | ((readBuffer[3] & 0x01) << 1) | (readBuffer[1] & 0x01);
89 if (ack && revision) {
90 // Congrats, these parts are better
91 if (revision == 1) {
92 gyro->mpuDetectionResult.resolution = MPU_HALF_RESOLUTION;
93 } else if (revision == 2) {
94 gyro->mpuDetectionResult.resolution = MPU_FULL_RESOLUTION;
95 } else if ((revision == 3) || (revision == 7)) {
96 gyro->mpuDetectionResult.resolution = MPU_FULL_RESOLUTION;
97 } else {
98 failureMode(FAILURE_ACC_INCOMPATIBLE);
100 } else {
101 uint8_t productId;
102 ack = busReadRegisterBuffer(&gyro->dev, MPU_RA_PRODUCT_ID, &productId, 1);
103 revision = productId & 0x0F;
104 if (!ack || revision == 0) {
105 failureMode(FAILURE_ACC_INCOMPATIBLE);
106 } else if (revision == 4) {
107 gyro->mpuDetectionResult.resolution = MPU_HALF_RESOLUTION;
108 } else {
109 gyro->mpuDetectionResult.resolution = MPU_FULL_RESOLUTION;
113 #endif
116 * Gyro interrupt service routine
118 #ifdef USE_SPI_GYRO
119 // Called in ISR context
120 // Gyro read has just completed
121 busStatus_e mpuIntcallback(uint32_t arg)
123 gyroDev_t *gyro = (gyroDev_t *)arg;
124 int32_t gyroDmaDuration = cmpTimeCycles(getCycleCounter(), gyro->gyroLastEXTI);
126 if (gyroDmaDuration > gyro->gyroDmaMaxDuration) {
127 gyro->gyroDmaMaxDuration = gyroDmaDuration;
130 gyro->dataReady = true;
132 return BUS_READY;
135 static void mpuIntExtiHandler(extiCallbackRec_t *cb)
137 gyroDev_t *gyro = container_of(cb, gyroDev_t, exti);
139 // Ideally we'd use a timer to capture such information, but unfortunately the port used for EXTI interrupt does
140 // not have an associated timer
141 uint32_t nowCycles = getCycleCounter();
142 int32_t gyroLastPeriod = cmpTimeCycles(nowCycles, gyro->gyroLastEXTI);
143 // This detects the short (~79us) EXTI interval of an MPU6xxx gyro
144 if ((gyro->gyroShortPeriod == 0) || (gyroLastPeriod < gyro->gyroShortPeriod)) {
145 gyro->gyroSyncEXTI = gyro->gyroLastEXTI + gyro->gyroDmaMaxDuration;
147 gyro->gyroLastEXTI = nowCycles;
149 if (gyro->gyroModeSPI == GYRO_EXTI_INT_DMA) {
150 spiSequence(&gyro->dev, gyro->segments);
153 gyro->detectedEXTI++;
155 #else
156 static void mpuIntExtiHandler(extiCallbackRec_t *cb)
158 gyroDev_t *gyro = container_of(cb, gyroDev_t, exti);
159 gyro->dataReady = true;
161 #endif
163 static void mpuIntExtiInit(gyroDev_t *gyro)
165 if (gyro->mpuIntExtiTag == IO_TAG_NONE) {
166 return;
169 const IO_t mpuIntIO = IOGetByTag(gyro->mpuIntExtiTag);
171 #ifdef ENSURE_MPU_DATA_READY_IS_LOW
172 uint8_t status = IORead(mpuIntIO);
173 if (status) {
174 return;
176 #endif
178 IOInit(mpuIntIO, OWNER_GYRO_EXTI, 0);
179 EXTIHandlerInit(&gyro->exti, mpuIntExtiHandler);
180 EXTIConfig(mpuIntIO, &gyro->exti, NVIC_PRIO_MPU_INT_EXTI, IOCFG_IN_FLOATING, BETAFLIGHT_EXTI_TRIGGER_RISING);
181 EXTIEnable(mpuIntIO);
184 bool mpuAccRead(accDev_t *acc)
186 uint8_t data[6];
188 const bool ack = busReadRegisterBuffer(&acc->gyro->dev, acc->gyro->accDataReg, data, 6);
189 if (!ack) {
190 return false;
193 acc->ADCRaw[X] = (int16_t)((data[0] << 8) | data[1]);
194 acc->ADCRaw[Y] = (int16_t)((data[2] << 8) | data[3]);
195 acc->ADCRaw[Z] = (int16_t)((data[4] << 8) | data[5]);
197 return true;
200 bool mpuGyroRead(gyroDev_t *gyro)
202 uint8_t data[6];
204 const bool ack = busReadRegisterBuffer(&gyro->dev, gyro->gyroDataReg, data, 6);
205 if (!ack) {
206 return false;
209 gyro->gyroADCRaw[X] = (int16_t)((data[0] << 8) | data[1]);
210 gyro->gyroADCRaw[Y] = (int16_t)((data[2] << 8) | data[3]);
211 gyro->gyroADCRaw[Z] = (int16_t)((data[4] << 8) | data[5]);
213 return true;
217 #ifdef USE_SPI_GYRO
218 bool mpuAccReadSPI(accDev_t *acc)
220 switch (acc->gyro->gyroModeSPI) {
221 case GYRO_EXTI_INT:
222 case GYRO_EXTI_NO_INT:
224 acc->gyro->dev.txBuf[0] = acc->gyro->accDataReg | 0x80;
226 busSegment_t segments[] = {
227 {.u.buffers = {NULL, NULL}, 7, true, NULL},
228 {.u.link = {NULL, NULL}, 0, true, NULL},
230 segments[0].u.buffers.txData = acc->gyro->dev.txBuf;
231 segments[0].u.buffers.rxData = &acc->gyro->dev.rxBuf[1];
233 spiSequence(&acc->gyro->dev, &segments[0]);
235 // Wait for completion
236 spiWait(&acc->gyro->dev);
238 // Fall through
239 FALLTHROUGH;
242 case GYRO_EXTI_INT_DMA:
244 // If read was triggered in interrupt don't bother waiting. The worst that could happen is that we pick
245 // up an old value.
247 // This data was read from the gyro, which is the same SPI device as the acc
248 int16_t *accData = (int16_t *)acc->gyro->dev.rxBuf;
249 acc->ADCRaw[X] = __builtin_bswap16(accData[1]);
250 acc->ADCRaw[Y] = __builtin_bswap16(accData[2]);
251 acc->ADCRaw[Z] = __builtin_bswap16(accData[3]);
252 break;
255 case GYRO_EXTI_INIT:
256 default:
257 break;
260 return true;
263 bool mpuGyroReadSPI(gyroDev_t *gyro)
265 int16_t *gyroData = (int16_t *)gyro->dev.rxBuf;
266 switch (gyro->gyroModeSPI) {
267 case GYRO_EXTI_INIT:
269 // Initialise the tx buffer to all 0xff
270 memset(gyro->dev.txBuf, 0xff, 16);
272 // Check that minimum number of interrupts have been detected
274 // We need some offset from the gyro interrupts to ensure sampling after the interrupt
275 gyro->gyroDmaMaxDuration = 5;
276 if (gyro->detectedEXTI > GYRO_EXTI_DETECT_THRESHOLD) {
277 if (spiUseDMA(&gyro->dev)) {
278 gyro->dev.callbackArg = (uint32_t)gyro;
279 gyro->dev.txBuf[0] = gyro->accDataReg | 0x80;
280 gyro->segments[0].len = gyro->gyroDataReg - gyro->accDataReg + 7;
281 gyro->segments[0].callback = mpuIntcallback;
282 gyro->segments[0].u.buffers.txData = gyro->dev.txBuf;
283 gyro->segments[0].u.buffers.rxData = &gyro->dev.rxBuf[1];
284 gyro->segments[0].negateCS = true;
285 gyro->gyroModeSPI = GYRO_EXTI_INT_DMA;
286 } else {
287 // Interrupts are present, but no DMA
288 gyro->gyroModeSPI = GYRO_EXTI_INT;
290 } else {
291 gyro->gyroModeSPI = GYRO_EXTI_NO_INT;
293 break;
296 case GYRO_EXTI_INT:
297 case GYRO_EXTI_NO_INT:
299 gyro->dev.txBuf[0] = gyro->gyroDataReg | 0x80;
301 busSegment_t segments[] = {
302 {.u.buffers = {NULL, NULL}, 7, true, NULL},
303 {.u.link = {NULL, NULL}, 0, true, NULL},
305 segments[0].u.buffers.txData = gyro->dev.txBuf;
306 segments[0].u.buffers.rxData = &gyro->dev.rxBuf[1];
308 spiSequence(&gyro->dev, &segments[0]);
310 // Wait for completion
311 spiWait(&gyro->dev);
313 gyro->gyroADCRaw[X] = __builtin_bswap16(gyroData[1]);
314 gyro->gyroADCRaw[Y] = __builtin_bswap16(gyroData[2]);
315 gyro->gyroADCRaw[Z] = __builtin_bswap16(gyroData[3]);
316 break;
319 case GYRO_EXTI_INT_DMA:
321 // Acc and gyro data may not be continuous (MPU6xxx has temperature in between)
322 const uint8_t gyroDataIndex = ((gyro->gyroDataReg - gyro->accDataReg) >> 1) + 1;
324 // If read was triggered in interrupt don't bother waiting. The worst that could happen is that we pick
325 // up an old value.
326 gyro->gyroADCRaw[X] = __builtin_bswap16(gyroData[gyroDataIndex]);
327 gyro->gyroADCRaw[Y] = __builtin_bswap16(gyroData[gyroDataIndex + 1]);
328 gyro->gyroADCRaw[Z] = __builtin_bswap16(gyroData[gyroDataIndex + 2]);
329 break;
332 default:
333 break;
336 return true;
339 typedef uint8_t (*gyroSpiDetectFn_t)(const extDevice_t *dev);
341 static gyroSpiDetectFn_t gyroSpiDetectFnTable[] = {
342 #ifdef USE_GYRO_SPI_MPU6000
343 mpu6000SpiDetect,
344 #endif
345 #ifdef USE_GYRO_SPI_MPU6500
346 mpu6500SpiDetect, // some targets using MPU_9250_SPI, ICM_20608_SPI or ICM_20602_SPI state sensor is MPU_65xx_SPI
347 #endif
348 #ifdef USE_GYRO_SPI_MPU9250
349 mpu9250SpiDetect,
350 #endif
351 #ifdef USE_GYRO_SPI_ICM20689
352 icm20689SpiDetect, // icm20689SpiDetect detects ICM20602 and ICM20689
353 #endif
354 #ifdef USE_ACCGYRO_LSM6DSO
355 lsm6dsoDetect,
356 #endif
357 #ifdef USE_ACCGYRO_BMI160
358 bmi160Detect,
359 #endif
360 #ifdef USE_ACCGYRO_BMI270
361 bmi270Detect,
362 #endif
363 #if defined(USE_GYRO_SPI_ICM42605) || defined(USE_GYRO_SPI_ICM42688P)
364 icm426xxSpiDetect,
365 #endif
366 #ifdef USE_GYRO_SPI_ICM20649
367 icm20649SpiDetect,
368 #endif
369 #ifdef USE_GYRO_L3GD20
370 l3gd20Detect,
371 #endif
372 NULL // Avoid an empty array
375 static bool detectSPISensorsAndUpdateDetectionResult(gyroDev_t *gyro, const gyroDeviceConfig_t *config)
377 if (!config->csnTag || !spiSetBusInstance(&gyro->dev, config->spiBus)) {
378 return false;
381 gyro->dev.busType_u.spi.csnPin = IOGetByTag(config->csnTag);
383 IOInit(gyro->dev.busType_u.spi.csnPin, OWNER_GYRO_CS, RESOURCE_INDEX(config->index));
384 IOConfigGPIO(gyro->dev.busType_u.spi.csnPin, SPI_IO_CS_CFG);
385 IOHi(gyro->dev.busType_u.spi.csnPin); // Ensure device is disabled, important when two devices are on the same bus.
387 uint8_t sensor = MPU_NONE;
389 // Allow 100ms before attempting to access gyro's SPI bus
390 // Do this once here rather than in each detection routine to speed boot
391 while (millis() < GYRO_SPI_STARTUP_MS);
393 // Set a slow SPI clock that all potential devices can handle during gyro detection
394 spiSetClkDivisor(&gyro->dev, spiCalculateDivider(MPU_MAX_SPI_DETECT_CLK_HZ));
396 // It is hard to use hardware to optimize the detection loop here,
397 // as hardware type and detection function name doesn't match.
398 // May need a bitmap of hardware to detection function to do it right?
400 for (size_t index = 0 ; gyroSpiDetectFnTable[index] ; index++) {
401 sensor = (gyroSpiDetectFnTable[index])(&gyro->dev);
402 if (sensor != MPU_NONE) {
403 gyro->mpuDetectionResult.sensor = sensor;
404 busDeviceRegister(&gyro->dev);
405 return true;
409 // Detection failed, disable CS pin again
411 spiPreinitByTag(config->csnTag);
413 return false;
415 #endif
417 void mpuPreInit(const struct gyroDeviceConfig_s *config)
419 #ifdef USE_SPI_GYRO
420 spiPreinitRegister(config->csnTag, IOCFG_IPU, 1);
421 #else
422 UNUSED(config);
423 #endif
426 bool mpuDetect(gyroDev_t *gyro, const gyroDeviceConfig_t *config)
428 static busDevice_t bus;
429 gyro->dev.bus = &bus;
431 // MPU datasheet specifies 30ms.
432 delay(35);
434 if (config->busType == BUS_TYPE_NONE) {
435 return false;
438 if (config->busType == BUS_TYPE_GYRO_AUTO) {
439 gyro->dev.bus->busType = BUS_TYPE_I2C;
440 } else {
441 gyro->dev.bus->busType = config->busType;
444 #ifdef USE_I2C_GYRO
445 if (gyro->dev.bus->busType == BUS_TYPE_I2C) {
446 gyro->dev.bus->busType_u.i2c.device = I2C_CFG_TO_DEV(config->i2cBus);
447 gyro->dev.busType_u.i2c.address = config->i2cAddress ? config->i2cAddress : MPU_ADDRESS;
449 uint8_t sig = 0;
450 bool ack = busReadRegisterBuffer(&gyro->dev, MPU_RA_WHO_AM_I, &sig, 1);
452 if (ack) {
453 busDeviceRegister(&gyro->dev);
454 // If an MPU3050 is connected sig will contain 0.
455 uint8_t inquiryResult;
456 ack = busReadRegisterBuffer(&gyro->dev, MPU_RA_WHO_AM_I_LEGACY, &inquiryResult, 1);
457 inquiryResult &= MPU_INQUIRY_MASK;
458 if (ack && inquiryResult == MPUx0x0_WHO_AM_I_CONST) {
459 gyro->mpuDetectionResult.sensor = MPU_3050;
460 return true;
463 sig &= MPU_INQUIRY_MASK;
464 if (sig == MPUx0x0_WHO_AM_I_CONST) {
465 gyro->mpuDetectionResult.sensor = MPU_60x0;
466 mpu6050FindRevision(gyro);
467 } else if (sig == MPU6500_WHO_AM_I_CONST) {
468 gyro->mpuDetectionResult.sensor = MPU_65xx_I2C;
470 return true;
473 #endif
475 #ifdef USE_SPI_GYRO
476 gyro->dev.bus->busType = BUS_TYPE_SPI;
478 return detectSPISensorsAndUpdateDetectionResult(gyro, config);
479 #else
480 return false;
481 #endif
484 void mpuGyroInit(gyroDev_t *gyro)
486 gyro->accDataReg = MPU_RA_ACCEL_XOUT_H;
487 gyro->gyroDataReg = MPU_RA_GYRO_XOUT_H;
488 mpuIntExtiInit(gyro);
491 uint8_t mpuGyroDLPF(gyroDev_t *gyro)
493 uint8_t ret = 0;
495 // If gyro is in 32KHz mode then the DLPF bits aren't used
496 if (gyro->gyroRateKHz <= GYRO_RATE_8_kHz) {
497 switch (gyro->hardware_lpf) {
498 #ifdef USE_GYRO_DLPF_EXPERIMENTAL
499 case GYRO_HARDWARE_LPF_EXPERIMENTAL:
500 // experimental mode not supported for MPU60x0 family
501 if ((gyro->gyroHardware != GYRO_MPU6050) && (gyro->gyroHardware != GYRO_MPU6000)) {
502 ret = 7;
503 } else {
504 ret = 0;
506 break;
507 #endif
509 case GYRO_HARDWARE_LPF_NORMAL:
510 default:
511 ret = 0;
512 break;
515 return ret;
518 #ifdef USE_GYRO_REGISTER_DUMP
519 uint8_t mpuGyroReadRegister(const extDevice_t *dev, uint8_t reg)
521 uint8_t data;
522 const bool ack = busReadRegisterBuffer(dev, reg, &data, 1);
523 if (ack) {
524 return data;
525 } else {
526 return 0;
530 #endif