How can I access mag(AK8953) with spi ?

By eaglezcy , 17 March 2016

I have a strange problem with the MPU9255, when using SPI, I can access acc and gyro, but I can not access mag constantly .
I try to use the following codes to get 0x48 but not constantly successful. It seems that AK8963 may not always work properly when it is powered up. Any idea how I can access AK8975 and read reliable data stably? Thank you!
And I am sure the the 1st pin of MPU9255 is connected to VDDIO.
WriteReg(MPUREG_PWR_MGMT_1, BIT_H_RESET); //Reset the internal registers and restores the default settings.
delay_ms(20);
WriteReg(MPUREG_PWR_MGMT_1, 0x01); //clock source
delay_ms(20);
WriteReg(MPUREG_USER_CTRL, 0x10); // Reset I2C Slave module and put the serial interface in SPI mode only.
delay_ms(20);
WriteReg(MPUREG_PWR_MGMT_2, 0x00); // Enable Acc & Gyro
delay_ms(20);
WriteReg(MPUREG_USER_CTRL, 0x20); // Enable the I2C Master I/F module; pins ES_DA and ES_SCL are isolated from pins SDA/SDI and SCL/ SCLK.
delay_ms(20);
WriteReg(MPUREG_I2C_MST_CTRL, 0x0D); // I2C configuration multi-master IIC 400KHz
delay_ms(20);

WriteReg(MPUREG_I2C_SLV0_ADDR, AK8963_I2C_ADDR | I2C_READ_FLAG); //Set the I2C slave addres of AK8963 and set for read.
WriteReg(MPUREG_I2C_SLV0_REG, AK8963_WIA); //I2C slave 0 register address from where to begin data transfer
WriteReg(MPUREG_I2C_SLV0_CTRL, 0x81); //Read 1 byte from the magnetometer
delay_ms(10);
ReadReg(MPUREG_EXT_SENS_DATA_00, &whoami); //Read I2C

phpbb Topic ID
33763