PX4 Firmware
PX4 Autopilot Software http://px4.io
MPU9250 Class Reference

#include <mpu9250.h>

Inheritance diagram for MPU9250:
Collaboration diagram for MPU9250:

Public Member Functions

 MPU9250 (device::Device *interface, device::Device *mag_interface, enum Rotation rotation)
 
virtual ~MPU9250 ()
 
virtual int init ()
 
uint8_t get_whoami ()
 
void print_info ()
 Diagnostics - print some basic information about the driver. More...
 

Protected Member Functions

virtual int probe ()
 whoami result More...
 
void Run () override
 

Protected Attributes

device::Device_interface
 
uint8_t _whoami {0}
 

Private Member Functions

bool check_null_data (uint16_t *data, uint8_t size)
 
bool check_duplicate (uint8_t *accel_data)
 
void start ()
 
void stop ()
 
int reset ()
 
int reset_mpu ()
 Resets the main chip (excluding the magnetometer if any). More...
 
void measure ()
 Fetch measurements from the sensor and update the report buffers. More...
 
uint8_t read_reg (unsigned reg, uint32_t speed=MPU9250_LOW_BUS_SPEED)
 Read a register from the mpu. More...
 
uint16_t read_reg16 (unsigned reg)
 
uint8_t read_reg_range (unsigned start_reg, uint32_t speed, uint8_t *buf, uint16_t count)
 Read a register range from the mpu. More...
 
void write_reg (unsigned reg, uint8_t value)
 Write a register in the mpu. More...
 
void modify_reg (unsigned reg, uint8_t clearbits, uint8_t setbits)
 Modify a register in the mpu. More...
 
void write_checked_reg (unsigned reg, uint8_t value)
 Write a register in the mpu, updating _checked_values. More...
 
void modify_checked_reg (unsigned reg, uint8_t clearbits, uint8_t setbits)
 Modify a checked register in the mpu. More...
 
int set_accel_range (unsigned max_g)
 Set the mpu measurement range. More...
 
uint16_t swap16 (uint16_t val)
 Swap a 16-bit value read from the mpu to native byte order. More...
 
bool is_external ()
 Get the internal / external state. More...
 
void _set_dlpf_filter (uint16_t frequency_hz)
 
void _set_sample_rate (unsigned desired_sample_rate_hz)
 
void check_registers ()
 

Private Attributes

PX4Accelerometer _px4_accel
 
PX4Gyroscope _px4_gyro
 
MPU9250_mag _mag
 
unsigned _call_interval {1000}
 
unsigned _dlpf_freq {0}
 
unsigned _sample_rate {1000}
 
perf_counter_t _sample_perf
 
perf_counter_t _bad_transfers
 
perf_counter_t _bad_registers
 
perf_counter_t _good_transfers
 
perf_counter_t _duplicates
 
uint8_t _register_wait {0}
 
uint64_t _reset_wait {0}
 
const uint16_t * _checked_registers {nullptr}
 
uint8_t _checked_values [MPU9250_NUM_CHECKED_REGISTERS] {}
 
uint8_t _checked_bad [MPU9250_NUM_CHECKED_REGISTERS] {}
 
unsigned _checked_next {0}
 
unsigned _num_checked_registers {0}
 
float _last_temperature {0.0f}
 
uint8_t _last_accel_data [6] {}
 
bool _got_duplicate {false}
 

Static Private Attributes

static constexpr int MPU9250_NUM_CHECKED_REGISTERS {11}
 
static const uint16_t _mpu9250_checked_registers [MPU9250_NUM_CHECKED_REGISTERS]
 

Friends

class MPU9250_mag
 

Detailed Description

Definition at line 226 of file mpu9250.h.

Constructor & Destructor Documentation

◆ MPU9250()

MPU9250::MPU9250 ( device::Device interface,
device::Device mag_interface,
enum Rotation  rotation 
)

Definition at line 73 of file mpu9250.cpp.

References _px4_accel, _px4_gyro, DRV_ACC_DEVTYPE_MPU9250, DRV_GYR_DEVTYPE_MPU9250, PX4Accelerometer::set_device_type(), and PX4Gyroscope::set_device_type().

Here is the call graph for this function:

◆ ~MPU9250()

MPU9250::~MPU9250 ( )
virtual

