cancel
Showing results for 
Search instead for 
Did you mean: 

Problem with L3G4200 Sensor using I2C

bassem.saleh93
Associate II

I have a problem interfacing with L3G4200 using I2C on STM32F469 Discovery. Here is my code to config the I2C

void I2CConfig(void){
	I2CHandle1.Instance =  I2Cx;
	I2CHandle1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
	I2CHandle1.Init.ClockSpeed = 100000;
	I2CHandle1.Init.DutyCycle = I2C_DUTYCYCLE_2;
	I2CHandle1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
	I2CHandle1.Init.OwnAddress1 = 0xD0;
	I2CHandle1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
	I2CHandle1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
	if (HAL_I2C_Init(&I2CHandle1) != HAL_OK)
		Error_Handler();
}

And here is the code to initialize the I2C on HAL_Init file

void HAL_I2C_MspInit(I2C_HandleTypeDef *hi2c){
	GPIO_InitTypeDef  GPIO_InitStruct;
 
	/*##-1- Enable peripherals and GPIO Clocks #################################*/
	/* Enable GPIO TX/RX clock */
	I2Cx_SCL_GPIO_CLK_ENABLE();
	I2Cx_SDA_GPIO_CLK_ENABLE();
	/* Enable I2Cx clock */
	I2Cx_CLK_ENABLE();
 
	/*##-2- Configure peripheral GPIO ##########################################*/
	/* I2C TX GPIO pin configuration  */
	GPIO_InitStruct.Pin       = I2Cx_SCL_PIN;
	GPIO_InitStruct.Mode      = GPIO_MODE_AF_PP;
	GPIO_InitStruct.Pull	  =	GPIO_PULLUP;
	GPIO_InitStruct.Speed     = GPIO_SPEED_FAST;
	GPIO_InitStruct.Alternate = I2Cx_SCL_SDA_AF;
	HAL_GPIO_Init(I2Cx_SCL_GPIO_PORT, &GPIO_InitStruct);
 
	/* I2C RX GPIO pin configuration  */
	GPIO_InitStruct.Pin       = I2Cx_SDA_PIN;
	GPIO_InitStruct.Alternate = I2Cx_SCL_SDA_AF;
	HAL_GPIO_Init(I2Cx_SDA_GPIO_PORT, &GPIO_InitStruct);
}

I can't figure out where is the problem because I successfully interfaced i2c using another sensor (BMP180) and also interfaced SPI with the L3G4200 with the same board.

1 ACCEPTED SOLUTION

Accepted Solutions
Winfred LU
ST Employee

Hi,

I2C is an open drain interface, neither the master nor the slave shall pull the bits high.

External pull-up resistors are required for both I2C clock and data pins. The pins can only be pulled down when needed.

So, please instead use:

GPIO_InitStruct.Mode      = GPIO_MODE_AF_OD;
GPIO_InitStruct.Pull	  = GPIO_NOPULL;

Best Regards,

Winfred

View solution in original post

1 REPLY 1
Winfred LU
ST Employee

Hi,

I2C is an open drain interface, neither the master nor the slave shall pull the bits high.

External pull-up resistors are required for both I2C clock and data pins. The pins can only be pulled down when needed.

So, please instead use:

GPIO_InitStruct.Mode      = GPIO_MODE_AF_OD;
GPIO_InitStruct.Pull	  = GPIO_NOPULL;

Best Regards,

Winfred