특정 i2c bus line(여기서는 /dev/i2c-0 - 즉, 첫번째 버스라인)에 붙어 있는 i2c를 scan하는
소스입니다.
참고하시기 바랍니다.
=====================================
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <sys/user.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include <errno.h>
#define I2C_RDWR 0x0707
struct i2c_msg {
unsigned short addr;
unsigned short flags;
unsigned short len;
unsigned char * buf;
};
struct i2c_rdwr_ioctl_data {
struct i2c_msg *msgs;
unsigned int nmsgs;
};
int fd;
int i2c_scan()
{
int rc, adr, found=0;
struct i2c_msg msg;
struct i2c_rdwr_ioctl_data msgset;
/* scan the whole I²C bus range */
for (adr = 1; adr < 128; adr++) {
msg.addr = adr;
msg.flags = 0;
msg.buf = 0;
msg.len = 0;
msgset.msgs = &msg;
msgset.nmsgs = 1;
rc = ioctl(fd , I2C_RDWR , &msgset );
if ( rc == 1 ) {
found++;
printf("Find Device I2C Addr = %x \n", adr);
}
}
}
int main (int argc, char *argv[])
{
int j;
int n, i;
int iret = 0;
int chip;
unsigned char value = 0x00;
fd = open("/dev/i2c-0", O_RDWR);
if (fd < 0) {
printf("Open Error...!\n");
exit(0);
}
i2c_scan();
close(fd);
}