56 #include "rnr/rnrconfig.h" 88 .usage_args =
"byte1 [byte2 ...]",
90 .synopsis =
"Do a write/read transaction for a device on the I2C Bus.",
93 "The %P command performs a message request/response transaction with " 94 "a device of the specified address on the I2C Bus. " 95 "The specified bytes are writen to the device. Then <count> bytes are " 97 "The bytes read are printed to stdout in ASCII hex format.",
100 "Exit status is 0 if the transaction was completed successfully. " 101 "Exist status is >= 2 if error(s) are encountered.",
111 .long_opt =
"address",
113 .has_arg = required_argument,
115 .fn_cvt = OptsCvtArgInt,
116 .fn_fmt = OptsFmtInt,
117 .arg_name =
"<addr>",
118 .opt_desc =
"I2C device address. REQUIRED" 125 .has_arg = required_argument,
128 .fn_cvt = OptsCvtArgInt,
129 .fn_fmt = OptsFmtInt,
130 .arg_name =
"<count>",
131 .opt_desc =
"I2C device read byte count" 136 .long_opt =
"device",
138 .has_arg = required_argument,
141 .fn_cvt = OptsCvtArgStr,
142 .fn_fmt = OptsFmtStr,
143 .arg_name =
"<device>",
144 .opt_desc =
"I2C device." 150 .short_opt = OPTS_NO_SHORT,
151 .has_arg = required_argument,
153 .fn_cvt = OptsCvtArgInt,
154 .fn_fmt = OptsFmtInt,
156 .opt_desc =
"Opened I2C device file descriptor." 161 .long_opt =
"verbose",
163 .has_arg = no_argument,
166 .fn_fmt = OptsFmtBool,
167 .opt_desc =
"Set print verbosity." 187 byte_t readbuf[1024];
191 if( readlen >
sizeof(readbuf) )
193 readlen =
sizeof(readbuf);
195 "%s: Warning: %d: I2C read length too long. Resetting to %u\n",
196 Argv0, readlen, (uint_t)
sizeof(readbuf));
201 printf(
"%s: i2cdev 0x%02x: transaction write %u bytes, read %u bytes\n",
202 Argv0, (byte_t)addr, (uint_t)writelen, (uint_t)readlen);
206 readbuf, (uint_t)readlen);
210 LOGSYSERROR(
"i2c_transfer()");
218 printf(
"Response %d bytes: ", readlen);
220 for(i=0; i<readlen; ++i)
222 printf(
"0x%02x ", readbuf[i]);
246 if( (s == NULL) || (*s == 0) )
251 lVal = strtol(s, &sEnd, 0);
263 *pVal = (uint_t)lVal;
284 else if( uVal > 0xff )
290 *pByte = (byte_t)uVal;
310 printf(
"%s Error: At least one byte must be specified\n",
Argv0);
313 else if( argc > max )
316 "%s: Warning: %d: I2C write. Only %u bytes will be written.\n",
317 Argv0, argc, (uint_t)max);
320 for(i=0; i<argc && i<max; ++i)
324 printf(
"%s Error: byte %d = '%s': bad byte value\n",
Argv0, i, argv[i]);
342 Argv0 = basename(argv[0]);
354 fprintf(stderr,
"%s: Address option required\n",
Argv0);
359 fprintf(stderr,
"%s: Address out of range: 0x%x\n",
Argv0,
OptI2CAddr);
370 pI2C->
addr = (ushort_t)(-1);
388 LOGSYSERROR(
"%s: Failed to open.",
OptDevName);
402 int main(
int argc,
char *argv[])
i2c_addr_t addr
address of the currently selected attached I2C device
static int StrToByte(const char *s, byte_t *pByte)
Convert string to byte.
int fd
opened file descriptor of the I2C bus device
ushort_t i2c_addr_t
I2C Device Address Type.
static byte_t ArgWriteBuf[1024]
write buffer argument
static int StrToUnsigned(const char *s, uint_t *pVal)
Convert string to unsigned integer.
#define I2C_ADDR_DEV_HIGH
last available device address
static OptsInfo_T I2CTransOptsInfo[]
Command Line Options Information.
static void MainInit(int argc, char *argv[], i2c_t *pI2C)
Command initialization.
static void ArgsGet(int argc, char *argv[])
Get write bytes from command-line arguments.
static int OptVerbose
verbose option
int main(int argc, char *argv[])
i2ctrans main()
static char * OptDevName
i2c bus device option
int i2c_transfer(i2c_t *i2c, i2c_addr_t addr, const byte_t *write_buf, uint_t write_len, byte_t *read_buf, uint_t read_len)
Perform a transfer with an I2C device.
I2C character device interface.
static int ArgWriteLen
write byte count argument
static const PkgInfo_T PkgInfo
static int OptDevFd
opened i2c bus device fd option
static char * Argv0
command name
Package version information.
static OptsPgmInfo_T I2CTransPgmInfo
Program Information.
static int OptReadCount
read byte count option
int i2c_open(i2c_t *i2c, const char *device)
Open the host I2C Bus device.
#define I2C_ADDR_DEV_LOW
first available device address
static int OptI2CAddr
slave i2c device address option
static int execTrans(i2c_t *pI2C, int addr, byte_t writebuf[], int writelen, int readlen)
Execute I2C slave device write/read transaction.
Low-level I2C communication level.