cancel
Showing results for 
Search instead for 
Did you mean: 

I2C2 hangs at while(!I2C_CheckEvent(LSM_I2C, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));

harinath
Associate III
Posted on May 18, 2012 at 06:21

I have STM32-P103 board which uses STM32F103RB micro. I connected an I2C sensor to I2C2 pins. STM32-P103 board, MPU6050 sensor breakout board schematics are below :

0690X000006052GQAQ.png I use only 4 pins of the sensor, SCL, SDA, Vcc, GND.I have the following Initialization code & reading code:

#define MPU6050_I2C I2C2
#define MPU6050_I2C_RCC_Periph RCC_APB1Periph_I2C2
#define MPU6050_I2C_Port GPIOB
#define MPU6050_I2C_SCL_Pin GPIO_Pin_10
#define MPU6050_I2C_SDA_Pin GPIO_Pin_11
#define MPU6050_I2C_RCC_Port RCC_APB2Periph_GPIOB
#define MPU6050_I2C_Speed 100000
void
MPU6050_I2C_Init(
void
)
{
I2C_InitTypeDef I2C_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
/* Enable I2C and GPIO clocks */
RCC_APB1PeriphClockCmd(MPU6050_I2C_RCC_Periph, ENABLE);
RCC_APB2PeriphClockCmd(MPU6050_I2C_RCC_Port, ENABLE);
/* Configure I2C pins: SCL and SDA */
GPIO_InitStructure.GPIO_Pin = MPU6050_I2C_SCL_Pin | MPU6050_I2C_SDA_Pin;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;
GPIO_Init(MPU6050_I2C_Port, &GPIO_InitStructure);
I2C_DeInit(MPU6050_I2C);
/* I2C Peripheral Enable */
I2C_Cmd(MPU6050_I2C, ENABLE);
/* I2C configuration */
I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;
I2C_InitStructure.I2C_OwnAddress1 = 0x68;
// This is sensor address
I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
I2C_InitStructure.I2C_ClockSpeed = MPU6050_I2C_Speed;
/* Apply I2C configuration after enabling it */
I2C_Init(MPU6050_I2C, &I2C_InitStructure);
}
void
I2C_BufferRead(u8 slAddr, u8* pBuffer, u8 ReadAddr, u16 NumByteToRead)
{
// ENTR_CRT_SECTION();
/* While the bus is busy */
while
(I2C_GetFlagStatus(MPU6050_I2C, I2C_FLAG_BUSY));
/* Send START condition */
I2C_GenerateSTART(MPU6050_I2C, ENABLE);
/* Test on EV5 and clear it */
while
(!I2C_CheckEvent(MPU6050_I2C, I2C_EVENT_MASTER_MODE_SELECT));
/* Send 
MPU6050 address for write */
I2C_Send7bitAddress(MPU6050_I2C, slAddr, I2C_Direction_Transmitter);
/* Test on EV6 and clear it */
while
(!I2C_CheckEvent(MPU6050_I2C, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
/* Clear EV6 by setting again the PE bit */
I2C_Cmd(MPU6050_I2C, ENABLE);
/* Send the MPU6050_Magn's internal address to write to */
I2C_SendData(MPU6050_I2C, ReadAddr);
/* Test on EV8 and clear it */
while
(!I2C_CheckEvent(MPU6050_I2C, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
/* Send STRAT condition a second time */
I2C_GenerateSTART(MPU6050_I2C, ENABLE);
/* Test on EV5 and clear it */
while
(!I2C_CheckEvent(MPU6050_I2C, I2C_EVENT_MASTER_MODE_SELECT));
/* Send MPU6050 address for read */
I2C_Send7bitAddress(MPU6050_I2C, slAddr, I2C_Direction_Receiver);
/* Test on EV6 and clear it */
while
(!I2C_CheckEvent(MPU6050_I2C, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));
/* While there is data to be read */
while
(NumByteToRead)
{
if
(NumByteToRead == 1)
{
/* Disable Acknowledgement */
I2C_AcknowledgeConfig(MPU6050_I2C, DISABLE);
/* Send STOP Condition */
I2C_GenerateSTOP(MPU6050_I2C, ENABLE);
}
/* Test on EV7 and clear it */
if
(I2C_CheckEvent(MPU6050_I2C, I2C_EVENT_MASTER_BYTE_RECEIVED))
{
/* Read a byte from the MPU6050 */
*pBuffer = I2C_ReceiveData(MPU6050_I2C);
/* Point to the next location where the byte read will be saved */
pBuffer++;
/* Decrement the read bytes counter */
NumByteToRead--;
}
}
/* Enable Acknowledgement to be ready for another reception */
I2C_AcknowledgeConfig(MPU6050_I2C, ENABLE);
// EXT_CRT_SECTION();
}

I call the I2C_BufferRead function just after initialization.Execution hangs at

while
(!I2C_CheckEvent(MPU6050_I2C, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));

Any helpappreciated. Thank you #stm32f103rb #i2c2
10 REPLIES 10
harinath
Associate III
Posted on May 22, 2012 at 08:18 @fm: Thanks for suggestion. I used the 1-bit left shifted 7-bit address, It works now. The actuall 7-bit address of MPU6050 is 0x68, the 1-bit left shifted address is 0xD0. I used this in the following place & in other calling functions.

I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;
I2C_InitStructure.I2C_OwnAddress1 = 0xD0; 
// it was 0x68 
I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
I2C_InitStructure.I2C_ClockSpeed = MPU6050_I2C_Speed; 
/* Apply I2C configuration after enabling it */
I2C_Init(MPU6050_I2C, &I2C_InitStructure);

uint8_t b;
/*read 1 byte */
MPU6050_I2C_BufferRead(devAddr, &b, regAddr, 1); 
// devAddr=0xD0

Now the problem is solved. I have to validate the data which is been read. Thank you all.