MPU9250 and SPI

By osaft , 16 December 2014

Hi,
I try to acces to MPU9250 with the SPI bus.
I unsuccessfully try to read the Who Am I register (number 117)

The CS, CLK and SDI line seems correct at oscilloscope but there is nothing on SDO line.

do i need to set before with I2C bus the I2C_IF_DIS (register 106)?

The data sheet take about SPI address but don't give this address. It is 0x68 like I2C address?

In the register map for who I am register the reset value is 0x68 and the default value is 0x71. what's the good one?

this is my read register function

regards,
Arnaud

int MPU_readReg(char regId,unsigned int *val ){
unsigned int timeOut=10000;

/*txBuffer init*/
memset(txBuffer,0,3);
txBuffer[0]=(char)((0x68)|0x80);
txBuffer[1]=(char)(regId);
MPU_setupRXInt(rxBuffer,3);
MPU_sendBuffer(txBuffer,3);
while(MPURxBufferIndex!=3 && --timeOut);
if(!timeOut)
return -2; /*rx timeout */
*val=rxBuffer[2];
return 0;
}

osaft

11 years 3 months ago

It's ok I found some others post who detail the SPI access and success to do it;
Unlike mention on the datasheet there is no SPI address to send to the MPU.
I also do an error but the MPU documentation is really bad.

below my init code
with
MPU_SPI_BAUDRATE=1000000
WHO_AM_I=0x71

int MPU_Init(void){

unsigned int regData;
USART_InitSync_TypeDef init = USART_INITSYNC_DEFAULT;

GPIO_PinModeSet(MPU_SPI_CS_PORT, MPU_SPI_CS_PIN, gpioModePushPull, 1);
GPIO_PinModeSet(MPU_SPI_CLK_PORT, MPU_SPI_CLK_PIN, gpioModePushPull, 1);
GPIO_PinModeSet(MPU_SPI_MISO_PORT, MPU_SPI_MISO_PIN, gpioModeInput, 0);
GPIO_PinModeSet(MPU_SPI_MOSI_PORT, MPU_SPI_MOSI_PIN, gpioModePushPull, 1);
GPIO_PinModeSet(MPU_IRQ_PORT, MPU_IRQ_PIN, gpioModeInput, 0);

/* init MPU IRQ */
NVIC_EnableIRQ(GPIO_EVEN_IRQn);
/*rising edge interupt*/
GPIO_IntConfig(MPU_IRQ_PORT, MPU_IRQ_PIN, true, false, true);

/* Enable clock for USART1 */
CMU_ClockEnable(cmuClock_USART1, true);

init.baudrate = MPU_SPI_BAUDRATE;
init.databits = usartDatabits8;
init.msbf = true;
init.master = true;
init.clockMode = usartClockMode3;
init.prsRxEnable = 0;
init.autoTx = 0;

USART_InitSync(USART1, &init);
/* Enable signals TX, RX, CLK, CS */
USART1->ROUTE |= USART_ROUTE_TXPEN | USART_ROUTE_RXPEN | USART_ROUTE_CLKPEN ;

/*MPU reset */
MPU_writeReg(MPUREG_PWR_MGMT_1,BIT_H_RESET);
RTCDRV_Delay(100);
/*desable I2C bus for SPI*/
MPU_writeReg(MPUREG_USER_CTRL,BIT_I2C_IF_DIS);

/* init validation by reading WHO AM I register */
MPU_readReg(MPUREG_WHOAMI,&regData);
if (regData==WHO_AM_I) return 0;
return -1; /*who am i register invalid */


}




/* MPU_readReg */
/* read an internal register of the DW1000 */
int MPU_readReg(char regId,unsigned int *val ){
unsigned int timeOut=10000;

/*txBuffer init*/
memset(txBuffer,0,MPU_FRAME_LENGTH);
txBuffer[0]=(char)(regId|0x80);
MPU_setupRXInt(rxBuffer,MPU_FRAME_LENGTH);
MPU_sendBuffer(txBuffer,MPU_FRAME_LENGTH);
while(MPURxBufferIndex!=MPU_FRAME_LENGTH && --timeOut);
if(!timeOut)
return -2; /*rx timeout */
*val=rxBuffer[1];
return 0;
}

void MPU_writeReg(char regId,unsigned int val ){

txBuffer[0]=(char)regId;
txBuffer[1]=(char)val;
MPU_sendBuffer(txBuffer,MPU_FRAME_LENGTH);

}

phpbb Post ID
26172

allbridge

11 years 1 month ago

are you able to read compass data through SPI?

phpbb Post ID
26175
phpbb Topic ID
16824