Get-To-Byte

I2C Peripheral in STM32F103

Overview

In this blog we will be discussing another special functionality of GPIO pins I2C or Inter-Integrated Circuit or I2c functionality . I2C is a two wire interface or TWI which was developed by the Philips corporation for use in consumer products . It is a bidirectional bus that can be easily implemented in an IC process.I2C combines best features of SPI as well as UART it lets the user control multiple slaves via multiple masters which is useful when logging data to sd cards or displaying on LCD. Just like SPI the output data bits are synchronized to sampling of the clock shared by both the parties involved in the communication . The master always generates the clock.The protocol finds applications in hardware sensors or displays , reading memory ICs , communicating with microcontrollers , ADC or DACs.

I2C Theory

I2C consists of 2 lines SCL and SDA

 1.SCL (Serial Clock ) – For synchronizing data transfer between master and slave 

 2, SDA (Serial Data ) – The data transmission or receiving line

Multiple Master and Slave lines are connected to the SDA and SCL lines and both these lines are pulled up using resistors to Vdd (5v)

Operation Modes In I2C 

  • Master Transmitter
  • Master Receiver
  • Slave Transmitter 
  • Slave Receiver 

I2C Clock Speed

The speed of the I2C bus should be in reference to the one provided in the datasheet

The modes of I2C clock speed are as follows:-

  • Standard-mode : 100 KHZ max
  • Fast-mode: 400 KHz max
  • Fast-mode Plus:1MHz
  • High-speed mode: 3.4 MHz

I2C duty cycle specifies the ratio between Tlow and Thigh of the I2C SCL line

The values being:

I2C_DUTYCYCLE_2=2:1

I2C_DUTYCYCLE_16_9= 16:9

 The desired clock speed can be achieved using the appropriate duty cycle to prescale

How data is transmitted in I2C Protocol

 

I2C Features in STM32F103

  • Parallel-bus/I2C protocol converter  

  • Multimaster capability: the same interface can act as Master or Slave  

     

  • I 2C Master features:

     – Clock generation  – Start and Stop generation 

  •  I 2C Slave features:

     – Programmable I2C Address detection  – Dual Addressing Capability to acknowledge 2 slave addresses  – Stop bit detection  

  • Generation and detection of 7-bit/10-bit addressing and General Call  

  • Supports different communication speeds: 

    – Standard Speed (up to 100 kHz)  – Fast Speed (up to 400 kHz) 

  • Analog noise filter  

  • Status flags: 

    – Transmitter/Receiver mode flag – End-of-Byte transmission flag 

  •  Error flags: 

    – Arbitration lost condition for master mode  – Acknowledgment failure after address/ data transmission  – Detection of misplaced start or stop condition  – Overrun/Underrun if clock stretching is disabled  

  • 2 Interrupt vectors:

     – 1 Interrupt for successful address/ data communication  – 1 Interrupt for error condition  

  • Optional clock stretching 

  • 1-byte buffer with DMA capability  

  • Configurable PEC (packet error checking) generation or verification: 

    – PEC value can be transmitted as last byte in Tx mode  – PEC error checking for last received byte

  • SMBus 2.0 Compatibility:

     – 25 ms clock low timeout delay  – 10 ms master cumulative clock low extend time  – 25 ms slave cumulative clock low extend time  – Hardware PEC generation/verification with ACK control  – Address Resolution Protocol (ARP) supported  

  • PMBus Compatibility

I2C Instances in STM32F103

The I2C instances vary from microcontroller to microcontroller i.e the stm32f103c676A has one I2c instance with stm32 f411 RE having I2C1 and I2C2 . The pins for I2C1 are 

  • PB7 – This is used as SDA 
  • PB6 – This is used as SCL

The pins for I2C2 are:-

  • PB3 – This is used as SDA
  • PB10 – This is used as SCL

I2C configuration Parameters in STM32F103

This parameter defines the clock speed which as mentioned previously can be 100000 in standard mode and 400000 fast mode.

This parameter helps in configuring the HIGH and LOW ratio of the clock and has value 2:1 and 16:9

This parameter takes in the address of the first device which can be 7 bit or 10 bit long

This parameter suggests the type of the address size being chosen which can be 7 bit or 10 bit

This parameter is used to disable or enable the dual addressing mode of I2C

This parameter is used to disable or enable the general call addressing mode.

This parameters checks the nostrech mode

Applications of I2C

SSD1306 OLED Screen with STM32F103
GEsture sensor with STM32F103
Time of Flight Sensor with STM32F103
INA219 DC Current Sensor with STM32F103

How to configure the I2C peripheral in STM32F103

We would be using STM32 HAL and STM32CubeIDE for using the I2C peripheral in STM32F103 in this blog tutorial series.

CONFIGURATION IN STM32CUBEIDE

FIG 1- Selecting the SCL and SDA pin

FIG 2 – Configuring the i2c parameters

I2C data handling API types

SDK files

  1. Stm32f1xx_hal.c
  2. Stm32f1xx_hal_i2c.c
  3. Stm32f1xx_hal_i2c.h

The hal.c contains all the macros , function declarations , alternate function mapping of GPIO peripheral and Clock Configurations .The i2c.c file contains initialization and declaration of various low level registers , macros , static void functions , callback functions  , configuration of various parameters of both master as well as slave . The i2c.h file contains all the enums , structure parameters such as clock speed , ownaddress declarations etc.It also contains the declaration of macros of the values that will be filled by the structure parameters such as duty cycle value .

