Difference between revisions of "STM32 HAL I²C"

From Stm32World Wiki
Jump to navigation Jump to search
Line 12: Line 12:
 
I2C was also configured identically with the exception of the slave address:
 
I2C was also configured identically with the exception of the slave address:
  
[[File:I2C Play Clock Settings.png|600px]]
+
[[File:I2C Configuration.png|600px]]
  
 
On the intended "master" the address was configured to 0x2 and on the intended slave it was configured to 0x3.
 
On the intended "master" the address was configured to 0x2 and on the intended slave it was configured to 0x3.
Line 18: Line 18:
 
One thing worth noticing with [[I2C]] is that the addresses should be shifted left:
 
One thing worth noticing with [[I2C]] is that the addresses should be shifted left:
  
[[File:I2C Play Clock Settings.png|600px]]
+
[[File:I2C bit stream.png|600px]]
  
 
So, STM32CubeMX will - based on above configuration, create the following I2C initialization code:
 
So, STM32CubeMX will - based on above configuration, create the following I2C initialization code:

Revision as of 07:07, 7 December 2020

This page documents some of my findings experimenting with I2C using the STM32 HAL.

For this exercise I used two Black Pill boards with a jumper lead going from PB6 and PB7 to the same pins on the other board.

I2C Connection.jpg

The clock on both board was configured identically:

I2C Play Clock Settings.png

I2C was also configured identically with the exception of the slave address:

I2C Configuration.png

On the intended "master" the address was configured to 0x2 and on the intended slave it was configured to 0x3.

One thing worth noticing with I2C is that the addresses should be shifted left:

I2C bit stream.png

So, STM32CubeMX will - based on above configuration, create the following I2C initialization code:

  hi2c1.Instance = I2C1;
  hi2c1.Init.ClockSpeed = 100000;
  hi2c1.Init.DutyCycle = I2C_DUTYCYCLE_2;
  hi2c1.Init.OwnAddress1 = 4;
  hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
  hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
  hi2c1.Init.OwnAddress2 = 0;
  hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
  hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_ENABLE;
  if (HAL_I2C_Init(&hi2c1) != HAL_OK)
  {
    Error_Handler();
  }