Recent Changes - Search:

Home

edit SideBar

I2C

I2C (or, more properly I2C, pronounced "eye two see" or "eye squared see") is a 2-wire serial bus developed by Phillips to facilitate communication between multiple peripherals on a single bus. There is a distinct advantage for using a bus over a standard serial line because devices can be connected to the same bus and be accessed individually. This saves much time wiring, and makes it much easier to manage in software.

I2C consists of a signal wire (SDA), clock wire (SCL), and of course a common ground. There is a lot that happens on the bus with addressing, syncing and data transfer, but lucky for us it has all been abstracted in a kernel module for Linux. On the gumstix, the I2C bus is linked to /dev/i2c-0 using mknod. If you simply #include <i2c-dev.h>; from your C/C++ applications, you have access to a host of interface options with the bus. When combined with the i2c-api.c/h  written by Dave Hylands , you are given a powerful set of tools for interacting with the I2C bus.

I2C slave devices each have a unique 7-bit address to be used when reading/writing registers. A call to device using the aforementioned API would look something like this:

// Try to open the i2c device
if (( i2cDev = open( i2cDevName, O_RDWR )) < 0 ) {
    printf( "Error  opening '%s': %s\n", i2cDevName, strerror( errno ));
    exit(1);
}
I2cSetSlaveAddress( i2cDev, 0x70, I2C_NO_CRC );
if (( rc = I2cWriteBytes( i2cDev, 0x00, 0x51, 1 )) != 0 ) {
    printf( "I2cWriteBytes failed: %d\n", rc );
    exit(1);
}

Addresses

A "gotcha" that tied us up for a while is that most manufacturers list their devices address as 8 bits, while the I2C bus only uses the most significant 7 bits (the MSB is a read/write bit). If you are having troubles, try shifting your address 1 bit to the right. For example, the SRF-02 manual states the address is 0xE0, but we need to address it as 0x70.

Edit - History - Print - Recent Changes - Search
Page last modified on May 15, 2008, at 10:32 PM EST