The dev-interface of I2C to realize the reading and writing of EEPROM

Source: Internet
Author: User
Tags usleep

The EEPROM is a storage chip for storing small amounts of data. AT24C02 is a kind of EEPROM using the I2C interface.

AT24XXX EEPROM Working principle reference: http://blog.csdn.net/jklinux/article/details/74162876

Dev-interface is the interface to which the I2C controller is invoked by the application. Please refer to If you are not familiar with: http://blog.csdn.net/jklinux/article/details/78676741

Main reading and writing timing:

The IOCTL (FD, I2C_RDWR, ...) is visible from the upper sequence diagram, allowing the EEPROM to store data (information) on the specified location (Word address:0 ~ 255). The function needs to be called once, requiring a struct i2c_msg message.

The IOCTL (FD, I2C_RDWR, ...) is visible when the EEPROM is stored on the specified position (Word address:0 ~ 255), as seen from the upper sequence diagram. function needs to be invoked once and requires two struct I2C_MSG messages. The first write message needs to read the location to the EEPROM, the second read the message received EEPROM output data.

The implementation of the EEPROM in the circulation of ' A ' ~ ' Z ' character:

* eeprom_write.c/* #include <stdio.h> #include <unistd.h> #include <fcntl.h> #include <sys/ioctl. h> #include <linux/i2c.h> #include <linux/i2c-dev.h> #define EEPROM_ADDR 0x50 int main (void) {int F

    D, I;
    FD = open ("/dev/i2c-0", O_RDWR);
        if (FD < 0) {perror ("Open I2C Controller");
    return 1;
    ///////////////////////////////////////the ' A '--' Z ' char wdata[2] in the EEPROM;
    struct I2c_msg msg = {eeprom_addr, 0, 2, wdata};          

    struct I2c_rdwr_ioctl_data Wdat = {. Msgs = &msg,. nmsgs = 1,};
        for (i = 0; i < 256 i++) {wdata[0] = i;

        WDATA[1] = i%26+ ' A ';
            if (IOCTL (FD, I2C_RDWR, &wdat) < 0) {perror ("I2C rdwr failed\n");
        Break } usleep (1000);
    When the stop signal is issued, a delay of//////////////////////////////////////close (FD) is required before the start signal is issued;
return 0; }

read out all the contents of the EEPROM:

/* EEPROM_READ.C/#include <stdio.h> #include <unistd.h> #include <fcntl.h> #include <sys/ioctl.h > #include <linux/i2c.h> #include <linux/i2c-dev.h> #define EEPROM_ADDR 0x50 int main (void) {int FD

    I
    FD = open ("/dev/i2c-0", O_RDWR);
        if (FD < 0) {perror ("Open I2C Controller");
    return 1;
    } char reg, DAT;
    struct I2c_msg msgs[2] = {eeprom_addr, 0, 1, &reg}, {eeprom_addr, i2c_m_rd, 1, &dat},};          

    struct I2c_rdwr_ioctl_data Rwdat = {. Msgs = msgs,. nmsgs = 2,};
        for (i = 0; i < 256; i++) {reg = i;
            if (IOCTL (FD, I2C_RDWR, &rwdat) < 0) {perror ("I2C rdwr failed\n");
        Break } usleep (1000);
        When a stop signal is issued, a delay is required before the start signal is issued (0 = i%26) printf ("\ n");

    printf ("%c", DAT);
    }//////////////////////////////////////Close (FD);return 0; }

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.