Driver Design of I2C Bus in Linux System

Publisher:SparklingMelodyLatest update time:2013-03-13 Source: dzsc Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
    introduction

  The Linux operating system has become a popular choice for embedded systems due to its advantages such as open source code, easy tailoring, and wide processor support. Keyboard expansion devices based on the I2C bus are mainly used to meet the needs of embedded devices for multiple keys. The driver initializes the hardware when the system starts. After the system starts, data interaction between hardware and applications is realized. Aiming at the S3C2410 microprocessor and keyboard scanning management device, this paper discusses in depth how to implement the driver of ZLG7290 in the embedded operating system ARM Linux.

  2 I2C bus

  The I2C bus is a serial transmission bus between devices. It is widely used for its specifications and peripheral devices with I2C interfaces. The S3C2410 processor has a built-in I2C bus interface. The I2C bus is a serial bus composed of a data line SDA and a clock SCL. It can send and receive data. Each device has a unique address identification. There are three types of signals in the I2C bus during data transmission: (1) Start signal: When SCL is high, SDA jumps from high to low to start data transmission. (2) End signal: When SCL is low, SDA jumps from low to high to end data transmission. (3) Response signal: After receiving 8 bits of data, the IC that receives data sends a specific low-level pulse to the IC that sends data, indicating that the data has been received. After the CPU sends a signal to the controlled unit, it waits for the controlled unit to send a response signal. After receiving the response signal, the CPU makes a decision on whether to continue transmitting the signal based on the actual situation. If no response signal is received, it is determined that the controlled unit has a fault. Figure 1 is a connection diagram of S3C2410 and ZLG7290.

  3. Structure of the driver module

  S3C2410A has an embedded I2C bus controller. In the Linux operating system, the character driver of this controller includes four modules: (1) i2c-dev: the source program is i2c-dev. c, the I2C bus character driver, which has nothing to do with the specific I2C controller used. (2) i2c-core: the source program is i2c-core. c, the I2C bus operation core function, which has nothing to do with the specific I2C controller used. (3) i2c-algo-s3c2410: the source program is i2c-algo-s3c2410. c, the I2C bus controller I2C driver algorithm, which is related to the specific controller. (4) i2c-s3c2410: the source program is i2c-s3c2410. c, the I2C bus controller hardware abstraction layer, which is related to the specific controller. The above four modules are in the relationship between the bottom layer and the upper layer, and the hierarchical structure of the I2C driver module is shown in Figure 2.

  4 Compilation of driver source files

  The driver source files include 4 source files: i2c-algo-s3c2410. c, i2c-dev. c, i2c-core. c, i2c-s3c2410. c. Compiling them into modules will generate 4 module files. The compilation work is completed by the Makefile file, and its file content is as follows:

[page]

  When you need to use the I2C bus controller driver module, you can use the device management file loadi2c of the I2C bus controller driver. After the management file is run, it will automatically create a device node i2c in the Linux /dev/ directory. The contents of the loadi2c file are as follows:

  Since there is a hierarchical relationship between the modules, the order of loading modules should be from top to bottom, i2c-core should be loaded first, and i2c-s3c2410 should be loaded last. The order of unloading is the opposite. When loading the module i2c-algo-s3c2410, a delay of 1 s must be made to wait for the initialization of the module, otherwise the next module i2c-s3c2410 cannot be loaded.

  5 Use of driver files

  After successfully creating the device node i2c in the Linux / dev / directory, you can use a series of functions provided by LINUX to implement the driver. These functions include ioctl(), open(), close(), read() and write().

  5.1 Use of ioctl() function

  The I2C bus controller driver (character type) provides the ioctl() function to set some parameters of the I2C bus controller. The common commands of this function are:

  (1) The second parameter value of I2C_SLAVE is the I2C slave address, which is used to set the I2C slave address; I2C_SLAVE_FORCE: The second parameter value is the I2C slave address, which is used to modify the I2C slave address; I2C_TENBIT: The second parameter value is 0: the slave address is 7 bits; The second parameter value is 1: the slave address is 10 bits. It is used to specify the number of bits of the I2C slave address; I2C_S3C2410_SET_SPEED: The second parameter value is the I2C bus controller division value. It is used to set the I2C bus controller clock frequency;

  (2) Common settings Set the I2C slave address to 0xA0. Since it is a 7-bit address, it needs to be shifted right by 1 bit: ret = ioctl (fd, I2C_SLAVE, 0xA0>>1); Specify the slave address as 7 bits, ret = ioctl (fd, I2C_TENBIT, 0).

  5.2 Use of open(), close()

  To use this driver to access external I2C devices, applications must first open the driver using open() and then close it using close(). To open and close the I2C bus controller driver:

  5.3 Use of read() and write() functions

  Use the read() and write() functions to read/write the I2C bus. Common settings are as follows:

  write(fd, &data, 1); send 1 byte of data data to the I2C bus;

  Assuming the subaddress is 0, write 8 bytes to the FC device with subaddress: unsigned char sendbuf[9]={0, 1, 2, 3, 4, 5, 6, 7, 8); /*The first byte 0 is the subaddress*/write(fd, sendbuf, 9); /*Write 8 bytes, the first byte is the subaddress*/ Read 8 bytes from the I2C device with subaddress, assuming the subaddress is 0:

  unsigned char suba=0;recbuf[8];

  write(fd, &suba, 1); /*Send subaddress 0*/

  read(fd, recbuf, 8); /*Read 8 bytes starting from subaddress 0*/

  6 Conclusion

  The device driver is the interface between the operating system kernel and the machine hardware, through which the device can be documented. This article first introduces the working mode and characteristics of the I2C bus interface, briefly introduces the characteristics of the I2C character driver module, studies the driver of the keyboard expansion device ZLG7290 under ARM Linux, and describes in detail the key value reading process based on I2C bus communication, which has a wide range of application value.

