Created
October 19, 2012 08:05
-
-
Save suapapa/3916862 to your computer and use it in GitHub Desktop.
simple CLI tool to scan i2c devices
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
LOCAL_PATH := $(call my-dir) | |
include $(CLEAR_VARS) | |
LOCAL_SRC_FILES += i2c_scan.c | |
LOCAL_MODULE_TAGS := optional | |
LOCAL_MODULE := i2c_scan | |
include $(BUILD_EXECUTABLE) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <fcntl.h> | |
#include <sys/ioctl.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; | |
}; | |
static int i2c_scan(int fd) | |
{ | |
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 = 0x%02x \n", adr); | |
} | |
} | |
return 0; | |
} | |
int main(int argc, char *argv[]) | |
{ | |
int j; | |
int n, i; | |
int iret = 0; | |
int chip; | |
unsigned char value = 0x00; | |
if (argc != 2) { | |
printf("Usage: %s [i2c_ch_num]\n", argv[0]); | |
exit(1); | |
} | |
char i2c_dev_path[15]; | |
sprintf(i2c_dev_path, "/dev/i2c-%s", argv[1]); | |
printf("Scanning %s...\n", i2c_dev_path); | |
int fd = open(i2c_dev_path, O_RDWR); | |
if (fd < 0) { | |
printf("Open Error...!\n"); | |
exit(1); | |
} | |
i2c_scan(fd); | |
close(fd); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thank you~