56 #include "rnr/rnrconfig.h" 86 .usage_args =
"--address <addr>",
88 .synopsis =
"Read <count> bytes from an I2C device.",
91 "The %P command attempts to read <count> bytes from the specified I2C " 92 "device. All read bytes are printed to stdout in ASCII hex format.",
95 "Exit status is 0 if <count> bytes are read, 1 otherwise. " 96 "Exit status is >=2 if error(s) are encountered." 106 .long_opt =
"address",
108 .has_arg = required_argument,
110 .fn_cvt = OptsCvtArgInt,
111 .fn_fmt = OptsFmtInt,
112 .arg_name =
"<addr>",
113 .opt_desc =
"I2C device address. REQUIRED" 120 .has_arg = required_argument,
123 .fn_cvt = OptsCvtArgInt,
124 .fn_fmt = OptsFmtInt,
125 .arg_name =
"<count>",
126 .opt_desc =
"I2C device read byte count" 131 .long_opt =
"device",
133 .has_arg = required_argument,
136 .fn_cvt = OptsCvtArgStr,
137 .fn_fmt = OptsFmtStr,
138 .arg_name =
"<device>",
139 .opt_desc =
"I2C device." 145 .short_opt = OPTS_NO_SHORT,
146 .has_arg = required_argument,
148 .fn_cvt = OptsCvtArgInt,
149 .fn_fmt = OptsFmtInt,
151 .opt_desc =
"Opened I2C device file descriptor." 156 .long_opt =
"verbose",
158 .has_arg = no_argument,
161 .fn_fmt = OptsFmtBool,
162 .opt_desc =
"Set print verbosity." 180 byte_t readbuf[1024];
182 if( readlen >
sizeof(readbuf) )
184 readlen =
sizeof(readbuf);
186 "%s: Warning: %d: I2C read length too long. Resetting to %u]\n",
187 Argv0, readlen, (uint_t)
sizeof(readbuf));
192 printf(
"%s Reading 0x%02x for %d bytes...\n",
Argv0, (byte_t)addr, readlen);
199 LOGSYSERROR(
"i2c_read()");
205 printf(
"Read %d bytes: ", readlen);
207 for(i=0; i<readlen; ++i)
209 printf(
"0x%02x ", readbuf[i]);
229 Argv0 = basename(argv[0]);
241 fprintf(stderr,
"%s: Address option required\n",
Argv0);
246 fprintf(stderr,
"%s: Address out of range: 0x%x\n",
Argv0,
OptI2CAddr);
254 pI2C->
addr = (ushort_t)(-1);
272 LOGSYSERROR(
"%s: Failed to open.",
OptDevName);
286 int main(
int argc,
char *argv[])
295 return rc > 0? 0: (rc == 0? 1: 2);
i2c_addr_t addr
address of the currently selected attached I2C device
static OptsInfo_T I2CReadOptsInfo[]
Command Line Options Information.
static char * OptDevName
i2c bus device option
int fd
opened file descriptor of the I2C bus device
static int OptVerbose
verbose option
ushort_t i2c_addr_t
I2C Device Address Type.
int main(int argc, char *argv[])
i2cread main()
#define I2C_ADDR_DEV_HIGH
last available device address
static int OptI2CAddr
slave device address option
static char * Argv0
command namve
static int OptI2CReadCount
read byte count option
static int execRead(i2c_t *pI2C, int addr, int readlen)
Execute I2C slave device read.
I2C character device interface.
static const PkgInfo_T PkgInfo
static void MainInit(int argc, char *argv[], i2c_t *pI2C)
Command initialization.
Package version information.
static int OptDevFd
opened i2c bus dev option
static OptsPgmInfo_T I2CReadPgmInfo
Program Information.
int i2c_open(i2c_t *i2c, const char *device)
Open the host I2C Bus device.
int i2c_read(i2c_t *i2c, i2c_addr_t addr, byte_t *buf, uint_t len)
Read from an I2C device.
#define I2C_ADDR_DEV_LOW
first available device address
Low-level I2C communication level.