HAL APIs Involved

FUNCTION NAME

HAL_StatusTypeDef HAL_I2C_Init (I2C_HandleTypeDef * hi2c)

FUNCTION DESCRIPTION

Initializes the I2C according to the specified parameters in the I2C_InitTypeDef and initialize the associated handle. 

PARAMETERS

  •  hi2c: Pointer to a I2C_HandleTypeDef structure that contains the configuration information for the specified I2C

 RETURN VALUES 

  •  HAL: status

FUNCTION NAME

void HAL_I2C_MspInit (I2C_HandleTypeDef * hi2c)

FUNCTION DESCRIPTION

Initialize the I2C MSP.

PARAMETERS

  •  hi2c: Pointer to a I2C_HandleTypeDef structure that contains the configuration information for the specified I2C

 RETURN VALUES 

  •  HAL: status

FUNCTION NAME

HAL_StatusTypeDef HAL_I2C_Master_Transmit (I2C_HandleTypeDef * hi2c, uint16_t DevAddress, uint8_t * pData, uint16_t Size, uint32_t Timeout)

FUNCTION DESCRIPTION

Transmits in master mode an amount of data in blocking mode according to the values in the parameters 

PARAMETERS

  •  hi2c: Pointer to a I2C_HandleTypeDef structure that contains the configuration information for the specified I2C.
  •  DevAddress: Target device address: The device 7 bits address value in datasheet must be shifted to the left before calling the interface 
  •  pData: Pointer to data buffer
  •  Size: Amount of data to be sent 
  •  Timeout: Timeout duration

 RETURN VALUES 

  •  HAL: status

FUNCTION NAME

HAL_StatusTypeDef HAL_I2C_Master_Receive (I2C_HandleTypeDef * hi2c, uint16_t DevAddress, uint8_t * pData, uint16_t Size, uint32_t Timeout)

FUNCTION DESCRIPTION

Receives in master mode an amount of data in blocking mode according to the values in the parameters 

PARAMETERS

  •  hi2c: Pointer to a I2C_HandleTypeDef structure that contains the configuration information for the specified I2C.
  •  DevAddress: Target device address: The device 7 bits address value in datasheet must be shifted to the left before calling the interface 
  •  pData: Pointer to data buffer
  •  Size: Amount of data to be sent 
  •  Timeout: Timeout duration

 RETURN VALUES 

  •  HAL: status

FUNCTION NAME

HAL_StatusTypeDef HAL_I2C_Slave_Transmit (I2C_HandleTypeDef * hi2c, uint8_t * pData, uint16_t Size, uint32_t Timeout)

FUNCTION DESCRIPTION

Transmits in slave  mode an amount of data in blocking mode according to the values in the parameters 

PARAMETERS

  •  hi2c: Pointer to a I2C_HandleTypeDef structure that contains the configuration information for the specified I2C.
  •  pData: Pointer to data buffer
  •  Size: Amount of data to be sent 
  •  Timeout: Timeout duration

 RETURN VALUES 

  •  HAL: status

FUNCTION NAME

HAL_StatusTypeDef HAL_I2C_Slave_Receive (I2C_HandleTypeDef * hi2c, uint8_t * pData, uint16_t Size, uint32_t Timeout)

FUNCTION DESCRIPTION

Receive in slave  mode an amount of data in blocking mode according to the values in the parameters 

PARAMETERS

  •  hi2c: Pointer to a I2C_HandleTypeDef structure that contains the configuration information for the specified I2C.
  •  pData: Pointer to data buffer
  •  Size: Amount of data to be sent 
  •  Timeout: Timeout duration

 RETURN VALUES 

  •  HAL: status

FUNCTION NAME

HAL_StatusTypeDef HAL_I2C_Mem_Write (I2C_HandleTypeDef * hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint8_t * pData, uint16_t Size, uint32_t Timeout)

FUNCTION DESCRIPTION

Write an amount of data in blocking mode to a specific memory address.

PARAMETERS

  •  hi2c: Pointer to a I2C_HandleTypeDef structure that contains the configuration information for the specified I2C.
  •  pData: Pointer to data buffer
  •  Size: Amount of data to be sent 
  •  Timeout: Timeout duration
  • MemAddress:The address of the memory that has to be written
  • MemAddSize: The size of memory address
  • DevAddress:The device 7 bits address value in datasheet must be shifted to the left before calling the interface

 RETURN VALUES 

  •  HAL: status

FUNCTION NAME

HAL_StatusTypeDef HAL_I2C_Mem_Read(I2C_HandleTypeDef * hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint8_t * pData, uint16_t Size, uint32_t Timeout)

FUNCTION DESCRIPTION

Read an amount of data in blocking mode to a specific memory address.

PARAMETERS

  •  hi2c: Pointer to a I2C_HandleTypeDef structure that contains the configuration information for the specified I2C.
  •  pData: Pointer to data buffer
  •  Size: Amount of data to be sent 
  •  Timeout: Timeout duration
  • MemAddress:The address of the memory that has to be read
  • MemAddSize: The size of memory address
  • DevAddress:The device 7 bits address value in datasheet must be shifted to the left before calling the interface

 RETURN VALUES 

  •  HAL: status

FUNCTION NAME

static void MX_I2C1_Init(void)

FUNCTION DESCRIPTION

This function is used to intialize the I2C1 along with the parameters

PARAMETERS

NONE

RETURN VALUES

NONE

CODE

Sending code to arduino using I2C via STM32

Leave a Comment

Scroll to Top