2015-09-18 12:23:50 -07:00
|
|
|
/*
|
|
|
|
* This file is part of Cleanflight.
|
|
|
|
*
|
|
|
|
* Cleanflight is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Cleanflight is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with Cleanflight. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "platform.h"
|
2016-08-07 03:46:47 -07:00
|
|
|
|
2017-01-05 04:22:49 -08:00
|
|
|
#include "build/atomic.h"
|
2016-08-07 03:46:47 -07:00
|
|
|
#include "build/build_config.h"
|
|
|
|
#include "build/debug.h"
|
2015-09-18 12:23:50 -07:00
|
|
|
|
|
|
|
#include "common/maths.h"
|
2016-11-19 06:11:03 -08:00
|
|
|
#include "common/utils.h"
|
2015-09-18 12:23:50 -07:00
|
|
|
|
2017-07-15 10:32:34 -07:00
|
|
|
#include "drivers/bus.h"
|
2017-05-26 04:31:41 -07:00
|
|
|
#include "drivers/bus_i2c.h"
|
2017-06-28 21:40:31 -07:00
|
|
|
#include "drivers/bus_spi.h"
|
2017-05-26 04:31:41 -07:00
|
|
|
#include "drivers/exti.h"
|
|
|
|
#include "drivers/io.h"
|
2017-04-06 00:21:54 -07:00
|
|
|
#include "drivers/nvic.h"
|
2017-05-26 04:31:41 -07:00
|
|
|
#include "drivers/sensor.h"
|
2017-04-06 00:21:54 -07:00
|
|
|
#include "drivers/system.h"
|
2017-05-26 04:31:41 -07:00
|
|
|
#include "drivers/time.h"
|
2015-09-18 12:23:50 -07:00
|
|
|
|
2017-08-30 00:50:40 -07:00
|
|
|
#include "drivers/accgyro/accgyro.h"
|
|
|
|
#include "drivers/accgyro/accgyro_mpu3050.h"
|
|
|
|
#include "drivers/accgyro/accgyro_mpu6050.h"
|
|
|
|
#include "drivers/accgyro/accgyro_mpu6500.h"
|
|
|
|
#include "drivers/accgyro/accgyro_spi_bmi160.h"
|
2017-09-01 07:57:54 -07:00
|
|
|
#include "drivers/accgyro/accgyro_spi_icm20649.h"
|
2017-08-30 00:50:40 -07:00
|
|
|
#include "drivers/accgyro/accgyro_spi_icm20689.h"
|
|
|
|
#include "drivers/accgyro/accgyro_spi_mpu6000.h"
|
|
|
|
#include "drivers/accgyro/accgyro_spi_mpu6500.h"
|
|
|
|
#include "drivers/accgyro/accgyro_spi_mpu9250.h"
|
|
|
|
#include "drivers/accgyro/accgyro_mpu.h"
|
2015-09-18 12:23:50 -07:00
|
|
|
|
|
|
|
|
2017-01-29 09:07:18 -08:00
|
|
|
mpuResetFnPtr mpuResetFn;
|
2016-12-17 16:16:23 -08:00
|
|
|
|
2016-06-03 04:20:11 -07:00
|
|
|
#ifndef MPU_I2C_INSTANCE
|
|
|
|
#define MPU_I2C_INSTANCE I2C_DEVICE
|
|
|
|
#endif
|
|
|
|
|
2017-03-05 12:16:37 -08:00
|
|
|
#ifndef MPU_ADDRESS
|
2015-09-19 06:17:19 -07:00
|
|
|
#define MPU_ADDRESS 0x68
|
2017-03-05 12:16:37 -08:00
|
|
|
#endif
|
2015-09-19 06:17:19 -07:00
|
|
|
|
|
|
|
#define MPU_INQUIRY_MASK 0x7E
|
|
|
|
|
2017-11-09 23:27:35 -08:00
|
|
|
#ifdef USE_I2C
|
2016-12-17 16:16:23 -08:00
|
|
|
static void mpu6050FindRevision(gyroDev_t *gyro)
|
2015-09-19 06:17:19 -07:00
|
|
|
{
|
2015-09-18 12:23:50 -07:00
|
|
|
// There is a map of revision contained in the android source tree which is quite comprehensive and may help to understand this code
|
|
|
|
// See https://android.googlesource.com/kernel/msm.git/+/eaf36994a3992b8f918c18e4f7411e8b2320a35f/drivers/misc/mpu6050/mldl_cfg.c
|
|
|
|
|
2017-11-09 23:27:35 -08:00
|
|
|
// determine product ID and revision
|
|
|
|
uint8_t readBuffer[6];
|
|
|
|
bool ack = busReadRegisterBuffer(&gyro->bus, MPU_RA_XA_OFFS_H, readBuffer, 6);
|
|
|
|
uint8_t revision = ((readBuffer[5] & 0x01) << 2) | ((readBuffer[3] & 0x01) << 1) | (readBuffer[1] & 0x01);
|
|
|
|
if (ack && revision) {
|
|
|
|
// Congrats, these parts are better
|
2015-09-18 12:23:50 -07:00
|
|
|
if (revision == 1) {
|
2016-12-17 16:16:23 -08:00
|
|
|
gyro->mpuDetectionResult.resolution = MPU_HALF_RESOLUTION;
|
2015-09-18 12:23:50 -07:00
|
|
|
} else if (revision == 2) {
|
2016-12-17 16:16:23 -08:00
|
|
|
gyro->mpuDetectionResult.resolution = MPU_FULL_RESOLUTION;
|
2015-08-04 18:15:42 -07:00
|
|
|
} else if ((revision == 3) || (revision == 7)) {
|
2016-12-17 16:16:23 -08:00
|
|
|
gyro->mpuDetectionResult.resolution = MPU_FULL_RESOLUTION;
|
2015-09-18 12:23:50 -07:00
|
|
|
} else {
|
|
|
|
failureMode(FAILURE_ACC_INCOMPATIBLE);
|
|
|
|
}
|
|
|
|
} else {
|
2017-11-09 23:27:35 -08:00
|
|
|
uint8_t productId;
|
|
|
|
ack = busReadRegisterBuffer(&gyro->bus, MPU_RA_PRODUCT_ID, &productId, 1);
|
2015-09-18 12:23:50 -07:00
|
|
|
revision = productId & 0x0F;
|
2017-11-09 23:27:35 -08:00
|
|
|
if (!ack || revision == 0) {
|
2015-09-18 12:23:50 -07:00
|
|
|
failureMode(FAILURE_ACC_INCOMPATIBLE);
|
|
|
|
} else if (revision == 4) {
|
2016-12-17 16:16:23 -08:00
|
|
|
gyro->mpuDetectionResult.resolution = MPU_HALF_RESOLUTION;
|
2015-09-18 12:23:50 -07:00
|
|
|
} else {
|
2016-12-17 16:16:23 -08:00
|
|
|
gyro->mpuDetectionResult.resolution = MPU_FULL_RESOLUTION;
|
2015-09-18 12:23:50 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-11-09 23:27:35 -08:00
|
|
|
#endif
|
2015-09-18 12:23:50 -07:00
|
|
|
|
2016-11-19 06:11:03 -08:00
|
|
|
/*
|
|
|
|
* Gyro interrupt service routine
|
|
|
|
*/
|
|
|
|
#if defined(MPU_INT_EXTI)
|
|
|
|
static void mpuIntExtiHandler(extiCallbackRec_t *cb)
|
2016-06-02 05:23:52 -07:00
|
|
|
{
|
2017-01-05 04:22:49 -08:00
|
|
|
#ifdef DEBUG_MPU_DATA_READY_INTERRUPT
|
|
|
|
static uint32_t lastCalledAtUs = 0;
|
|
|
|
const uint32_t nowUs = micros();
|
|
|
|
debug[0] = (uint16_t)(nowUs - lastCalledAtUs);
|
|
|
|
lastCalledAtUs = nowUs;
|
|
|
|
#endif
|
2016-12-11 04:17:30 -08:00
|
|
|
gyroDev_t *gyro = container_of(cb, gyroDev_t, exti);
|
2016-11-19 06:11:03 -08:00
|
|
|
gyro->dataReady = true;
|
2015-09-18 12:23:50 -07:00
|
|
|
#ifdef DEBUG_MPU_DATA_READY_INTERRUPT
|
2017-01-05 04:22:49 -08:00
|
|
|
const uint32_t now2Us = micros();
|
|
|
|
debug[1] = (uint16_t)(now2Us - nowUs);
|
2015-09-18 12:23:50 -07:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2016-11-19 06:11:03 -08:00
|
|
|
static void mpuIntExtiInit(gyroDev_t *gyro)
|
2015-09-18 12:23:50 -07:00
|
|
|
{
|
2017-03-29 03:01:26 -07:00
|
|
|
if (gyro->mpuIntExtiTag == IO_TAG_NONE) {
|
2016-06-27 11:26:02 -07:00
|
|
|
return;
|
|
|
|
}
|
2015-09-18 12:23:50 -07:00
|
|
|
|
2017-03-29 03:01:26 -07:00
|
|
|
const IO_t mpuIntIO = IOGetByTag(gyro->mpuIntExtiTag);
|
2016-07-09 06:39:39 -07:00
|
|
|
|
2015-09-18 12:23:50 -07:00
|
|
|
#ifdef ENSURE_MPU_DATA_READY_IS_LOW
|
2016-06-27 11:26:02 -07:00
|
|
|
uint8_t status = IORead(mpuIntIO);
|
|
|
|
if (status) {
|
|
|
|
return;
|
|
|
|
}
|
2015-09-18 12:23:50 -07:00
|
|
|
#endif
|
|
|
|
|
2016-09-09 23:51:18 -07:00
|
|
|
#if defined (STM32F7)
|
2016-11-09 23:56:13 -08:00
|
|
|
IOInit(mpuIntIO, OWNER_MPU_EXTI, 0);
|
2016-12-11 04:17:30 -08:00
|
|
|
EXTIHandlerInit(&gyro->exti, mpuIntExtiHandler);
|
|
|
|
EXTIConfig(mpuIntIO, &gyro->exti, NVIC_PRIO_MPU_INT_EXTI, IO_CONFIG(GPIO_MODE_INPUT,0,GPIO_NOPULL)); // TODO - maybe pullup / pulldown ?
|
2016-09-09 23:51:18 -07:00
|
|
|
#else
|
2016-11-09 23:56:13 -08:00
|
|
|
IOInit(mpuIntIO, OWNER_MPU_EXTI, 0);
|
2016-06-27 11:26:02 -07:00
|
|
|
IOConfigGPIO(mpuIntIO, IOCFG_IN_FLOATING); // TODO - maybe pullup / pulldown ?
|
2015-09-18 12:23:50 -07:00
|
|
|
|
2016-12-11 04:17:30 -08:00
|
|
|
EXTIHandlerInit(&gyro->exti, mpuIntExtiHandler);
|
|
|
|
EXTIConfig(mpuIntIO, &gyro->exti, NVIC_PRIO_MPU_INT_EXTI, EXTI_Trigger_Rising);
|
2016-09-09 23:51:18 -07:00
|
|
|
#endif
|
2018-03-03 14:49:36 -08:00
|
|
|
EXTIEnable(mpuIntIO, true);
|
2015-09-19 08:21:50 -07:00
|
|
|
}
|
2017-11-09 23:27:35 -08:00
|
|
|
#endif // MPU_INT_EXTI
|
2015-09-19 08:21:50 -07:00
|
|
|
|
2016-12-14 06:32:38 -08:00
|
|
|
bool mpuAccRead(accDev_t *acc)
|
2015-09-18 12:23:50 -07:00
|
|
|
{
|
2015-09-19 08:21:50 -07:00
|
|
|
uint8_t data[6];
|
2015-09-18 12:23:50 -07:00
|
|
|
|
2017-11-09 23:27:35 -08:00
|
|
|
const bool ack = busReadRegisterBuffer(&acc->bus, MPU_RA_ACCEL_XOUT_H, data, 6);
|
2015-09-18 12:23:50 -07:00
|
|
|
if (!ack) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-12-14 06:32:38 -08:00
|
|
|
acc->ADCRaw[X] = (int16_t)((data[0] << 8) | data[1]);
|
|
|
|
acc->ADCRaw[Y] = (int16_t)((data[2] << 8) | data[3]);
|
|
|
|
acc->ADCRaw[Z] = (int16_t)((data[4] << 8) | data[5]);
|
2015-09-18 12:23:50 -07:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-11-19 06:11:03 -08:00
|
|
|
bool mpuGyroRead(gyroDev_t *gyro)
|
2015-09-18 12:23:50 -07:00
|
|
|
{
|
2015-09-19 08:21:50 -07:00
|
|
|
uint8_t data[6];
|
2015-09-18 12:23:50 -07:00
|
|
|
|
2017-11-09 23:27:35 -08:00
|
|
|
const bool ack = busReadRegisterBuffer(&gyro->bus, MPU_RA_GYRO_XOUT_H, data, 6);
|
2015-09-18 12:23:50 -07:00
|
|
|
if (!ack) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-11-19 06:11:03 -08:00
|
|
|
gyro->gyroADCRaw[X] = (int16_t)((data[0] << 8) | data[1]);
|
|
|
|
gyro->gyroADCRaw[Y] = (int16_t)((data[2] << 8) | data[3]);
|
|
|
|
gyro->gyroADCRaw[Z] = (int16_t)((data[4] << 8) | data[5]);
|
2015-09-18 12:23:50 -07:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2015-10-07 08:12:54 -07:00
|
|
|
|
2017-07-01 09:26:45 -07:00
|
|
|
bool mpuGyroReadSPI(gyroDev_t *gyro)
|
|
|
|
{
|
|
|
|
static const uint8_t dataToSend[7] = {MPU_RA_GYRO_XOUT_H | 0x80, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
|
|
|
|
uint8_t data[7];
|
|
|
|
|
2017-07-02 00:35:14 -07:00
|
|
|
const bool ack = spiBusTransfer(&gyro->bus, dataToSend, data, 7);
|
2017-07-01 09:26:45 -07:00
|
|
|
if (!ack) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
gyro->gyroADCRaw[X] = (int16_t)((data[1] << 8) | data[2]);
|
|
|
|
gyro->gyroADCRaw[Y] = (int16_t)((data[3] << 8) | data[4]);
|
|
|
|
gyro->gyroADCRaw[Z] = (int16_t)((data[5] << 8) | data[6]);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-01-25 10:16:05 -08:00
|
|
|
#ifdef USE_SPI
|
|
|
|
static bool detectSPISensorsAndUpdateDetectionResult(gyroDev_t *gyro)
|
|
|
|
{
|
2017-03-07 01:01:16 -08:00
|
|
|
UNUSED(gyro); // since there are FCs which have gyro on I2C but other devices on SPI
|
2017-07-12 06:40:08 -07:00
|
|
|
|
2017-07-12 07:11:39 -07:00
|
|
|
uint8_t sensor = MPU_NONE;
|
|
|
|
UNUSED(sensor);
|
|
|
|
|
2018-03-03 14:29:31 -08:00
|
|
|
// note, when USE_DUAL_GYRO is enabled the gyro->bus must already be initialised.
|
|
|
|
|
2017-01-25 10:16:05 -08:00
|
|
|
#ifdef USE_GYRO_SPI_MPU6000
|
2018-03-03 14:29:31 -08:00
|
|
|
#ifndef USE_DUAL_GYRO
|
2017-07-01 01:18:43 -07:00
|
|
|
spiBusSetInstance(&gyro->bus, MPU6000_SPI_INSTANCE);
|
2017-07-12 06:40:08 -07:00
|
|
|
#endif
|
2017-03-07 01:01:16 -08:00
|
|
|
#ifdef MPU6000_CS_PIN
|
2017-07-14 20:55:18 -07:00
|
|
|
gyro->bus.busdev_u.spi.csnPin = gyro->bus.busdev_u.spi.csnPin == IO_NONE ? IOGetByTag(IO_TAG(MPU6000_CS_PIN)) : gyro->bus.busdev_u.spi.csnPin;
|
2017-03-07 01:01:16 -08:00
|
|
|
#endif
|
2017-07-12 07:11:39 -07:00
|
|
|
sensor = mpu6000SpiDetect(&gyro->bus);
|
|
|
|
if (sensor != MPU_NONE) {
|
|
|
|
gyro->mpuDetectionResult.sensor = sensor;
|
2017-01-25 10:16:05 -08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef USE_GYRO_SPI_MPU6500
|
2018-03-03 14:29:31 -08:00
|
|
|
#ifndef USE_DUAL_GYRO
|
2017-07-01 01:18:43 -07:00
|
|
|
spiBusSetInstance(&gyro->bus, MPU6500_SPI_INSTANCE);
|
2017-07-12 06:40:08 -07:00
|
|
|
#endif
|
|
|
|
#ifdef MPU6500_CS_PIN
|
2017-07-15 10:32:34 -07:00
|
|
|
gyro->bus.busdev_u.spi.csnPin = gyro->bus.busdev_u.spi.csnPin == IO_NONE ? IOGetByTag(IO_TAG(MPU6500_CS_PIN)) : gyro->bus.busdev_u.spi.csnPin;
|
2017-07-12 06:40:08 -07:00
|
|
|
#endif
|
2017-07-12 07:11:39 -07:00
|
|
|
sensor = mpu6500SpiDetect(&gyro->bus);
|
2017-03-07 01:01:16 -08:00
|
|
|
// some targets using MPU_9250_SPI, ICM_20608_SPI or ICM_20602_SPI state sensor is MPU_65xx_SPI
|
2017-07-12 07:11:39 -07:00
|
|
|
if (sensor != MPU_NONE) {
|
|
|
|
gyro->mpuDetectionResult.sensor = sensor;
|
2017-01-25 10:16:05 -08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef USE_GYRO_SPI_MPU9250
|
2018-03-03 14:29:31 -08:00
|
|
|
#ifndef USE_DUAL_GYRO
|
2017-07-01 01:18:43 -07:00
|
|
|
spiBusSetInstance(&gyro->bus, MPU9250_SPI_INSTANCE);
|
2017-07-12 06:40:08 -07:00
|
|
|
#endif
|
|
|
|
#ifdef MPU9250_CS_PIN
|
2017-07-15 21:31:37 -07:00
|
|
|
gyro->bus.busdev_u.spi.csnPin = gyro->bus.busdev_u.spi.csnPin == IO_NONE ? IOGetByTag(IO_TAG(MPU9250_CS_PIN)) : gyro->bus.busdev_u.spi.csnPin;
|
2017-07-12 06:40:08 -07:00
|
|
|
#endif
|
2017-07-12 07:11:39 -07:00
|
|
|
sensor = mpu9250SpiDetect(&gyro->bus);
|
|
|
|
if (sensor != MPU_NONE) {
|
|
|
|
gyro->mpuDetectionResult.sensor = sensor;
|
2017-03-07 01:01:16 -08:00
|
|
|
gyro->mpuConfiguration.resetFn = mpu9250SpiResetGyro;
|
2017-01-25 10:16:05 -08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2017-09-01 07:57:54 -07:00
|
|
|
#ifdef USE_GYRO_SPI_ICM20649
|
|
|
|
#ifdef ICM20649_SPI_INSTANCE
|
|
|
|
spiBusSetInstance(&gyro->bus, ICM20649_SPI_INSTANCE);
|
|
|
|
#endif
|
|
|
|
#ifdef ICM20649_CS_PIN
|
|
|
|
gyro->bus.busdev_u.spi.csnPin = gyro->bus.busdev_u.spi.csnPin == IO_NONE ? IOGetByTag(IO_TAG(ICM20649_CS_PIN)) : gyro->bus.busdev_u.spi.csnPin;
|
|
|
|
#endif
|
|
|
|
sensor = icm20649SpiDetect(&gyro->bus);
|
|
|
|
if (sensor != MPU_NONE) {
|
|
|
|
gyro->mpuDetectionResult.sensor = sensor;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2017-01-25 10:16:05 -08:00
|
|
|
#ifdef USE_GYRO_SPI_ICM20689
|
2018-03-03 14:29:31 -08:00
|
|
|
#ifndef USE_DUAL_GYRO
|
2017-07-01 01:18:43 -07:00
|
|
|
spiBusSetInstance(&gyro->bus, ICM20689_SPI_INSTANCE);
|
2017-07-12 06:40:08 -07:00
|
|
|
#endif
|
|
|
|
#ifdef ICM20689_CS_PIN
|
2017-07-15 21:31:37 -07:00
|
|
|
gyro->bus.busdev_u.spi.csnPin = gyro->bus.busdev_u.spi.csnPin == IO_NONE ? IOGetByTag(IO_TAG(ICM20689_CS_PIN)) : gyro->bus.busdev_u.spi.csnPin;
|
2017-07-12 06:40:08 -07:00
|
|
|
#endif
|
2017-07-12 07:11:39 -07:00
|
|
|
sensor = icm20689SpiDetect(&gyro->bus);
|
2018-03-03 14:29:31 -08:00
|
|
|
// icm20689SpiDetect detects ICM20602 and ICM20689
|
2017-07-12 07:11:39 -07:00
|
|
|
if (sensor != MPU_NONE) {
|
|
|
|
gyro->mpuDetectionResult.sensor = sensor;
|
2017-03-07 01:01:16 -08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef USE_ACCGYRO_BMI160
|
2018-03-03 14:29:31 -08:00
|
|
|
#ifndef USE_DUAL_GYRO
|
2017-07-01 01:18:43 -07:00
|
|
|
spiBusSetInstance(&gyro->bus, BMI160_SPI_INSTANCE);
|
2017-07-12 06:40:08 -07:00
|
|
|
#endif
|
|
|
|
#ifdef BMI160_CS_PIN
|
2017-07-15 21:31:37 -07:00
|
|
|
gyro->bus.busdev_u.spi.csnPin = gyro->bus.busdev_u.spi.csnPin == IO_NONE ? IOGetByTag(IO_TAG(BMI160_CS_PIN)) : gyro->bus.busdev_u.spi.csnPin;
|
2017-07-12 06:40:08 -07:00
|
|
|
#endif
|
2017-07-12 07:11:39 -07:00
|
|
|
sensor = bmi160Detect(&gyro->bus);
|
|
|
|
if (sensor != MPU_NONE) {
|
|
|
|
gyro->mpuDetectionResult.sensor = sensor;
|
2017-01-25 10:16:05 -08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2017-03-07 01:01:16 -08:00
|
|
|
void mpuDetect(gyroDev_t *gyro)
|
2017-01-25 10:16:05 -08:00
|
|
|
{
|
|
|
|
// MPU datasheet specifies 30ms.
|
|
|
|
delay(35);
|
|
|
|
|
2017-03-07 01:01:16 -08:00
|
|
|
#ifdef USE_I2C
|
2018-03-03 14:29:31 -08:00
|
|
|
if (gyro->bus.bustype == BUSTYPE_NONE) {
|
|
|
|
// if no bustype is selected try I2C first.
|
|
|
|
gyro->bus.bustype = BUSTYPE_I2C;
|
2017-01-25 10:16:05 -08:00
|
|
|
}
|
|
|
|
|
2018-03-03 14:29:31 -08:00
|
|
|
if (gyro->bus.bustype == BUSTYPE_I2C) {
|
|
|
|
gyro->bus.busdev_u.i2c.device = MPU_I2C_INSTANCE;
|
|
|
|
gyro->bus.busdev_u.i2c.address = MPU_ADDRESS;
|
|
|
|
|
|
|
|
uint8_t sig = 0;
|
|
|
|
bool ack = busReadRegisterBuffer(&gyro->bus, MPU_RA_WHO_AM_I, &sig, 1);
|
|
|
|
|
|
|
|
if (ack) {
|
|
|
|
// If an MPU3050 is connected sig will contain 0.
|
|
|
|
uint8_t inquiryResult;
|
|
|
|
ack = busReadRegisterBuffer(&gyro->bus, MPU_RA_WHO_AM_I_LEGACY, &inquiryResult, 1);
|
|
|
|
inquiryResult &= MPU_INQUIRY_MASK;
|
|
|
|
if (ack && inquiryResult == MPUx0x0_WHO_AM_I_CONST) {
|
|
|
|
gyro->mpuDetectionResult.sensor = MPU_3050;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
sig &= MPU_INQUIRY_MASK;
|
|
|
|
if (sig == MPUx0x0_WHO_AM_I_CONST) {
|
|
|
|
gyro->mpuDetectionResult.sensor = MPU_60x0;
|
|
|
|
mpu6050FindRevision(gyro);
|
|
|
|
} else if (sig == MPU6500_WHO_AM_I_CONST) {
|
|
|
|
gyro->mpuDetectionResult.sensor = MPU_65xx_I2C;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2017-01-25 10:16:05 -08:00
|
|
|
}
|
2018-03-03 14:29:31 -08:00
|
|
|
#endif
|
2017-01-25 10:16:05 -08:00
|
|
|
|
2018-03-03 14:29:31 -08:00
|
|
|
#ifdef USE_SPI
|
|
|
|
gyro->bus.bustype = BUSTYPE_SPI;
|
|
|
|
detectSPISensorsAndUpdateDetectionResult(gyro);
|
2017-11-09 23:27:35 -08:00
|
|
|
#endif
|
2017-01-25 10:16:05 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void mpuGyroInit(gyroDev_t *gyro)
|
|
|
|
{
|
2017-11-09 23:27:35 -08:00
|
|
|
#ifdef MPU_INT_EXTI
|
2017-01-25 10:16:05 -08:00
|
|
|
mpuIntExtiInit(gyro);
|
2017-11-09 23:27:35 -08:00
|
|
|
#else
|
|
|
|
UNUSED(gyro);
|
|
|
|
#endif
|
2017-01-25 10:16:05 -08:00
|
|
|
}
|
2018-03-21 18:02:30 -07:00
|
|
|
|
|
|
|
uint8_t mpuGyroDLPF(gyroDev_t *gyro)
|
|
|
|
{
|
|
|
|
uint8_t ret;
|
|
|
|
if (gyro->gyroRateKHz > GYRO_RATE_8_kHz) {
|
|
|
|
ret = 0; // If gyro is in 32KHz mode then the DLPF bits aren't used - set to 0
|
|
|
|
} else {
|
|
|
|
switch (gyro->hardware_lpf) {
|
|
|
|
case GYRO_HARDWARE_LPF_NORMAL:
|
|
|
|
ret = 0;
|
|
|
|
break;
|
|
|
|
case GYRO_HARDWARE_LPF_EXPERIMENTAL:
|
|
|
|
ret = 7;
|
|
|
|
break;
|
|
|
|
case GYRO_HARDWARE_LPF_1KHZ_SAMPLE:
|
|
|
|
ret = 1;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ret = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t mpuGyroFCHOICE(gyroDev_t *gyro)
|
|
|
|
{
|
|
|
|
if (gyro->gyroRateKHz > GYRO_RATE_8_kHz) {
|
|
|
|
if (gyro->hardware_32khz_lpf == GYRO_32KHZ_HARDWARE_LPF_EXPERIMENTAL) {
|
|
|
|
return FCB_8800_32;
|
|
|
|
} else {
|
|
|
|
return FCB_3600_32;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return FCB_DISABLED; // Not in 32KHz mode, set FCHOICE to select 8KHz sampling
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef USE_GYRO_REGISTER_DUMP
|
|
|
|
uint8_t mpuGyroReadRegister(const busDevice_t *bus, uint8_t reg)
|
|
|
|
{
|
|
|
|
uint8_t data;
|
|
|
|
const bool ack = busReadRegisterBuffer(bus, reg, &data, 1);
|
|
|
|
if (ack) {
|
|
|
|
return data;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
#endif
|