2014-05-01 17:41:22 -03:00
/// -*- tab-width: 4; Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*-
/*
This program 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 .
This program 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 this program . If not , see < http : //www.gnu.org/licenses/>.
2014-08-12 07:00:31 -03:00
- - Coded by Victor Mayoral Vilches - -
2014-05-01 17:41:22 -03:00
*/
# include <AP_HAL.h>
2014-07-06 23:03:57 -03:00
# if CONFIG_HAL_BOARD == HAL_BOARD_LINUX
2014-05-01 17:41:22 -03:00
# include "AP_InertialSensor_MPU9250.h"
2014-06-03 10:12:46 -03:00
# include "../AP_HAL_Linux/GPIO.h"
2014-05-01 17:41:22 -03:00
extern const AP_HAL : : HAL & hal ;
2015-04-28 02:29:41 -03:00
// MPU9250 accelerometer scaling for 16g range
# define MPU9250_ACCEL_SCALE_1G (GRAVITY_MSS / 2048.0f)
2014-05-01 17:41:22 -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
// MPU9250 registers
# define MPUREG_XA_OFFS_H 0x77 // X axis accelerometer offset (high byte)
# define MPUREG_XA_OFFS_L 0x78 // X axis accelerometer offset (low byte)
# define MPUREG_YA_OFFS_H 0x7A // Y axis accelerometer offset (high byte)
# define MPUREG_YA_OFFS_L 0x0B // Y axis accelerometer offset (low byte)
# define MPUREG_ZA_OFFS_H 0x0D // Z axis accelerometer offset (high byte)
# define MPUREG_ZA_OFFS_L 0x0E // Z axis accelerometer offset (low byte)
// MPU6000 & MPU9250 registers
// not sure if present in MPU9250
2014-08-12 07:00:31 -03:00
// #define MPUREG_PRODUCT_ID 0x0C // Product ID Register
2014-05-01 17:41:22 -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)
# 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
# define MPUREG_GYRO_CONFIG 0x1B
// bit definitions for MPUREG_GYRO_CONFIG
# 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 MPUREG_INT_PIN_CFG 0x37
# define BIT_INT_RD_CLEAR 0x10 // clear the interrupt when any read occurs
2014-08-12 07:00:31 -03:00
# define BIT_LATCH_INT_EN 0x20 // latch data ready pin
2014-05-01 17:41:22 -03:00
# define MPUREG_INT_ENABLE 0x38
// bit definitions for MPUREG_INT_ENABLE
# 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
// bit definitions for MPUREG_INT_STATUS (same bit pattern as above because this register shows what interrupt actually fired)
# 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
# define MPUREG_USER_CTRL 0x6A
// bit definitions for MPUREG_USER_CTRL
# 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
2015-03-12 23:26:49 -03:00
# define MPUREG_WHOAMI_MPU9250 0x71
# define MPUREG_WHOAMI_MPU9255 0x73
2014-05-01 17:41:22 -03:00
// Configuration bits MPU 3000, MPU 6000 and MPU9250
# define BITS_DLPF_CFG_256HZ_NOLPF2 0x00
# 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
# define BITS_DLPF_CFG_2100HZ_NOLPF 0x07
# define BITS_DLPF_CFG_MASK 0x07
/*
* PS - MPU - 9250 A - 00. pdf , page 8 , lists LSB sensitivity of
* gyro as 16.4 LSB / DPS at scale factor of + / - 2000 dps ( FS_SEL = = 3 )
*/
2014-08-19 06:39:21 -03:00
# define GYRO_SCALE (0.0174532f / 16.4f)
2014-05-01 17:41:22 -03:00
/*
* PS - MPU - 9250 A - 00. pdf , page 9 , lists LSB sensitivity of
* accel as 4096 LSB / mg at scale factor of + / - 8 g ( AFS_SEL = = 2 )
*
* See note below about accel scaling of engineering sample MPUXk
* variants however
*/
2014-10-15 20:31:06 -03:00
AP_InertialSensor_MPU9250 : : AP_InertialSensor_MPU9250 ( AP_InertialSensor & imu ) :
AP_InertialSensor_Backend ( imu ) ,
2015-03-11 21:58:36 -03:00
_last_accel_filter_hz ( - 1 ) ,
_last_gyro_filter_hz ( - 1 ) ,
2014-08-20 18:48:44 -03:00
_shared_data_idx ( 0 ) ,
2015-02-17 18:48:26 -04:00
_accel_filter ( 1000 , 15 ) ,
_gyro_filter ( 1000 , 15 ) ,
2015-06-08 17:06:17 -03:00
_have_sample_available ( false ) ,
# if CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_PXF
_default_rotation ( ROTATION_ROLL_180_YAW_270 )
# elif CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_NAVIO
/* no rotation needed */
_default_rotation ( ROTATION_NONE )
# elif CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_BBBMINI
2015-07-05 17:15:33 -03:00
_default_rotation ( ROTATION_NONE )
2015-06-08 17:06:17 -03:00
# else /* rotate for bbone default (and other boards) */
_default_rotation ( ROTATION_ROLL_180_YAW_90 )
# endif
2014-05-01 17:41:22 -03:00
{
}
2014-10-15 20:31:06 -03:00
2014-08-19 06:39:21 -03:00
/*
2014-10-15 20:31:06 -03:00
detect the sensor
2014-08-19 06:39:21 -03:00
*/
2014-10-16 17:52:21 -03:00
AP_InertialSensor_Backend * AP_InertialSensor_MPU9250 : : detect ( AP_InertialSensor & _imu )
2014-05-01 17:41:22 -03:00
{
2014-10-15 20:31:06 -03:00
AP_InertialSensor_MPU9250 * sensor = new AP_InertialSensor_MPU9250 ( _imu ) ;
if ( sensor = = NULL ) {
return NULL ;
}
2014-10-16 17:52:21 -03:00
if ( ! sensor - > _init_sensor ( ) ) {
2014-10-15 20:31:06 -03:00
delete sensor ;
return NULL ;
}
return sensor ;
}
2014-05-01 17:41:22 -03:00
2015-07-02 20:27:55 -03:00
bool AP_InertialSensor_MPU9250 : : initialize_driver_state ( ) {
AP_HAL : : SPIDeviceDriver * spi = hal . spi - > device ( AP_HAL : : SPIDevice_MPU9250 ) ;
if ( ! spi )
return false ;
2014-08-12 07:00:31 -03:00
2015-07-02 20:27:55 -03:00
AP_HAL : : SPIDeviceDriver : : State state = spi - > get_state ( ) ;
if ( state = = AP_HAL : : SPIDeviceDriver : : State : : FAILED )
return false ;
if ( state = = AP_HAL : : SPIDeviceDriver : : State : : RUNNING )
return true ;
/* First time trying the initialization: if it fails from now on we will
* set the device state to State : : Failed so we don ' t try again if another
* driver asks it */
spi - > set_state ( AP_HAL : : SPIDeviceDriver : : State : : FAILED ) ;
2014-05-01 17:41:22 -03:00
2015-07-02 20:27:55 -03:00
uint8_t whoami = _register_read ( spi , MPUREG_WHOAMI ) ;
2015-03-12 23:26:49 -03:00
if ( whoami ! = MPUREG_WHOAMI_MPU9250 & & whoami ! = MPUREG_WHOAMI_MPU9255 ) {
2014-07-07 03:32:52 -03:00
hal . console - > printf ( " MPU9250: unexpected WHOAMI 0x%x \n " , ( unsigned ) whoami ) ;
2015-07-02 20:27:55 -03:00
goto fail_whoami ;
2014-07-07 03:32:52 -03:00
}
2015-07-02 20:27:55 -03:00
// initially run the bus at low speed
spi - > set_bus_speed ( AP_HAL : : SPIDeviceDriver : : SPI_SPEED_LOW ) ;
// Chip reset
uint8_t tries ;
for ( tries = 0 ; tries < 5 ; tries + + ) {
2015-07-02 21:23:46 -03:00
uint8_t user_ctrl = _register_read ( spi , MPUREG_USER_CTRL ) ;
/* First disable the master I2C to avoid hanging the slaves on the
* aulixiliar I2C bus */
if ( user_ctrl & BIT_USER_CTRL_I2C_MST_EN ) {
_register_write ( spi , MPUREG_USER_CTRL , user_ctrl & ~ BIT_USER_CTRL_I2C_MST_EN ) ;
hal . scheduler - > delay ( 10 ) ;
}
// reset device
_register_write ( spi , MPUREG_PWR_MGMT_1 , BIT_PWR_MGMT_1_DEVICE_RESET ) ;
hal . scheduler - > delay ( 100 ) ;
2015-07-02 20:27:55 -03:00
// disable I2C as recommended by the datasheet
_register_write ( spi , MPUREG_USER_CTRL , BIT_USER_CTRL_I2C_IF_DIS ) ;
// 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
_register_write ( spi , MPUREG_PWR_MGMT_1 , BIT_PWR_MGMT_1_CLK_ZGYRO ) ;
hal . scheduler - > delay ( 5 ) ;
// check it has woken up
if ( _register_read ( spi , MPUREG_PWR_MGMT_1 ) = = BIT_PWR_MGMT_1_CLK_ZGYRO ) {
break ;
2014-05-01 17:41:22 -03:00
}
2015-07-02 20:27:55 -03:00
hal . scheduler - > delay ( 10 ) ;
uint8_t status = _register_read ( spi , MPUREG_INT_STATUS ) ;
if ( ( status & BIT_RAW_RDY_INT ) ! = 0 ) {
break ;
2014-05-01 17:41:22 -03:00
}
2015-07-02 20:27:55 -03:00
# if MPU9250_DEBUG
_dump_registers ( _spi ) ;
# endif
}
2014-05-01 17:41:22 -03:00
2015-07-02 20:27:55 -03:00
if ( tries = = 5 ) {
hal . console - > println_P ( PSTR ( " Failed to boot MPU9250 5 times " ) ) ;
goto fail_tries ;
}
spi - > set_state ( AP_HAL : : SPIDeviceDriver : : State : : RUNNING ) ;
return true ;
fail_tries :
fail_whoami :
spi - > set_bus_speed ( AP_HAL : : SPIDeviceDriver : : SPI_SPEED_HIGH ) ;
return false ;
}
/*
initialise the sensor
*/
bool AP_InertialSensor_MPU9250 : : _init_sensor ( void )
{
_spi = hal . spi - > device ( AP_HAL : : SPIDevice_MPU9250 ) ;
_spi_sem = _spi - > get_semaphore ( ) ;
if ( ! _hardware_init ( ) )
return false ;
2014-08-12 07:00:31 -03:00
2014-10-15 20:31:06 -03:00
_gyro_instance = _imu . register_gyro ( ) ;
_accel_instance = _imu . register_accel ( ) ;
2014-05-01 17:41:22 -03:00
2014-10-15 23:14:56 -03:00
_product_id = AP_PRODUCT_ID_MPU9250 ;
2014-05-01 17:41:22 -03:00
// start the timer process to read samples
2015-05-24 20:24:11 -03:00
hal . scheduler - > register_timer_process ( FUNCTOR_BIND_MEMBER ( & AP_InertialSensor_MPU9250 : : _poll_data , void ) ) ;
2014-05-01 17:41:22 -03:00
# if MPU9250_DEBUG
2015-07-02 17:33:22 -03:00
_dump_registers ( _spi ) ;
2014-05-01 17:41:22 -03:00
# endif
2014-10-15 20:31:06 -03:00
return true ;
2014-05-01 17:41:22 -03:00
}
2014-08-19 06:39:21 -03:00
/*
update the accel and gyro vectors
*/
2014-05-01 17:41:22 -03:00
bool AP_InertialSensor_MPU9250 : : update ( void )
{
2014-08-20 18:48:44 -03:00
// pull the data from the timer shared data buffer
uint8_t idx = _shared_data_idx ;
2014-10-15 20:31:06 -03:00
Vector3f gyro = _shared_data [ idx ] . _gyro_filtered ;
Vector3f accel = _shared_data [ idx ] . _accel_filtered ;
2014-05-01 17:41:22 -03:00
2014-10-15 20:31:06 -03:00
_have_sample_available = false ;
2014-05-01 17:41:22 -03:00
2014-10-15 20:31:06 -03:00
accel * = MPU9250_ACCEL_SCALE_1G ;
gyro * = GYRO_SCALE ;
2014-05-01 17:41:22 -03:00
2015-06-08 17:06:17 -03:00
accel . rotate ( _default_rotation ) ;
gyro . rotate ( _default_rotation ) ;
2014-08-18 02:57:33 -03:00
2015-02-17 02:50:59 -04:00
_publish_gyro ( _gyro_instance , gyro ) ;
_publish_accel ( _accel_instance , accel ) ;
2014-05-01 17:41:22 -03:00
2015-03-11 21:58:36 -03:00
if ( _last_accel_filter_hz ! = _accel_filter_cutoff ( ) ) {
_set_accel_filter ( _accel_filter_cutoff ( ) ) ;
_last_accel_filter_hz = _accel_filter_cutoff ( ) ;
}
if ( _last_gyro_filter_hz ! = _gyro_filter_cutoff ( ) ) {
_set_gyro_filter ( _gyro_filter_cutoff ( ) ) ;
_last_gyro_filter_hz = _gyro_filter_cutoff ( ) ;
2014-05-01 17:41:22 -03:00
}
return true ;
}
/*================ HARDWARE FUNCTIONS ==================== */
/**
* Timer process to poll for new data from the MPU9250 .
*/
void AP_InertialSensor_MPU9250 : : _poll_data ( void )
{
2014-08-19 06:39:21 -03:00
if ( ! _spi_sem - > take_nonblocking ( ) ) {
/*
the semaphore being busy is an expected condition when the
mainline code is calling wait_for_sample ( ) which will
grab the semaphore . We return now and rely on the mainline
code grabbing the latest sample .
*/
return ;
}
2014-08-19 07:01:29 -03:00
_read_data_transaction ( ) ;
2014-08-19 06:39:21 -03:00
_spi_sem - > give ( ) ;
2014-05-01 17:41:22 -03:00
}
2014-08-19 06:39:21 -03:00
/*
read from the data registers and update filtered data
*/
void AP_InertialSensor_MPU9250 : : _read_data_transaction ( )
{
2014-05-01 17:41:22 -03:00
/* one resister address followed by seven 2-byte registers */
struct PACKED {
uint8_t cmd ;
uint8_t int_status ;
uint8_t v [ 14 ] ;
} rx , tx = { cmd : MPUREG_INT_STATUS | 0x80 , } ;
2014-08-12 07:00:31 -03:00
2014-05-01 17:41:22 -03:00
_spi - > transaction ( ( const uint8_t * ) & tx , ( uint8_t * ) & rx , sizeof ( rx ) ) ;
# define int16_val(v, idx) ((int16_t)(((uint16_t)v[2*idx] << 8) | v[2*idx+1]))
2014-08-19 06:39:21 -03:00
2015-02-17 18:48:26 -04:00
Vector3f _accel_filtered = _accel_filter . apply ( Vector3f ( int16_val ( rx . v , 1 ) ,
int16_val ( rx . v , 0 ) ,
- int16_val ( rx . v , 2 ) ) ) ;
2014-08-20 18:48:44 -03:00
2015-02-17 18:48:26 -04:00
Vector3f _gyro_filtered = _gyro_filter . apply ( Vector3f ( int16_val ( rx . v , 5 ) ,
int16_val ( rx . v , 4 ) ,
- int16_val ( rx . v , 6 ) ) ) ;
2014-08-20 18:48:44 -03:00
// update the shared buffer
uint8_t idx = _shared_data_idx ^ 1 ;
_shared_data [ idx ] . _accel_filtered = _accel_filtered ;
_shared_data [ idx ] . _gyro_filtered = _gyro_filtered ;
_shared_data_idx = idx ;
2014-10-15 20:31:06 -03:00
_have_sample_available = true ;
2014-05-01 17:41:22 -03:00
}
2014-08-19 06:39:21 -03:00
/*
read an 8 bit register
*/
2015-07-02 17:33:22 -03:00
uint8_t AP_InertialSensor_MPU9250 : : _register_read ( AP_HAL : : SPIDeviceDriver * spi ,
uint8_t reg )
2014-05-01 17:41:22 -03:00
{
uint8_t addr = reg | 0x80 ; // Set most significant bit
uint8_t tx [ 2 ] ;
uint8_t rx [ 2 ] ;
tx [ 0 ] = addr ;
tx [ 1 ] = 0 ;
2015-07-02 17:33:22 -03:00
spi - > transaction ( tx , rx , 2 ) ;
2014-05-01 17:41:22 -03:00
return rx [ 1 ] ;
}
2014-08-19 06:39:21 -03:00
/*
write an 8 bit register
*/
2015-07-02 17:33:22 -03:00
void AP_InertialSensor_MPU9250 : : _register_write ( AP_HAL : : SPIDeviceDriver * spi ,
uint8_t reg , uint8_t val )
2014-05-01 17:41:22 -03:00
{
uint8_t tx [ 2 ] ;
uint8_t rx [ 2 ] ;
tx [ 0 ] = reg ;
tx [ 1 ] = val ;
2015-07-02 17:33:22 -03:00
spi - > transaction ( tx , rx , 2 ) ;
}
inline uint8_t AP_InertialSensor_MPU9250 : : _register_read ( uint8_t reg )
{
return _register_read ( _spi , reg ) ;
}
inline void AP_InertialSensor_MPU9250 : : _register_write ( uint8_t reg , uint8_t val )
{
_register_write ( _spi , reg , val ) ;
2014-05-01 17:41:22 -03:00
}
/*
2015-03-11 21:58:36 -03:00
set the accel filter frequency
2014-05-01 17:41:22 -03:00
*/
2015-03-11 21:58:36 -03:00
void AP_InertialSensor_MPU9250 : : _set_accel_filter ( uint8_t filter_hz )
2014-05-01 17:41:22 -03:00
{
2015-02-17 18:48:26 -04:00
_accel_filter . set_cutoff_frequency ( 1000 , filter_hz ) ;
2015-03-11 21:58:36 -03:00
}
2014-05-01 17:41:22 -03:00
2015-03-11 21:58:36 -03:00
/*
set the gyro filter frequency
*/
void AP_InertialSensor_MPU9250 : : _set_gyro_filter ( uint8_t filter_hz )
{
2015-02-17 18:48:26 -04:00
_gyro_filter . set_cutoff_frequency ( 1000 , filter_hz ) ;
2014-05-01 17:41:22 -03:00
}
2014-08-19 06:39:21 -03:00
/*
initialise the sensor configuration registers
*/
2014-10-16 17:52:21 -03:00
bool AP_InertialSensor_MPU9250 : : _hardware_init ( void )
2014-05-01 17:41:22 -03:00
{
2015-07-02 20:27:55 -03:00
// we need to suspend timers to prevent other SPI drivers grabbing
// the bus while we do the long initialisation
hal . scheduler - > suspend_timer_procs ( ) ;
2014-05-01 17:41:22 -03:00
if ( ! _spi_sem - > take ( 100 ) ) {
2014-10-15 20:31:06 -03:00
hal . console - > printf ( " MPU9250: Unable to get semaphore " ) ;
return false ;
2014-05-01 17:41:22 -03:00
}
2015-07-02 20:27:55 -03:00
if ( ! initialize_driver_state ( ) )
2014-05-01 17:41:22 -03:00
return false ;
_register_write ( MPUREG_PWR_MGMT_2 , 0x00 ) ; // only used for wake-up in accelerometer only low power mode
2015-03-11 21:58:36 -03:00
// used no filter of 256Hz on the sensor, then filter using
2014-08-19 06:39:21 -03:00
// the 2-pole software filter
2015-03-11 21:58:36 -03:00
_register_write ( MPUREG_CONFIG , BITS_DLPF_CFG_256HZ_NOLPF2 ) ;
2014-05-01 17:41:22 -03:00
2014-08-19 06:39:21 -03:00
// set sample rate to 1kHz, and use the 2 pole filter to give the
// desired rate
_register_write ( MPUREG_SMPLRT_DIV , MPUREG_SMPLRT_1000HZ ) ;
2014-05-01 17:41:22 -03:00
_register_write ( MPUREG_GYRO_CONFIG , BITS_GYRO_FS_2000DPS ) ; // Gyro scale 2000º/s
2015-04-28 02:29:41 -03:00
// RM-MPU-9250A-00.pdf, pg. 15, select accel full scale 16g
_register_write ( MPUREG_ACCEL_CONFIG , 3 < < 3 ) ;
2014-05-01 17:41:22 -03:00
// configure interrupt to fire when new data arrives
_register_write ( MPUREG_INT_ENABLE , BIT_RAW_RDY_EN ) ;
// 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 ) ;
// now that we have initialised, we set the SPI bus speed to high
// (8MHz on APM2)
_spi - > set_bus_speed ( AP_HAL : : SPIDeviceDriver : : SPI_SPEED_HIGH ) ;
_spi_sem - > give ( ) ;
2015-07-02 20:27:55 -03:00
hal . scheduler - > resume_timer_procs ( ) ;
2014-05-01 17:41:22 -03:00
return true ;
}
# if MPU9250_DEBUG
// dump all config registers - used for debug
2015-07-02 17:33:22 -03:00
void AP_InertialSensor_MPU9250 : : _dump_registers ( AP_HAL : : SPIDeviceDriver * spi )
2014-05-01 17:41:22 -03:00
{
hal . console - > println_P ( PSTR ( " MPU9250 registers " ) ) ;
for ( uint8_t reg = 0 ; reg < = 126 ; reg + + ) {
2015-07-02 17:33:22 -03:00
uint8_t v = _register_read ( spi , reg ) ;
2014-05-01 17:41:22 -03:00
hal . console - > printf_P ( PSTR ( " %02x:%02x " ) , ( unsigned ) reg , ( unsigned ) v ) ;
if ( ( reg - ( MPUREG_PRODUCT_ID - 1 ) ) % 16 = = 0 ) {
hal . console - > println ( ) ;
}
}
hal . console - > println ( ) ;
}
# endif
2014-07-06 23:03:57 -03:00
# endif // CONFIG_HAL_BOARD