STM32Cube  Version 2.1.0
Create Projects for STM32U5 Series with STM32Cube HAL and STM32CubeMX
I2C

Revision History

  • Version 1.1
    • Updated PowerControl function (added check if Instance is valid)
    • Updated Control function implementation for transfer abort (added check for master mode)
  • Version 1.0
    • Initial release

Capabilities

This driver supports Master and Slave modes.

Instances

Hardware resource relating to driver instance is shown in the table below:

Driver Instance Hardware Resource
Driver_I2C1 I2C1
Driver_I2C2 I2C2
Driver_I2C3 I2C3
Driver_I2C4 I2C4
Driver_I2C5 I2C5
Driver_I2C6 I2C6

Limitations

Hardware limitations:

  • I2C3 with LPDMA1 can access only SRAM4 memory. If LPDMA1 is used on I2C3 ensure that Tx and Rx buffers are be positioned in SRAM4 memory.

Configuration

Compile-time

Definitions used for compile-time configuration of this driver are shown in the table below:

Definition Default value Value Description
I2C1_ANF_ENABLE 0 0 I2C1 analog noise filter: disabled
1 I2C1 analog noise filter: enabled
I2C2_ANF_ENABLE 0 0 I2C2 analog noise filter: disabled
1 I2C2 analog noise filter: enabled
I2C3_ANF_ENABLE 0 0 I2C3 analog noise filter: disabled
1 I2C3 analog noise filter: enabled
I2C4_ANF_ENABLE 0 0 I2C4 analog noise filter: disabled
1 I2C4 analog noise filter: enabled
I2C5_ANF_ENABLE 0 0 I2C5 analog noise filter: disabled
1 I2C5 analog noise filter: enabled
I2C6_ANF_ENABLE 0 0 I2C6 analog noise filter: disabled
1 I2C6 analog noise filter: enabled
I2C1_DNF_COEFFICIENT 0 0 I2C1 digital noise filter coefficient: disabled
1..15 I2C1 digital noise filter coefficient number
I2C2_DNF_COEFFICIENT 0 0 I2C2 digital noise filter coefficient: disabled
1..15 I2C2 digital noise filter coefficient number
I2C3_DNF_COEFFICIENT 0 0 I2C3 digital noise filter coefficient: disabled
1..15 I2C3 digital noise filter coefficient number
I2C4_DNF_COEFFICIENT 0 0 I2C4 digital noise filter coefficient: disabled
1..15 I2C4 digital noise filter coefficient number
I2C5_DNF_COEFFICIENT 0 0 I2C5 digital noise filter coefficient: disabled
1..15 I2C5 digital noise filter coefficient number
I2C6_DNF_COEFFICIENT 0 0 I2C6 digital noise filter coefficient: disabled
1..15 I2C6 digital noise filter coefficient number

STM32CubeMX

The I2C driver requires:

  • I2C peripheral clock
  • I2C peripheral configured as I2C
  • I2C SCL and SDA pins
  • optional DMA configuration for transfers

B-U585I-IOT02A Board Configuration

These settings are relevant for this board, for different board please refer to the board schematic for proper configuration.

Link to B-U585I-IOT02A board schematic.

STM32CubeMX

Required peripherals for the B-U585I-IOT02A board are listed in the table below:

Peripheral Mode Description
I2C1 I2C I2C1 Controller
PB6 Alternate Function Push Pull I2C1 SCL Pin
PB7 Alternate Function Push Pull I2C1 SDA Pin
Note
All settings have to be configured as described in the procedure below. Important settings, typically the ones different from default, are emphasized in bold.

