Library 09- I2C for STM32F4

I2C or Inter-Integrated Circuit is a multimaster serial single ended bus. This protocol is commonly used with RTC modules, temperature sensors, EEPROMs, IO expanders and more.

I2C protocol uses 2 wires:

  • SCL: Serial Clock, clock for serial synchronization
  • SDA: Serial data, bidirection line for receving and transmitting
  • Both wires need external pull up resistor, from about 4k7 to 47k, if you don’t use pull up resistors in MCU. In our case, you don’t need external pull ups, because library uses internal in STM32F4.

More about I2C is described here.

STM32F4 has up to 3 I2Cs, every of them has (as always) at least 2 pins pack for each I2C. Pins used for each I2C are described in table below:

I2C pins
PinS PACK 1 PINS PACK 2 PINS PACK 3
I2Cx SCL SDA SCL SDA SCL SDA APB
I2C1 PB6 PB7 PB8 PB9 PB6 PB9 1
I2C2 PB10 PB11 PF1 PF0 PH4 PH5 1
I2C3 PA8 PC9 PH7 PH8 1

Library

Features

  • Master mode
  • 7 bit slave address
  • Up to 127 different slaves on 1 I2C bus
  • Read/Write single byte
  • Read/Write multiple bytes from slave
  • Version 1.1 – August 09, 2014
    • Checks if device is connected to I2C bus
  • Version 1.2 – August 14, 2014
    • If you connect more devices on one I2C with different max SCL speed, low speed will be always selected.
    • Added some additional pins for I2C
  • Version 1.3 – December 22, 2014
    • Added option to read multi bytes from device without setting register from where
  • Version 1.4 – March 08, 2015
    • Added support for new GPIO settings

Dependencies

  • CMSIS
    • STM32F4xx
    • STM32F4xx RCC
    • STM32F4xx GPIO
    • STM32F4xx I2C
  • TM
    • TM GPIO
    • defines.h
    • attributes.h

Initialization

When you use library, you have to initialize it first. You can do this with

or

Now, you are able to read/write data with slave.

Read

 Write

Functions and enumerations

Example

Project available on Github, download library below.

tilz0R

Owner of this site. Application engineer, currently employed by STMicroelectronics. Exploring latest technologies and owner of different libraries posted on Github.

You may also like...