cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F407 I2C Addressing Problem

Sany
Associate III

Hello!

I have a new project with a STM32F407VET6 and a STM32F103xxx over a RJ45 Jack with I2C.
i worked a lot of time with I2C EEPROMs and it works fine, but my communication between the 2 STM's are horrible, or i am too confused.

i found the tutorial on https://controllerstech.com/stm32-as-i2c-slave-part-1 

in this tutorial, has the Slave the Address 0x12 (without shifting <<1) 

the code of the master:

 

 

  /* USER CODE BEGIN 2 */

  uint16_t slaveADDR = 0x12<<1;
  uint8_t TxData[6] = {0x1, 0x2, 0x3, 0x4, 0x5, 0x6};
  while (1)
  {
	  HAL_I2C_Master_Transmit(&hi2c1, slaveADDR, TxData, 6, 1000);
	  HAL_Delay (1000);
  }

 

 

okay, here are the address shifted, but my slave are not receiving data on 0x24.

my logic analyzer says, the master sends data to the address 0x24 (it's shifted.. <<1) and not to 0x12. I used the function HAL_I2C_IsDeviceReady(), but i am wondering, in the description of this function are no comments like "shifted address" or any comment..

can anyone help me, what is right for the communication?

Is my slave address 0x12 (without shifting) right? and the master communicates to 0x12<<1 (also 0x24 ?) 

edit 1: in a STM32 Example on the fw lib, are the adresses 0x12F by the way....

 

i am confused..

Thank you! :)

1 ACCEPTED SOLUTION

Accepted Solutions
TDK
Guru

If this finds a device (i.e. returns HAL_OK):

HAL_I2C_IsDeviceReady(&hi2c2, 0x12, ...);

Then your 7-bit slave address is 0x09 and you should be passing the same value to HAL_I2C_Master_Transmit.

HAL_I2C_Master_Transmit(&hi2c2, 0x12, ...);

That doesn't line up with what you say you see on the logic analyzer, but that is how the library works.

Logic analyzer could be reporting an 8-bit address, but until you show that information (screenshot, etc.) we can only guess.

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

View solution in original post

9 REPLIES 9
TDK
Guru

> Is my slave address 0x12 (without shifting) right?

Yes, the code you show will work with a slave set with 0x12 as the 7-bit address. HAL functions expect the address to be left-shifted.

Slave needs to be active and waiting prior to the master sending data.

CubeMX repository examples can be a bit more reliable than others.

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

Thanks,

But the code doesn't work, or my slave is *** ?

heres my master code:

void i2c_function(void)
{
  i2c_scanner();
  uint8_t slave = 0x12;
  char *data = "Test";
  if(HAL_I2C_Transmit_IT(&hi2c2, (uint8_t*) data, (uint16_t) slave<<1, sizeof(data)) != HAL_OK)
       print("I2C send Error");
}

void MX_I2C2_Init(void)
{

  /* USER CODE BEGIN I2C2_Init 0 */

  /* USER CODE END I2C2_Init 0 */

  /* USER CODE BEGIN I2C2_Init 1 */

  /* USER CODE END I2C2_Init 1 */
  hi2c2.Instance = I2C2;
  hi2c2.Init.ClockSpeed = 100000;
  hi2c2.Init.DutyCycle = I2C_DUTYCYCLE_2;
  hi2c2.Init.OwnAddress1 = 0x08;
  hi2c2.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
  hi2c2.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
  hi2c2.Init.OwnAddress2 = 0;
  hi2c2.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
  hi2c2.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
  if (HAL_I2C_Init(&hi2c2) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN I2C2_Init 2 */

  /* USER CODE END I2C2_Init 2 */
}

int i2c_scanner()
{
	uint8_t i = 0;
	int found = 0;

	for(i=1; i<128; i++)
	{
		if(HAL_I2C_IsDeviceReady(&hi2c2, (uint16_t) i, 2, 50) == HAL_OK)
		{
			if(i2c_deviceFound(i))
			{
				found++;
			}
		}
	}

	if(found == 0)
		error = true;

	return found;
}

 my function i2c_scanner, founds my slave on address 0x12.., when i add the shifting to HAL_I2C_IsDeviceReady (uint16_t) (i<<1), my slave are not found...

the same on Transmit, when i send data to (uint16_t) slave<<1, i see over my analyzer the address is 0x24 and not 0x12...

TDK
Guru

If this finds a device (i.e. returns HAL_OK):

HAL_I2C_IsDeviceReady(&hi2c2, 0x12, ...);

Then your 7-bit slave address is 0x09 and you should be passing the same value to HAL_I2C_Master_Transmit.

HAL_I2C_Master_Transmit(&hi2c2, 0x12, ...);

That doesn't line up with what you say you see on the logic analyzer, but that is how the library works.

Logic analyzer could be reporting an 8-bit address, but until you show that information (screenshot, etc.) we can only guess.

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

Be aware that OLDER versions of CubeMX (I don't recall WHICH versions, but certainly 4.x) had you enter the I2C address as a "7-bit address in the lower 7 bits".  HAL_I2C_Init() then left shifted that value by 1 before writing to the OAR1 register.  Newer versions want the I2C address as "7-bit address in upper 7 bits" (i.e. left shifted by 1). And HAL_I2C_Init()  now writes that values directly to OAR1 (without shifting).

That tutorial you linked to has a copyright of 2017, so it was using the OLDER CubeMX version.  With newer CubeMX, make ALL of your I2C addresses be "7-bits << 1" in slave and master and you should be OK.

What I2C EEPROM are you using cite a specific part and data sheet?

How is it wired? How are the pins configured? External pull-ups? Schematic would be great

What library are you using

if(HAL_I2C_Transmit_IT(&hi2c2, (uint8_t*) data, (uint16_t) slave<<1, sizeof(data)) != HAL_OK)
print("I2C send Error");

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

Thank you, it's complicated with 7-Bit and 8-Bit Addresses...

But now it works. :)

Hey,

my EEPROM works fine...

I have the "standard" pull-ups with 4k7 on my circuit and 3.3V.. many poeples say thats to high for 3.3v?

Bob S
Principal

It depends partly on bus capacitance.  Use a scope to look at the rise time of your signals.  100KHz I2C is fairly tolerant of a wide range of resistor values.  That said, I typically use 1K to 2.2K at 3.3V

The actual I2C Address is a 7-bit address.  The 8th bit is the read/write bit and isn't considered part of the I2C Address.  Far too many confuse everyone (and themselves) by not using the proper terminology.