Definition at line 90 of file mpu9250.cpp.

References _bad_registers, _bad_transfers, _duplicates, _good_transfers, _sample_perf, perf_free(), and stop().

Here is the call graph for this function:

Member Function Documentation

◆ _set_dlpf_filter()

void MPU9250::_set_dlpf_filter ( uint16_t  frequency_hz)
private

Definition at line 328 of file mpu9250.cpp.

References _dlpf_freq, _whoami, BITS_DLPF_CFG_10HZ, BITS_DLPF_CFG_184HZ, BITS_DLPF_CFG_20HZ, BITS_DLPF_CFG_250HZ, BITS_DLPF_CFG_3600HZ, BITS_DLPF_CFG_41HZ, BITS_DLPF_CFG_5HZ, BITS_DLPF_CFG_92HZ, MPU_WHOAMI_6500, MPU_WHOAMI_9250, MPUREG_CONFIG, and write_checked_reg().

Referenced by reset_mpu().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ _set_sample_rate()

void MPU9250::_set_sample_rate ( unsigned  desired_sample_rate_hz)
private

Definition at line 295 of file mpu9250.cpp.

References _sample_rate, _whoami, MPU9250_GYRO_DEFAULT_RATE, MPU_WHOAMI_6500, MPU_WHOAMI_9250, MPUREG_SMPLRT_DIV, and write_checked_reg().

Referenced by reset_mpu().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ check_duplicate()

bool MPU9250::check_duplicate ( uint8_t *  accel_data)
private

Definition at line 594 of file mpu9250.cpp.

References _duplicates, _got_duplicate, _last_accel_data, _sample_perf, perf_count(), and perf_end().

Referenced by measure().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ check_null_data()

bool MPU9250::check_null_data ( uint16_t *  data,
uint8_t  size 
)
private

Definition at line 574 of file mpu9250.cpp.

References _bad_transfers, _good_transfers, _sample_perf, perf_count(), and perf_end().

Referenced by measure().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ check_registers()

void MPU9250::check_registers ( void  )
private

Definition at line 514 of file mpu9250.cpp.

References _bad_registers, _checked_bad, _checked_next, _checked_registers, _checked_values, _num_checked_registers, _register_wait, _reset_wait, BIT_H_RESET, hrt_absolute_time(), MPU9250_HIGH_BUS_SPEED, MPU_CLK_SEL_AUTO, MPUREG_PWR_MGMT_1, MPUREG_PWR_MGMT_2, perf_count(), read_reg(), and write_reg().

Referenced by measure().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_whoami()

uint8_t MPU9250::get_whoami ( )
inline

Definition at line 233 of file mpu9250.h.

◆ init()

int MPU9250::init ( )
virtual

Definition at line 104 of file mpu9250.cpp.

References MPU9250_mag::_interface, _interface, _mag, _reset_wait, _sample_rate, _whoami, MPU9250_mag::ak8963_reset(), device::Device::DeviceBusType_I2C, device::Device::get_device_bus_type(), hrt_absolute_time(), device::Device::init(), MPU9250_mag::is_passthrough(), MPU_WHOAMI_9250, OK, probe(), reset_mpu(), and start().

Referenced by DfMpu9250Wrapper::start(), and mpu9250::start_bus().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ is_external()

bool MPU9250::is_external ( )
inlineprivate

Get the internal / external state.

Returns
true if the sensor is not on the main MCU board

Definition at line 389 of file mpu9250.h.

References device::Device::external().

Referenced by MPU9250_mag::_measure().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ measure()

void MPU9250::measure ( )
private

Fetch measurements from the sensor and update the report buffers.

Definition at line 619 of file mpu9250.cpp.

References _bad_registers, _bad_transfers, _last_temperature, _mag, MPU9250_mag::_measure(), _px4_accel, _px4_gyro, _register_wait, _reset_wait, _sample_perf, _whoami, check_duplicate(), check_null_data(), check_registers(), f(), hrt_absolute_time(), hrt_abstime, int16_t_from_bytes(), MPU9250_mag::is_passthrough(), MPU9250_mag::measure(), MPU9250_HIGH_BUS_SPEED, MPU_WHOAMI_6500, MPU_WHOAMI_9250, MPUREG_INT_STATUS, OK, perf_begin(), perf_end(), perf_event_count(), read_reg_range(), PX4Accelerometer::set_error_count(), PX4Gyroscope::set_error_count(), PX4Accelerometer::set_temperature(), PX4Gyroscope::set_temperature(), PX4Accelerometer::update(), and PX4Gyroscope::update().

