From 9b054f6fdec3f632daeb51bebf6b78cde546db50 Mon Sep 17 00:00:00 2001 From: Dominic Clifton Date: Thu, 15 Oct 2015 23:03:11 +0100 Subject: [PATCH] More cleanup of MPU driver code. Support MPU INT on CC3D and Naze32 Rev6. --- src/main/drivers/accgyro_mpu.c | 14 +- .../drivers/{accgyro_mpu6050.c => accgyro_mpu.h} | 178 +++++++++------------ src/main/drivers/accgyro_mpu6050.c | 94 ----------- src/main/drivers/accgyro_mpu6500.c | 20 +-- src/main/drivers/accgyro_mpu6500.h | 16 -- src/main/drivers/accgyro_spi_mpu6000.c | 37 +++-- src/main/drivers/accgyro_spi_mpu6000.h | 74 +++------ src/main/drivers/accgyro_spi_mpu6500.c | 2 +- src/main/drivers/barometer_bmp085.c | 4 +- src/main/drivers/compass_hmc5883l.c | 2 +- src/main/drivers/system.c | 60 ++++--- src/main/drivers/system.h | 6 +- src/main/flight/failsafe.c | 2 + src/main/sensors/battery.c | 2 + src/main/sensors/initialisation.c | 13 ++ src/main/target/CC3D/target.h | 3 + src/main/target/NAZE/hardware_revision.c | 4 +- src/main/target/NAZE/target.h | 2 +- src/main/target/RMDO/target.h | 2 +- src/main/target/SPRACINGF3/target.h | 2 +- 20 files changed, 203 insertions(+), 334 deletions(-) copy src/main/drivers/{accgyro_mpu6050.c => accgyro_mpu.h} (58%) rewrite src/main/drivers/accgyro_spi_mpu6000.h (68%) diff --git a/src/main/drivers/accgyro_mpu.c b/src/main/drivers/accgyro_mpu.c index 92139974a..266014bbe 100644 --- a/src/main/drivers/accgyro_mpu.c +++ b/src/main/drivers/accgyro_mpu.c @@ -61,14 +61,6 @@ static const extiConfig_t *mpuIntExtiConfig = NULL; #define MPU_ADDRESS 0x68 -// MPU6050 -#define MPU_RA_WHO_AM_I 0x75 -#define MPU_RA_WHO_AM_I_LEGACY 0x00 -#define MPU_RA_XA_OFFS_H 0x06 //[15:0] XA_OFFS -#define MPU_RA_PRODUCT_ID 0x0C // Product ID Register -#define MPU_RA_ACCEL_XOUT_H 0x3B -#define MPU_RA_GYRO_XOUT_H 0x43 - // WHO_AM_I register contents for MPU3050, 6050 and 6500 #define MPU6500_WHO_AM_I_CONST (0x70) #define MPUx0x0_WHO_AM_I_CONST (0x68) @@ -133,7 +125,7 @@ static bool detectSPISensorsAndUpdateDetectionResult(void) #ifdef USE_GYRO_SPI_MPU6500 if (mpu6500SpiDetect()) { mpuDetectionResult.sensor = MPU_65xx_SPI; - mpuConfiguration.gyroReadXRegister = MPU6500_RA_GYRO_XOUT_H; + mpuConfiguration.gyroReadXRegister = MPU_RA_GYRO_XOUT_H; mpuConfiguration.read = mpu6500ReadRegister; mpuConfiguration.write = mpu6500WriteRegister; return true; @@ -143,7 +135,7 @@ static bool detectSPISensorsAndUpdateDetectionResult(void) #ifdef USE_GYRO_SPI_MPU6000 if (mpu6000SpiDetect()) { mpuDetectionResult.sensor = MPU_60x0_SPI; - mpuConfiguration.gyroReadXRegister = MPU6000_GYRO_XOUT_H; + mpuConfiguration.gyroReadXRegister = MPU_RA_GYRO_XOUT_H; mpuConfiguration.read = mpu6000ReadRegister; mpuConfiguration.write = mpu6000WriteRegister; return true; @@ -243,7 +235,7 @@ void configureMPUDataReadyInterruptHandling(void) } #endif - registerExti15_10_CallbackHandler(MPU_DATA_READY_EXTI_Handler); + registerExtiCallbackHandler(mpuIntExtiConfig->exti_irqn, MPU_DATA_READY_EXTI_Handler); EXTI_ClearITPendingBit(mpuIntExtiConfig->exti_line); diff --git a/src/main/drivers/accgyro_mpu6050.c b/src/main/drivers/accgyro_mpu.h similarity index 58% copy from src/main/drivers/accgyro_mpu6050.c copy to src/main/drivers/accgyro_mpu.h index c235fe5fb..500d68354 100644 --- a/src/main/drivers/accgyro_mpu6050.c +++ b/src/main/drivers/accgyro_mpu.h @@ -15,38 +15,11 @@ * along with Cleanflight. If not, see . */ -#include -#include -#include +#pragma once -#include "platform.h" -#include "build_config.h" -#include "debug.h" - -#include "common/maths.h" - -#include "nvic.h" - -#include "system.h" -#include "gpio.h" -#include "exti.h" -#include "bus_i2c.h" - -#include "sensor.h" -#include "accgyro.h" -#include "accgyro_mpu.h" -#include "accgyro_mpu6050.h" - -extern uint8_t mpuLowPassFilter; - -//#define DEBUG_MPU_DATA_READY_INTERRUPT - -// MPU6050, Standard address 0x68 -// MPU_INT on PB13 on rev4 Naze32 hardware -#define MPU6050_ADDRESS 0x68 - -#define DMP_MEM_START_ADDR 0x6E -#define DMP_MEM_R_W 0x6F +// MPU6050 +#define MPU_RA_WHO_AM_I 0x75 +#define MPU_RA_WHO_AM_I_LEGACY 0x00 // RA = Register Address @@ -142,77 +115,72 @@ extern uint8_t mpuLowPassFilter; // RF = Register Flag #define MPU_RF_DATA_RDY_EN (1 << 0) -#define MPU6050_SMPLRT_DIV 0 // 8000Hz - -static void mpu6050AccInit(void); -static void mpu6050GyroInit(uint16_t lpf); - -bool mpu6050AccDetect(acc_t *acc) -{ - if (mpuDetectionResult.sensor != MPU_60x0) { - return false; - } - - acc->init = mpu6050AccInit; - acc->read = mpuAccRead; - acc->revisionCode = (mpuDetectionResult.resolution == MPU_HALF_RESOLUTION ? 'o' : 'n'); // es/non-es variance between MPU6050 sensors, half of the naze boards are mpu6000ES. - - return true; -} - -bool mpu6050GyroDetect(gyro_t *gyro) -{ - if (mpuDetectionResult.sensor != MPU_60x0) { - return false; - } - gyro->init = mpu6050GyroInit; - gyro->read = mpuGyroRead; - - // 16.4 dps/lsb scalefactor - gyro->scale = 1.0f / 16.4f; - - return true; -} - -static void mpu6050AccInit(void) -{ - mpuIntExtiInit(); - - switch (mpuDetectionResult.resolution) { - case MPU_HALF_RESOLUTION: - acc_1G = 256 * 8; - break; - case MPU_FULL_RESOLUTION: - acc_1G = 512 * 8; - break; - } -} - -static void mpu6050GyroInit(uint16_t lpf) -{ - bool ack; - - mpuIntExtiInit(); - - uint8_t mpuLowPassFilter = determineMPULPF(lpf); - - ack = mpuConfiguration.write(MPU_RA_PWR_MGMT_1, 0x80); //PWR_MGMT_1 -- DEVICE_RESET 1 - delay(100); - ack = mpuConfiguration.write(MPU_RA_SMPLRT_DIV, 0x00); //SMPLRT_DIV -- SMPLRT_DIV = 0 Sample Rate = Gyroscope Output Rate / (1 + SMPLRT_DIV) - ack = mpuConfiguration.write(MPU_RA_PWR_MGMT_1, 0x03); //PWR_MGMT_1 -- SLEEP 0; CYCLE 0; TEMP_DIS 0; CLKSEL 3 (PLL with Z Gyro reference) - delay(15); //PLL Settling time when changing CLKSEL is max 10ms. Use 15ms to be sure - ack = mpuConfiguration.write(MPU_RA_CONFIG, mpuLowPassFilter); //CONFIG -- EXT_SYNC_SET 0 (disable input pin for data sync) ; default DLPF_CFG = 0 => ACC bandwidth = 260Hz GYRO bandwidth = 256Hz) - ack = mpuConfiguration.write(MPU_RA_GYRO_CONFIG, INV_FSR_2000DPS << 3); //GYRO_CONFIG -- FS_SEL = 3: Full scale set to 2000 deg/sec - - // ACC Init stuff. - // Accel scale 8g (4096 LSB/g) - ack = mpuConfiguration.write(MPU_RA_ACCEL_CONFIG, INV_FSR_8G << 3); - - ack = mpuConfiguration.write(MPU_RA_INT_PIN_CFG, - 0 << 7 | 0 << 6 | 0 << 5 | 0 << 4 | 0 << 3 | 0 << 2 | 1 << 1 | 0 << 0); // INT_PIN_CFG -- INT_LEVEL_HIGH, INT_OPEN_DIS, LATCH_INT_DIS, INT_RD_CLEAR_DIS, FSYNC_INT_LEVEL_HIGH, FSYNC_INT_DIS, I2C_BYPASS_EN, CLOCK_DIS - -#ifdef USE_MPU_DATA_READY_SIGNAL - ack = mpuConfiguration.write(MPU_RA_INT_ENABLE, MPU_RF_DATA_RDY_EN); -#endif - UNUSED(ack); -} +typedef bool (*mpuReadRegisterFunc)(uint8_t reg, uint8_t length, uint8_t* data); +typedef bool (*mpuWriteRegisterFunc)(uint8_t reg, uint8_t data); + +typedef struct mpuConfiguration_s { + uint8_t gyroReadXRegister; // Y and Z must registers follow this, 2 words each + mpuReadRegisterFunc read; + mpuWriteRegisterFunc write; +} mpuConfiguration_t; + +extern mpuConfiguration_t mpuConfiguration; + +enum lpf_e { + INV_FILTER_256HZ_NOLPF2 = 0, + INV_FILTER_188HZ, + INV_FILTER_98HZ, + INV_FILTER_42HZ, + INV_FILTER_20HZ, + INV_FILTER_10HZ, + INV_FILTER_5HZ, + INV_FILTER_2100HZ_NOLPF, + NUM_FILTER +}; +enum gyro_fsr_e { + INV_FSR_250DPS = 0, + INV_FSR_500DPS, + INV_FSR_1000DPS, + INV_FSR_2000DPS, + NUM_GYRO_FSR +}; +enum clock_sel_e { + INV_CLK_INTERNAL = 0, + INV_CLK_PLL, + NUM_CLK +}; +enum accel_fsr_e { + INV_FSR_2G = 0, + INV_FSR_4G, + INV_FSR_8G, + INV_FSR_16G, + NUM_ACCEL_FSR +}; + +typedef enum { + MPU_NONE, + MPU_3050, + MPU_60x0, + MPU_60x0_SPI, + MPU_65xx_I2C, + MPU_65xx_SPI +} detectedMPUSensor_e; + +typedef enum { + MPU_HALF_RESOLUTION, + MPU_FULL_RESOLUTION +} mpu6050Resolution_e; + +typedef struct mpuDetectionResult_s { + detectedMPUSensor_e sensor; + mpu6050Resolution_e resolution; +} mpuDetectionResult_t; + +extern mpuDetectionResult_t mpuDetectionResult; + +uint8_t determineMPULPF(uint16_t lpf); +void configureMPUDataReadyInterruptHandling(void); +void mpuIntExtiInit(void); +bool mpuAccRead(int16_t *accData); +bool mpuGyroRead(int16_t *gyroADC); +mpuDetectionResult_t *detectMpu(const extiConfig_t *configToUse); diff --git a/src/main/drivers/accgyro_mpu6050.c b/src/main/drivers/accgyro_mpu6050.c index c235fe5fb..4c0bfce96 100644 --- a/src/main/drivers/accgyro_mpu6050.c +++ b/src/main/drivers/accgyro_mpu6050.c @@ -48,100 +48,6 @@ extern uint8_t mpuLowPassFilter; #define DMP_MEM_START_ADDR 0x6E #define DMP_MEM_R_W 0x6F -// RA = Register Address - -#define MPU_RA_XG_OFFS_TC 0x00 //[7] PWR_MODE, [6:1] XG_OFFS_TC, [0] OTP_BNK_VLD -#define MPU_RA_YG_OFFS_TC 0x01 //[7] PWR_MODE, [6:1] YG_OFFS_TC, [0] OTP_BNK_VLD -#define MPU_RA_ZG_OFFS_TC 0x02 //[7] PWR_MODE, [6:1] ZG_OFFS_TC, [0] OTP_BNK_VLD -#define MPU_RA_X_FINE_GAIN 0x03 //[7:0] X_FINE_GAIN -#define MPU_RA_Y_FINE_GAIN 0x04 //[7:0] Y_FINE_GAIN -#define MPU_RA_Z_FINE_GAIN 0x05 //[7:0] Z_FINE_GAIN -#define MPU_RA_XA_OFFS_H 0x06 //[15:0] XA_OFFS -#define MPU_RA_XA_OFFS_L_TC 0x07 -#define MPU_RA_YA_OFFS_H 0x08 //[15:0] YA_OFFS -#define MPU_RA_YA_OFFS_L_TC 0x09 -#define MPU_RA_ZA_OFFS_H 0x0A //[15:0] ZA_OFFS -#define MPU_RA_ZA_OFFS_L_TC 0x0B -#define MPU_RA_PRODUCT_ID 0x0C // Product ID Register -#define MPU_RA_XG_OFFS_USRH 0x13 //[15:0] XG_OFFS_USR -#define MPU_RA_XG_OFFS_USRL 0x14 -#define MPU_RA_YG_OFFS_USRH 0x15 //[15:0] YG_OFFS_USR -#define MPU_RA_YG_OFFS_USRL 0x16 -#define MPU_RA_ZG_OFFS_USRH 0x17 //[15:0] ZG_OFFS_USR -#define MPU_RA_ZG_OFFS_USRL 0x18 -#define MPU_RA_SMPLRT_DIV 0x19 -#define MPU_RA_CONFIG 0x1A -#define MPU_RA_GYRO_CONFIG 0x1B -#define MPU_RA_ACCEL_CONFIG 0x1C -#define MPU_RA_FF_THR 0x1D -#define MPU_RA_FF_DUR 0x1E -#define MPU_RA_MOT_THR 0x1F -#define MPU_RA_MOT_DUR 0x20 -#define MPU_RA_ZRMOT_THR 0x21 -#define MPU_RA_ZRMOT_DUR 0x22 -#define MPU_RA_FIFO_EN 0x23 -#define MPU_RA_I2C_MST_CTRL 0x24 -#define MPU_RA_I2C_SLV0_ADDR 0x25 -#define MPU_RA_I2C_SLV0_REG 0x26 -#define MPU_RA_I2C_SLV0_CTRL 0x27 -#define MPU_RA_I2C_SLV1_ADDR 0x28 -#define MPU_RA_I2C_SLV1_REG 0x29 -#define MPU_RA_I2C_SLV1_CTRL 0x2A -#define MPU_RA_I2C_SLV2_ADDR 0x2B -#define MPU_RA_I2C_SLV2_REG 0x2C -#define MPU_RA_I2C_SLV2_CTRL 0x2D -#define MPU_RA_I2C_SLV3_ADDR 0x2E -#define MPU_RA_I2C_SLV3_REG 0x2F -#define MPU_RA_I2C_SLV3_CTRL 0x30 -#define MPU_RA_I2C_SLV4_ADDR 0x31 -#define MPU_RA_I2C_SLV4_REG 0x32 -#define MPU_RA_I2C_SLV4_DO 0x33 -#define MPU_RA_I2C_SLV4_CTRL 0x34 -#define MPU_RA_I2C_SLV4_DI 0x35 -#define MPU_RA_I2C_MST_STATUS 0x36 -#define MPU_RA_INT_PIN_CFG 0x37 -#define MPU_RA_INT_ENABLE 0x38 -#define MPU_RA_DMP_INT_STATUS 0x39 -#define MPU_RA_INT_STATUS 0x3A -#define MPU_RA_ACCEL_XOUT_H 0x3B -#define MPU_RA_ACCEL_XOUT_L 0x3C -#define MPU_RA_ACCEL_YOUT_H 0x3D -#define MPU_RA_ACCEL_YOUT_L 0x3E -#define MPU_RA_ACCEL_ZOUT_H 0x3F -#define MPU_RA_ACCEL_ZOUT_L 0x40 -#define MPU_RA_TEMP_OUT_H 0x41 -#define MPU_RA_TEMP_OUT_L 0x42 -#define MPU_RA_GYRO_XOUT_H 0x43 -#define MPU_RA_GYRO_XOUT_L 0x44 -#define MPU_RA_GYRO_YOUT_H 0x45 -#define MPU_RA_GYRO_YOUT_L 0x46 -#define MPU_RA_GYRO_ZOUT_H 0x47 -#define MPU_RA_GYRO_ZOUT_L 0x48 -#define MPU_RA_EXT_SENS_DATA_00 0x49 -#define MPU_RA_MOT_DETECT_STATUS 0x61 -#define MPU_RA_I2C_SLV0_DO 0x63 -#define MPU_RA_I2C_SLV1_DO 0x64 -#define MPU_RA_I2C_SLV2_DO 0x65 -#define MPU_RA_I2C_SLV3_DO 0x66 -#define MPU_RA_I2C_MST_DELAY_CTRL 0x67 -#define MPU_RA_SIGNAL_PATH_RESET 0x68 -#define MPU_RA_MOT_DETECT_CTRL 0x69 -#define MPU_RA_USER_CTRL 0x6A -#define MPU_RA_PWR_MGMT_1 0x6B -#define MPU_RA_PWR_MGMT_2 0x6C -#define MPU_RA_BANK_SEL 0x6D -#define MPU_RA_MEM_START_ADDR 0x6E -#define MPU_RA_MEM_R_W 0x6F -#define MPU_RA_DMP_CFG_1 0x70 -#define MPU_RA_DMP_CFG_2 0x71 -#define MPU_RA_FIFO_COUNTH 0x72 -#define MPU_RA_FIFO_COUNTL 0x73 -#define MPU_RA_FIFO_R_W 0x74 -#define MPU_RA_WHO_AM_I 0x75 - -// RF = Register Flag -#define MPU_RF_DATA_RDY_EN (1 << 0) - #define MPU6050_SMPLRT_DIV 0 // 8000Hz static void mpu6050AccInit(void); diff --git a/src/main/drivers/accgyro_mpu6500.c b/src/main/drivers/accgyro_mpu6500.c index 5d15a4503..8a2ccc6ad 100644 --- a/src/main/drivers/accgyro_mpu6500.c +++ b/src/main/drivers/accgyro_mpu6500.c @@ -88,22 +88,22 @@ void mpu6500GyroInit(uint16_t lpf) uint8_t mpuLowPassFilter = determineMPULPF(lpf); - mpuConfiguration.write(MPU6500_RA_PWR_MGMT_1, MPU6500_BIT_RESET); + mpuConfiguration.write(MPU_RA_PWR_MGMT_1, MPU6500_BIT_RESET); delay(100); - mpuConfiguration.write(MPU6500_RA_SIGNAL_PATH_RST, 0x07); + mpuConfiguration.write(MPU_RA_SIGNAL_PATH_RESET, 0x07); delay(100); - mpuConfiguration.write(MPU6500_RA_PWR_MGMT_1, 0); + mpuConfiguration.write(MPU_RA_PWR_MGMT_1, 0); delay(100); - mpuConfiguration.write(MPU6500_RA_PWR_MGMT_1, INV_CLK_PLL); - mpuConfiguration.write(MPU6500_RA_GYRO_CFG, INV_FSR_2000DPS << 3); - mpuConfiguration.write(MPU6500_RA_ACCEL_CFG, INV_FSR_8G << 3); - mpuConfiguration.write(MPU6500_RA_LPF, mpuLowPassFilter); - mpuConfiguration.write(MPU6500_RA_RATE_DIV, 0); // 1kHz S/R + mpuConfiguration.write(MPU_RA_PWR_MGMT_1, INV_CLK_PLL); + mpuConfiguration.write(MPU_RA_GYRO_CONFIG, INV_FSR_2000DPS << 3); + mpuConfiguration.write(MPU_RA_ACCEL_CONFIG, INV_FSR_8G << 3); + mpuConfiguration.write(MPU_RA_CONFIG, mpuLowPassFilter); + mpuConfiguration.write(MPU_RA_SMPLRT_DIV, 0); // 1kHz S/R // Data ready interrupt configuration - mpuConfiguration.write(MPU6500_RA_INT_PIN_CFG, 0 << 7 | 0 << 6 | 0 << 5 | 1 << 4 | 0 << 3 | 0 << 2 | 1 << 1 | 0 << 0); // INT_ANYRD_2CLEAR, BYPASS_EN + mpuConfiguration.write(MPU_RA_INT_PIN_CFG, 0 << 7 | 0 << 6 | 0 << 5 | 1 << 4 | 0 << 3 | 0 << 2 | 1 << 1 | 0 << 0); // INT_ANYRD_2CLEAR, BYPASS_EN #ifdef USE_MPU_DATA_READY_SIGNAL - mpuConfiguration.write(MPU6500_RA_INT_ENABLE, 0x01); // RAW_RDY_EN interrupt enable + mpuConfiguration.write(MPU_RA_INT_ENABLE, 0x01); // RAW_RDY_EN interrupt enable #endif } diff --git a/src/main/drivers/accgyro_mpu6500.h b/src/main/drivers/accgyro_mpu6500.h index fbd3245e2..0c64cfa16 100644 --- a/src/main/drivers/accgyro_mpu6500.h +++ b/src/main/drivers/accgyro_mpu6500.h @@ -15,22 +15,6 @@ * along with Cleanflight. If not, see . */ -#define MPU6500_RA_RATE_DIV (0x19) -#define MPU6500_RA_LPF (0x1A) -#define MPU6500_RA_GYRO_CFG (0x1B) -#define MPU6500_RA_ACCEL_CFG (0x1C) -#define MPU6500_RA_ACCEL_XOUT_H (0x3B) -#define MPU6500_RA_INT_PIN_CFG (0x37) -#define MPU6500_RA_INT_ENABLE (0x38) -#define MPU6500_RA_GYRO_XOUT_H (0x43) -#define MPU6500_RA_SIGNAL_PATH_RST (0x68) -#define MPU6500_RA_USER_CTRL (0x6A) -#define MPU6500_RA_PWR_MGMT_1 (0x6B) -#define MPU6500_RA_BANK_SEL (0x6D) -#define MPU6500_RA_MEM_RW (0x6F) -#define MPU6500_RA_WHOAMI (0x75) -#define MPU6500_RA_XA_OFFS_H (0x77) - #define MPU6500_WHO_AM_I_CONST (0x70) #define MPU6500_BIT_RESET (0x80) diff --git a/src/main/drivers/accgyro_spi_mpu6000.c b/src/main/drivers/accgyro_spi_mpu6000.c index 177b646e7..b216975aa 100644 --- a/src/main/drivers/accgyro_spi_mpu6000.c +++ b/src/main/drivers/accgyro_spi_mpu6000.c @@ -122,6 +122,8 @@ bool mpu6000ReadRegister(uint8_t reg, uint8_t length, uint8_t *data) void mpu6000SpiGyroInit(uint16_t lpf) { + mpuIntExtiInit(); + uint8_t mpuLowPassFilter = determineMPULPF(lpf); mpu6000AccAndGyroInit(); @@ -145,6 +147,8 @@ void mpu6000SpiGyroInit(uint16_t lpf) void mpu6000SpiAccInit(void) { + mpuIntExtiInit(); + acc_1G = 512 * 8; } @@ -155,12 +159,12 @@ bool mpu6000SpiDetect(void) spiSetDivisor(MPU6000_SPI_INSTANCE, SPI_0_5625MHZ_CLOCK_DIVIDER); - mpu6000WriteRegister(MPU6000_PWR_MGMT_1, BIT_H_RESET); + mpu6000WriteRegister(MPU_RA_PWR_MGMT_1, BIT_H_RESET); do { delay(150); - mpu6000ReadRegister(MPU6000_WHOAMI, 1, &in); + mpu6000ReadRegister(MPU_RA_WHO_AM_I, 1, &in); if (in == MPU6000_WHO_AM_I_CONST) { break; } @@ -170,7 +174,7 @@ bool mpu6000SpiDetect(void) } while (attemptsRemaining--); - mpu6000ReadRegister(MPU6000_PRODUCT_ID, 1, &in); + mpu6000ReadRegister(MPU_RA_PRODUCT_ID, 1, &in); /* look for a product ID we recognise */ @@ -203,35 +207,44 @@ static void mpu6000AccAndGyroInit(void) { spiSetDivisor(MPU6000_SPI_INSTANCE, SPI_0_5625MHZ_CLOCK_DIVIDER); // Device Reset - mpu6000WriteRegister(MPU6000_PWR_MGMT_1, BIT_H_RESET); + mpu6000WriteRegister(MPU_RA_PWR_MGMT_1, BIT_H_RESET); delay(150); - mpu6000WriteRegister(MPU6000_SIGNAL_PATH_RESET, BIT_GYRO | BIT_ACC | BIT_TEMP); + mpu6000WriteRegister(MPU_RA_SIGNAL_PATH_RESET, BIT_GYRO | BIT_ACC | BIT_TEMP); delay(150); // Clock Source PPL with Z axis gyro reference - mpu6000WriteRegister(MPU6000_PWR_MGMT_1, MPU_CLK_SEL_PLLGYROZ); + mpu6000WriteRegister(MPU_RA_PWR_MGMT_1, MPU_CLK_SEL_PLLGYROZ); delayMicroseconds(1); // Disable Primary I2C Interface - mpu6000WriteRegister(MPU6000_USER_CTRL, BIT_I2C_IF_DIS); + mpu6000WriteRegister(MPU_RA_USER_CTRL, BIT_I2C_IF_DIS); delayMicroseconds(1); - mpu6000WriteRegister(MPU6000_PWR_MGMT_2, 0x00); + mpu6000WriteRegister(MPU_RA_PWR_MGMT_2, 0x00); delayMicroseconds(1); // Accel Sample Rate 1kHz // Gyroscope Output Rate = 1kHz when the DLPF is enabled - mpu6000WriteRegister(MPU6000_SMPLRT_DIV, 0x00); + mpu6000WriteRegister(MPU_RA_SMPLRT_DIV, 0x00); + delayMicroseconds(1); + + // Gyro +/- 1000 DPS Full Scale + mpu6000WriteRegister(MPU_RA_GYRO_CONFIG, INV_FSR_2000DPS << 3); delayMicroseconds(1); // Accel +/- 8 G Full Scale - mpu6000WriteRegister(MPU6000_ACCEL_CONFIG, BITS_FS_8G); + mpu6000WriteRegister(MPU_RA_ACCEL_CONFIG, INV_FSR_8G << 3); delayMicroseconds(1); - // Gyro +/- 1000 DPS Full Scale - mpu6000WriteRegister(MPU6000_GYRO_CONFIG, BITS_FS_2000DPS); + + mpu6000WriteRegister(MPU_RA_INT_PIN_CFG, 0 << 7 | 0 << 6 | 0 << 5 | 1 << 4 | 0 << 3 | 0 << 2 | 0 << 1 | 0 << 0); // INT_ANYRD_2CLEAR + delayMicroseconds(1); + +#ifdef USE_MPU_DATA_READY_SIGNAL + mpu6000WriteRegister(MPU_RA_INT_ENABLE, MPU_RF_DATA_RDY_EN); delayMicroseconds(1); +#endif spiSetDivisor(MPU6000_SPI_INSTANCE, SPI_18MHZ_CLOCK_DIVIDER); // 18 MHz SPI clock diff --git a/src/main/drivers/accgyro_spi_mpu6000.h b/src/main/drivers/accgyro_spi_mpu6000.h dissimilarity index 68% index ecadc23c7..01f6870d5 100644 --- a/src/main/drivers/accgyro_spi_mpu6000.h +++ b/src/main/drivers/accgyro_spi_mpu6000.h @@ -1,53 +1,21 @@ - -#pragma once - -#define MPU6000_CONFIG 0x1A - -// Registers -#define MPU6000_PRODUCT_ID 0x0C -#define MPU6000_SMPLRT_DIV 0x19 -#define MPU6000_GYRO_CONFIG 0x1B -#define MPU6000_ACCEL_CONFIG 0x1C -#define MPU6000_FIFO_EN 0x23 -#define MPU6000_INT_PIN_CFG 0x37 -#define MPU6000_INT_ENABLE 0x38 -#define MPU6000_INT_STATUS 0x3A -#define MPU6000_ACCEL_XOUT_H 0x3B -#define MPU6000_ACCEL_XOUT_L 0x3C -#define MPU6000_ACCEL_YOUT_H 0x3D -#define MPU6000_ACCEL_YOUT_L 0x3E -#define MPU6000_ACCEL_ZOUT_H 0x3F -#define MPU6000_ACCEL_ZOUT_L 0x40 -#define MPU6000_TEMP_OUT_H 0x41 -#define MPU6000_TEMP_OUT_L 0x42 -#define MPU6000_GYRO_XOUT_H 0x43 -#define MPU6000_GYRO_XOUT_L 0x44 -#define MPU6000_GYRO_YOUT_H 0x45 -#define MPU6000_GYRO_YOUT_L 0x46 -#define MPU6000_GYRO_ZOUT_H 0x47 -#define MPU6000_GYRO_ZOUT_L 0x48 -#define MPU6000_USER_CTRL 0x6A -#define MPU6000_SIGNAL_PATH_RESET 0x68 -#define MPU6000_PWR_MGMT_1 0x6B -#define MPU6000_PWR_MGMT_2 0x6C -#define MPU6000_FIFO_COUNTH 0x72 -#define MPU6000_FIFO_COUNTL 0x73 -#define MPU6000_FIFO_R_W 0x74 -#define MPU6000_WHOAMI 0x75 - -#define BITS_DLPF_CFG_256HZ 0x00 -#define BITS_DLPF_CFG_188HZ 0x01 -#define BITS_DLPF_CFG_98HZ 0x02 -#define BITS_DLPF_CFG_42HZ 0x03 - -#define GYRO_SCALE_FACTOR 0.00053292f // (4/131) * pi/180 (32.75 LSB = 1 DPS) - -#define MPU6000_WHO_AM_I_CONST (0x68) - -bool mpu6000SpiDetect(void); - -bool mpu6000SpiAccDetect(acc_t *acc); -bool mpu6000SpiGyroDetect(gyro_t *gyro); - -bool mpu6000WriteRegister(uint8_t reg, uint8_t data); -bool mpu6000ReadRegister(uint8_t reg, uint8_t length, uint8_t *data); + +#pragma once + +#define MPU6000_CONFIG 0x1A + +#define BITS_DLPF_CFG_256HZ 0x00 +#define BITS_DLPF_CFG_188HZ 0x01 +#define BITS_DLPF_CFG_98HZ 0x02 +#define BITS_DLPF_CFG_42HZ 0x03 + +#define GYRO_SCALE_FACTOR 0.00053292f // (4/131) * pi/180 (32.75 LSB = 1 DPS) + +#define MPU6000_WHO_AM_I_CONST (0x68) + +bool mpu6000SpiDetect(void); + +bool mpu6000SpiAccDetect(acc_t *acc); +bool mpu6000SpiGyroDetect(gyro_t *gyro); + +bool mpu6000WriteRegister(uint8_t reg, uint8_t data); +bool mpu6000ReadRegister(uint8_t reg, uint8_t length, uint8_t *data); diff --git a/src/main/drivers/accgyro_spi_mpu6500.c b/src/main/drivers/accgyro_spi_mpu6500.c index faafd69a5..b464e5dc4 100755 --- a/src/main/drivers/accgyro_spi_mpu6500.c +++ b/src/main/drivers/accgyro_spi_mpu6500.c @@ -105,7 +105,7 @@ bool mpu6500SpiDetect(void) mpu6500SpiInit(); - mpu6500ReadRegister(MPU6500_RA_WHOAMI, 1, &tmp); + mpu6500ReadRegister(MPU_RA_WHO_AM_I, 1, &tmp); if (tmp != MPU6500_WHO_AM_I_CONST) return false; diff --git a/src/main/drivers/barometer_bmp085.c b/src/main/drivers/barometer_bmp085.c index a46b8f20c..ee6f4da62 100644 --- a/src/main/drivers/barometer_bmp085.c +++ b/src/main/drivers/barometer_bmp085.c @@ -169,7 +169,7 @@ bool bmp085Detect(const bmp085Config_t *config, baro_t *baro) gpioInit(config->eocGpioPort, &gpio); BMP085_ON; - registerExti15_10_CallbackHandler(BMP085_EOC_EXTI_Handler); + registerExtiCallbackHandler(EXTI15_10_IRQn, BMP085_EOC_EXTI_Handler); // EXTI interrupt for barometer EOC gpioExtiLineConfig(GPIO_PortSourceGPIOC, GPIO_PinSource14); @@ -224,7 +224,7 @@ bool bmp085Detect(const bmp085Config_t *config, baro_t *baro) EXTI_InitStructure.EXTI_LineCmd = DISABLE; EXTI_Init(&EXTI_InitStructure); - unregisterExti15_10_CallbackHandler(BMP085_EOC_EXTI_Handler); + unregisterExtiCallbackHandler(EXTI15_10_IRQn, BMP085_EOC_EXTI_Handler); #endif BMP085_OFF; diff --git a/src/main/drivers/compass_hmc5883l.c b/src/main/drivers/compass_hmc5883l.c index 6911034a7..0f34be0a7 100644 --- a/src/main/drivers/compass_hmc5883l.c +++ b/src/main/drivers/compass_hmc5883l.c @@ -176,7 +176,7 @@ static void hmc5883lConfigureDataReadyInterruptHandling(void) } #endif - registerExti15_10_CallbackHandler(MAG_DATA_READY_EXTI_Handler); + registerExtiCallbackHandler(hmc5883Config->exti_irqn, MAG_DATA_READY_EXTI_Handler); EXTI_ClearITPendingBit(hmc5883Config->exti_line); diff --git a/src/main/drivers/system.c b/src/main/drivers/system.c index 0f99b1bec..2377d69d5 100644 --- a/src/main/drivers/system.c +++ b/src/main/drivers/system.c @@ -31,45 +31,61 @@ #include "system.h" - -#ifndef EXTI15_10_CALLBACK_HANDLER_COUNT -#define EXTI15_10_CALLBACK_HANDLER_COUNT 1 +#ifndef EXTI_CALLBACK_HANDLER_COUNT +#define EXTI_CALLBACK_HANDLER_COUNT 1 #endif -static extiCallbackHandler* exti15_10_handlers[EXTI15_10_CALLBACK_HANDLER_COUNT]; +typedef struct extiCallbackHandlerConfig_s { + IRQn_Type irqn; + extiCallbackHandlerFunc* fn; +} extiCallbackHandlerConfig_t; + +static extiCallbackHandlerConfig_t extiHandlerConfigs[EXTI_CALLBACK_HANDLER_COUNT]; -void registerExti15_10_CallbackHandler(extiCallbackHandler *fn) +void registerExtiCallbackHandler(IRQn_Type irqn, extiCallbackHandlerFunc *fn) { - for (int index = 0; index < EXTI15_10_CALLBACK_HANDLER_COUNT; index++) { - extiCallbackHandler *candidate = exti15_10_handlers[index]; - if (!candidate) { - exti15_10_handlers[index] = fn; + for (int index = 0; index < EXTI_CALLBACK_HANDLER_COUNT; index++) { + extiCallbackHandlerConfig_t *candidate = &extiHandlerConfigs[index]; + if (!candidate->fn) { + candidate->fn = fn; + candidate->irqn = irqn; return; } } - failureMode(FAILURE_DEVELOPER); // EXTI15_10_CALLBACK_HANDLER_COUNT is too low for the amount of handlers required. + failureMode(FAILURE_DEVELOPER); // EXTI_CALLBACK_HANDLER_COUNT is too low for the amount of handlers required. } -void unregisterExti15_10_CallbackHandler(extiCallbackHandler *fn) +void unregisterExtiCallbackHandler(IRQn_Type irqn, extiCallbackHandlerFunc *fn) { - for (int index = 0; index < EXTI15_10_CALLBACK_HANDLER_COUNT; index++) { - extiCallbackHandler *candidate = exti15_10_handlers[index]; - if (candidate == fn) { - exti15_10_handlers[index] = 0; + for (int index = 0; index < EXTI_CALLBACK_HANDLER_COUNT; index++) { + extiCallbackHandlerConfig_t *candidate = &extiHandlerConfigs[index]; + if (candidate->fn == fn && candidate->irqn == irqn) { + candidate->fn = NULL; + candidate->irqn = 0; return; } } } -void EXTI15_10_IRQHandler(void) +static void extiHandler(IRQn_Type irqn) { - for (int index = 0; index < EXTI15_10_CALLBACK_HANDLER_COUNT; index++) { - extiCallbackHandler *fn = exti15_10_handlers[index]; - if (!fn) { - continue; + for (int index = 0; index < EXTI_CALLBACK_HANDLER_COUNT; index++) { + extiCallbackHandlerConfig_t *candidate = &extiHandlerConfigs[index]; + if (candidate->fn && candidate->irqn == irqn) { + candidate->fn(); } - fn(); } + +} + +void EXTI15_10_IRQHandler(void) +{ + extiHandler(EXTI15_10_IRQn); +} + +void EXTI3_IRQHandler(void) +{ + extiHandler(EXTI3_IRQn); } // cycles per microsecond @@ -149,7 +165,7 @@ void systemInit(void) cycleCounterInit(); - memset(&exti15_10_handlers, 0x00, sizeof(exti15_10_handlers)); + memset(extiHandlerConfigs, 0x00, sizeof(extiHandlerConfigs)); // SysTick SysTick_Config(SystemCoreClock / 1000); } diff --git a/src/main/drivers/system.h b/src/main/drivers/system.h index 65d0f18a7..b92e6ffce 100644 --- a/src/main/drivers/system.h +++ b/src/main/drivers/system.h @@ -36,10 +36,10 @@ void enableGPIOPowerUsageAndNoiseReductions(void); // current crystal frequency - 8 or 12MHz extern uint32_t hse_value; -typedef void extiCallbackHandler(void); +typedef void extiCallbackHandlerFunc(void); -void registerExti15_10_CallbackHandler(extiCallbackHandler *fn); -void unregisterExti15_10_CallbackHandler(extiCallbackHandler *fn); +void registerExtiCallbackHandler(IRQn_Type irqn, extiCallbackHandlerFunc *fn); +void unregisterExtiCallbackHandler(IRQn_Type irqn, extiCallbackHandlerFunc *fn); extern uint32_t cachedRccCsrValue; diff --git a/src/main/flight/failsafe.c b/src/main/flight/failsafe.c index 037c42414..c6659ca38 100644 --- a/src/main/flight/failsafe.c +++ b/src/main/flight/failsafe.c @@ -18,6 +18,8 @@ #include #include +#include "platform.h" + #include "debug.h" #include "common/axis.h" diff --git a/src/main/sensors/battery.c b/src/main/sensors/battery.c index 58c4a7774..7419eded9 100644 --- a/src/main/sensors/battery.c +++ b/src/main/sensors/battery.c @@ -18,6 +18,8 @@ #include "stdbool.h" #include "stdint.h" +#include "platform.h" + #include "common/maths.h" #include "drivers/adc.h" diff --git a/src/main/sensors/initialisation.c b/src/main/sensors/initialisation.c index f1dc52791..1b7d69103 100755 --- a/src/main/sensors/initialisation.c +++ b/src/main/sensors/initialisation.c @@ -124,6 +124,19 @@ const extiConfig_t *selectMPUIntExtiConfig(void) return &spRacingF3MPUIntExtiConfig; #endif +#if defined(CC3D) + static const extiConfig_t cc3dMPUIntExtiConfig = { + .gpioAPB2Peripherals = RCC_APB2Periph_GPIOA, + .gpioPort = GPIOA, + .gpioPin = Pin_3, + .exti_port_source = GPIO_PortSourceGPIOA, + .exti_pin_source = GPIO_PinSource3, + .exti_line = EXTI_Line3, + .exti_irqn = EXTI3_IRQn + }; + return &cc3dMPUIntExtiConfig; +#endif + return NULL; } diff --git a/src/main/target/CC3D/target.h b/src/main/target/CC3D/target.h index 16f7080a1..ddc67a5da 100644 --- a/src/main/target/CC3D/target.h +++ b/src/main/target/CC3D/target.h @@ -44,6 +44,9 @@ #define USABLE_TIMER_CHANNEL_COUNT 12 +#define DEBUG_MPU_DATA_READY_INTERRUPT +#define USE_MPU_DATA_READY_SIGNAL + #define GYRO #define USE_GYRO_SPI_MPU6000 diff --git a/src/main/target/NAZE/hardware_revision.c b/src/main/target/NAZE/hardware_revision.c index 26ade1a33..4f268289a 100755 --- a/src/main/target/NAZE/hardware_revision.c +++ b/src/main/target/NAZE/hardware_revision.c @@ -26,7 +26,9 @@ #include "drivers/system.h" #include "drivers/bus_spi.h" #include "drivers/sensor.h" +#include "drivers/exti.h" #include "drivers/accgyro.h" +#include "drivers/accgyro_mpu.h" #include "drivers/accgyro_mpu6500.h" #include "hardware_revision.h" @@ -80,7 +82,7 @@ uint8_t detectSpiDevice(void) // try autodetect MPU delay(50); ENABLE_SPI_CS; - spiTransferByte(NAZE_SPI_INSTANCE, MPU6500_RA_WHOAMI | MPU6500_BIT_RESET); + spiTransferByte(NAZE_SPI_INSTANCE, MPU_RA_WHO_AM_I | MPU6500_BIT_RESET); in[0] = spiTransferByte(NAZE_SPI_INSTANCE, 0xff); DISABLE_SPI_CS; diff --git a/src/main/target/NAZE/target.h b/src/main/target/NAZE/target.h index 836ce7aab..10423c5a0 100644 --- a/src/main/target/NAZE/target.h +++ b/src/main/target/NAZE/target.h @@ -73,7 +73,7 @@ #define USE_FLASH_M25P16 -#define EXTI15_10_CALLBACK_HANDLER_COUNT 3 // MPU data ready, MAG data ready, BMP085 EOC +#define EXTI_CALLBACK_HANDLER_COUNT 3 // MPU data ready, MAG data ready, BMP085 EOC //#define DEBUG_MPU_DATA_READY_INTERRUPT #define USE_MPU_DATA_READY_SIGNAL diff --git a/src/main/target/RMDO/target.h b/src/main/target/RMDO/target.h index 652a46e10..faccf9b80 100644 --- a/src/main/target/RMDO/target.h +++ b/src/main/target/RMDO/target.h @@ -30,7 +30,7 @@ #define USABLE_TIMER_CHANNEL_COUNT 17 -#define EXTI15_10_CALLBACK_HANDLER_COUNT 2 // MPU data ready and MAG data ready +#define EXTI_CALLBACK_HANDLER_COUNT 2 // MPU data ready and MAG data ready #define USE_MPU_DATA_READY_SIGNAL #define ENSURE_MPU_DATA_READY_IS_LOW diff --git a/src/main/target/SPRACINGF3/target.h b/src/main/target/SPRACINGF3/target.h index 9ed3d3cca..fdb0a082a 100644 --- a/src/main/target/SPRACINGF3/target.h +++ b/src/main/target/SPRACINGF3/target.h @@ -30,7 +30,7 @@ #define USABLE_TIMER_CHANNEL_COUNT 17 -#define EXTI15_10_CALLBACK_HANDLER_COUNT 2 // MPU data ready and MAG data ready +#define EXTI_CALLBACK_HANDLER_COUNT 2 // MPU data ready and MAG data ready #define USE_MPU_DATA_READY_SIGNAL #define ENSURE_MPU_DATA_READY_IS_LOW -- 2.11.4.GIT