cancel
Showing results for 
Search instead for 
Did you mean: 

I2C does not work - no data on I2C pin

Stastny.Petr
Associate III

Hello, I have a problem to communicate with 24LC02B memory. If I look with the logic analyzer, no data on CLK and DATA pins. Here is my code. What is wrong?

Thank you

#define		I2C1_DEVICE_ADDRESS 		0x50
#define		MEMORY_ADDRESS			 		0x08
uint8_t xBuffer[1];
 
  __HAL_RCC_GPIOB_CLK_ENABLE();
  __HAL_RCC_AFIO_CLK_ENABLE();
 
	GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
	GPIO_InitStruct.Pull = GPIO_PULLUP;
	GPIO_InitStruct.Pin = I2C_SDA_Pin;
  HAL_GPIO_Init(I2C_SDA_Port, &GPIO_InitStruct);	
	
	GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
	GPIO_InitStruct.Pull = GPIO_PULLUP;
	GPIO_InitStruct.Pin = I2C_SCL_Pin;
  HAL_GPIO_Init(I2C_SCL_Port, &GPIO_InitStruct);
 
 
	I2C_EEPROM.Instance             = I2C1;
	I2C_EEPROM.Init.ClockSpeed      = 100000;
	I2C_EEPROM.Init.DutyCycle       = I2C_DUTYCYCLE_2;
	I2C_EEPROM.Init.OwnAddress1     = 0x00;
	I2C_EEPROM.Init.OwnAddress2     = 0x00;
	I2C_EEPROM.Init.AddressingMode  = I2C_ADDRESSINGMODE_7BIT;
	I2C_EEPROM.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
	I2C_EEPROM.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
	I2C_EEPROM.Init.NoStretchMode   = I2C_NOSTRETCH_DISABLE;
	if(HAL_I2C_Init(&I2C_EEPROM) != HAL_OK)
	{
		Error_Handler();
	}
	__HAL_RCC_I2C1_CLK_ENABLE();
	
...............
 
bool EEPROM24XX_IsConnected(void) {
	if(HAL_I2C_IsDeviceReady(&I2C_EEPROM,(uint16_t) I2C1_DEVICE_ADDRESS<<1,1,100)==HAL_OK)
		return true;
	else
		return false;	
}
 
bool EEPROM24XX_Save(void) {
	xBuffer[0] = 55; // for test only
	if(HAL_I2C_Mem_Write(&I2C_EEPROM,(uint16_t) I2C1_DEVICE_ADDRESS<<1,MEMORY_ADDRESS,1,xBuffer,1,100) == HAL_OK) {
		HAL_Delay(10);
		return true;
	}
	else return false;
}
 
bool EEPROM24XX_Load(void) { // should read 55
	if(HAL_I2C_Mem_Read(&I2C_EEPROM,(uint16_t) I2C1_DEVICE_ADDRESS<<1,MEMORY_ADDRESS,1,xBuffer,1,100) == HAL_OK) {
		return true;
	}	else return false;		
}

response of EEPROM24XX_IsConnected os always false, and the same also WRITE function,

1 ACCEPTED SOLUTION

Accepted Solutions

What STM32 part?

You need to specify the AF mux setting on most STM32 family parts.

GPIO_InitStruct.Alternate = I2Cx_SCL_AF;

And enable the I2C peripheral clock before you initialize the peripheral, synchronous logic sensitive to that.

Inspect the RCC, GPIO and I2C peripheral registers in the debugger to confirm you've got the settings straight.

/* Definition for I2Cx Pins */

#define I2Cx_SCL_PIN                   GPIO_PIN_8

#define I2Cx_SCL_GPIO_PORT             GPIOB

#define I2Cx_SCL_AF                    GPIO_AF4_I2C1

#define I2Cx_SDA_PIN                   GPIO_PIN_9

#define I2Cx_SDA_GPIO_PORT             GPIOB

#define I2Cx_SDA_AF                    GPIO_AF4_I2C1

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

View solution in original post

5 REPLIES 5

What STM32 part?

You need to specify the AF mux setting on most STM32 family parts.

GPIO_InitStruct.Alternate = I2Cx_SCL_AF;

And enable the I2C peripheral clock before you initialize the peripheral, synchronous logic sensitive to that.

Inspect the RCC, GPIO and I2C peripheral registers in the debugger to confirm you've got the settings straight.

/* Definition for I2Cx Pins */

#define I2Cx_SCL_PIN                   GPIO_PIN_8

#define I2Cx_SCL_GPIO_PORT             GPIOB

#define I2Cx_SCL_AF                    GPIO_AF4_I2C1

#define I2Cx_SDA_PIN                   GPIO_PIN_9

#define I2Cx_SDA_GPIO_PORT             GPIOB

#define I2Cx_SDA_AF                    GPIO_AF4_I2C1

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Stastny.Petr
Associate III

Hi, STM32F103RB. I believe I do not need this here

Still going to enable the clock earlier. ​

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Stastny.Petr
Associate III

Now it is just after pin init. No change. Is possible that HAL_I2C_Mem_Read is not compatible with 24LC02B memory? Anyway, still no data on scope during I2C communication.

AvaTar
Lead

Do you see anything at all with the scope ?

Is the slave address on the bus correct ?

And does the slave ACKnowledge it ?

If nothing at all shows up, the problem is (as Clive pointed out) your initialization.