Difference between revisions of "STM32 HAL I²C"

From Stm32World Wiki
Jump to navigation Jump to search
Line 1: Line 1:
[[Category:STM32]][[Category:STM32 Development]][[Category:Black Pill]][[Category:Work in progress]]
+
[[Category:STM32]][[Category:STM32 Development]][[Category:Black Pill]][[Category:I²C]][[Category:Work in progress]]{{metadesc|I²C on STM32 using HAL}}
This page documents some of my findings experimenting with [[I2C]] using the [[STM32]] HAL.
+
This page documents some of my findings experimenting with [[I²C]] 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.
 
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.
Line 20: Line 20:
 
[[File:I2C bit stream.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 I²C initialization code:
  
 
<pre>
 
<pre>

Revision as of 03:23, 11 July 2021

This page documents some of my findings experimenting with I²C 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 I²C 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();
  }