HAL I2C Generic Driver
1.
Declare a I2C_HandleTypeDef handle structure, for example: I2C_HandleTypeDef
hi2c;
2.
Initialize the I2C low level resources by implement the HAL_I2C_MspInit() API:
a.
b.
c.
d.
3.
Configure the Communication Speed, Duty cycle, Addressing mode, Own Address1,
Dual Addressing mode, Own Address2, General call and Nostretch mode in the hi2c
Init structure.
4.
Initialize the I2C registers by calling the HAL_I2C_Init(), configures also the low level
Hardware (GPIO, CLOCK, NVIC...etc) by calling the customized
HAL_I2C_MspInit(&hi2c) API.
5.
To check if target device is ready for communication, use the function
HAL_I2C_IsDeviceReady()
6.
For I2C IO and IO MEM operations, three operation modes are available within this
driver :
Polling mode IO operation
Transmit in master mode an amount of data in blocking mode using
HAL_I2C_Master_Transmit()
Receive in master mode an amount of data in blocking mode using
HAL_I2C_Master_Receive()
Transmit in slave mode an amount of data in blocking mode using
HAL_I2C_Slave_Transmit()
Receive in slave mode an amount of data in blocking mode using
HAL_I2C_Slave_Receive()
Polling mode IO MEM operation
Write an amount of data in blocking mode to a specific memory address using
HAL_I2C_Mem_Write()
Read an amount of data in blocking mode from a specific memory address using
HAL_I2C_Mem_Read()
Interrupt mode IO operation
Transmit in master mode an amount of data in non blocking mode using
HAL_I2C_Master_Transmit_IT()
At transmission end of transfer HAL_I2C_MasterTxCpltCallback is executed and user
can add his own code by customization of function pointer
HAL_I2C_MasterTxCpltCallback
290/1371
Enable the I2Cx interface clock
I2C pins configuration
Enable the clock for the I2C GPIOs
Configure I2C pins as alternate function open-drain
NVIC configuration if you need to use interrupt process
Configure the I2Cx interrupt priority
Enable the NVIC I2C IRQ Channel
DMA Configuration if you need to use DMA process
Declare a DMA_HandleTypeDef handle structure for the transmit or receive
stream
Enable the DMAx interface clock using
Configure the DMA handle parameters
Configure the DMA Tx or Rx Stream
Associate the initialized DMA handle to the hi2c DMA Tx or Rx handle
Configure the priority and enable the NVIC for the transfer complete interrupt
on the DMA Tx or Rx Stream
DocID028236 Rev 2
UM1940
Need help?
Do you have a question about the STM32F2 and is the answer not in the manual?
Questions and answers