File indexing completed on 2025-05-11 08:24:19
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
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 };