RT-Thread Studio-based I2C Driver Development Document

Introduction

I2C is a serial protocol for a two-wire interface to connect low-speed devices like microcontrollers, EEPROMs, A/D and D/A converters, I/O interfaces, and other similar peripherals in embedded systems. It was invented by Philips and now it is used by almost all major IC manufacturers. The I2C bus transmits data using only two signal lines, one bidirectional data cable SDA and one bidirectional clock line SCL.

The I2C bus can be implemented via the on-board I2C peripheral on the chip or simulate through the GPIO pin.

The hardware I2C corresponds to the I2C peripheral on the chip, consisting of the corresponding I2C controller and drive circuit, and the pins used are also dedicated. The soft I2C uses a common GPIO pin to simulate I2C communication waveforms by controlling the state of the GPIO pin. Therefore, the soft I2C is not limited by pins, the interface is more flexible.

RT-Thread I2C device driver framework supports hardware I2C as well as soft I2C. This article will cover the driver development of soft I2C based on the stm32l475-atk-pandora board.

The configuration steps for the soft I2C are summarized below:

  • Create a RT-Thread Standard Version project.
  • Enable the I2C device drive framework.
  • Define Soft I2C-related Macros.
  • Use the I2C bus.

For more details about soft I2C configuration and addition processes please refer to the introduction of the soft I2C in the corresponding project file board.h.

Create RT-Thread Project

Using RT-Thread Studio to create an RT-Thread v4.0.2 project, as shown below:

The configuration process can be summarized as follows:

  • Define your own project name and the storage path for the generated project files.
  • Select 'Base on MCU' to create a project and select RT-Thread version v4.0.2.
  • Select the vendor and chip series.
  • Configure serial information.
  • Configure the debugger information.

Once the project is configured, click the 'Finish' button to create the RT-Thread project.

Enable I2C Device Driver Framework

Enable the I2C driver framework by using the graphical configuration tool in the RT-Thread Setting file, as shown below:

Left-click to enable 'soft I2C' (components will also be enabled and the icon lights up ), right-click 'soft I2C' there is an option to view its 'details configuration', as shown below:

RT-Thread Setting
----Components
--------Device Driver Program
------------Use I2C Device Driver Program 
----------------Use GPIO to simulate I2C

The configuration results are shown below:

Define the related macros of soft I2C on the board.h file.

#define BSP_USING_I2C1                         /* Use I2C1 Bus */
#define BSP_I2C1_SCL_PIN    GET_PIN(C, 1)      /* SCL -> PC1 */
#define BSP_I2C1_SDA_PIN    GET_PIN(D, 6)      /* SDA -> PD6 */

The above I2C bus related macros present that the SCL clock line of the I2C1 bus is connected to the PC1 pin and the SDA data cable is connected to the PD6 pin. If you need to use more than one I2C bus, please refer to the macro of I2C1 on the board.h file for more information about the relevant macro definition.

Use I2C Bus

Compile and download the program, enter the list_device command in the terminal you can observe that the I2C1 has been successfully registered in the system, as shown below: