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
0035
0036 #include <dev/i2c/i2c.h>
0037 #include <fcntl.h>
0038 #include <stdio.h>
0039 #include <stdlib.h>
0040
0041 #include <rtems/shell.h>
0042
0043 static const char rtems_i2cset_shell_usage [] =
0044 "i2cset <I2C_BUS> <CHIP-ADDRESS> <DATA-ADDRESS> <VALUE> [<VALUE> [...]]\n"
0045 "\tset one byte of an EEPROM like i2c device\n";
0046
0047 static int
0048 rtems_i2cset_shell_main(int argc, char *argv[])
0049 {
0050 int fd;
0051 int rv;
0052 const char *bus;
0053 uint16_t chip_address;
0054
0055 uint8_t writebuff[argc];
0056 size_t len;
0057 size_t i;
0058 i2c_msg msgs[] = {{
0059 .flags = 0,
0060 .buf = writebuff,
0061 .len = 0,
0062 }};
0063 struct i2c_rdwr_ioctl_data payload = {
0064 .msgs = msgs,
0065 .nmsgs = sizeof(msgs)/sizeof(msgs[0]),
0066 };
0067
0068 if (argc < 5) {
0069 printf(rtems_i2cset_shell_usage);
0070 return 1;
0071 }
0072
0073 errno = 0;
0074 chip_address = (uint16_t) strtoul(argv[2], NULL, 0);
0075 if (errno != 0) {
0076 perror("Couldn't read CHIP_ADDRESS");
0077 return 1;
0078 }
0079 msgs[0].addr = chip_address;
0080
0081 errno = 0;
0082 writebuff[0] = (uint8_t) strtoul(argv[3], NULL, 0);
0083 if (errno != 0) {
0084 perror("Couldn't read DATA_ADDRESS");
0085 return 1;
0086 }
0087
0088
0089 i = 4;
0090 len = 0;
0091 while (i < argc) {
0092 errno = 0;
0093 writebuff[len + 1] = (uint8_t) strtoul(argv[i], NULL, 0);
0094 if (errno != 0) {
0095 perror("Couldn't read VALUE");
0096 return 1;
0097 }
0098 ++i;
0099 ++len;
0100 }
0101 msgs[0].len = len + 1;
0102
0103 bus = argv[1];
0104 fd = open(bus, O_RDWR);
0105 if (fd < 0) {
0106 perror("Couldn't open bus");
0107 return 1;
0108 }
0109
0110 rv = ioctl(fd, I2C_RDWR, &payload);
0111 if (rv < 0) {
0112 perror("ioctl failed");
0113 }
0114 close(fd);
0115
0116 return rv;
0117 }
0118
0119 rtems_shell_cmd_t rtems_shell_I2CSET_Command = {
0120 .name = "i2cset",
0121 .usage = rtems_i2cset_shell_usage,
0122 .topic = "misc",
0123 .command = rtems_i2cset_shell_main,
0124 };