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  * Additionally it is possible to write multiple values as a continuous write.
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   /* Necessary: data-address and values. This will be a bit more. */
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   /* Read values starting from the fifth argument (index 4) */
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; /* Don't forget address */
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 };