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:Work in progress]]
+
[[Category:STM32]][[Category:STM32 Development]][[Category:Black Pill]][[Category:Work in progress]]
 
This page documents some of my findings experimenting with [[I2C]] using the [[STM32]] HAL.
 
This page documents some of my findings experimenting with [[I2C]] using the [[STM32]] HAL.
  

Revision as of 04:32, 3 June 2021

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();
  }