2015-08-16 16:06:41 -03:00
# include <assert.h>
2016-01-12 14:22:11 -04:00
# include <utility>
2016-11-08 20:33:05 -04:00
# include <stdio.h>
2015-08-16 16:06:41 -03:00
2015-08-11 03:28:43 -03:00
# include <AP_HAL/AP_HAL.h>
2016-01-12 14:22:11 -04:00
2011-11-12 23:20:25 -04:00
# include "AP_InertialSensor_MPU6000.h"
2012-10-11 21:27:19 -03:00
extern const AP_HAL : : HAL & hal ;
2011-11-12 23:20:25 -04:00
2015-11-03 09:46:29 -04:00
# if CONFIG_HAL_BOARD == HAL_BOARD_LINUX
2015-08-15 19:52:30 -03:00
# include <AP_HAL_Linux/GPIO.h>
2015-08-17 23:10:05 -03:00
# if CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_ERLEBOARD || CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_PXF
2014-07-05 05:19:08 -03:00
# define MPU6000_DRDY_PIN BBB_P8_14
2015-08-17 23:10:05 -03:00
# elif CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_RASPILOT
# define MPU6000_DRDY_PIN RPI_GPIO_24
2015-10-29 17:13:51 -03:00
# elif CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_MINLURE
# define MPU6000_DRDY_PIN MINNOW_GPIO_I2S_CLK
2016-10-03 11:00:18 -03:00
# elif CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_DISCO || CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_BEBOP
2016-08-31 01:56:27 -03:00
# define MPU6000_EXT_SYNC_ENABLE 1
# endif
2014-07-05 05:19:08 -03:00
# endif
2016-08-31 01:56:27 -03:00
/*
EXT_SYNC allows for frame synchronisation with an external device
such as a camera . When enabled the LSB of AccelZ holds the FSYNC bit
*/
# ifndef MPU6000_EXT_SYNC_ENABLE
# define MPU6000_EXT_SYNC_ENABLE 0
2014-07-06 23:03:57 -03:00
# endif
2014-07-05 05:19:08 -03:00
2011-11-12 23:20:25 -04:00
// MPU 6000 registers
2015-07-25 14:29:13 -03:00
# define MPUREG_XG_OFFS_TC 0x00
# define MPUREG_YG_OFFS_TC 0x01
# define MPUREG_ZG_OFFS_TC 0x02
# define MPUREG_X_FINE_GAIN 0x03
# define MPUREG_Y_FINE_GAIN 0x04
# define MPUREG_Z_FINE_GAIN 0x05
# define MPUREG_XA_OFFS_H 0x06 // X axis accelerometer offset (high byte)
# define MPUREG_XA_OFFS_L 0x07 // X axis accelerometer offset (low byte)
# define MPUREG_YA_OFFS_H 0x08 // Y axis accelerometer offset (high byte)
# define MPUREG_YA_OFFS_L 0x09 // Y axis accelerometer offset (low byte)
# define MPUREG_ZA_OFFS_H 0x0A // Z axis accelerometer offset (high byte)
# define MPUREG_ZA_OFFS_L 0x0B // Z axis accelerometer offset (low byte)
# define MPUREG_PRODUCT_ID 0x0C // Product ID Register
2012-08-17 03:19:56 -03:00
# define MPUREG_XG_OFFS_USRH 0x13 // X axis gyro offset (high byte)
# define MPUREG_XG_OFFS_USRL 0x14 // X axis gyro offset (low byte)
# define MPUREG_YG_OFFS_USRH 0x15 // Y axis gyro offset (high byte)
# define MPUREG_YG_OFFS_USRL 0x16 // Y axis gyro offset (low byte)
# define MPUREG_ZG_OFFS_USRH 0x17 // Z axis gyro offset (high byte)
# define MPUREG_ZG_OFFS_USRL 0x18 // Z axis gyro offset (low byte)
2015-07-25 14:29:13 -03:00
# define MPUREG_SMPLRT_DIV 0x19 // sample rate. Fsample= 1Khz/(<this value>+1) = 200Hz
# define MPUREG_SMPLRT_1000HZ 0x00
# define MPUREG_SMPLRT_500HZ 0x01
# define MPUREG_SMPLRT_250HZ 0x03
# define MPUREG_SMPLRT_200HZ 0x04
# define MPUREG_SMPLRT_100HZ 0x09
# define MPUREG_SMPLRT_50HZ 0x13
# define MPUREG_CONFIG 0x1A
2016-08-31 01:56:27 -03:00
# define MPUREG_CONFIG_EXT_SYNC_SHIFT 3
# define MPUREG_CONFIG_EXT_SYNC_GX 0x02
# define MPUREG_CONFIG_EXT_SYNC_GY 0x03
# define MPUREG_CONFIG_EXT_SYNC_GZ 0x04
# define MPUREG_CONFIG_EXT_SYNC_AX 0x05
# define MPUREG_CONFIG_EXT_SYNC_AY 0x06
# define MPUREG_CONFIG_EXT_SYNC_AZ 0x07
2015-07-25 14:29:13 -03:00
# define MPUREG_GYRO_CONFIG 0x1B
2012-07-28 02:14:43 -03:00
// bit definitions for MPUREG_GYRO_CONFIG
2015-07-25 14:29:13 -03:00
# define BITS_GYRO_FS_250DPS 0x00
# define BITS_GYRO_FS_500DPS 0x08
# define BITS_GYRO_FS_1000DPS 0x10
# define BITS_GYRO_FS_2000DPS 0x18
# define BITS_GYRO_FS_MASK 0x18 // only bits 3 and 4 are used for gyro full scale so use this to mask off other bits
# define BITS_GYRO_ZGYRO_SELFTEST 0x20
# define BITS_GYRO_YGYRO_SELFTEST 0x40
# define BITS_GYRO_XGYRO_SELFTEST 0x80
# define MPUREG_ACCEL_CONFIG 0x1C
# define MPUREG_MOT_THR 0x1F // detection threshold for Motion interrupt generation. Motion is detected when the absolute value of any of the accelerometer measurements exceeds this
# define MPUREG_MOT_DUR 0x20 // duration counter threshold for Motion interrupt generation. The duration counter ticks at 1 kHz, therefore MOT_DUR has a unit of 1 LSB = 1 ms
# define MPUREG_ZRMOT_THR 0x21 // detection threshold for Zero Motion interrupt generation.
# define MPUREG_ZRMOT_DUR 0x22 // duration counter threshold for Zero Motion interrupt generation. The duration counter ticks at 16 Hz, therefore ZRMOT_DUR has a unit of 1 LSB = 64 ms.
# define MPUREG_FIFO_EN 0x23
# define BIT_TEMP_FIFO_EN 0x80
# define BIT_XG_FIFO_EN 0x40
# define BIT_YG_FIFO_EN 0x20
# define BIT_ZG_FIFO_EN 0x10
# define BIT_ACCEL_FIFO_EN 0x08
# define BIT_SLV2_FIFO_EN 0x04
# define BIT_SLV1_FIFO_EN 0x02
# define BIT_SLV0_FIFI_EN0 0x01
2015-08-16 16:23:24 -03:00
# define MPUREG_I2C_MST_CTRL 0x24
# define BIT_I2C_MST_P_NSR 0x10
# define BIT_I2C_MST_CLK_400KHZ 0x0D
# define MPUREG_I2C_SLV0_ADDR 0x25
# define MPUREG_I2C_SLV1_ADDR 0x28
# define MPUREG_I2C_SLV2_ADDR 0x2B
# define MPUREG_I2C_SLV3_ADDR 0x2E
2015-07-25 14:29:13 -03:00
# define MPUREG_INT_PIN_CFG 0x37
# define BIT_INT_RD_CLEAR 0x10 // clear the interrupt when any read occurs
# define BIT_LATCH_INT_EN 0x20 // latch data ready pin
2015-08-16 16:23:24 -03:00
# define MPUREG_I2C_SLV4_CTRL 0x34
2015-07-25 14:29:13 -03:00
# define MPUREG_INT_ENABLE 0x38
2012-07-28 02:14:43 -03:00
// bit definitions for MPUREG_INT_ENABLE
2015-07-25 14:29:13 -03:00
# define BIT_RAW_RDY_EN 0x01
# define BIT_DMP_INT_EN 0x02 // enabling this bit (DMP_INT_EN) also enables RAW_RDY_EN it seems
# define BIT_UNKNOWN_INT_EN 0x04
# define BIT_I2C_MST_INT_EN 0x08
# define BIT_FIFO_OFLOW_EN 0x10
# define BIT_ZMOT_EN 0x20
# define BIT_MOT_EN 0x40
# define BIT_FF_EN 0x80
# define MPUREG_INT_STATUS 0x3A
2012-07-28 02:14:43 -03:00
// bit definitions for MPUREG_INT_STATUS (same bit pattern as above because this register shows what interrupt actually fired)
2015-07-25 14:29:13 -03:00
# define BIT_RAW_RDY_INT 0x01
# define BIT_DMP_INT 0x02
# define BIT_UNKNOWN_INT 0x04
# define BIT_I2C_MST_INT 0x08
# define BIT_FIFO_OFLOW_INT 0x10
# define BIT_ZMOT_INT 0x20
# define BIT_MOT_INT 0x40
# define BIT_FF_INT 0x80
# define MPUREG_ACCEL_XOUT_H 0x3B
# define MPUREG_ACCEL_XOUT_L 0x3C
# define MPUREG_ACCEL_YOUT_H 0x3D
# define MPUREG_ACCEL_YOUT_L 0x3E
# define MPUREG_ACCEL_ZOUT_H 0x3F
# define MPUREG_ACCEL_ZOUT_L 0x40
# define MPUREG_TEMP_OUT_H 0x41
# define MPUREG_TEMP_OUT_L 0x42
# define MPUREG_GYRO_XOUT_H 0x43
# define MPUREG_GYRO_XOUT_L 0x44
# define MPUREG_GYRO_YOUT_H 0x45
# define MPUREG_GYRO_YOUT_L 0x46
# define MPUREG_GYRO_ZOUT_H 0x47
# define MPUREG_GYRO_ZOUT_L 0x48
2015-08-16 16:23:24 -03:00
# define MPUREG_EXT_SENS_DATA_00 0x49
# define MPUREG_I2C_SLV0_DO 0x63
# define MPUREG_I2C_MST_DELAY_CTRL 0x67
# define BIT_I2C_SLV0_DLY_EN 0x01
# define BIT_I2C_SLV1_DLY_EN 0x02
# define BIT_I2C_SLV2_DLY_EN 0x04
# define BIT_I2C_SLV3_DLY_EN 0x08
2015-07-25 14:29:13 -03:00
# define MPUREG_USER_CTRL 0x6A
2012-07-28 02:14:43 -03:00
// bit definitions for MPUREG_USER_CTRL
2015-07-25 14:29:13 -03:00
# define BIT_USER_CTRL_SIG_COND_RESET 0x01 // resets signal paths and results registers for all sensors (gyros, accel, temp)
# define BIT_USER_CTRL_I2C_MST_RESET 0x02 // reset I2C Master (only applicable if I2C_MST_EN bit is set)
# define BIT_USER_CTRL_FIFO_RESET 0x04 // Reset (i.e. clear) FIFO buffer
# define BIT_USER_CTRL_DMP_RESET 0x08 // Reset DMP
# define BIT_USER_CTRL_I2C_IF_DIS 0x10 // Disable primary I2C interface and enable hal.spi->interface
# define BIT_USER_CTRL_I2C_MST_EN 0x20 // Enable MPU to act as the I2C Master to external slave sensors
# define BIT_USER_CTRL_FIFO_EN 0x40 // Enable FIFO operations
# define BIT_USER_CTRL_DMP_EN 0x80 // Enable DMP operations
# define MPUREG_PWR_MGMT_1 0x6B
# define BIT_PWR_MGMT_1_CLK_INTERNAL 0x00 // clock set to internal 8Mhz oscillator
# define BIT_PWR_MGMT_1_CLK_XGYRO 0x01 // PLL with X axis gyroscope reference
# define BIT_PWR_MGMT_1_CLK_YGYRO 0x02 // PLL with Y axis gyroscope reference
# define BIT_PWR_MGMT_1_CLK_ZGYRO 0x03 // PLL with Z axis gyroscope reference
# define BIT_PWR_MGMT_1_CLK_EXT32KHZ 0x04 // PLL with external 32.768kHz reference
# define BIT_PWR_MGMT_1_CLK_EXT19MHZ 0x05 // PLL with external 19.2MHz reference
# define BIT_PWR_MGMT_1_CLK_STOP 0x07 // Stops the clock and keeps the timing generator in reset
# define BIT_PWR_MGMT_1_TEMP_DIS 0x08 // disable temperature sensor
# define BIT_PWR_MGMT_1_CYCLE 0x20 // put sensor into cycle mode. cycles between sleep mode and waking up to take a single sample of data from active sensors at a rate determined by LP_WAKE_CTRL
# define BIT_PWR_MGMT_1_SLEEP 0x40 // put sensor into low power sleep mode
# define BIT_PWR_MGMT_1_DEVICE_RESET 0x80 // reset entire device
# define MPUREG_PWR_MGMT_2 0x6C // allows the user to configure the frequency of wake-ups in Accelerometer Only Low Power Mode
# define MPUREG_BANK_SEL 0x6D // DMP bank selection register (used to indirectly access DMP registers)
# define MPUREG_MEM_START_ADDR 0x6E // DMP memory start address (used to indirectly write to dmp memory)
# define MPUREG_MEM_R_W 0x6F // DMP related register
# define MPUREG_DMP_CFG_1 0x70 // DMP related register
# define MPUREG_DMP_CFG_2 0x71 // DMP related register
# define MPUREG_FIFO_COUNTH 0x72
# define MPUREG_FIFO_COUNTL 0x73
# define MPUREG_FIFO_R_W 0x74
# define MPUREG_WHOAMI 0x75
2011-11-12 23:20:25 -04:00
2016-11-05 06:43:28 -03:00
// ICM2608 specific registers
2016-11-08 20:33:05 -04:00
# define ICMREG_ACCEL_CONFIG2 0x1D
# define ICM_ACC_DLPF_CFG_1046HZ_NOLPF 0x00
# define ICM_ACC_DLPF_CFG_218HZ 0x01
# define ICM_ACC_DLPF_CFG_99HZ 0x02
# define ICM_ACC_DLPF_CFG_44HZ 0x03
# define ICM_ACC_DLPF_CFG_21HZ 0x04
# define ICM_ACC_DLPF_CFG_10HZ 0x05
# define ICM_ACC_DLPF_CFG_5HZ 0x06
# define ICM_ACC_DLPF_CFG_420HZ 0x07
# define ICM_ACC_FCHOICE_B 0x08
2016-11-05 06:43:28 -03:00
/* this is an undocumented register which
if set incorrectly results in getting a 2.7 m / s / s offset
on the Y axis of the accelerometer
*/
# define MPUREG_ICM_UNDOC1 0x11
# define MPUREG_ICM_UNDOC1_VALUE 0xc9
// WHOAMI values
# define MPU_WHOAMI_6000 0x68
# define ICM_WHOAMI_20608 0xaf
2015-08-16 16:06:41 -03:00
# define BIT_READ_FLAG 0x80
2015-08-16 16:23:24 -03:00
# define BIT_I2C_SLVX_EN 0x80
2015-08-16 16:06:41 -03:00
2011-11-12 23:20:25 -04:00
// Configuration bits MPU 3000 and MPU 6000 (not revised)?
2012-08-17 03:19:56 -03:00
# define BITS_DLPF_CFG_256HZ_NOLPF2 0x00
2015-07-25 14:29:13 -03:00
# define BITS_DLPF_CFG_188HZ 0x01
# define BITS_DLPF_CFG_98HZ 0x02
# define BITS_DLPF_CFG_42HZ 0x03
# define BITS_DLPF_CFG_20HZ 0x04
# define BITS_DLPF_CFG_10HZ 0x05
# define BITS_DLPF_CFG_5HZ 0x06
2012-08-17 03:19:56 -03:00
# define BITS_DLPF_CFG_2100HZ_NOLPF 0x07
2015-07-25 14:29:13 -03:00
# define BITS_DLPF_CFG_MASK 0x07
2012-08-17 03:19:56 -03:00
// Product ID Description for MPU6000
// high 4 bits low 4 bits
// Product Name Product Revision
# define MPU6000ES_REV_C4 0x14 // 0001 0100
# define MPU6000ES_REV_C5 0x15 // 0001 0101
# define MPU6000ES_REV_D6 0x16 // 0001 0110
# define MPU6000ES_REV_D7 0x17 // 0001 0111
# define MPU6000ES_REV_D8 0x18 // 0001 1000
# define MPU6000_REV_C4 0x54 // 0101 0100
# define MPU6000_REV_C5 0x55 // 0101 0101
# define MPU6000_REV_D6 0x56 // 0101 0110
# define MPU6000_REV_D7 0x57 // 0101 0111
# define MPU6000_REV_D8 0x58 // 0101 1000
# define MPU6000_REV_D9 0x59 // 0101 1001
2012-05-08 22:25:09 -03:00
2016-11-09 01:16:52 -04:00
# define MPU6000_SAMPLE_SIZE 14
2016-11-08 21:11:17 -04:00
# define MPU6000_MAX_FIFO_SAMPLES 20
2016-01-12 14:22:11 -04:00
# define MAX_DATA_READ (MPU6000_MAX_FIFO_SAMPLES * MPU6000_SAMPLE_SIZE)
2015-06-15 09:58:30 -03:00
2016-01-12 14:22:11 -04:00
# define int16_val(v, idx) ((int16_t)(((uint16_t)v[2*idx] << 8) | v[2*idx+1]))
# define uint16_val(v, idx)(((uint16_t)v[2*idx] << 8) | v[2*idx+1])
2015-08-16 16:23:24 -03:00
2012-08-17 03:19:56 -03:00
/*
2012-11-19 11:37:42 -04:00
* RM - MPU - 6000 A - 00. pdf , page 33 , section 4.25 lists LSB sensitivity of
2012-08-17 03:19:56 -03:00
* gyro as 16.4 LSB / DPS at scale factor of + / - 2000 dps ( FS_SEL = = 3 )
2011-11-12 23:20:25 -04:00
*/
2016-01-12 14:22:11 -04:00
static const float GYRO_SCALE = ( 0.0174532f / 16.4f ) ;
2011-11-12 23:20:25 -04:00
2012-11-05 00:27:03 -04:00
/*
2012-11-19 11:37:42 -04:00
* RM - MPU - 6000 A - 00. pdf , page 31 , section 4.23 lists LSB sensitivity of
2012-11-05 00:27:03 -04:00
* accel as 4096 LSB / mg at scale factor of + / - 8 g ( AFS_SEL = = 2 )
*
* See note below about accel scaling of engineering sample MPU6k
* variants however
*/
2016-01-12 14:22:11 -04:00
AP_InertialSensor_MPU6000 : : AP_InertialSensor_MPU6000 ( AP_InertialSensor & imu ,
AP_HAL : : OwnPtr < AP_HAL : : Device > dev ,
2016-11-03 06:19:04 -03:00
enum Rotation rotation )
2016-01-12 14:22:11 -04:00
: AP_InertialSensor_Backend ( imu )
2016-11-09 01:16:52 -04:00
, _temp_filter ( 1000 , 1 )
2016-01-12 14:22:11 -04:00
, _dev ( std : : move ( dev ) )
2016-11-03 06:19:04 -03:00
, _rotation ( rotation )
2015-07-02 14:22:36 -03:00
{
}
2015-07-28 13:43:27 -03:00
AP_InertialSensor_MPU6000 : : ~ AP_InertialSensor_MPU6000 ( )
2015-07-02 14:22:36 -03:00
{
2016-11-08 20:33:05 -04:00
if ( _fifo_buffer ! = nullptr ) {
delete [ ] _fifo_buffer ;
}
2015-10-02 15:02:16 -03:00
delete _auxiliary_bus ;
2015-07-28 13:43:27 -03:00
}
2015-07-02 14:22:36 -03:00
2016-01-12 14:22:11 -04:00
AP_InertialSensor_Backend * AP_InertialSensor_MPU6000 : : probe ( AP_InertialSensor & imu ,
2016-11-03 06:19:04 -03:00
AP_HAL : : OwnPtr < AP_HAL : : I2CDevice > dev ,
enum Rotation rotation )
2015-07-28 13:43:27 -03:00
{
2016-11-07 23:01:53 -04:00
if ( ! dev ) {
return nullptr ;
}
2016-01-12 14:22:11 -04:00
AP_InertialSensor_MPU6000 * sensor =
2016-11-08 20:33:05 -04:00
new AP_InertialSensor_MPU6000 ( imu , std : : move ( dev ) , rotation ) ;
2016-01-12 14:22:11 -04:00
if ( ! sensor | | ! sensor - > _init ( ) ) {
delete sensor ;
2015-07-28 13:43:27 -03:00
return nullptr ;
2016-01-12 14:22:11 -04:00
}
sensor - > _id = HAL_INS_MPU60XX_I2C ;
2015-07-02 14:22:36 -03:00
2016-01-12 14:22:11 -04:00
return sensor ;
2015-07-28 13:43:27 -03:00
}
2016-01-12 14:22:11 -04:00
AP_InertialSensor_Backend * AP_InertialSensor_MPU6000 : : probe ( AP_InertialSensor & imu ,
2016-11-03 06:19:04 -03:00
AP_HAL : : OwnPtr < AP_HAL : : SPIDevice > dev ,
enum Rotation rotation )
2015-07-28 13:43:27 -03:00
{
2016-11-07 23:51:27 -04:00
if ( ! dev ) {
return nullptr ;
}
2016-06-13 17:39:12 -03:00
AP_InertialSensor_MPU6000 * sensor ;
dev - > set_read_flag ( 0x80 ) ;
2016-11-08 20:33:05 -04:00
sensor = new AP_InertialSensor_MPU6000 ( imu , std : : move ( dev ) , rotation ) ;
2016-01-12 14:22:11 -04:00
if ( ! sensor | | ! sensor - > _init ( ) ) {
2015-07-02 14:22:36 -03:00
delete sensor ;
2015-07-28 13:43:27 -03:00
return nullptr ;
2015-07-02 14:22:36 -03:00
}
2016-01-12 14:22:11 -04:00
sensor - > _id = HAL_INS_MPU60XX_SPI ;
2015-08-05 13:29:35 -03:00
2015-07-02 14:22:36 -03:00
return sensor ;
}
2016-01-12 14:22:11 -04:00
bool AP_InertialSensor_MPU6000 : : _init ( )
2011-11-12 23:20:25 -04:00
{
2014-07-05 05:19:08 -03:00
# ifdef MPU6000_DRDY_PIN
_drdy_pin = hal . gpio - > channel ( MPU6000_DRDY_PIN ) ;
_drdy_pin - > mode ( HAL_GPIO_INPUT ) ;
# endif
2013-01-10 18:12:19 -04:00
2015-08-05 13:29:35 -03:00
bool success = _hardware_init ( ) ;
2013-01-10 18:12:19 -04:00
2015-08-05 13:29:35 -03:00
# if MPU6000_DEBUG
_dump_registers ( ) ;
# endif
return success ;
}
2016-01-12 14:22:11 -04:00
void AP_InertialSensor_MPU6000 : : _fifo_reset ( )
2015-08-05 13:29:35 -03:00
{
2016-11-09 04:54:53 -04:00
uint8_t user_ctrl = _master_i2c_enable ? BIT_USER_CTRL_I2C_MST_EN : 0 ;
_register_write ( MPUREG_USER_CTRL , user_ctrl ) ;
_register_write ( MPUREG_USER_CTRL , user_ctrl | BIT_USER_CTRL_FIFO_RESET ) ;
_register_write ( MPUREG_USER_CTRL , user_ctrl | BIT_USER_CTRL_FIFO_EN ) ;
2016-01-12 14:22:11 -04:00
}
void AP_InertialSensor_MPU6000 : : _fifo_enable ( )
{
_register_write ( MPUREG_FIFO_EN , BIT_XG_FIFO_EN | BIT_YG_FIFO_EN |
2016-11-10 02:27:22 -04:00
BIT_ZG_FIFO_EN | BIT_ACCEL_FIFO_EN | BIT_TEMP_FIFO_EN ,
true ) ;
2016-01-12 14:22:11 -04:00
_fifo_reset ( ) ;
hal . scheduler - > delay ( 1 ) ;
}
2015-08-05 13:29:35 -03:00
2016-01-12 14:22:11 -04:00
bool AP_InertialSensor_MPU6000 : : _has_auxiliary_bus ( )
{
2016-11-04 06:24:24 -03:00
return _dev - > bus_type ( ) ! = AP_HAL : : Device : : BUS_TYPE_I2C ;
2016-01-12 14:22:11 -04:00
}
void AP_InertialSensor_MPU6000 : : start ( )
{
if ( ! _dev - > get_semaphore ( ) - > take ( 100 ) ) {
2015-11-19 23:11:52 -04:00
AP_HAL : : panic ( " MPU6000: Unable to get semaphore " ) ;
2015-08-05 13:29:35 -03:00
}
// initially run the bus at low speed
2016-01-12 14:22:11 -04:00
_dev - > set_speed ( AP_HAL : : Device : : SPEED_LOW ) ;
2015-08-05 13:29:35 -03:00
// only used for wake-up in accelerometer only low power mode
_register_write ( MPUREG_PWR_MGMT_2 , 0x00 ) ;
hal . scheduler - > delay ( 1 ) ;
2016-11-08 20:33:05 -04:00
// always use FIFO
_fifo_enable ( ) ;
2015-08-05 13:29:35 -03:00
2016-11-08 23:07:31 -04:00
// setup ODR and on-sensor filtering
_set_filter_register ( ) ;
2015-08-05 13:29:35 -03:00
// set sample rate to 1000Hz and apply a software filter
// In this configuration, the gyro sample rate is 8kHz
2016-11-10 02:27:22 -04:00
_register_write ( MPUREG_SMPLRT_DIV , 0 , true ) ;
2015-08-05 13:29:35 -03:00
hal . scheduler - > delay ( 1 ) ;
2016-01-12 14:22:11 -04:00
// Gyro scale 2000º/s
2016-11-10 02:27:22 -04:00
_register_write ( MPUREG_GYRO_CONFIG , BITS_GYRO_FS_2000DPS , true ) ;
2015-08-05 13:29:35 -03:00
hal . scheduler - > delay ( 1 ) ;
// read the product ID rev c has 1/2 the sensitivity of rev d
2016-09-03 12:37:47 -03:00
uint8_t product_id = _register_read ( MPUREG_PRODUCT_ID ) ;
2015-08-05 13:29:35 -03:00
//Serial.printf("Product_ID= 0x%x\n", (unsigned) _mpu6000_product_id);
2016-11-05 06:43:28 -03:00
if ( ! _is_icm_device & &
( ( product_id = = MPU6000ES_REV_C4 ) | |
( product_id = = MPU6000ES_REV_C5 ) | |
( product_id = = MPU6000_REV_C4 ) | |
( product_id = = MPU6000_REV_C5 ) ) ) {
2015-08-05 13:29:35 -03:00
// Accel scale 8g (4096 LSB/g)
// Rev C has different scaling than rev D
2016-11-10 02:27:22 -04:00
_register_write ( MPUREG_ACCEL_CONFIG , 1 < < 3 , true ) ;
2016-08-08 12:38:11 -03:00
_accel_scale = GRAVITY_MSS / 4096.f ;
2015-08-05 13:29:35 -03:00
} else {
2016-08-08 12:38:11 -03:00
// Accel scale 16g (2048 LSB/g)
2016-11-10 02:27:22 -04:00
_register_write ( MPUREG_ACCEL_CONFIG , 3 < < 3 , true ) ;
2016-08-08 12:38:11 -03:00
_accel_scale = GRAVITY_MSS / 2048.f ;
2015-08-05 13:29:35 -03:00
}
hal . scheduler - > delay ( 1 ) ;
2016-11-05 06:43:28 -03:00
if ( _is_icm_device ) {
// this avoids a sensor bug, see description above
2016-11-10 02:27:22 -04:00
_register_write ( MPUREG_ICM_UNDOC1 , MPUREG_ICM_UNDOC1_VALUE , true ) ;
2016-11-05 06:43:28 -03:00
}
2015-08-05 13:29:35 -03:00
// configure interrupt to fire when new data arrives
_register_write ( MPUREG_INT_ENABLE , BIT_RAW_RDY_EN ) ;
hal . scheduler - > delay ( 1 ) ;
// clear interrupt on any read, and hold the data ready pin high
// until we clear the interrupt
_register_write ( MPUREG_INT_PIN_CFG , BIT_INT_RD_CLEAR | BIT_LATCH_INT_EN ) ;
2016-01-12 14:22:11 -04:00
// now that we have initialised, we set the bus speed to high
_dev - > set_speed ( AP_HAL : : Device : : SPEED_HIGH ) ;
2015-08-05 13:29:35 -03:00
2016-01-12 14:22:11 -04:00
_dev - > get_semaphore ( ) - > give ( ) ;
2013-01-10 18:12:19 -04:00
2014-10-14 01:48:33 -03:00
// grab the used instances
2016-11-04 21:11:00 -03:00
_gyro_instance = _imu . register_gyro ( 1000 , _dev - > get_bus_id_devtype ( DEVTYPE_GYR_MPU6000 ) ) ;
_accel_instance = _imu . register_accel ( 1000 , _dev - > get_bus_id_devtype ( DEVTYPE_ACC_MPU6000 ) ) ;
2015-08-28 16:21:39 -03:00
2016-11-03 06:19:04 -03:00
// setup sensor rotations from probe()
set_gyro_orientation ( _gyro_instance , _rotation ) ;
set_accel_orientation ( _accel_instance , _rotation ) ;
2016-11-08 20:33:05 -04:00
// allocate fifo buffer
_fifo_buffer = new uint8_t [ MAX_DATA_READ ] ;
if ( _fifo_buffer = = nullptr ) {
AP_HAL : : panic ( " MPU6000: Unable to allocate FIFO buffer " ) ;
}
2016-11-03 06:19:04 -03:00
2015-08-05 13:29:35 -03:00
// start the timer process to read samples
2016-11-07 23:51:27 -04:00
_dev - > register_periodic_callback ( 1000 , FUNCTOR_BIND_MEMBER ( & AP_InertialSensor_MPU6000 : : _poll_data , bool ) ) ;
2013-01-03 14:22:55 -04:00
}
2015-08-05 13:29:35 -03:00
2014-10-14 01:48:33 -03:00
/*
2016-01-12 14:22:11 -04:00
process any
2014-10-14 01:48:33 -03:00
*/
2016-01-12 14:22:11 -04:00
bool AP_InertialSensor_MPU6000 : : update ( )
{
2015-11-15 20:05:20 -04:00
update_accel ( _accel_instance ) ;
update_gyro ( _gyro_instance ) ;
2014-11-17 13:09:38 -04:00
2015-11-15 20:05:20 -04:00
_publish_temperature ( _accel_instance , _temp_filtered ) ;
2016-01-12 14:22:11 -04:00
2013-02-06 19:23:08 -04:00
return true ;
2012-08-17 03:19:56 -03:00
}
2011-11-12 23:20:25 -04:00
2015-10-02 15:02:16 -03:00
AuxiliaryBus * AP_InertialSensor_MPU6000 : : get_auxiliary_bus ( )
2015-08-16 16:23:24 -03:00
{
2016-01-12 14:22:11 -04:00
if ( _auxiliary_bus ) {
2015-10-02 15:02:16 -03:00
return _auxiliary_bus ;
2016-01-12 14:22:11 -04:00
}
2015-08-16 16:23:24 -03:00
2016-01-12 14:22:11 -04:00
if ( _has_auxiliary_bus ( ) ) {
2016-11-04 06:24:24 -03:00
_auxiliary_bus = new AP_MPU6000_AuxiliaryBus ( * this , _dev - > get_bus_id ( ) ) ;
2016-01-12 14:22:11 -04:00
}
2015-08-16 16:23:24 -03:00
2015-10-02 15:02:16 -03:00
return _auxiliary_bus ;
2015-08-16 16:23:24 -03:00
}
2016-01-12 14:22:11 -04:00
/*
2013-01-03 14:22:55 -04:00
* Return true if the MPU6000 has new data available for reading .
*
* We use the data ready pin if it is available . Otherwise , read the
* status register .
*/
2013-01-03 15:48:01 -04:00
bool AP_InertialSensor_MPU6000 : : _data_ready ( )
2013-01-03 14:22:55 -04:00
{
if ( _drdy_pin ) {
return _drdy_pin - > read ( ) ! = 0 ;
2013-01-09 05:30:20 -04:00
}
2013-10-28 04:20:47 -03:00
uint8_t status = _register_read ( MPUREG_INT_STATUS ) ;
return ( status & BIT_RAW_RDY_INT ) ! = 0 ;
2013-01-03 14:22:55 -04:00
}
2016-01-12 14:22:11 -04:00
/*
2016-11-01 19:11:55 -03:00
* Timer process to poll for new data from the MPU6000 . Called from bus thread with semaphore held
2013-01-03 14:22:55 -04:00
*/
2016-11-01 19:11:55 -03:00
bool AP_InertialSensor_MPU6000 : : _poll_data ( )
2013-01-03 14:22:55 -04:00
{
2016-11-08 20:33:05 -04:00
_read_fifo ( ) ;
2016-11-01 19:11:55 -03:00
return true ;
2013-01-03 14:22:55 -04:00
}
2015-07-02 14:22:36 -03:00
void AP_InertialSensor_MPU6000 : : _accumulate ( uint8_t * samples , uint8_t n_samples )
{
2016-01-12 14:22:11 -04:00
for ( uint8_t i = 0 ; i < n_samples ; i + + ) {
2015-07-02 14:22:36 -03:00
uint8_t * data = samples + MPU6000_SAMPLE_SIZE * i ;
2015-08-28 10:36:05 -03:00
Vector3f accel , gyro ;
2016-08-31 01:56:27 -03:00
bool fsync_set = false ;
2015-08-28 10:36:05 -03:00
2016-08-31 01:56:27 -03:00
# if MPU6000_EXT_SYNC_ENABLE
fsync_set = ( int16_val ( data , 2 ) & 1U ) ! = 0 ;
# endif
2015-08-28 10:36:05 -03:00
accel = Vector3f ( int16_val ( data , 1 ) ,
int16_val ( data , 0 ) ,
- int16_val ( data , 2 ) ) ;
2016-08-08 12:38:11 -03:00
accel * = _accel_scale ;
2015-08-28 10:36:05 -03:00
2016-11-09 01:16:52 -04:00
float temp = int16_val ( data , 3 ) ;
temp = temp / 340 + 36.53 ;
_last_temp = temp ;
gyro = Vector3f ( int16_val ( data , 5 ) ,
int16_val ( data , 4 ) ,
- int16_val ( data , 6 ) ) ;
2016-01-12 14:22:11 -04:00
gyro * = GYRO_SCALE ;
2014-10-16 19:24:08 -03:00
2015-08-28 10:36:05 -03:00
_rotate_and_correct_accel ( _accel_instance , accel ) ;
_rotate_and_correct_gyro ( _gyro_instance , gyro ) ;
2016-08-31 01:56:27 -03:00
_notify_new_accel_raw_sample ( _accel_instance , accel , AP_HAL : : micros64 ( ) , fsync_set ) ;
2015-09-08 14:05:37 -03:00
_notify_new_gyro_raw_sample ( _gyro_instance , gyro ) ;
2016-11-09 01:16:52 -04:00
_temp_filtered = _temp_filter . apply ( temp ) ;
2016-11-08 20:33:05 -04:00
}
}
2015-08-27 16:05:13 -03:00
2016-11-08 20:33:05 -04:00
void AP_InertialSensor_MPU6000 : : _accumulate_fast_sampling ( uint8_t * samples , uint8_t n_samples )
{
2016-11-08 21:11:17 -04:00
Vector3l asum , gsum ;
2016-11-09 06:54:27 -04:00
float tsum = 0 ;
2016-11-09 22:39:17 -04:00
const int32_t clip_limit = AP_INERTIAL_SENSOR_ACCEL_CLIP_THRESH_MSS / _accel_scale ;
bool clipped = false ;
2016-11-08 20:33:05 -04:00
for ( uint8_t i = 0 ; i < n_samples ; i + + ) {
uint8_t * data = samples + MPU6000_SAMPLE_SIZE * i ;
2016-11-09 22:39:17 -04:00
Vector3l a ( int16_val ( data , 1 ) ,
int16_val ( data , 0 ) ,
- int16_val ( data , 2 ) ) ;
if ( abs ( a . x ) > clip_limit | |
abs ( a . y ) > clip_limit | |
abs ( a . z ) > clip_limit ) {
clipped = true ;
}
asum + = a ;
2016-11-09 01:16:52 -04:00
gsum + = Vector3l ( int16_val ( data , 5 ) ,
int16_val ( data , 4 ) ,
- int16_val ( data , 6 ) ) ;
float temp = int16_val ( data , 3 ) ;
temp = temp / 340 + 36.53 ;
2016-11-09 06:54:27 -04:00
tsum + = temp ;
2016-11-09 01:16:52 -04:00
_last_temp = temp ;
2015-07-02 14:22:36 -03:00
}
2016-11-08 20:33:05 -04:00
2016-11-09 22:39:17 -04:00
if ( clipped ) {
increment_clip_count ( _accel_instance ) ;
}
2016-11-08 21:11:17 -04:00
float ascale = _accel_scale / n_samples ;
Vector3f accel ( asum . x * ascale , asum . y * ascale , asum . z * ascale ) ;
float gscale = GYRO_SCALE / n_samples ;
Vector3f gyro ( gsum . x * gscale , gsum . y * gscale , gsum . z * gscale ) ;
2016-11-08 20:33:05 -04:00
_rotate_and_correct_accel ( _accel_instance , accel ) ;
_rotate_and_correct_gyro ( _gyro_instance , gyro ) ;
_notify_new_accel_raw_sample ( _accel_instance , accel , AP_HAL : : micros64 ( ) , false ) ;
_notify_new_gyro_raw_sample ( _gyro_instance , gyro ) ;
2016-11-09 06:54:27 -04:00
_temp_filtered = _temp_filter . apply ( tsum / n_samples ) ;
2015-07-02 14:22:36 -03:00
}
2016-11-09 04:53:55 -04:00
/*
* check the FIFO integrity by cross - checking the temperature against
* the last FIFO reading
*/
void AP_InertialSensor_MPU6000 : : _check_temperature ( void )
{
uint8_t rx [ 2 ] ;
if ( ! _block_read ( MPUREG_TEMP_OUT_H , rx , 2 ) ) {
return ;
}
float temp = int16_val ( rx , 0 ) / 340 + 36.53 ;
if ( fabsf ( _last_temp - temp ) > 2 & & ! is_zero ( _last_temp ) ) {
// a 2 degree change in one sample is a highly likely
// sign of a FIFO alignment error
printf ( " FIFO temperature reset: %.2f %.2f \n " ,
( double ) temp , ( double ) _last_temp ) ;
_last_temp = temp ;
_fifo_reset ( ) ;
}
}
2016-01-12 14:22:11 -04:00
void AP_InertialSensor_MPU6000 : : _read_fifo ( )
2015-07-02 14:22:36 -03:00
{
uint8_t n_samples ;
2016-01-12 14:22:11 -04:00
uint16_t bytes_read ;
2016-11-08 20:33:05 -04:00
uint8_t * rx = _fifo_buffer ;
2016-01-12 14:22:11 -04:00
if ( ! _block_read ( MPUREG_FIFO_COUNTH , rx , 2 ) ) {
hal . console - > printf ( " MPU60x0: error in fifo read \n " ) ;
2016-11-10 02:27:22 -04:00
goto check_registers ;
2016-01-12 14:22:11 -04:00
}
bytes_read = uint16_val ( rx , 0 ) ;
n_samples = bytes_read / MPU6000_SAMPLE_SIZE ;
if ( n_samples = = 0 ) {
/* Not enough data in FIFO */
2016-11-10 02:27:22 -04:00
goto check_registers ;
2016-01-12 14:22:11 -04:00
}
2015-07-02 14:22:36 -03:00
2016-01-12 14:22:11 -04:00
if ( n_samples > MPU6000_MAX_FIFO_SAMPLES ) {
2016-11-08 20:33:05 -04:00
printf ( " bytes_read = %u, n_samples %u > %u, dropping samples \n " ,
bytes_read , n_samples , MPU6000_MAX_FIFO_SAMPLES ) ;
2016-01-12 14:22:11 -04:00
/* Too many samples, do a FIFO RESET */
_fifo_reset ( ) ;
2016-11-10 02:27:22 -04:00
goto check_registers ;
2016-01-12 14:22:11 -04:00
}
if ( ! _block_read ( MPUREG_FIFO_R_W , rx , n_samples * MPU6000_SAMPLE_SIZE ) ) {
2016-11-08 20:33:05 -04:00
printf ( " MPU60x0: error in fifo read %u bytes \n " ,
n_samples * MPU6000_SAMPLE_SIZE ) ;
2016-11-10 02:27:22 -04:00
goto check_registers ;
2016-01-12 14:22:11 -04:00
}
2016-11-08 20:33:05 -04:00
if ( _fast_sampling ) {
_accumulate_fast_sampling ( rx , n_samples ) ;
} else {
_accumulate ( rx , n_samples ) ;
}
2016-11-09 04:53:55 -04:00
if ( _temp_counter + + = = 255 ) {
// check FIFO integrity every 0.25s
_check_temperature ( ) ;
}
2016-11-10 02:27:22 -04:00
check_registers :
if ( _reg_check_counter + + = = 10 ) {
_reg_check_counter = 0 ;
// check next register value for correctness
if ( ! _dev - > check_next_register ( ) ) {
_inc_gyro_error_count ( _gyro_instance ) ;
_inc_accel_error_count ( _accel_instance ) ;
}
}
2013-01-03 15:48:01 -04:00
}
2012-10-11 21:27:19 -03:00
2016-01-12 14:22:11 -04:00
bool AP_InertialSensor_MPU6000 : : _block_read ( uint8_t reg , uint8_t * buf ,
2015-08-16 16:06:41 -03:00
uint32_t size )
{
2016-01-12 14:22:11 -04:00
return _dev - > read_registers ( reg , buf , size ) ;
2015-08-16 16:06:41 -03:00
}
2016-01-12 14:22:11 -04:00
uint8_t AP_InertialSensor_MPU6000 : : _register_read ( uint8_t reg )
2011-11-12 23:20:25 -04:00
{
2016-01-12 14:22:11 -04:00
uint8_t val = 0 ;
_dev - > read_registers ( reg , & val , 1 ) ;
2015-06-05 04:47:31 -03:00
return val ;
2011-11-12 23:20:25 -04:00
}
2016-11-10 02:27:22 -04:00
void AP_InertialSensor_MPU6000 : : _register_write ( uint8_t reg , uint8_t val , bool checked )
2011-11-12 23:20:25 -04:00
{
2016-11-10 02:27:22 -04:00
_dev - > write_register ( reg , val , checked ) ;
2011-11-12 23:20:25 -04:00
}
2013-02-06 19:23:08 -04:00
/*
set the DLPF filter frequency . Assumes caller has taken semaphore
*/
2016-11-08 23:07:31 -04:00
void AP_InertialSensor_MPU6000 : : _set_filter_register ( void )
2013-02-06 19:23:08 -04:00
{
2016-11-08 23:07:31 -04:00
uint8_t config ;
2016-08-31 01:56:27 -03:00
# if MPU6000_EXT_SYNC_ENABLE
// add in EXT_SYNC bit if enabled
2016-11-08 23:07:31 -04:00
config = ( MPUREG_CONFIG_EXT_SYNC_AZ < < MPUREG_CONFIG_EXT_SYNC_SHIFT ) ;
# else
config = 0 ;
2016-08-31 01:56:27 -03:00
# endif
2016-11-08 23:07:31 -04:00
if ( _is_icm_device & & _dev - > bus_type ( ) = = AP_HAL : : Device : : BUS_TYPE_SPI ) {
// this gives us 8kHz sampling on gyros and 4kHz on accels
config | = BITS_DLPF_CFG_256HZ_NOLPF2 ;
_fast_sampling = true ;
} else {
// limit to 1kHz if not on SPI
config | = BITS_DLPF_CFG_188HZ ;
2016-11-08 20:33:05 -04:00
}
2016-11-10 02:27:22 -04:00
_register_write ( MPUREG_CONFIG , config , true ) ;
2016-11-08 20:33:05 -04:00
2016-11-08 23:07:31 -04:00
if ( _is_icm_device ) {
if ( _fast_sampling ) {
// setup for 4kHz accels
2016-11-10 02:27:22 -04:00
_register_write ( ICMREG_ACCEL_CONFIG2 , ICM_ACC_FCHOICE_B , true ) ;
2016-11-08 23:07:31 -04:00
} else {
2016-11-10 02:27:22 -04:00
_register_write ( ICMREG_ACCEL_CONFIG2 , ICM_ACC_DLPF_CFG_218HZ , true ) ;
2016-11-08 23:07:31 -04:00
}
2016-11-08 20:33:05 -04:00
}
2013-02-06 19:23:08 -04:00
}
2016-11-05 06:43:28 -03:00
/*
check whoami for MPU6000 or ICM - 20608
*/
bool AP_InertialSensor_MPU6000 : : _check_whoami ( void )
{
uint8_t whoami = _register_read ( MPUREG_WHOAMI ) ;
switch ( whoami ) {
case MPU_WHOAMI_6000 :
_is_icm_device = false ;
return true ;
case ICM_WHOAMI_20608 :
_is_icm_device = true ;
return true ;
}
// not a value WHOAMI result
return false ;
}
2013-02-06 19:23:08 -04:00
2014-10-16 17:52:21 -03:00
bool AP_InertialSensor_MPU6000 : : _hardware_init ( void )
2011-11-12 23:20:25 -04:00
{
2016-01-12 14:22:11 -04:00
if ( ! _dev - > get_semaphore ( ) - > take ( 100 ) ) {
2015-11-19 23:11:52 -04:00
AP_HAL : : panic ( " MPU6000: Unable to get semaphore " ) ;
2013-01-09 05:30:20 -04:00
}
2016-11-10 02:27:22 -04:00
// setup for register checking
_dev - > setup_checked_registers ( 7 ) ;
2015-11-03 09:46:29 -04:00
// initially run the bus at low speed
2016-01-12 14:22:11 -04:00
_dev - > set_speed ( AP_HAL : : Device : : SPEED_LOW ) ;
2013-10-11 04:02:17 -03:00
2016-11-05 06:43:28 -03:00
if ( ! _check_whoami ( ) ) {
_dev - > get_semaphore ( ) - > give ( ) ;
return false ;
}
2011-11-12 23:20:25 -04:00
// Chip reset
2012-12-27 06:28:41 -04:00
uint8_t tries ;
2016-01-12 14:22:11 -04:00
for ( tries = 0 ; tries < 5 ; tries + + ) {
2015-08-16 16:23:24 -03:00
uint8_t user_ctrl = _register_read ( MPUREG_USER_CTRL ) ;
/* First disable the master I2C to avoid hanging the slaves on the
* aulixiliar I2C bus - it will be enabled again if the AuxiliaryBus
* is used */
if ( user_ctrl & BIT_USER_CTRL_I2C_MST_EN ) {
_register_write ( MPUREG_USER_CTRL , user_ctrl & ~ BIT_USER_CTRL_I2C_MST_EN ) ;
hal . scheduler - > delay ( 10 ) ;
}
2015-08-05 13:29:35 -03:00
/* reset device */
2013-10-29 03:42:35 -03:00
_register_write ( MPUREG_PWR_MGMT_1 , BIT_PWR_MGMT_1_DEVICE_RESET ) ;
2012-12-27 06:28:41 -04:00
hal . scheduler - > delay ( 100 ) ;
2016-01-12 14:22:11 -04:00
/* bus-dependent initialization */
2016-11-04 06:24:24 -03:00
if ( _dev - > bus_type ( ) = = AP_HAL : : Device : : BUS_TYPE_SPI ) {
2016-01-12 14:22:11 -04:00
/* Disable I2C bus if SPI selected (Recommended in Datasheet to be
* done just after the device is reset ) */
_register_write ( MPUREG_USER_CTRL , BIT_USER_CTRL_I2C_IF_DIS ) ;
}
2015-08-05 13:29:35 -03:00
2012-12-27 06:28:41 -04:00
// Wake up device and select GyroZ clock. Note that the
// MPU6000 starts up in sleep mode, and it can take some time
// for it to come out of sleep
2013-10-29 03:42:35 -03:00
_register_write ( MPUREG_PWR_MGMT_1 , BIT_PWR_MGMT_1_CLK_ZGYRO ) ;
2012-12-27 06:28:41 -04:00
hal . scheduler - > delay ( 5 ) ;
// check it has woken up
2016-01-12 14:22:11 -04:00
if ( _register_read ( MPUREG_PWR_MGMT_1 ) = = BIT_PWR_MGMT_1_CLK_ZGYRO ) {
2012-12-27 06:28:41 -04:00
break ;
2016-01-12 14:22:11 -04:00
}
2015-07-02 14:22:36 -03:00
2015-08-05 13:29:35 -03:00
hal . scheduler - > delay ( 10 ) ;
2016-01-12 14:22:11 -04:00
if ( _data_ready ( ) ) {
2015-08-05 13:29:35 -03:00
break ;
2016-01-12 14:22:11 -04:00
}
2015-08-05 13:29:35 -03:00
2013-02-13 04:33:40 -04:00
# if MPU6000_DEBUG
_dump_registers ( ) ;
# endif
2012-12-27 06:28:41 -04:00
}
2016-01-12 14:22:11 -04:00
_dev - > set_speed ( AP_HAL : : Device : : SPEED_HIGH ) ;
_dev - > get_semaphore ( ) - > give ( ) ;
2012-12-27 06:28:41 -04:00
if ( tries = = 5 ) {
2015-10-25 16:50:51 -03:00
hal . console - > println ( " Failed to boot MPU6000 5 times " ) ;
2016-01-12 14:22:11 -04:00
return false ;
2012-12-27 06:28:41 -04:00
}
2012-07-28 02:14:43 -03:00
2016-11-05 06:43:28 -03:00
if ( _is_icm_device ) {
// this avoids a sensor bug, see description above
2016-11-10 02:27:22 -04:00
_register_write ( MPUREG_ICM_UNDOC1 , MPUREG_ICM_UNDOC1_VALUE , true ) ;
2016-11-05 06:43:28 -03:00
}
2013-01-10 18:12:19 -04:00
return true ;
2011-11-12 23:20:25 -04:00
}
2012-12-27 06:28:41 -04:00
# if MPU6000_DEBUG
// dump all config registers - used for debug
void AP_InertialSensor_MPU6000 : : _dump_registers ( void )
{
2015-10-25 16:50:51 -03:00
hal . console - > println ( " MPU6000 registers " ) ;
2016-01-12 14:22:11 -04:00
if ( ! _dev - > get_semaphore ( ) - > take ( 100 ) ) {
return ;
}
for ( uint8_t reg = MPUREG_PRODUCT_ID ; reg < = 108 ; reg + + ) {
uint8_t v = _register_read ( reg ) ;
hal . console - > printf ( " %02x:%02x " , ( unsigned ) reg , ( unsigned ) v ) ;
if ( ( reg - ( MPUREG_PRODUCT_ID - 1 ) ) % 16 = = 0 ) {
hal . console - > println ( ) ;
2012-12-27 06:28:41 -04:00
}
}
2016-01-12 14:22:11 -04:00
hal . console - > println ( ) ;
_dev - > get_semaphore ( ) - > give ( ) ;
2012-12-27 06:28:41 -04:00
}
# endif
2015-08-16 16:23:24 -03:00
AP_MPU6000_AuxiliaryBusSlave : : AP_MPU6000_AuxiliaryBusSlave ( AuxiliaryBus & bus , uint8_t addr ,
uint8_t instance )
: AuxiliaryBusSlave ( bus , addr , instance )
, _mpu6000_addr ( MPUREG_I2C_SLV0_ADDR + _instance * 3 )
, _mpu6000_reg ( _mpu6000_addr + 1 )
, _mpu6000_ctrl ( _mpu6000_addr + 2 )
, _mpu6000_do ( MPUREG_I2C_SLV0_DO + _instance )
{
}
int AP_MPU6000_AuxiliaryBusSlave : : _set_passthrough ( uint8_t reg , uint8_t size ,
uint8_t * out )
{
2015-10-01 19:49:21 -03:00
auto & backend = AP_InertialSensor_MPU6000 : : from ( _bus . get_backend ( ) ) ;
2015-08-16 16:23:24 -03:00
uint8_t addr ;
/* Ensure the slave read/write is disabled before changing the registers */
backend . _register_write ( _mpu6000_ctrl , 0 ) ;
if ( out ) {
backend . _register_write ( _mpu6000_do , * out ) ;
addr = _addr ;
} else {
addr = _addr | BIT_READ_FLAG ;
}
backend . _register_write ( _mpu6000_addr , addr ) ;
backend . _register_write ( _mpu6000_reg , reg ) ;
backend . _register_write ( _mpu6000_ctrl , BIT_I2C_SLVX_EN | size ) ;
return 0 ;
}
int AP_MPU6000_AuxiliaryBusSlave : : passthrough_read ( uint8_t reg , uint8_t * buf ,
2016-01-20 18:12:15 -04:00
uint8_t size )
2015-08-16 16:23:24 -03:00
{
assert ( buf ) ;
if ( _registered ) {
2015-10-25 16:50:51 -03:00
hal . console - > println ( " Error: can't passthrough when slave is already configured " ) ;
2015-08-16 16:23:24 -03:00
return - 1 ;
}
int r = _set_passthrough ( reg , size ) ;
2016-01-20 18:12:15 -04:00
if ( r < 0 ) {
2015-08-16 16:23:24 -03:00
return r ;
2016-01-20 18:12:15 -04:00
}
2015-08-16 16:23:24 -03:00
/* wait the value to be read from the slave and read it back */
hal . scheduler - > delay ( 10 ) ;
2015-10-01 19:49:21 -03:00
auto & backend = AP_InertialSensor_MPU6000 : : from ( _bus . get_backend ( ) ) ;
2016-08-02 17:52:44 -03:00
if ( ! backend . _block_read ( MPUREG_EXT_SENS_DATA_00 + _ext_sens_data , buf , size ) ) {
return - 1 ;
}
2015-08-16 16:23:24 -03:00
/* disable new reads */
backend . _register_write ( _mpu6000_ctrl , 0 ) ;
return size ;
}
int AP_MPU6000_AuxiliaryBusSlave : : passthrough_write ( uint8_t reg , uint8_t val )
{
if ( _registered ) {
2015-10-25 16:50:51 -03:00
hal . console - > println ( " Error: can't passthrough when slave is already configured " ) ;
2015-08-16 16:23:24 -03:00
return - 1 ;
}
int r = _set_passthrough ( reg , 1 , & val ) ;
2016-01-20 18:12:15 -04:00
if ( r < 0 ) {
2015-08-16 16:23:24 -03:00
return r ;
2016-01-20 18:12:15 -04:00
}
2015-08-16 16:23:24 -03:00
/* wait the value to be written to the slave */
hal . scheduler - > delay ( 10 ) ;
2015-10-01 19:49:21 -03:00
auto & backend = AP_InertialSensor_MPU6000 : : from ( _bus . get_backend ( ) ) ;
2015-08-16 16:23:24 -03:00
/* disable new writes */
backend . _register_write ( _mpu6000_ctrl , 0 ) ;
2016-03-03 17:22:26 -04:00
return 1 ;
2015-08-16 16:23:24 -03:00
}
int AP_MPU6000_AuxiliaryBusSlave : : read ( uint8_t * buf )
{
if ( ! _registered ) {
2015-10-25 16:50:51 -03:00
hal . console - > println ( " Error: can't read before configuring slave " ) ;
2015-08-16 16:23:24 -03:00
return - 1 ;
}
2015-10-01 19:49:21 -03:00
auto & backend = AP_InertialSensor_MPU6000 : : from ( _bus . get_backend ( ) ) ;
2016-03-03 17:22:26 -04:00
if ( ! backend . _block_read ( MPUREG_EXT_SENS_DATA_00 + _ext_sens_data , buf , _sample_size ) ) {
return - 1 ;
}
2015-08-16 16:23:24 -03:00
2016-03-03 17:22:26 -04:00
return _sample_size ;
2015-08-16 16:23:24 -03:00
}
/* MPU6000 provides up to 5 slave devices, but the 5th is way too different to
* configure and is seldom used */
2016-11-04 06:24:24 -03:00
AP_MPU6000_AuxiliaryBus : : AP_MPU6000_AuxiliaryBus ( AP_InertialSensor_MPU6000 & backend , uint32_t devid )
: AuxiliaryBus ( backend , 4 , devid )
2015-08-16 16:23:24 -03:00
{
}
AP_HAL : : Semaphore * AP_MPU6000_AuxiliaryBus : : get_semaphore ( )
{
2016-01-12 14:22:11 -04:00
return static_cast < AP_InertialSensor_MPU6000 & > ( _ins_backend ) . _dev - > get_semaphore ( ) ;
2015-08-16 16:23:24 -03:00
}
AuxiliaryBusSlave * AP_MPU6000_AuxiliaryBus : : _instantiate_slave ( uint8_t addr , uint8_t instance )
{
/* Enable slaves on MPU6000 if this is the first time */
2016-01-20 18:12:15 -04:00
if ( _ext_sens_data = = 0 ) {
2015-08-16 16:23:24 -03:00
_configure_slaves ( ) ;
2016-01-20 18:12:15 -04:00
}
2015-08-16 16:23:24 -03:00
return new AP_MPU6000_AuxiliaryBusSlave ( * this , addr , instance ) ;
}
void AP_MPU6000_AuxiliaryBus : : _configure_slaves ( )
{
2015-10-01 19:49:21 -03:00
auto & backend = AP_InertialSensor_MPU6000 : : from ( _ins_backend ) ;
2015-08-16 16:23:24 -03:00
2016-01-20 18:12:15 -04:00
/* Enable the I2C master to slaves on the auxiliary I2C bus*/
uint8_t user_ctrl = backend . _register_read ( MPUREG_USER_CTRL ) ;
2015-08-16 16:23:24 -03:00
backend . _register_write ( MPUREG_USER_CTRL , user_ctrl | BIT_USER_CTRL_I2C_MST_EN ) ;
2016-11-09 04:54:53 -04:00
backend . _master_i2c_enable = true ;
2015-08-16 16:23:24 -03:00
/* stop condition between reads; clock at 400kHz */
backend . _register_write ( MPUREG_I2C_MST_CTRL ,
BIT_I2C_MST_P_NSR | BIT_I2C_MST_CLK_400KHZ ) ;
/* Hard-code divider for internal sample rate, 1 kHz, resulting in a
* sample rate of 100 Hz */
backend . _register_write ( MPUREG_I2C_SLV4_CTRL , 9 ) ;
/* All slaves are subject to the sample rate */
backend . _register_write ( MPUREG_I2C_MST_DELAY_CTRL ,
BIT_I2C_SLV0_DLY_EN | BIT_I2C_SLV1_DLY_EN |
BIT_I2C_SLV2_DLY_EN | BIT_I2C_SLV3_DLY_EN ) ;
}
int AP_MPU6000_AuxiliaryBus : : _configure_periodic_read ( AuxiliaryBusSlave * slave ,
uint8_t reg , uint8_t size )
{
2016-01-20 18:12:15 -04:00
if ( _ext_sens_data + size > MAX_EXT_SENS_DATA ) {
2015-08-16 16:23:24 -03:00
return - 1 ;
2016-01-20 18:12:15 -04:00
}
2015-08-16 16:23:24 -03:00
AP_MPU6000_AuxiliaryBusSlave * mpu_slave =
static_cast < AP_MPU6000_AuxiliaryBusSlave * > ( slave ) ;
mpu_slave - > _set_passthrough ( reg , size ) ;
mpu_slave - > _ext_sens_data = _ext_sens_data ;
_ext_sens_data + = size ;
return 0 ;
}