Referenced by Run().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ modify_checked_reg()

void MPU9250::modify_checked_reg ( unsigned  reg,
uint8_t  clearbits,
uint8_t  setbits 
)
private

Modify a checked register in the mpu.

Bits are cleared before bits are set.

Parameters
regThe register to modify.
clearbitsBits in the register to clear.
setbitsBits in the register to set.

Definition at line 429 of file mpu9250.cpp.

References read_reg(), and write_checked_reg().

Referenced by MPU9250_mag::ak8963_setup(), and MPU9250_mag::ak8963_setup_master_i2c().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ modify_reg()

void MPU9250::modify_reg ( unsigned  reg,
uint8_t  clearbits,
uint8_t  setbits 
)
private

Modify a register in the mpu.

Bits are cleared before bits are set.

Parameters
regThe register to modify.
clearbitsBits in the register to clear.
setbitsBits in the register to set.

Definition at line 420 of file mpu9250.cpp.

References read_reg(), and write_reg().

Referenced by MPU9250_mag::ak8963_setup().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ print_info()

void MPU9250::print_info ( )

Diagnostics - print some basic information about the driver.

Definition at line 743 of file mpu9250.cpp.

References _bad_registers, _bad_transfers, _duplicates, _good_transfers, _mag, _px4_accel, _px4_gyro, _sample_perf, perf_print_counter(), PX4Accelerometer::print_status(), PX4Gyroscope::print_status(), and MPU9250_mag::print_status().

Referenced by mpu9250::status().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ probe()

int MPU9250::probe ( )
protectedvirtual

whoami result

Definition at line 265 of file mpu9250.cpp.

References _checked_bad, _checked_registers, _checked_values, _mpu9250_checked_registers, _num_checked_registers, _whoami, MPU9250_NUM_CHECKED_REGISTERS, MPU_WHOAMI_6500, MPU_WHOAMI_9250, MPUREG_WHOAMI, and read_reg().

Referenced by init().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ read_reg()

uint8_t MPU9250::read_reg ( unsigned  reg,
uint32_t  speed = MPU9250_LOW_BUS_SPEED 
)
private

Read a register from the mpu.

Parameters
Theregister to read.
Thebus speed to read with.
Returns
The value that was read.

Definition at line 382 of file mpu9250.cpp.

References _interface, MPU9250_SET_SPEED, and device::Device::read().

Referenced by check_registers(), modify_checked_reg(), modify_reg(), probe(), and reset_mpu().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ read_reg16()

uint16_t MPU9250::read_reg16 ( unsigned  reg)
private

Definition at line 402 of file mpu9250.cpp.

References _interface, arraySize, MPU9250_LOW_SPEED_OP, and device::Device::read().

Here is the call graph for this function:

◆ read_reg_range()

uint8_t MPU9250::read_reg_range ( unsigned  start_reg,
uint32_t  speed,
uint8_t *  buf,
uint16_t  count 
)
private

Read a register range from the mpu.

Parameters
Thestart address to read from.
Thebus speed to read with.
Theaddress of the target data buffer.
Thecount of bytes to be read.
Returns
The value that was read.

Definition at line 392 of file mpu9250.cpp.

References _interface, MPU9250_SET_SPEED, and device::Device::read().

Referenced by measure().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ reset()

int MPU9250::reset ( )
private

Definition at line 157 of file mpu9250.cpp.

References _mag, _reset_wait, _whoami, MPU9250_mag::ak8963_reset(), hrt_absolute_time(), MPU_WHOAMI_9250, OK, and reset_mpu().

Here is the call graph for this function:

◆ reset_mpu()

int MPU9250::reset_mpu ( )
private

Resets the main chip (excluding the magnetometer if any).

Definition at line 182 of file mpu9250.cpp.

