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
|
|
|
|
|
|
|
#include "nvic.h"
|
|
|
|
|
|
|
|
#include "system.h"
|
2016-08-04 11:45:43 -07:00
|
|
|
#include "io.h"
|
2015-09-18 12:23:50 -07:00
|
|
|
#include "exti.h"
|
|
|
|
#include "bus_i2c.h"
|
|
|
|
|
|
|
|
#include "sensor.h"
|
|
|
|
#include "accgyro.h"
|
2015-09-19 06:17:19 -07:00
|
|
|
#include "accgyro_mpu3050.h"
|
2015-09-18 12:23:50 -07:00
|
|
|
#include "accgyro_mpu6050.h"
|
2015-09-19 10:30:34 -07:00
|
|
|
#include "accgyro_mpu6500.h"
|
2015-09-19 09:11:04 -07:00
|
|
|
#include "accgyro_spi_mpu6000.h"
|
2015-09-19 08:21:50 -07:00
|
|
|
#include "accgyro_spi_mpu6500.h"
|
2016-08-28 02:25:44 -07:00
|
|
|
#include "accgyro_spi_icm20689.h"
|
2016-06-07 12:37:08 -07:00
|
|
|
#include "accgyro_spi_mpu9250.h"
|
2015-09-18 12:23:50 -07:00
|
|
|
#include "accgyro_mpu.h"
|
|
|
|
|
|
|
|
|
2016-12-17 16:16:23 -08:00
|
|
|
mpuResetFuncPtr mpuReset;
|
|
|
|
|
2016-06-03 04:20:11 -07:00
|
|
|
#ifndef MPU_I2C_INSTANCE
|
|
|
|
#define MPU_I2C_INSTANCE I2C_DEVICE
|
|
|
|
#endif
|
|
|
|
|
2015-09-19 06:17:19 -07:00
|
|
|
#define MPU_ADDRESS 0x68
|
|
|
|
|
|
|
|
// WHO_AM_I register contents for MPU3050, 6050 and 6500
|
|
|
|
#define MPU6500_WHO_AM_I_CONST (0x70)
|
|
|
|
#define MPUx0x0_WHO_AM_I_CONST (0x68)
|
|
|
|
|
|
|
|
#define MPU_INQUIRY_MASK 0x7E
|
|
|
|
|
2016-12-17 16:16:23 -08:00
|
|
|
static void mpu6050FindRevision(gyroDev_t *gyro)
|
2015-09-19 06:17:19 -07:00
|
|
|
{
|
|
|
|
bool ack;
|
|
|
|
UNUSED(ack);
|
|
|
|
|
|
|
|
uint8_t readBuffer[6];
|
|
|
|
uint8_t revision;
|
|
|
|
uint8_t productId;
|
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
|
|
|
|
|
|
|
|
// determine product ID and accel revision
|
2016-12-17 16:16:23 -08:00
|
|
|
ack = gyro->mpuConfiguration.read(MPU_RA_XA_OFFS_H, 6, readBuffer);
|
2015-09-18 12:23:50 -07:00
|
|
|
revision = ((readBuffer[5] & 0x01) << 2) | ((readBuffer[3] & 0x01) << 1) | (readBuffer[1] & 0x01);
|
|
|
|
if (revision) {
|
|
|
|
/* Congrats, these parts are better. */
|
|
|
|
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 {
|
2016-12-17 16:16:23 -08:00
|
|
|
ack = gyro->mpuConfiguration.read(MPU_RA_PRODUCT_ID, 1, &productId);
|
2015-09-18 12:23:50 -07:00
|
|
|
revision = productId & 0x0F;
|
|
|
|
if (!revision) {
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
2017-01-05 04:22:49 -08:00
|
|
|
if (gyro->update) {
|
|
|
|
gyro->update(gyro);
|
|
|
|
}
|
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
|
|
|
#endif
|
2015-09-18 12:23:50 -07:00
|
|
|
|
2016-11-19 06:11:03 -08:00
|
|
|
static void mpuIntExtiInit(gyroDev_t *gyro)
|
2015-09-18 12:23:50 -07:00
|
|
|
{
|
2016-11-19 06:11:03 -08:00
|
|
|
#if defined(MPU_INT_EXTI)
|
2016-12-17 16:16:23 -08:00
|
|
|
if (!gyro->mpuIntExtiConfig) {
|
2016-06-27 11:26:02 -07:00
|
|
|
return;
|
|
|
|
}
|
2015-09-18 12:23:50 -07:00
|
|
|
|
2016-12-17 16:16:23 -08:00
|
|
|
IO_t mpuIntIO = IOGetByTag(gyro->mpuIntExtiConfig->tag);
|
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-06-27 11:26:02 -07:00
|
|
|
EXTIEnable(mpuIntIO, true);
|
2016-09-09 23:51:18 -07:00
|
|
|
#endif
|
2016-11-10 23:10:38 -08:00
|
|
|
|
2016-12-11 04:17:30 -08:00
|
|
|
#else
|
|
|
|
UNUSED(gyro);
|
2016-11-19 06:11:03 -08:00
|
|
|
#endif
|
2015-09-18 12:23:50 -07:00
|
|
|
}
|
|
|
|
|
2015-09-19 08:21:50 -07:00
|
|
|
static bool mpuReadRegisterI2C(uint8_t reg, uint8_t length, uint8_t* data)
|
|
|
|
{
|
2016-06-27 11:26:02 -07:00
|
|
|
bool ack = i2cRead(MPU_I2C_INSTANCE, MPU_ADDRESS, reg, length, data);
|
2015-09-19 08:21:50 -07:00
|
|
|
return ack;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool mpuWriteRegisterI2C(uint8_t reg, uint8_t data)
|
|
|
|
{
|
2016-06-27 11:26:02 -07:00
|
|
|
bool ack = i2cWrite(MPU_I2C_INSTANCE, MPU_ADDRESS, reg, data);
|
2015-09-19 08:21:50 -07:00
|
|
|
return ack;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2016-12-17 16:16:23 -08:00
|
|
|
bool ack = acc->mpuConfiguration.read(MPU_RA_ACCEL_XOUT_H, 6, data);
|
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;
|
|
|
|
}
|
|
|
|
|
2017-01-05 04:22:49 -08:00
|
|
|
void mpuGyroSetIsrUpdate(gyroDev_t *gyro, sensorGyroUpdateFuncPtr updateFn)
|
|
|
|
{
|
|
|
|
ATOMIC_BLOCK(NVIC_PRIO_MPU_INT_EXTI) {
|
|
|
|
gyro->update = updateFn;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2016-12-17 16:16:23 -08:00
|
|
|
const bool ack = gyro->mpuConfiguration.read(gyro->mpuConfiguration.gyroReadXRegister, 6, data);
|
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
|
|
|
|
2016-12-11 02:40:51 -08:00
|
|
|
bool mpuCheckDataReady(gyroDev_t* gyro)
|
2016-06-26 00:41:24 -07:00
|
|
|
{
|
|
|
|
bool ret;
|
2016-11-19 06:11:03 -08:00
|
|
|
if (gyro->dataReady) {
|
2016-06-26 00:41:24 -07:00
|
|
|
ret = true;
|
2016-11-19 06:11:03 -08:00
|
|
|
gyro->dataReady= false;
|
2015-10-07 08:12:54 -07:00
|
|
|
} else {
|
2016-06-26 00:41:24 -07:00
|
|
|
ret = false;
|
2015-10-07 08:12:54 -07:00
|
|
|
}
|
2016-06-26 00:41:24 -07:00
|
|
|
return ret;
|
2015-10-07 08:12:54 -07:00
|
|
|
}
|
2017-01-25 10:16:05 -08:00
|
|
|
|
|
|
|
#ifdef USE_SPI
|
|
|
|
static bool detectSPISensorsAndUpdateDetectionResult(gyroDev_t *gyro)
|
|
|
|
{
|
|
|
|
#ifdef USE_GYRO_SPI_MPU6000
|
|
|
|
if (mpu6000SpiDetect()) {
|
|
|
|
gyro->mpuDetectionResult.sensor = MPU_60x0_SPI;
|
|
|
|
gyro->mpuConfiguration.gyroReadXRegister = MPU_RA_GYRO_XOUT_H;
|
|
|
|
gyro->mpuConfiguration.read = mpu6000ReadRegister;
|
|
|
|
gyro->mpuConfiguration.write = mpu6000WriteRegister;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef USE_GYRO_SPI_MPU6500
|
|
|
|
uint8_t mpu6500Sensor = mpu6500SpiDetect();
|
|
|
|
if (mpu6500Sensor != MPU_NONE) {
|
|
|
|
gyro->mpuDetectionResult.sensor = mpu6500Sensor;
|
|
|
|
gyro->mpuConfiguration.gyroReadXRegister = MPU_RA_GYRO_XOUT_H;
|
|
|
|
gyro->mpuConfiguration.read = mpu6500ReadRegister;
|
|
|
|
gyro->mpuConfiguration.write = mpu6500WriteRegister;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef USE_GYRO_SPI_MPU9250
|
|
|
|
if (mpu9250SpiDetect()) {
|
|
|
|
gyro->mpuDetectionResult.sensor = MPU_9250_SPI;
|
|
|
|
gyro->mpuConfiguration.gyroReadXRegister = MPU_RA_GYRO_XOUT_H;
|
|
|
|
gyro->mpuConfiguration.read = mpu9250ReadRegister;
|
|
|
|
gyro->mpuConfiguration.slowread = mpu9250SlowReadRegister;
|
|
|
|
gyro->mpuConfiguration.verifywrite = verifympu9250WriteRegister;
|
|
|
|
gyro->mpuConfiguration.write = mpu9250WriteRegister;
|
|
|
|
gyro->mpuConfiguration.reset = mpu9250ResetGyro;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef USE_GYRO_SPI_ICM20689
|
|
|
|
if (icm20689SpiDetect()) {
|
|
|
|
gyro->mpuDetectionResult.sensor = ICM_20689_SPI;
|
|
|
|
gyro->mpuConfiguration.gyroReadXRegister = MPU_RA_GYRO_XOUT_H;
|
|
|
|
gyro->mpuConfiguration.read = icm20689ReadRegister;
|
|
|
|
gyro->mpuConfiguration.write = icm20689WriteRegister;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
UNUSED(gyro);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
mpuDetectionResult_t *mpuDetect(gyroDev_t *gyro)
|
|
|
|
{
|
|
|
|
bool ack;
|
|
|
|
uint8_t sig;
|
|
|
|
uint8_t inquiryResult;
|
|
|
|
|
|
|
|
// MPU datasheet specifies 30ms.
|
|
|
|
delay(35);
|
|
|
|
|
|
|
|
#ifndef USE_I2C
|
|
|
|
ack = false;
|
|
|
|
sig = 0;
|
|
|
|
#else
|
|
|
|
ack = mpuReadRegisterI2C(MPU_RA_WHO_AM_I, 1, &sig);
|
|
|
|
#endif
|
|
|
|
if (ack) {
|
|
|
|
gyro->mpuConfiguration.read = mpuReadRegisterI2C;
|
|
|
|
gyro->mpuConfiguration.write = mpuWriteRegisterI2C;
|
|
|
|
} else {
|
|
|
|
#ifdef USE_SPI
|
|
|
|
bool detectedSpiSensor = detectSPISensorsAndUpdateDetectionResult(gyro);
|
|
|
|
UNUSED(detectedSpiSensor);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return &gyro->mpuDetectionResult;
|
|
|
|
}
|
|
|
|
|
|
|
|
gyro->mpuConfiguration.gyroReadXRegister = MPU_RA_GYRO_XOUT_H;
|
|
|
|
|
|
|
|
// If an MPU3050 is connected sig will contain 0.
|
|
|
|
ack = mpuReadRegisterI2C(MPU_RA_WHO_AM_I_LEGACY, 1, &inquiryResult);
|
|
|
|
inquiryResult &= MPU_INQUIRY_MASK;
|
|
|
|
if (ack && inquiryResult == MPUx0x0_WHO_AM_I_CONST) {
|
|
|
|
gyro->mpuDetectionResult.sensor = MPU_3050;
|
|
|
|
gyro->mpuConfiguration.gyroReadXRegister = MPU3050_GYRO_OUT;
|
|
|
|
return &gyro->mpuDetectionResult;
|
|
|
|
}
|
|
|
|
|
|
|
|
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 &gyro->mpuDetectionResult;
|
|
|
|
}
|
|
|
|
|
|
|
|
void mpuGyroInit(gyroDev_t *gyro)
|
|
|
|
{
|
|
|
|
mpuIntExtiInit(gyro);
|
|
|
|
}
|