cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F072RbT6 I2C1 not working.

YGehl.1
Associate II

Hello,

I am using STMCUBEMX 5.2.1 to initialize the configurations of my STM32F072RBT6. My application requires the use of i2c1 since i2c2 is already occupied on our board by a pressure sensor. However, it seems to me that i2c1 is not working (i.e. the MCU does not reset).

I feel it has something to be with i2c1 being used to take MCU out of the STOP condition. I'm not sure how to go on about it since I'm new to this.

I already have a working i2c2 hence I'm sure I'm not doing anything wrong on how to read the sensor.

It'll be of great help if someone could answer the same.

Thanks in advance!

Yaagyanika Gehlot.

5 REPLIES 5
TDK
Guru

> I already have a working i2c2 hence I'm sure I'm not doing anything wrong

It's unlikely that ST fielded a chip with an I2C peripheral that just doesn't work. The problem is likely in your code.

Try it out on a minimal example to convince yourself that I2C1 works just fine. And if it doesn't work on a minimal example, post the code here.

Make sure the I2C1 pins are free and only tied to other I2C components on the board.

> However, it seems to me that i2c1 is not working (i.e. the MCU does not reset).

Why would I2C1 reset the MCU?

If you feel a post has answered your question, please click "Accept as Solution".

I was looking through the datasheet and found this. Maybe I wasn't able to put in words properly about i2c1 being used to reset MCU.

0693W000003BmnAQAS.png

Also,

I'm posting the code I'm using to read.

The same thing works if I connect the sensor on i2c2 line and change the hi2c1 to hi2c2 in line 60.

void MX_I2C1_Init(void)
{
  hi2c1.Instance = I2C1;
  hi2c1.Init.Timing = 0x2000090E;
  hi2c1.Init.OwnAddress1 = 0;
  hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
  hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
  hi2c1.Init.OwnAddress2 = 0;
  hi2c1.Init.OwnAddress2Masks = I2C_OA2_NOMASK;
  hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
  hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
  if (HAL_I2C_Init(&hi2c1) != HAL_OK)
  {
    Error_Handler();
  }
  /** Configure Analogue filter */
  if (HAL_I2CEx_ConfigAnalogFilter(&hi2c1, I2C_ANALOGFILTER_ENABLE) != HAL_OK)
  {
    Error_Handler();
  }
  /** Configure Digital filter */
  if (HAL_I2CEx_ConfigDigitalFilter(&hi2c1, 0) != HAL_OK)
  {
    Error_Handler();
  }
}
 
 
void MX_I2C2_Init(void)
{
  hi2c2.Instance = I2C2;
  hi2c2.Init.Timing = 0x2000090E;
  hi2c2.Init.OwnAddress1 = 0;
  hi2c2.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
  hi2c2.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
  hi2c2.Init.OwnAddress2 = 0;
  hi2c2.Init.OwnAddress2Masks = I2C_OA2_NOMASK;
  hi2c2.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
  hi2c2.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
  if (HAL_I2C_Init(&hi2c2) != HAL_OK)
  {
    Error_Handler();
  }
  /** Configure Analogue filter 
  */
  if (HAL_I2CEx_ConfigAnalogFilter(&hi2c2, I2C_ANALOGFILTER_ENABLE) != HAL_OK)
  {
    Error_Handler();
  }
  /** Configure Digital filter 
  */
  if (HAL_I2CEx_ConfigDigitalFilter(&hi2c2, 0) != HAL_OK)
  {
    Error_Handler();
  }
}
 
double Read_Current_Sensor(void)
	{
		HAL_I2C_Master_Receive(&hi2c1,0x48<<1,buffer_receieve,2,100);  //	Recieve data from the sensor and store it in buffer[0] and buffer[1]
		rawADC = buffer_receieve[0]<<8 | buffer_receieve[1];			     //	Combine 2 8-bit into 1 16bit
		Curr_Sensor_ADC_Value = ((rawADC * FSR) / 32767); 			     //	((raw * FSR) / ADC Resolution) 
		return Curr_Sensor_ADC_Value;
	}

If the i2c1 is pulled up to 3.3.V (with 4.7k resistors) on the board, the code does not even flash in the MCU.

It still stores the old HEX file.

Once I remove the pull up resistors, I'm able to flash the code, reset it from Keil Debug mode and then Run.

I hope there won't be any miscommunication.

Thanks in advance!

Yaagyanika Gehlot.

Which pins are you using for I2C? Is this your board or a "known good" e.g. Nucleo or Disco? What is the I2C connected to? If your board, aren't there shorts from the I2C to neighbouring pins/tracks?

JW

> Which pins are you using for I2C?

PB8 (Pin 61): i2C1_SCL

PB9 (Pin 62): i2C1_SDA

Both pulled up to 3.3V using 4.7k resistors.

> Is this your board or a "known good" e.g. Nucleo or Disco?

It's my own designed board.

> What is the I2C connected to?

I2C is connected to an ADS1114 (16-bit ADC).

> If your board, aren't there shorts from the I2C to neighbouring pins/tracks? 

There is no short among neighbouring pins, I checked for each of them.

TDK
Guru

> If the i2c1 is pulled up to 3.3.V (with 4.7k resistors) on the board, the code does not even flash in the MCU.

That's not how it should behave. There has to be an issue somewhere causing this. I wouldn't worry about debugging code until you find/fix the hardware issue.

If you feel a post has answered your question, please click "Accept as Solution".