Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:24:19

0001 /*
0002  * SPDX-License-Identifier: BSD-2-Clause
0003  *
0004  * Copyright (C) 2020 embedded brains GmbH & Co. KG
0005  *
0006  * Redistribution and use in source and binary forms, with or without
0007  * modification, are permitted provided that the following conditions
0008  * are met:
0009  * 1. Redistributions of source code must retain the above copyright
0010  *    notice, this list of conditions and the following disclaimer.
0011  * 2. Redistributions in binary form must reproduce the above copyright
0012  *    notice, this list of conditions and the following disclaimer in the
0013  *    documentation and/or other materials provided with the distribution.
0014  *
0015  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0016  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0017  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0018  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0019  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0020  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0021  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0022  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0023  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0024  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0025  * POSSIBILITY OF SUCH DAMAGE.
0026  */
0027 
0028 /*
0029  * The command implemented here has a similar interface like the one from Linux
0030  * i2c tools. Think of it as a heavily simplified version of them. Instead of
0031  * the bus number they expect a bus path.
0032  */
0033 
0034 #include <dev/i2c/i2c.h>
0035 #include <fcntl.h>
0036 #include <stdio.h>
0037 #include <stdlib.h>
0038 #include <string.h>
0039 
0040 #include <rtems/shell.h>
0041 
0042 static const char rtems_i2cdetect_shell_usage [] =
0043   "i2cdetect <I2C_BUS>\n"
0044   "\ttry to detect i2c devices on the given bus\n";
0045 
0046 static int rtems_i2cdetect_shell_main(int argc, char *argv[])
0047 {
0048   int fd;
0049   int rv;
0050   const char *bus;
0051   const uint16_t first = 1;
0052   const uint16_t last = 0x7f;
0053   uint16_t current;
0054 
0055   if (argc != 2 || strcmp(argv[1], "-h") == 0) {
0056     printf(rtems_i2cdetect_shell_usage);
0057     return 1;
0058   }
0059 
0060   bus = argv[1];
0061   fd = open(bus, O_RDWR);
0062   if (fd < 0) {
0063     perror("Couldn't open bus");
0064     return 1;
0065   }
0066 
0067   printf("    x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF\n"
0068          "0x    ");
0069   for (current = first; current <= last; ++current) {
0070     i2c_msg msg = {
0071       .addr = current,
0072       .flags = 0,
0073       .len = 0,
0074       .buf = NULL,
0075     };
0076 
0077     struct i2c_rdwr_ioctl_data payload = {
0078       .msgs = &msg,
0079       .nmsgs = 1,
0080     };
0081 
0082     if ((current & 0x0F) == 0) {
0083       printf("\n%1xx ", current >> 4);
0084     }
0085 
0086     rv = ioctl(fd, I2C_RDWR, &payload);
0087     if (rv < 0) {
0088       if (errno != EIO) {
0089         perror("ioctl failed");
0090       }
0091       printf(" --");
0092     } else {
0093       printf(" %02x", current);
0094     }
0095   }
0096   printf("\n");
0097   close(fd);
0098 
0099   return 0;
0100 }
0101 
0102 rtems_shell_cmd_t rtems_shell_I2CDETECT_Command = {
0103   .name = "i2cdetect",
0104   .usage = rtems_i2cdetect_shell_usage,
0105   .topic = "misc",
0106   .command = rtems_i2cdetect_shell_main,
0107 };