STM32 HAL I²C
Jump to navigation
Jump to search
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.
The clock on both board was configured identically:
I2C was also configured identically with the exception of the slave address:
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:
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(); }