(20) Use of the Ioctl,write,read function of I²c in Linux

Source: Internet
Author: User

First, the use of the IOCTL function:
Prototype: struct ioctl(struct file *file,unsigned int cmd,unsigned long arg);
CMD has i2c_slave,i2c_slave_force,i2c_tenbit,i2c_s3c2410_set_speed several options;
I2c_slave: The corresponding arg value is the i²c slave address, used to set the i²c slave address;

I2c_slave_force: The corresponding arg value is the i²c slave address, which is used to modify the address of the i²c slave machine;

I2c_tenbit: The corresponding arg value is 0: The slave address is 7 bit, and the corresponding ARG value is 1: The slave address is 10bit. The number of digits used to specify the address of the i²c slave;

I2c_s3c2410_set_speed: The corresponding arg value is the crossover value of the I²C bus controller. Used to set the I²C bus controller clock frequency;
 

Common settings set the I²c slave address is 0xa0, if you choose the AT24C08 device, then the slave is a 7 bit address, so to move right 1 bits, specify the slave address is 7 bit,

IOCTL (fd,i2c_tenbit,0).
IOCTL (fd,i2c_slave,0xa0>>1);

Use of the Read () and write() functions
Assuming a sub-address of 12, write 7 bytes to the device with the child address:

unsigned char buf[8]={12, ' s ', ' j ', ' s ', ' u ', ' n ', ' n ', ' Y ');write (fd,buf,9);/* writes in 7 bytes, 1th byte is a sub-address */

Read 7 bytes from an I²C device with a sub-address:
unsigned char suba=0;recbuf[20];
Write (fd,buf,1);/* Send sub-address 0*/
Read (fd,recbuf,7);/* Start reading 7 bytes from sub-address 12 */

Ii. The difference between the IOCTL and read and write :

Known as the "Swiss Army Knife" of the UNIX system, the IOCTL was used as an extension of the Linux system
It can be used widely in Linux system by a common method.

IOCTL is typically used to transfer control between user-space programs and driver modules
The difference between theIOCTL and read and write is:
1, theIOCTL is generally used to transmit control parameters, such as: Serial port baud rate, serial port flow control method (Xon/xoff, DTR/DSR, rts/cts), etc., generally do not
Used to pass "primary" data (I don't have the right words to explain:).

2, theioctl semantics is generally non-blocking, read and write but the province is blocked.

3, theIOCTL interface is omnipotent,ioctl(FD, CMD, arg) The third parameter can be an integer variable, or a pointer to a data structure.

(20) Use of the Ioctl,write,read function of I²c in Linux

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.