STM32F103 SPL Tutorial 6 – I2C Interface

STM32F103 I2C

STM32F103 I2C (Inter-Integrated Circuit) or sometimes called TWI (Two Wire Interface) is a synchronous serial protocol that only needs 2 wires for communication. The wires are SCL for clock line and SDA for data line. With I2C, you can connect devices like temperature sensor, EEPROM, RTC, and etc (up to 112 devices) just using 2 wires (plus GND wire). For accessing these devices, I2C use 7-bit address. Theoretically, 7-bit address space allows 128 addresses, however some addresses are reserved for special purpose. Thus, only 112 addresses can be used.

I2C bus

I2C Protocol

I2C is a multi-master and multi-slave bus. I2C can consist of one or more master device, but only one master device can access the I2C bus each time. To communicate with a slave device, master device is responsible for sending clock signal to the slave device. The clock signal is generated by master. I2C pins are open-drain, so it can pull the signal to logic 0, but can’t drive it to logic 1. Each signal line (SCL and SDA) must have a pull-up resistor to restore the signal to logic 1 when no device is asserting it to logic 0.

The figure below explains how I2C protocol works. START and STOP conditions are defined as rising edge or falling edge on the SDA line, while the SCL line is high. After master sends the START condition, then it will be followed by the slave address and R/W bit. After that, slave is responsible to generate ACK (acknowledge). ACK is defined as logic 0 on SDA line. If master wants to write data to slave (indicated by R/W bit = 0), then master will send the data byte to slave. The MSB of data byte will be sent first to slave. Slave is responsible to generate ACK for every received data byte.

I2C protocol

If master wants to read data from slave (indicated by R/W bit = 0), then after the first ACK of slave address and R/W bit, master will read data byte from slave. The MSB of data byte will be read first from slave. If the data size is only one byte or that byte is the last data byte, then master will send NACK to slave, otherwise master will send ACK. NACK is defined as logic 1 on SDA line. From the figure below, you can see the detail of I2C bus timing diagram.

I2C timing

Example Code

In this tutorial, I will explain how to use I2C on STM32F103 as a master device and for the slave device I use Arduino. The slave is very simple, when master write data byte 0x01 to it, then the LED on Arduino board will blinking every 250 ms. To turn off the LED blinking, master must write data byte 0x00. Master can also read the LED blinking status (on/off) from the slave which returns 1 or 0. This is the code for Arduino as an I2C slave.

The code below is for STM32F103 as an I2C master. You can use i2c_init() function to initialize the I2C peripheral. In the main function, I write a command to turn on and turn off LED blinking on Arduino board every 2500 ms. Every write command is followed by a command for reading the current LED blinking status, then the status is displayed on LCD 16×2.

You can get the project file from here. This is the result:

Go to the part 7 of the tutorial.