Difference between revisions of "STM32 HAL I²C"
Jump to navigation
Jump to search
(Created page with "Category:STM32Category:STM32 DevelopmentCategory:Work in progress This page documents some of my findings") |
m (Lth moved page STM32 HAL I2C to STM32 HAL I²C) |
||
(6 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
− | [[Category:STM32]][[Category:STM32 Development]][[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 | + | 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. | ||
+ | |||
+ | [[File:I2C Connection.jpg|400px]] | ||
+ | |||
+ | The clock on both board was configured identically: | ||
+ | |||
+ | [[File:I2C Play Clock Settings.png|600px]] | ||
+ | |||
+ | I2C was also configured identically with the exception of the slave address: | ||
+ | |||
+ | [[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. | ||
+ | |||
+ | One thing worth noticing with [[I2C]] is that the addresses should be shifted left: | ||
+ | |||
+ | [[File:I2C bit stream.png|600px]] | ||
+ | |||
+ | So, STM32CubeMX will - based on above configuration, create the following I²C initialization code: | ||
+ | |||
+ | <pre> | ||
+ | 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(); | ||
+ | } | ||
+ | </pre> |
Latest revision as of 02:26, 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.
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 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(); }