STM32 I2C Lecture 11: Steps for I2C Init Implementation

  • Post author:
  • Post category:Blog

 

Steps for I2C init implementation


 

 

 

In this article, let’s discuss how to implement the I2C_init() API.

There are several steps that must be followed to initialize the I2C peripheral of any microcontroller. They are:

  1. First, the I2C peripheral clock has to be enabled. That is a mandatory step.
  2. Configure the mode: Specify whether the I2C peripheral operated in standard mode or fast mode or fast mode + ultra-fast mode. There are various modes according to which the mode should be configured.
  3. Configure the speed of the serial clock (SCL): The speed of the serial clock, usually in kilohertz (kHz), is configured here. It may be 50kHz, 100kHz, 200kHz, or 400kHz like that. Since the higher frequencies cannot drive the I2C bus to longer distances, the wire length has to be small while using higher frequencies. If the I2C serial clock runs at 25kHz or 50kHz, then the wire length may be a little longer.
  4. Configure the device address: The device address must be configured when a device acts as a slave because every device which acts as a slave needs an address. This step is applicable only when the device is behaving like a slave.
  5. Enable the Acking: Acking is a very important feature of I2C. Refer to the reference manual to explore whether the acking is enabled or disabled by default. If it is disabled by default, then enable it.
  6. Configure the rise time for I2C pins: Configure the rise time for I2C pins or SDA and SCL. The rise time is the time required for the signal to reach the VCC level from ground level. Rise time is also called a slew rate, which must be configured according to the I2C specification.

Remember that all the above configuration must be done when the peripheral is disabled. Usually, the peripheral is disabled by default; if not, first disable it in the control register and then do all the above initialization. Once the initialization is complete, then enable the peripheral in the control register.

Next, copy I2C Init and DeInit functions from i2c_driver.h and paste them into the i2c_driver.c file after peripheral control and peripheral clock control functions, as shown in Figure 1.

I2C_Init API Implementation steps
Figure 1. I2C_Init () and I2C_DeInit ().

Implementing I2C_DeInit is very simple and refer to the RCC reset registers to implement this.

Understanding the concept of serial clock control is very much crucial before initializing the peripheral.

Exercise: Please look into the reference manual and try to read their descriptions of FREQ and CCR fields of CR2 and CCR registers.

 

In the following article, let’s see I2C Serial clock settings with explanations.

 

FastBit Embedded Brain Academy Courses

Click here: https://fastbitlab.com/course1

FastBitLab

The FastBit Embedded Brain Academy uses the power of internet to bring the online courses related to the field of embedded system programming, Real time operating system, Embedded Linux systems, etc at your finger tip with very low cost. Backed with strong experience of industry, we have produced lots of courses with the customer enrolment over 3000+ across 100+ countries.