void MPU60X0::initialize9250MasterMode(){
#include "AK8963.h"
uint8_t buff[3];
uint8_t data[7];
float _magScaleX, _magScaleY, _magScaleZ;
bool test;
//set dev address for magnetometer
magDevAddr = 0x0C;
// SPI Configuration
if (bSPI) {
SPI.begin();
pinMode(devAddr, OUTPUT);
digitalWrite(devAddr, HIGH);
reset();
delay(100);
switchSPIEnabled(true);
delay(1);
} else {
Wire.begin(400000);
switchSPIEnabled(false);
}
setStandbyDisable();
setSleepEnabled(false);
// select clock source to gyro
if( !writeRegister(MPU60X0_RA_PWR_MGMT_1, MPU60X0_CLOCK_PLL_XGYRO) ){
Serial.println("Clock Source Not Set");
}
// enable I2C master mode
if( !writeRegister(MPU60X0_RA_USER_CTRL,I2C_MST_EN) ){
Serial.println("Master Mode Not Set");
}
// set the I2C bus speed to 400 kHz
if( !writeRegister(MPU60X0_RA_I2C_MST_CTRL,I2C_MST_CLK) ){
Serial.println("I2C Bus Speed Not Set");
}
// set AK8963 to Power Down
if( !writeAKRegister(AK8963_RA_CNTL1, AK8963_MODE_POWERDOWN) ){
Serial.println("AK Not Powered Down");
}
// reset the MPU9250
writeRegister(MPU60X0_RA_PWR_MGMT_1, PWR_RESET);
// wait for MPU-9250 to come back up
delay(1);
// reset the AK8963
writeAKRegister(AK8963_RA_CNTL2, PWR_RESET);
// select clock source to gyro
if( !writeRegister(MPU60X0_RA_PWR_MGMT_1, MPU60X0_CLOCK_PLL_XGYRO) ){
Serial.println("Clock Source Not Set");
}
// check the WHO AM I byte, expected value is 0x71 (decimal 113)
readRegister(MPU60X0_RA_WHO_AM_I, 1,&buff[0]);
if( buff[0] != 113 ){
Serial.println("9250 Not Recognized");
}
// enable accelerometer and gyro
if( !writeRegister(MPU60X0_RA_PWR_MGMT_2, SEN_ENABLE) ){
Serial.println("Accel and Gyro not Enabled");
}
/* setup the accel and gyro ranges */
setFullScaleGyroRange(MPU60X0_GYRO_FS_2000); // set gyro range to +/- 2000 deg/second
setFullScaleAccelRange(MPU60X0_ACCEL_FS_2); // set accel range to +- 2g
setDLPFMode(MPU60X0_DLPF_BW_98);
// enable I2C master mode
if( !writeRegister(MPU60X0_RA_USER_CTRL,I2C_MST_EN) ){
Serial.println("Master Mode Set");
}
// set the I2C bus speed to 400 kHz
if( !writeRegister(MPU60X0_RA_I2C_MST_CTRL,MPU60X0_CLOCK_PLL_XGYRO) ){
Serial.println("I2C Bus Set");
}
// check AK8963 WHO AM I register, expected value is 0x48 (decimal 72)
readAKRegisters(AK8963_RA_WIA, sizeof(buff), &buff[0]);
if( buff[0] != 72 ){
Serial.print(buff[0]); Serial.print(", ");Serial.println("AK does not match");
}
/* get the magnetometer calibration */
// set AK8963 to Power Down
if( !writeAKRegister(AK8963_RA_CNTL1, AK8963_MODE_POWERDOWN) ){
Serial.println("AK Not Powered Down");
}
delay(100); // long wait between AK8963 mode changes
// set AK8963 to FUSE ROM access
if( !writeAKRegister(AK8963_RA_ASAX, AK8963_MODE_FUSEROM)){
Serial.println("FUSE ROM Access Not set");
}
delay(100); // long wait between AK8963 mode changes
// read the AK8963 ASA registers and compute magnetometer scale factors
readAKRegisters(AK8963_RA_ASAX, sizeof(buff), &buff[0]);
_magScaleX = ((((float)buff[0]) - 128.0f)/(256.0f) + 1.0f) * 4912.0f / 32760.0f; // micro Tesla
_magScaleY = ((((float)buff[1]) - 128.0f)/(256.0f) + 1.0f) * 4912.0f / 32760.0f; // micro Tesla
_magScaleZ = ((((float)buff[2]) - 128.0f)/(256.0f) + 1.0f) * 4912.0f / 32760.0f; // micro Tesla
//_magScaleX = buff[0];
//_magScaleX = buff[1];
//_magScaleX = buff[2];
Serial.print(_magScaleX,8); Serial.print(", "); Serial.print(_magScaleY,8);
Serial.print(", "); Serial.println(_magScaleZ,8);
// set AK8963 to Power Down
if( !writeAKRegister(AK8963_RA_CNTL1, AK8963_MODE_POWERDOWN) ){
Serial.println("AK Not Powered Down");
}
delay(100); // long wait between AK8963 mode changes
// set AK8963 to 16 bit resolution, 100 Hz update rate
if( !writeRegister(AK8963_RA_CNTL1, 0x16) ){
Serial.println("Res Not Set");
}
delay(100); // long wait between AK8963 mode changes
// select clock source to gyro
if( !writeRegister(MPU60X0_RA_PWR_MGMT_1, MPU60X0_CLOCK_PLL_XGYRO) ){
Serial.println("Clock Source Not Set");
}
// instruct the MPU9250 to get 7 bytes of data from the AK8963 at the sample rate
readAKRegisters(AK8963_RA_HXL,sizeof(data),&data[0]);
// successful init, return 0
Serial.println("FINISHED");
}