AnsweredAssumed Answered

To get data from Invensense MPU9250 with I2C

Question asked by kano.shori on May 26, 2015
I'm trying to get data from Invensense MPU9250 with I2C on SC1 of stm32w108.
These are connected each other with 10k ohm register.
But it doesn't get data.

I coded as below.
Please tell me how to fix.

void SCinit(){
        GPIO_InitTypeDef GPIO_InitStruct;
        I2C_InitTypeDef I2C_InitStruct;
        
        GPIO_InitStruct.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_2;
        GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_OD;
        
        I2C_InitStruct.I2C_ClockRate = 400000;
        
        GPIO_Init(GPIOB, &GPIO_InitStruct);
        I2C_Init(SC1_I2C, &I2C_InitStruct);
        
        I2C_Cmd(SC1_I2C, ENABLE);
}

void MPU9250_initialize(){
     MPU9250_setClockSource(MPU9250_CLOCK_PLL_XGYRO);

     MPU9250_setFullScaleGyroRange(MPU9250_GYRO_FS_250);

     MPU9250_setFullScaleAccelRange(MPU9250_ACCEL_FS_2);

     MPU9250_setSleepModeStatus(0);
}

void MPU9250_setClockSource(uint8_t source){
     MPU9250_writeBits(MPU9250_RA_PWR_MGMT_1, MPU9250_PWR1_BIT_CLOCK_SELECT, MPU9250_PWR1_BIT_CLKSEL_LENGTH, source);
}

void MPU9250_setFullScaleGyroRange(uint8_t range){
     MPU9250_writeBits(MPU9250_RA_GYRO_CONFIG, MPU9250_RANGE_CONFIG_START, MPU9250_RANGE_CONFIG_LENGTH, range);
}

void MPU9250_setFullScaleAccelRange(uint8_t range){
     MPU9250_writeBits(MPU9250_RA_ACCEL_CONFIG, MPU9250_RANGE_CONFIG_START, MPU9250_RANGE_CONFIG_LENGTH, range);
}

void MPU9250_setSleepModeStatus(uint8_t status){
     MPU9250_writeBit(MPU9250_RA_PWR_MGMT_1, MPU9250_PWR1_BIT_SLEEP, status);
}

void MPU9250_writeBits(uint8_t regAddr, uint8_t bitStart, uint8_t bitLength, uint8_t data){
     uint8_t buff;
     uint8_t mask;

     MPU9250_i2C_BufferRead(&buff, regAddr, 1);

     mask = 1 << bitStart;
     mask--;
     mask <<= bitLength;

     data <<= bitStart;
     data &= mask;

     buff &= ~mask;

     buff |= data;

     MPU9250_i2C_ByteWrite(buff, regAddr);
}

void MPU9250_writeBit(uint8_t regAddr, uint8_t bitNum, uint8_t value){
     uint8_t buff;
     uint8_t mask;


     MPU9250_i2C_BufferRead(&buff, regAddr, 1);

     mask = 1 << bitNum;
     buff = (value != 0) ? (buff | mask) : (buff & ~mask);

     MPU9250_i2C_ByteWrite(buff, regAddr);
}



void MPU9250_getRawGyro(uint8_t* data){
     MPU9250_i2C_BufferRead(data, MPU9250_RA_GYRO_XOUT_H, 6);
        //MPU9250_i2C_BufferRead(data, MPU9250_RA_PWR_MGMT_1, 6);
}

void MPU9250_getRawAccel(uint8_t* data){
     MPU9250_i2C_BufferRead(data, MPU9250_RA_ACCEL_XOUT_H, 6);
}

void MPU9250_getRaw6axis(uint8_t* data){
     MPU9250_getRawAccel(data);
     
     MPU9250_getRawGyro(data + 6);
}

void MPU9250_i2C_BufferRead(uint8_t* buff, uint8_t regAddr, uint8_t num){
     I2C_GenerateSTART(SC1_I2C);
        
        while (!I2C_GetFlagStatus(SC1_I2C, I2C_FLAG_CMDFIN));


     I2C_Send7bitAddress(SC1_I2C, MPU9250_DEFAULT_SLAVE_ADDRESS, I2C_Direction_Transmitter);

     while (!I2C_GetFlagStatus(SC1_I2C, I2C_FLAG_BTF));

     I2C_SendData(SC1_I2C, regAddr);

     while (!I2C_GetFlagStatus(SC1_I2C, I2C_FLAG_BTF));
                
     I2C_GenerateSTART(SC1_I2C);

        while (!I2C_GetFlagStatus(SC1_I2C, I2C_FLAG_CMDFIN));
        
     I2C_Send7bitAddress(SC1_I2C, MPU9250_DEFAULT_SLAVE_ADDRESS, I2C_Direction_Receiver);

     while (!I2C_GetFlagStatus(SC1_I2C, I2C_FLAG_BTF));
        
        uint8_t count;
        
        for(count = 0; count < num - 1; count++){
          buff[count] = I2C_ReceiveData(SC1_I2C);
                
                while(!I2C_GetFlagStatus(SC1_I2C, I2C_FLAG_BRF));
                
                I2C_AcknowledgeConfig(SC1_I2C, ENABLE);
        }
        
        buff[num - 1] = I2C_ReceiveData(SC1_I2C);
        
        while(!I2C_GetFlagStatus(SC1_I2C, I2C_FLAG_BRF));
        
        I2C_AcknowledgeConfig(SC1_I2C, DISABLE);
        I2C_GenerateSTOP(SC1_I2C);
        
        I2C_AcknowledgeConfig(SC1_I2C, ENABLE);
}

void MPU9250_i2C_ByteWrite(uint8_t data, uint8_t regAddr){
     I2C_GenerateSTART(SC1_I2C);
        
        while (!I2C_GetFlagStatus(SC1_I2C, I2C_FLAG_CMDFIN));

     I2C_Send7bitAddress(SC1_I2C, MPU9250_DEFAULT_SLAVE_ADDRESS, I2C_Direction_Transmitter);

     while (!I2C_GetFlagStatus(SC1_I2C, I2C_FLAG_BTF));

     I2C_SendData(SC1_I2C, regAddr);

     while (!I2C_GetFlagStatus(SC1_I2C, I2C_FLAG_BTF));

     I2C_SendData(SC1_I2C, data);

     while (!I2C_GetFlagStatus(SC1_I2C, I2C_FLAG_BTF));

     I2C_GenerateSTOP(SC1_I2C);
}


main() of my source calls MPU9250_getRaw6axis() ciclicity.

And I2C_ReceiveData() always returns 0 any register i choose.


I apologize that I am biginner of micro controller and not good for english.

Outcomes