References _checked_registers, _checked_values, _interface, _mag, _num_checked_registers, _px4_gyro, _sample_rate, _set_dlpf_filter(), _set_sample_rate(), _whoami, ACCEL_RANGE_G, BIT_H_RESET, BIT_I2C_IF_DIS, BIT_INT_ANYRD_2CLEAR, BIT_INT_BYPASS_EN, BIT_RAW_RDY_EN, BITS_ACCEL_CONFIG2_41HZ, BITS_FS_2000DPS, device::Device::DeviceBusType_I2C, device::Device::get_device_bus_type(), MPU9250_mag::is_passthrough(), MPU9250_DEFAULT_ONCHIP_FILTER_FREQ, MPU_CLK_SEL_AUTO, MPU_WHOAMI_6500, MPU_WHOAMI_9250, MPUREG_ACCEL_CONFIG2, MPUREG_GYRO_CONFIG, MPUREG_INT_ENABLE, MPUREG_INT_PIN_CFG, MPUREG_PWR_MGMT_1, MPUREG_PWR_MGMT_2, MPUREG_USER_CTRL, OK, read_reg(), set_accel_range(), PX4Gyroscope::set_scale(), write_checked_reg(), and write_reg().

Referenced by init(), and reset().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ Run()

void MPU9250::Run ( )
overrideprotected

Definition at line 507 of file mpu9250.cpp.

References measure().

Here is the call graph for this function:

◆ set_accel_range()

int MPU9250::set_accel_range ( unsigned  max_g)
private

Set the mpu measurement range.

Parameters
max_gThe maximum G value the range must support.
Returns
OK if the value can be supported, -ERANGE otherwise.

Definition at line 452 of file mpu9250.cpp.

References _px4_accel, _whoami, CONSTANTS_ONE_G, MPU_WHOAMI_6500, MPU_WHOAMI_9250, MPUREG_ACCEL_CONFIG, OK, PX4Accelerometer::set_scale(), and write_checked_reg().

Referenced by reset_mpu().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ start()

void MPU9250::start ( )
private

Definition at line 492 of file mpu9250.cpp.

References _call_interval, MPU9250_TIMER_REDUCTION, and stop().

Referenced by init(), and DfMpu9250Wrapper::start().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ stop()

void MPU9250::stop ( )
private

Definition at line 501 of file mpu9250.cpp.

Referenced by start(), DfMpu9250Wrapper::stop(), and ~MPU9250().

Here is the caller graph for this function:

◆ swap16()

uint16_t MPU9250::swap16 ( uint16_t  val)
inlineprivate

Swap a 16-bit value read from the mpu to native byte order.

Definition at line 382 of file mpu9250.h.

◆ write_checked_reg()

void MPU9250::write_checked_reg ( unsigned  reg,
uint8_t  value 
)
private

Write a register in the mpu, updating _checked_values.

Parameters
regThe register to write.
valueThe new value to write.

Definition at line 438 of file mpu9250.cpp.

References _checked_bad, _checked_registers, _checked_values, _num_checked_registers, and write_reg().

Referenced by _set_dlpf_filter(), _set_sample_rate(), modify_checked_reg(), reset_mpu(), and set_accel_range().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ write_reg()

void MPU9250::write_reg ( unsigned  reg,
uint8_t  value 
)
private

Write a register in the mpu.

Parameters
regThe register to write.
valueThe new value to write.

Definition at line 413 of file mpu9250.cpp.

References _interface, MPU9250_LOW_SPEED_OP, and device::Device::write().

Referenced by MPU9250_mag::ak8963_setup(), MPU9250_mag::ak8963_setup_master_i2c(), check_registers(), modify_reg(), MPU9250_mag::passthrough_read(), MPU9250_mag::passthrough_write(), reset_mpu(), MPU9250_mag::set_passthrough(), and write_checked_reg().

Here is the call graph for this function:
Here is the caller graph for this function:

Friends And Related Function Documentation

◆ MPU9250_mag

friend class MPU9250_mag
friend

Definition at line 246 of file mpu9250.h.

Member Data Documentation

◆ _bad_registers

perf_counter_t MPU9250::_bad_registers
private

Definition at line 265 of file mpu9250.h.

Referenced by check_registers(), measure(), print_info(), and ~MPU9250().

◆ _bad_transfers

perf_counter_t MPU9250::_bad_transfers
private

Definition at line 264 of file mpu9250.h.

Referenced by check_null_data(), measure(), print_info(), and ~MPU9250().

