summaryrefslogtreecommitdiffstats
path: root/cpukit/libmisc/shell/main_i2cget.c
blob: 5726c6ea143391790f5aae33ecf15e404d856bfd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
 * SPDX-License-Identifier: BSD-2-Clause
 *
 * Copyright (C) 2020 embedded brains GmbH & Co. KG
 *
 * 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 COPYRIGHT HOLDERS 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 COPYRIGHT OWNER 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.
 */

/*
 * The command implemented here has a similar interface like the one from Linux
 * i2c tools. Think of it as a heavily simplified version of them. Instead of
 * the bus number they expect a bus path.
 *
 * Additionally the i2cget has a continuous read mode that isn't available on
 * Linux but does something similar to i2cdump.
 */

#include <dev/i2c/i2c.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>

#include <rtems/shell.h>

static const char rtems_i2cget_shell_usage [] =
  "i2cget <I2C_BUS> <CHIP-ADDRESS> <DATA-ADDRESS> [<NR-BYTES>]\n"
  "\tGet one or more bytes from an EEPROM like i2c device.\n"
  "\tNote that multiple bytes will be read in continuous mode.\n";

static int read_bytes(
  int fd,
  uint16_t i2c_address,
  uint8_t data_address,
  uint16_t nr_bytes
)
{
  int rv;
  uint8_t value[nr_bytes];
  i2c_msg msgs[] = {{
    .addr = i2c_address,
    .flags = 0,
    .buf = &data_address,
    .len = 1,
  }, {
    .addr = i2c_address,
    .flags = I2C_M_RD,
    .buf = value,
    .len = nr_bytes,
  }};
  struct i2c_rdwr_ioctl_data payload = {
    .msgs = msgs,
    .nmsgs = sizeof(msgs)/sizeof(msgs[0]),
  };
  uint16_t i;

  rv = ioctl(fd, I2C_RDWR, &payload);
  if (rv < 0) {
    perror("ioctl failed");
  } else {
    for (i = 0; i < nr_bytes; ++i) {
      printf("0x%02x ", value[i]);
    }
    printf("\n");
  }

  return rv;
}

static int rtems_i2cget_shell_main(int argc, char *argv[])
{
  int fd;
  int rv;
  const char *bus;
  uint16_t chip_address;
  uint8_t data_address;
  uint16_t nr_bytes;

  if (argc < 4 || argc > 5) {
    printf(rtems_i2cget_shell_usage);
    return 1;
  }

  errno = 0;
  chip_address = (uint16_t) strtoul(argv[2], NULL, 0);
  if (errno != 0) {
    perror("Couldn't read chip address");
    return 1;
  }

  errno = 0;
  data_address = (uint8_t) strtoul(argv[3], NULL, 0);
  if (errno != 0) {
    perror("Couldn't read data address");
    return 1;
  }

  nr_bytes = 1;
  if (argc == 5) {
    errno = 0;
    nr_bytes = (uint16_t) strtoul(argv[4], NULL, 0);
    if (errno != 0) {
      perror("Couldn't read number of bytes");
      return 1;
    }
  }

  bus = argv[1];
  fd = open(bus, O_RDWR);
  if (fd < 0) {
    perror("Couldn't open bus");
    return 1;
  }

  rv = read_bytes(fd, chip_address, data_address, nr_bytes);

  close(fd);

  return rv;
}

rtems_shell_cmd_t rtems_shell_I2CGET_Command = {
  .name = "i2cget",
  .usage = rtems_i2cget_shell_usage,
  .topic = "misc",
  .command = rtems_i2cget_shell_main,
};