Difference between revisions of "EMAC I²C Programming"

From wiki.emacinc.com
Jump to: navigation, search
(Background Information)
Line 1: Line 1:
{{todo|Write I2C Article; (12.4.13-15:35->MD+)|Michael Welling|project=oe 4,oe 5,mw,notstarted,md}}
+
{{todo|Write I2C Article; (12.4.13-15:35->MD+)|Michael Welling|project=oe 4,oe 5,mw,InProgress,md}}
 
=== Background Information ===
 
=== Background Information ===
 
I²C is a low-speed (400 kHz max.) two wire serial interface used to connect to a variety of sensor and I/O devices. The interface uses two bi-directional open-drain I/Os, SDA and SCL, to communicate to devices based on an address encoded within the data transmission. SDA is a data signal which sends and receives serially transmitted data. SCL is a clock signal which is used to determine when to latch data from the SDA line.
 
I²C is a low-speed (400 kHz max.) two wire serial interface used to connect to a variety of sensor and I/O devices. The interface uses two bi-directional open-drain I/Os, SDA and SCL, to communicate to devices based on an address encoded within the data transmission. SDA is a data signal which sends and receives serially transmitted data. SCL is a clock signal which is used to determine when to latch data from the SDA line.
 
  
 
For more information about the protocol see the following page:
 
For more information about the protocol see the following page:
Line 8: Line 7:
  
 
This page specifically covers the usage of the Linux I²C i2c-dev driver.
 
This page specifically covers the usage of the Linux I²C i2c-dev driver.
 
  
 
For more information about the Linux I²C subsystem:
 
For more information about the Linux I²C subsystem:
 
https://i2c.wiki.kernel.org/index.php/Main_Page
 
https://i2c.wiki.kernel.org/index.php/Main_Page
 +
 +
=== Accessing the i2c-dev Interface ===
 +
When the i2c-dev driver is properly loaded it exposes a device interface for each available I²C interface. Each interface will have an associated character device node at /dev/i2c-X, where X is replaced by a number representing the interface. This device node allows the user to access the I²C interface directly from a C program.
 +
 +
 +
<syntaxhighlight lang="c">
 +
#include "i2c-dev.h"
 +
 +
int main(int argc, char ** argv)
 +
{
 +
    int fd;
 +
    int ret;
 +
    int addr = 0x0e;
 +
 +
    fd = open("/dev/i2c-0", O_RDWR);
 +
   
 +
    if(fd < 0) {
 +
        fprintf(stderr, "Cannot open i2c device\n");
 +
        exit(-1);
 +
    }
 +
 +
    ret = ioctl(fd, I2C_SLAVE, addr);
 +
 +
    if(ret < 0) {
 +
        fprintf(stderr, "Cannot set slave address\n");
 +
        exit(-1);
 +
    }
 +
 +
    exit(0);
 +
}
 +
   
 +
</syntaxhighlight>

Revision as of 11:29, 13 December 2013

TODO: {{#todo:Write I2C Article; (12.4.13-15:35->MD+)|Michael Welling|oe 4,oe 5,mw,InProgress,md}}

Background Information

I²C is a low-speed (400 kHz max.) two wire serial interface used to connect to a variety of sensor and I/O devices. The interface uses two bi-directional open-drain I/Os, SDA and SCL, to communicate to devices based on an address encoded within the data transmission. SDA is a data signal which sends and receives serially transmitted data. SCL is a clock signal which is used to determine when to latch data from the SDA line.

For more information about the protocol see the following page: http://en.wikipedia.org/wiki/I²C

This page specifically covers the usage of the Linux I²C i2c-dev driver.

For more information about the Linux I²C subsystem: https://i2c.wiki.kernel.org/index.php/Main_Page

Accessing the i2c-dev Interface

When the i2c-dev driver is properly loaded it exposes a device interface for each available I²C interface. Each interface will have an associated character device node at /dev/i2c-X, where X is replaced by a number representing the interface. This device node allows the user to access the I²C interface directly from a C program.


#include "i2c-dev.h"

int main(int argc, char ** argv)
{
    int fd;
    int ret;
    int addr = 0x0e;

    fd = open("/dev/i2c-0", O_RDWR);
    
    if(fd < 0) {
        fprintf(stderr, "Cannot open i2c device\n");
        exit(-1);
    }

    ret = ioctl(fd, I2C_SLAVE, addr);

    if(ret < 0) {
        fprintf(stderr, "Cannot set slave address\n");
        exit(-1);
    }

    exit(0);
}