mirror of
https://github.com/lm-sensors/lm-sensors.git
synced 2026-10-25 12:14:33 +00:00
New program 'i2cdetect', which scans your i2c bus for connected devices
git-svn-id: http://lm-sensors.org/svn/lm-sensors/trunk@167 7894878c-1315-0410-8ee3-d5d059ff63e0
This commit is contained in:
1 parent
1867246ee0
commit
fefbf31867
3 files changed
+180
-2
No files matched your search
@@ -0,0 +1,88 @@
|
||||
/*
|
||||
i2cdetect.c - Part of i2cdetect, a user-space program to scan for I2C
|
||||
devices.
|
||||
Copyright (c) 1999 Frodo Looijaard <frodol@dds.nl>
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; if not, write to the Free Software
|
||||
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
#include "src/i2c-dev.h"
|
||||
|
||||
void help(void)
|
||||
{
|
||||
fprintf(stderr,"Syntax: i2cdetect I2CBUS\n");
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
char *end;
|
||||
int i,j,res,i2cbus,file;
|
||||
char filename[20];
|
||||
|
||||
|
||||
if (argc < 2) {
|
||||
fprintf(stderr,"Error: No i2c-bus specified!\n");
|
||||
help();
|
||||
exit(1);
|
||||
}
|
||||
|
||||
i2cbus = strtol(argv[1],&end,0);
|
||||
if (*end) {
|
||||
fprintf(stderr,"Error: First argument not a number!\n");
|
||||
help();
|
||||
exit(1);
|
||||
}
|
||||
if ((i2cbus < 0) || (i2cbus > 0xff)) {
|
||||
fprintf(stderr,"Error: I2CBUS argument out of range!\n");
|
||||
help();
|
||||
}
|
||||
|
||||
sprintf(filename,"/dev/i2c-%d",i2cbus);
|
||||
if ((file = open(filename,O_RDWR)) < 0) {
|
||||
fprintf(stderr,"Error: Could not open file `%s': %s\n",filename,
|
||||
strerror(errno));
|
||||
exit(1);
|
||||
}
|
||||
|
||||
fprintf(stderr," WARNING! This program can confuse your I2C bus, "
|
||||
"cause data loss and worse!\n");
|
||||
fprintf(stderr," I will probe file %s\n", filename);
|
||||
fprintf(stderr," You have five seconds to reconsider and press CTRL-C!\n\n");
|
||||
sleep(5);
|
||||
|
||||
printf(" 0 1 2 3 4 5 6 7 8 9 a b c d e f\n");
|
||||
for (i = 0; i < 128; i+=16) {
|
||||
printf("%02x: ",i);
|
||||
for(j = 0; j < 16; j++) {
|
||||
if (ioctl(file,I2C_SLAVE,i+j) < 0) {
|
||||
fprintf(stderr,"Error: Could not set address to %d: %s\n",i+j,
|
||||
strerror(errno));
|
||||
exit(1);
|
||||
}
|
||||
|
||||
res = i2c_smbus_read_byte(file);
|
||||
if (res < 0)
|
||||
printf("XX ");
|
||||
else
|
||||
printf("%02x ",i+j);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
exit(0);
|
||||
}
|
||||
+4
-2
@@ -22,8 +22,10 @@ MODULE_DIR := prog/dump
|
||||
|
||||
# Regrettably, even 'simply expanded variables' will not put their currently
|
||||
# defined value verbatim into the command-list of rules...
|
||||
PROGDUMPTARGETS := $(MODULE_DIR)/isadump $(MODULE_DIR)/i2cdump
|
||||
PROGDUMPSOURCES := $(MODULE_DIR)/isadump.c $(MODULE_DIR)/i2cdump.c
|
||||
PROGDUMPTARGETS := $(MODULE_DIR)/isadump $(MODULE_DIR)/i2cdump \
|
||||
$(MODULE_DIR)/i2cdetect
|
||||
PROGDUMPSOURCES := $(MODULE_DIR)/isadump.c $(MODULE_DIR)/i2cdump.c \
|
||||
$(MODULE_DIR)/i2cdetect.c
|
||||
|
||||
# Include all dependency files. We use '.rd' to indicate this will create
|
||||
# executables.
|
||||
|
||||
@@ -0,0 +1,88 @@
|
||||
/*
|
||||
i2cdetect.c - Part of i2cdetect, a user-space program to scan for I2C
|
||||
devices.
|
||||
Copyright (c) 1999 Frodo Looijaard <frodol@dds.nl>
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; if not, write to the Free Software
|
||||
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
#include "src/i2c-dev.h"
|
||||
|
||||
void help(void)
|
||||
{
|
||||
fprintf(stderr,"Syntax: i2cdetect I2CBUS\n");
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
char *end;
|
||||
int i,j,res,i2cbus,file;
|
||||
char filename[20];
|
||||
|
||||
|
||||
if (argc < 2) {
|
||||
fprintf(stderr,"Error: No i2c-bus specified!\n");
|
||||
help();
|
||||
exit(1);
|
||||
}
|
||||
|
||||
i2cbus = strtol(argv[1],&end,0);
|
||||
if (*end) {
|
||||
fprintf(stderr,"Error: First argument not a number!\n");
|
||||
help();
|
||||
exit(1);
|
||||
}
|
||||
if ((i2cbus < 0) || (i2cbus > 0xff)) {
|
||||
fprintf(stderr,"Error: I2CBUS argument out of range!\n");
|
||||
help();
|
||||
}
|
||||
|
||||
sprintf(filename,"/dev/i2c-%d",i2cbus);
|
||||
if ((file = open(filename,O_RDWR)) < 0) {
|
||||
fprintf(stderr,"Error: Could not open file `%s': %s\n",filename,
|
||||
strerror(errno));
|
||||
exit(1);
|
||||
}
|
||||
|
||||
fprintf(stderr," WARNING! This program can confuse your I2C bus, "
|
||||
"cause data loss and worse!\n");
|
||||
fprintf(stderr," I will probe file %s\n", filename);
|
||||
fprintf(stderr," You have five seconds to reconsider and press CTRL-C!\n\n");
|
||||
sleep(5);
|
||||
|
||||
printf(" 0 1 2 3 4 5 6 7 8 9 a b c d e f\n");
|
||||
for (i = 0; i < 128; i+=16) {
|
||||
printf("%02x: ",i);
|
||||
for(j = 0; j < 16; j++) {
|
||||
if (ioctl(file,I2C_SLAVE,i+j) < 0) {
|
||||
fprintf(stderr,"Error: Could not set address to %d: %s\n",i+j,
|
||||
strerror(errno));
|
||||
exit(1);
|
||||
}
|
||||
|
||||
res = i2c_smbus_read_byte(file);
|
||||
if (res < 0)
|
||||
printf("XX ");
|
||||
else
|
||||
printf("%02x ",i+j);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
exit(0);
|
||||
}
|
||||
Reference in new issue
Block a user