Reference address:Driver Design of I2C Bus in Linux System

Previous article:Design of Portable Data Acquisition System
Next article:Research on Prolonging the Life of Flash Memory

Recommended ReadingLatest update time:2024-11-17 03:01

In-depth interpretation of U-Boot transplantation of S3C2410A embedded system
0 Preface ARM embedded processors have been widely used in consumer electronics, wireless communications, network communications, industrial control and other fields. Among them, ARM9 chips occupy an important market in handheld devices because of their low price, low power consumption and high performance. Among embe
[Microcontroller]
In-depth interpretation of U-Boot transplantation of S3C2410A embedded system
S3C2410 clock and timer
Connect an external crystal oscillator and then generate a clock source through the internal circuit Directly use the external clock source Select by setting the pin   FCLK  cpu HCLK  AHB PCLK  APB   PLL increases the system clock speed through the clock control logic PLL   UPLL  USB MPLL  FCLK H
[Microcontroller]
S3C2410 DMA driver development under Linux
There are numerous examples of general driver development under LINUX on the Internet, but perhaps because they are simple, there are very few introductions to DMA drivers. Recently, Zhaoyang spent several days developing a driver for DMA communication of a certain device under Linux on the S3C2410 processor due to wor
[Microcontroller]
Design of digital image system for assisting reverse driving based on S3C2410
1. Introduction  According to statistics, traffic accidents caused by blind spots behind vehicles account for about 30% in China and 20% in the United States. The first two generations of reversing assistance products are reversing horns and reversing radars. The former can only remind passers-by to dodge on their o
[Microcontroller]
Design of digital image system for assisting reverse driving based on S3C2410
Research and Design of S3C2410 Peripheral Storage System
    0 Introduction   The 16/32-bit RISC microprocessor S3C2410 based on the ARM920T core launched by Samsung provides a low-cost, low-power, high-performance small microcontroller solution for handheld devices and general-type applications. In order to reduce the cost of the entire system, the S3C2410 provides a weal
[Microcontroller]
Research and Design of S3C2410 Peripheral Storage System
s3c2410 processor storage expansion - SDRAM
Since S3C2410 is a 32-bit processor (an instruction can operate 32 bits of data at a time (the arithmetic unit can process 32 bits of data at a time); most general registers are 32-bit registers; the internal data channel of the processor is also 32 bits; the external data bus width of the processor is usually 32 bits
[Microcontroller]
s3c2410 processor storage expansion - SDRAM
Pulse Width Modulation Timer in S3C2410
S3C2410 has five 16-bit timers. Timers 0-3 have pulse width modulation (PWM) function, and timer 4 is an internal timer with no output pins. Timer 0 has a dead-zone generator, which can ensure that a pair of reverse signals will not change state at the same time, and is often used in high-current devices.        Timer
[Microcontroller]
Pulse Width Modulation Timer in S3C2410
Design of digital image system for auxiliary reverse parking based on S3C2410
1. Introduction According to statistics, traffic accidents caused by blind spots behind cars account for about 30% in China and 20% in the United States. The first two generations of reversing assistance products are reversing horns and reversing radars. The former can only remind passers-by to dodge on the
[Automotive Electronics]
Design of digital image system for auxiliary reverse parking based on S3C2410
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号