/* * Copyright (c) 2015 Sulev-Madis Silber * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * * ORIGINAL CODE: * * i2cscan FreeBSD version of i2cdetect * * Compile with: * cc i2cscan.c -o i2cscan * * Winston Smith */ #include #include #include #include #include #include #include #include /* * Scan for the specified slave by trying to read a byte */ int scan(int fd, int slave) { uint8_t buf[2] = { 0, 0 }; uint16_t offset = 0; struct iic_msg msg[2]; struct iic_rdwr_data rdwr; slave = slave << 1; msg[0].slave = slave; msg[0].flags = !IIC_M_RD; msg[0].len = sizeof(offset); msg[0].buf = (uint8_t*)&offset; msg[1].slave = slave; msg[1].flags = IIC_M_RD; msg[1].len = sizeof(buf); msg[1].buf = buf; rdwr.nmsgs = 2; rdwr.msgs = msg; if (ioctl(fd, I2CRDWR, &rdwr) < 0) { switch (errno) { case ENXIO: // Doesn't exist return 1; case EBUSY: // Currently in use return 2; default: perror("ioctl(I2CRDWR) failed"); return -1; } } return 0; } int main(int argc, char **argv) { int c, r, fd, addr; char* dev = "/dev/iic0"; if (argc > 1) { dev = argv[1]; } if ((fd = open(dev, O_RDWR)) < 0) { perror("open failed"); exit(-1); } printf("Checking device: %s\n", dev); printf("Scan using 8-bit addresses, display using 7-bit addresses\n"); printf(" 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F\n"); for (r = 0; r < 8; r++) { printf("%02X", r * 16); for (c = 0; c < 16; c++) { addr = (r * 16) + c; switch (scan(fd, addr)) { case 0: printf(" %02X", addr); break; case 1: printf(" --"); break; case 2: printf(" UU"); break; default: break; } } printf("\n"); } close(fd); exit(0); }