◆ _call_interval

unsigned MPU9250::_call_interval {1000}
private

Definition at line 257 of file mpu9250.h.

Referenced by start().

◆ _checked_bad

uint8_t MPU9250::_checked_bad[MPU9250_NUM_CHECKED_REGISTERS] {}
private

Definition at line 282 of file mpu9250.h.

Referenced by check_registers(), probe(), and write_checked_reg().

◆ _checked_next

unsigned MPU9250::_checked_next {0}
private

Definition at line 283 of file mpu9250.h.

Referenced by check_registers().

◆ _checked_registers

const uint16_t* MPU9250::_checked_registers {nullptr}
private

Definition at line 279 of file mpu9250.h.

Referenced by check_registers(), probe(), reset_mpu(), and write_checked_reg().

◆ _checked_values

uint8_t MPU9250::_checked_values[MPU9250_NUM_CHECKED_REGISTERS] {}
private

Definition at line 281 of file mpu9250.h.

Referenced by check_registers(), probe(), reset_mpu(), and write_checked_reg().

◆ _dlpf_freq

unsigned MPU9250::_dlpf_freq {0}
private

Definition at line 259 of file mpu9250.h.

Referenced by _set_dlpf_filter().

◆ _duplicates

perf_counter_t MPU9250::_duplicates
private

Definition at line 267 of file mpu9250.h.

Referenced by check_duplicate(), print_info(), and ~MPU9250().

◆ _good_transfers

perf_counter_t MPU9250::_good_transfers
private

Definition at line 266 of file mpu9250.h.

Referenced by check_null_data(), print_info(), and ~MPU9250().

◆ _got_duplicate

bool MPU9250::_got_duplicate {false}
private

Definition at line 295 of file mpu9250.h.

Referenced by check_duplicate().

◆ _interface

device::Device* MPU9250::_interface
protected

◆ _last_accel_data

uint8_t MPU9250::_last_accel_data[6] {}
private

Definition at line 294 of file mpu9250.h.

Referenced by check_duplicate().

◆ _last_temperature

float MPU9250::_last_temperature {0.0f}
private

Definition at line 288 of file mpu9250.h.

Referenced by MPU9250_mag::_measure(), and measure().

◆ _mag

MPU9250_mag MPU9250::_mag
private

Definition at line 255 of file mpu9250.h.

Referenced by init(), measure(), print_info(), reset(), and reset_mpu().

◆ _mpu9250_checked_registers

const uint16_t MPU9250::_mpu9250_checked_registers
staticprivate

◆ _num_checked_registers

unsigned MPU9250::_num_checked_registers {0}
private

Definition at line 284 of file mpu9250.h.

Referenced by check_registers(), probe(), reset_mpu(), and write_checked_reg().

◆ _px4_accel

PX4Accelerometer MPU9250::_px4_accel
private

Definition at line 252 of file mpu9250.h.

Referenced by measure(), MPU9250(), print_info(), and set_accel_range().

◆ _px4_gyro

PX4Gyroscope MPU9250::_px4_gyro
private

Definition at line 253 of file mpu9250.h.

Referenced by measure(), MPU9250(), print_info(), and reset_mpu().

◆ _register_wait

uint8_t MPU9250::_register_wait {0}
private

Definition at line 269 of file mpu9250.h.

Referenced by check_registers(), and measure().

◆ _reset_wait

uint64_t MPU9250::_reset_wait {0}
private

Definition at line 270 of file mpu9250.h.

Referenced by check_registers(), init(), measure(), and reset().

◆ _sample_perf

perf_counter_t MPU9250::_sample_perf
private

Definition at line 263 of file mpu9250.h.

Referenced by check_duplicate(), check_null_data(), measure(), print_info(), and ~MPU9250().

◆ _sample_rate

unsigned MPU9250::_sample_rate {1000}
private

Definition at line 261 of file mpu9250.h.

Referenced by _set_sample_rate(), init(), and reset_mpu().

◆ _whoami

◆ MPU9250_NUM_CHECKED_REGISTERS

constexpr int MPU9250::MPU9250_NUM_CHECKED_REGISTERS {11}
staticprivate

Definition at line 276 of file mpu9250.h.

Referenced by probe().


The documentation for this class was generated from the following files: