[Country EMBED strategy] [155] [I²c user-driven design]

Source: Internet
Author: User

User Mode Driver Model

The user-driven model is first an application, followed by a kernel call in the user program to drive the device.

IIC Universal Driver Code

The code for the IIC generic driver is in/DRIVERS/I2C/I2C-DEV.C. One read operation or one write operation is a message.

EEPROM User State Driver

IIC generic equipment corresponds to/dev/i2c-0 equipment files.

1. Turn on universal device driver

2. Construct a message that writes data to the EEPROM

3. Writing data using the IOCTL

4. Constructs a message that reads data from the EEPROM

5. Read data using the IOCTL

6. Turn off the device

Configuring the IIC Drive

Make Menuconfig Arch=arm

Device DRVIERS-><*>I2C SOUPPORT-><*>I2C Device interface

Device drviers-><*>i2c SOUPPORT->I2C Hardware bus support-><*>s3c2410 i²c driver

I2capp.c

/********************************************************************* Header File ************************************ *********************************/#include<stdio.h>#include<stdlib.h>#include<sys/types.h>#include<sys/stat.h>#include<fcntl.h>#include<unistd.h>#include<sys/ioctl.h>/********************************************************************* Macro Definition ************************************ *********************************/#defineI2c_rdwr 0x0707//I²c Read and write commands/********************************************************************* type definition *********************************** **********************************/typedefstructi2c_msg{unsigned ShortAddr//Device AddressUnsigned ShortFlags//read/write FlagsUnsigned ShortLen//Message LengthUnsignedChar*buf;//Message Content}iic_msg;typedefstructi2c_rdwr_ioctl_data{structI2c_msg *msgs;//Message PointersUnsignedintNMSGS;//Number of messages}iic_data;/********************************************************************* Name: main* parameter: * none* return: * none* function: main function ****** ***************************************************************/intMain () {//Open the device file    intFD; FD= Open ("/dev/i2c-0", O_RDWR); //Construct Write MessageIic_data Wrdata; Wrdata.msgs= (IIC_MSG *) malloc (1*sizeof(iic_msg)); wrdata.msgs[0].ADDR =0x50;//Note that the device address is 0b01010000, but the chip manual address is 0b10100000wrdata.msgs[0].flags =0; wrdata.msgs[0].len =2; wrdata.msgs[0].buf = (unsignedChar*) malloc (2*sizeof(unsignedChar)); wrdata.msgs[0].buf[0] =0; wrdata.msgs[0].buf[1] =123; Wrdata.nmsgs=1; //Write message dataIOCTL (FD, I2C_RDWR, (unsignedLong) (&wrdata)); printf ("Wrdata is%d\n", wrdata.msgs[0].buf[1]); //Release Write MessageFree (wrdata.msgs[0].buf);        Free (WRDATA.MSGS); //construct Read MessageIic_data Rddata; Rddata.msgs= (IIC_MSG *) malloc (2*sizeof(iic_msg)); rddata.msgs[0].ADDR =0x50; rddata.msgs[0].flags =0; rddata.msgs[0].len =1; rddata.msgs[0].buf = (unsignedChar*) malloc (1*sizeof(unsignedChar)); rddata.msgs[0].buf[0] =0; rddata.msgs[1].ADDR =0x50; rddata.msgs[1].flags =1; rddata.msgs[1].len =1; rddata.msgs[1].buf = (unsignedChar*) malloc (1*sizeof(unsignedChar)); rddata.msgs[1].buf[0] =0; Rddata.nmsgs=2; //Reading message DataIOCTL (FD, I2C_RDWR, (unsignedLong) (&rddata)); printf ("Rddata is%d\n", rddata.msgs[1].buf[0]); //releasing read messagesFree (rddata.msgs[0].buf); Free (rddata.msgs[1].buf);        Free (RDDATA.MSGS); //Close Device FilesClose (FD);}

[Country EMBED strategy] [155] [I²c user-driven design]

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.