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.
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
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- Rambus Launches Industry's First HBM 4 Controller IP: What Are the Technical Details Behind It?
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- Recruiting embedded software development engineers, embedded BSP engineers
- Basic Theory of System Timing
- Mitsubishi Servo Drive MR-J2S-20B Disassembly
- Application of RFID and other Internet of Things technologies in smart ranches
- Which company can make PCB boards with a width of about 2MM?
- How to speed up the program running speed of Huada HC32F460 HC32F4A0?
- FPDLINK Spark Interference Optimization
- Why do electrolytic capacitors explode? Find out in one article!
- Namisoft's design report on labview spectrum analyzer
- [Iprober 520 current probe] Calibration and use in PCB mode