0 Replies Latest reply on Dec 21, 2016 12:56 AM by AlexPaul

    I2C bus and EEPROM of the Linux driver design

    AlexPaul Community Member

      I am in the development of embedded Linux system based on MPC8250 found in the I2C bus in the embedded system widely used, I2C bus controller type is more, the system provides the operating interface is also very different. I2C bus connected to the slave devices are microcontroller, EEPROM, real-time clock, A / D converter, etc .. MPC8250 processor is through the internal I2C bus controller and these connections in the I2C bus device for data exchange of. Due to the characteristics of the I2C bus, Linux I2C bus device driver designers in the design of the driver using a unique architecture. So that the development of I2C bus device driver and the development of general device driver method is very different. Therefore, the development of I2C bus device driver in addition to the general knowledge of the Linux kernel driver. But also on the I2C bus-driven architecture in-depth understanding. I use the device model AT24C01A EEPROM in the development process to test the I2C bus driver.


      2. Overview of the working principle

      Before the introduction of I2C bus structure. To figure out two concepts: I2C bus controller and I2C devices. I2C bus controller for the microcontroller or microprocessor to control I2C bus interface, which controls all I2C bus special sequence, protocol, arbitration, timing, here refers to the MPC8250 provides I2C bus control interface. I2C device refers to the I2C bus and microcontroller or microprocessor connected devices, such as EEPROM, LCD driver, etc., here refers to the EEPROM.

      In a serial data channel, the I2C bus controller can be configured as either Master or Slave mode. During development, MPC8250's I2C bus controller in the main mode, as the master device; I2C bus connected with the device for the AT24C01A EEPROM, as a slave device. Both the master device and the slave device can operate in the receive and transmit states. The bus must be controlled by the master device, which generates serial clocks to control the direction of the bus and generate start and stop conditions.


      2.1 I2C bus controller

      I2C uses a two-wire structure consisting of a serial data line SDA and a serial line clock line SCL to exchange data between the external integrated circuit and the controller. MPC8250 I2C bus controller includes the sending and receiving unit, a separate baud rate generator and a control unit. The transmit and receive units use the same clock signal if I2C is the master. Then the clock signal is generated by the I2C baud rate generator; if I2C is a slave, the clock signal is externally supplied.

      SDA and SCL are bi-directional and connected to the forward voltage via an external +3.3 V pull-up resistor. SDA and SCL should be high when the bus is in the Idle state.


      I2C receive and transmit units are double-buffered, the data from the transmit data register to the shift register to the clock rate output to the SDA line; in the data reception, the data from the SDA line into the shift register, and then enter Receive register.


      2.2 I2C bus controller and EEPROM basic operation

      I2C bus in the transmission of data in the course of a total of three types of signals, namely: the start signal, the end of the signal and the response signal.

      Start signal: SCL is high, SDA from high to low transition, began to transfer data;

      End signal: SCL is high, SDA from low to high transition, the end of transmission data;

      Acknowledgment: A device that receives data sends a specific low-level pulse to the device that is sending the data after receiving one byte of data. Indicates that data has been received.

      When the MPC8250 I2C bus is idle, its SDA and SCL are high, the main device by sending a start signal to start the transmission process. The timing requirement for this signal is that when SCL is high, SDA transitions from high to low. After the start condition. Must be the address byte of the slave device, where the upper 4 bits are device type identifiers (different chip types have different definitions, EEPROM should be 1010), then 3 bits are chip select, the last bit is read and write bit, When 1 is a read operation, 0 is a write operation.


      If the master writes data to the EEPROM, the master sends a write request to the EEPROM in the address byte (R / W = 0), followed by the address byte to be followed by the data to be transmitted. After each byte of data is sent, the EEPROM generates an acknowledge and the master monitors the acknowledge. If the EEPROM does not return an acknowledge after a byte has been sent, the master stops sending and generates an end signal .


      To read data from the EEPROM, set R / W = 1. After the EEPROM has transmitted one byte of data, the master responds with an acknowledge signal informing the EEPROM master that more data is required and one byte of data is sent for each acknowledge signal generated by the master. The operation is terminated when the master device does not transmit an acknowledge signal and then transmits the end bit.


      3.Linux in the I2C bus driver architecture

      In a Linux system, the I2C bus driver architecture consists of an I2C bus driver and an I2C device driver for a given I2C bus hardware configuration system. I2C bus driver which includes a specific controller driver and I2C bus algorithm driver. An algorithmic driver is applicable to a class of bus controllers. And a specific bus controller driver to use a certain algorithm. For example, the algorithm i2e-algo-8260 provided in the Linux kernel can be used on the I2C bus controller provided by the MPC82xx family of processors. The Linux kernel provides algorithmic drivers for some common processors such as the MPC82xx family. For I2C devices, basically each specific device has its own basic characteristics. Its drivers generally need to be specially designed.


      In the I2C bus driver architecture. Use the data structure Driver to represent the I2C device driver, using the data structure Client represents a specific I2C device. While for the I2C bus

      Controller, a variety of bus controllers in the data transmission using a variety of algorithms used, the controller using the same algorithm to provide the control interface may also be different. In the I2C bus driver architecture, with the data structure Algorithm to represent the algorithm, with the data structure Adapter to represent the different bus controller.



      A Client object corresponds to a specific I2C bus device, and a I2C device Driver can support multiple Client. Each adapter corresponds to a specific I2C bus controller. Different I2C bus controllers can use the same algorithm Algorithm. I2c-core is the core of the I2C bus driver architecture, in this module, in addition to the bus device driver provides a unified call interface to access specific bus driver functions for read and write or set operations, but also provides A method of adding various supported bus device drivers and bus drivers to the system, and a method of removing them from the system when they are no longer used. I2c-core bus driver system will be divided into two, independent of each other. It is possible to design an I2C device driver for an I2C-bus device without concern for the type of I2C-bus controller in the system, thus increasing its portability. On the other hand, the design of I2C bus driver can not be considered when it will be used to support what kind of equipment. Because i2c-core provides a unified interface, so also for the design of these two types of drive provides a convenient.



      4.development examples

      The Linux kernel already provides the basic blocks needed for I2C drivers. I2c-core, i2c-dev and i2c-proc are the Linux SOM required by the bus controller and I2C devices. For MPC8250 processor, the kernel also has MPC8260 algorithm module i2c-algo-8260, it also applies to MPC8250 I2C control interface. These modules in the default condition is not compiled into the kernel, so you need to configure the Linux kernel when these modules selected. In the development of the author needs to achieve is I2C bus controller driver and I2C device EEPROM driver.


      4.1 I2C bus controller driver design

      MPC8250 the I2C bus driver by the i2c-algo-8260 algorithm module and MPC8250 specific I2C bus controller driver components. Which i2c-algo-8260 algorithm module has been implemented in the kernel, so the main implementation of FC bus controller driver.

      I2c-algo-8260 algorithm module is mainly used to describe the MPC82xx processor how to transfer data on the I2C bus. The module mainly implements MPC82xx processor I2C bus initialization, read and write, ioctl control and interrupt requests and other functions. In addition, there are i2c_8260_add_bus and i2c_8260_del_bus two functions, which is the use of this algorithm Adapter initialization and exit call function, used to register and cancel a bus controller, you need to export from the module. These function functions are encapsulated in an i2c-algorithm structure, passed to the adapter using this algorithm. These functions in the algorithm module need to call the functions in the specific controller module to implement the concrete operation.

      In the I2C bus controller driver module to achieve the main structure of the two i2c_adapter and i2c_algo_8260_data, the definition of these two structures in the function pointer members. And initializes the algo_data member variable of the struct i2c_adapter structure with the initialized i2c_algo_826o_data structure. Among them, the definition i2e_algo_8260_data structure is:

      Struct i2c_algo_8260_data rw8250_data = {

      Setisr: rw8250_install_isr


      Here the member variable rw8250_install__isr provides the I2C bus controller MPC8250 request to the kernel in the end of the request function. The structure i2c_adapter is defined as follows:

      Struct i2c_adapter rw8250_ops = {"rw8250", I2C_HW_

      MPC8250_RW8250, NULL, & rw8250_data, rw8250_inc_use, rw8250_dec_use, rw8250_reg, rw8250_unreg,};

      The "rw8250" is the identification name of the bus controller. The macro name I2C_HW_MPC8250_RW8250 defines the ID number of the adapter registered in the kernel. The member functions rw8250_inc_use and rw8250_dec_use are used to increase and decrease the number of times the kernel uses the module.

      In addition, the module also completed a registration module initialization function rw8250_iic_init, in the function to initialize the I2C controller to use the common port number PortD14, PortD15, and dual-port RAM to send and receive data in the buffer allocation of space . The function rw8250_iic_init is called by init_module when the module is initialized.

      In short. These functions are designed for the i2c_algo_8650 algorithm module in the I2C controller module. And finally need to be encapsulated in the i2c-adapter structure. Through the i2c_algo_8260_data algorithm module output interface function is passed to the algorithm module.


      4.2 I2C device driver design

      I2C device EEPROM drive in addition to the specific characteristics of the design according to EEPROM outside. Also consider the characteristics of the I2C bus driver architecture. An i2c_driver structure needs to be implemented in the EEPROM device driver. Each Client corresponding to a specific device is constructed from this structure. There are two functions in the i2c_driver structure attach_adapter and detach_client must be implemented. The i2c_driver structure is defined as follows:

      Struct i2c_driver eeprom_driver = {

      / * Name * / "I2C_EEPROM_DRIVER", / * id * / I2C_DRIVERID_EEPROM,

      / * Flags * / I2C_DF_NOTIFY, / * attach_ adapter * / & eeprom_attach_adapter, / * detach_client * / & eeprom_detach_client,

      / * Command * / & eeprom_command, / * inc_use * / & eeprom_inc_use, / * dec_use * / & eeprom_dec_use


      In the device driver. Writing data to the EEPROM is done by calling the i2c_master_send function provided by i2c-core. Reading data from the EEPROM is done via another function, i2c_master_read. And general device driver is different from the initial function of the EEPROM driver module to call the i2c-core i2c_add_driver function to register the device. Call the i2c_del_driver function in the module exit function to unregister the device.



      I2C bus has the advantages of simple control and high communication speed. As a 2-wire bi-directional synchronous serial data bus, it provides a perfect integrated serial bus extension technology for embedded system hardware design, which greatly simplifies the application system Of the hardware design for the realization of the application system modular design to create a very favorable conditions. At the same time, in many cases the need for some of the dynamic information in the system power-down protection. In the case where the amount of data is not too large, through the I2C bus connected to the EEPROM in this regard can play a role. As a new operating system, Linux is widely used in embedded system. Its development prospects can not be estimated. Because Linux source code is open, and very easy to transplant, it is relatively easy to write device drivers for it. This paper describes the Linux I2C bus EEPROM driver under the general design method.



      Embedded single board computer