AK8963 (mpu9250) SPI problem

By aster , 24 November 2017

hello,

i am trying to use the MPU. The SPI works well but for some reason i am completely unable to communicate with the magnetometer. if i just ask for the who am i register i don't get nothing while the wai register of the mpu works correctly
any help would be very appreciate, thank, i losted days on it and maybe someone who use it could spot the error easily :D

these are the functions i use to write/read:

unsigned int MPU::WriteReg( uint_t WriteAddr, uint_t WriteData )
{
unsigned int temp_val;

select();
SPI.transfer(WriteAddr);
temp_val=SPI.transfer(WriteData);
deselect();

//delayMicroseconds();
return temp_val;
}
unsigned int MPU::ReadReg( uint_t WriteAddr, uint_t WriteData )
{
return WriteReg(WriteAddr | READ_FLAG,WriteData);
}
void MPU::ReadRegs( uint_t ReadAddr, uint_t *ReadBuf, unsigned int Bytes )
{
unsigned int i = ;

select();
SPI.transfer(ReadAddr | READ_FLAG);
for(i = ; i < Bytes; i++)
ReadBuf[i] = SPI.transfer(x);
deselect();

//delayMicroseconds();
}

this is the function to inizialize it:


bool MPU::init(bool calib_gyro, bool calib_acc){
SPI.begin();
if (my_bus != ) SPI.setModule(my_bus); //choose SPI if differnt from
pinMode(my_cs, OUTPUT);
#ifdef CORE_TEENSY
digitalWriteFast(my_cs, HIGH);
#else
digitalWrite(my_cs, HIGH);
#endif
float temp[];

if(calib_gyro && calib_acc){
calibrate(g_bias, a_bias);
}
else if(calib_gyro){
calibrate(g_bias, temp);
}
else if(calib_acc){
calibrate(temp, a_bias);
}

uint_t i = ;
uint_t MPU_Init_Data[MPU_InitRegNum][] = {
{BIT_H_RESET, MPUREG_PWR_MGMT_}, // Reset Device
{x, MPUREG_PWR_MGMT_}, // Clock Source
{x, MPUREG_PWR_MGMT_}, // Enable Acc & Gyro
{my_low_pass_filter, MPUREG_CONFIG}, // Use DLPF set Gyroscope bandwidth Hz, temperature bandwidth Hz
{BITS_FS_DPS, MPUREG_GYRO_CONFIG}, // +-dps
{BITS_FS_G, MPUREG_ACCEL_CONFIG}, // +-G
{my_low_pass_filter_acc, MPUREG_ACCEL_CONFIG_}, // Set Acc Data Rates, Enable Acc LPF , Bandwidth Hz
{x, MPUREG_INT_PIN_CFG}, //
//{x, MPUREG_IC_MST_CTRL}, // IC Speed kHz
//{x, MPUREG_USER_CTRL}, // Enable AUX
{x, MPUREG_USER_CTRL}, // IC Master mode and set IC_IF_DIS to disable slave mode IC bus
{xD, MPUREG_IC_MST_CTRL}, // IC configuration multi-master IIC KHz

{AK_IC_ADDR, MPUREG_IC_SLV_ADDR}, // Set the IC slave addres of AK and set for write.
//{x, MPUREG_IC_SLV_CTRL},
//{x, MPUREG_IC_MST_DELAY_CTRL}, // Enable IC delay

{AK_CNTL, MPUREG_IC_SLV_REG}, // IC slave register address from where to begin data transfer
{x, MPUREG_IC_SLV_DO}, // Reset AK
{x, MPUREG_IC_SLV_CTRL}, // Enable IC and set byte

{AK_CNTL, MPUREG_IC_SLV_REG}, // IC slave register address from where to begin data transfer
#ifdef AKFASTMODE
{x, MPUREG_IC_SLV_DO}, // Register value to Hz continuous measurement in bit
#else
{x, MPUREG_IC_SLV_DO}, // Register value to Hz continuous measurement in bit
#endif
{x, MPUREG_IC_SLV_CTRL} //Enable IC and set byte

};

for(i = ; i < MPU_InitRegNum; i++) {
WriteReg(MPU_Init_Data[i][], MPU_Init_Data[i][]);
delayMicroseconds(); // IC must slow down the write speed, otherwise it won't work
}

set_acc_scale(BITS_FS_G);
set_gyro_scale(BITS_FS_DPS);

calib_mag(); // If experiencing problems here, just comment it out. Should still be somewhat functional.
return ;
}

and this is the wai:


uint_t MPU::AK_wai(){
uint_t response;
WriteReg(MPUREG_IC_SLV_ADDR,AK_IC_ADDR|READ_FLAG); //Set the IC slave addres of AK and set for read.
WriteReg(MPUREG_IC_SLV_REG, AK_WAI); //IC slave register address from where to begin data transfer
WriteReg(MPUREG_IC_SLV_CTRL, x); //Read byte from the magnetometer

//WriteReg(MPUREG_IC_SLV_CTRL, x); //Enable IC and set bytes
delayMicroseconds();
response = WriteReg(MPUREG_EXT_SENS_DATA_|READ_FLAG, x); //Read IC
//ReadRegs(MPUREG_EXT_SENS_DATA_,response,);
//response=WriteReg(MPUREG_IC_SLV_DO, x); //Read IC

return response;
}

Am i doing something wrong?
in the attachments i put the header file if someone wants to try
edit: library

aster

8 years 3 months ago

ok solved, basically i was doing quite everything wrong

phpbb Post ID
36611

michaelremley

8 years ago

aster, how did you solve this problem? I am experiencing it as well and have been stuck for months!!

phpbb Post ID
36800

saevar2018

7 years 10 months ago

What is the solution to this problem, I am also having difficulties with this?

phpbb Post ID
36919
phpbb Topic ID
36606