Pinout & Configuration tab

  1. In the Pinout view window click on a pin and select it's functionality:
    Pin Functionality
    PB8 I2C1_SCL
    PB9 I2C1_SDA

  2. Under Categories: Connectivity select I2C1:

    Mode:

    • Mode: I2C

    Configuration:

    • Parameter Settings: not used
    • User Constants: not used
    • NVIC Settings: configured in later step (under Category: System Core: NVIC)
    • DMA Settings: configured in later step (under Category: System Core: GPDMA1)
    • GPIO Settings:
      Pin Name Signal on Pin Pin Context.. GPIO output.. GPIO mode GPIO Pull-up/Pull.. Maximum out.. Fast Mode User Label
      PB6 I2C1_SCL n/a n/a Alternate Function Push Pull No pull-up and no.. Low n/a .
      PB7 I2C1_SDA n/a n/a Alternate Function Push Pull No pull-up and no.. Low n/a .

  3. Under Categories: System Core select GPDMA1:

    Mode:

    • Channel 0 - 2 Words Internal FIFO: Standard Request Mode
    • Channel 1 - 2 Words Internal FIFO: Standard Request Mode

    Configuration:

    • All Channels: not used
    • Security:
      • CH1:
        • Enable Channel as Privileged: NON PRIVILEGED
      • CH0:
        • Enable Channel as Privileged: NON PRIVILEGED
    • CH0:
      • Circular configuration:
        • Circular Mode: Disable
      • Request Configuration:
        • Request: I2C1_TX
        • DMA Handle in IP Structure: hdmatx
        • Block HW request protocol: Single/Burst Level
      • Channel configuration:
        • Priority: Low
        • Transaction Mode: Normal
        • Direction: Memory to Peripheral
      • Source Data Setting:
        • Source Address Increment After Transfer: Enabled
        • Data Width: Byte
        • Burst Length: 1
        • Allocated Port for Transfer: Port 0
      • Destination Data Setting:
        • Destination Address Increment After Transfer: Disabled
        • Data Width: Byte
        • Burst Length: 1
        • Allocated Port for Transfer: Port 0
      • Data Handling:
        • Data Handling Configuration: Disable
      • Trigger:
        • Trigger Configuration: Disable
      • Transfer Event Configuration:
        • Transfer Event Generation: The TC (and the HT) ...
    • CH1:
      • Circular configuration:
        • Circular Mode: Disable
      • Request Configuration:
        • Request: I2C1_RX
        • DMA Handle in IP Structure: hdmarx
        • Block HW request protocol: Single/Burst Level
      • Channel configuration:
        • Priority: Low
        • Transaction Mode: Normal
        • Direction: Peripheral to Memory
      • Source Data Setting:
        • Source Address Increment After Transfer: Disabled
        • Data Width: Byte
        • Burst Length: 1
        • Allocated Port for Transfer: Port 1
      • Destination Data Setting:
        • Destination Address Increment After Transfer: Enabled
        • Data Width: Byte
        • Burst Length: 1
        • Allocated Port for Transfer: Port 1
      • Data Handling:
        • Data Handling Configuration: Disable
      • Trigger:
        • Trigger Configuration: Disable
      • Transfer Event Configuration:
        • Transfer Event Generation: The TC (and the HT) ...
    • User Constants: not used

  4. Under Categories: System Core select NVIC:

    Configuration:

    • NVIC:
      NVIC Interrupt Table Enabled Preemption Priority Sub Priority
      GPDMA1 Channel 0 global interrupt checked 0 0
      GPDMA1 Channel 1 global interrupt checked 0 0
      I2C1 event interrupt checked 0 0
      I2C1 error interrupt checked 0 0
    • Code generation:
      Enabled interrupt table Select for.. Generate Enable in.. Generate IRQ h.. Call HAL handler
      GPDMA1 Channel 0 global interrupt unchecked checked checked checked
      GPDMA1 Channel 1 global interrupt unchecked checked checked checked
      I2C1 event interrupt unchecked checked checked checked
      I2C1 error interrupt unchecked checked checked checked

Clock Configuration tab

  1. Configure To I2C1 (MHz): 160

Project Manager tab

  1. Under Advanced Settings:

    Generated Function Calls:

    Generate Code Function Name Peripheral Inst.. Do not generate .. Visibility (Static)
    checked MX_I2C1_Init I2C1 checked checked

Generate Code

Generate source code by clicking on the GENERATE CODE button on the toolbar.