You are on page 1of 4

Linux I2C Driver Architecture

Source:LM_Sensors and I2C Documentation Page http://www2.lm-sensors.nu/~lm78/docs.html

Associated Source Codes Related to Lab 7

i2c-dev.c

/dev/i2c0 for user program access

i2c-core.c

Implement I2C and SMBus protocal (kernel level access)

i2c-algo-bit.c

Bit-banging implementation using two generic signal pins

i2c-assabet.c

Hardware interface

I2C Device Files

Examine /proc/bus/i2c

to see what number corresponds to which adapter.

I2C device files

character device files major device number = 89 "i2c-%d" (i2c-0, i2c-1, ..., i2c-10, ...

C Example

#include <linux/i2c.h> #include <linux/i2c-dev.h> int file; int adapter_nr = 2; /* probably dynamically determined */ char filename[20]; sprintf(filename,"/dev/i2c-%d",adapter_nr); if ((file = open(filename,O_RDWR)) < 0) { /* ERROR HANDLING*/ exit(1); } /* ERROR HANDLING*/ exit(1);

int addr = 0x40; /* The I2C address */ if (ioctl(file,I2C_SLAVE,addr) < 0) { }

__u8 register = 0x10; /* Device register to access */ __s32 res; char buf[10]; /* Using I2C Write*/ buf[0] = register; buf[1] = 0x43; buf[2] = 0x65; if ( write(file,buf,3) != 3) { /* ERROR HANDLING: i2c transaction failed */ }

/* Using I2C Read*/ if (read(file,buf,1) != 1) { /* ERROR HANDLING: i2c transaction failed */ } else { /* buf[0] contains the read byte */ }

You might also like