56 #include "rnr/rnrconfig.h" 87 .usage_args =
"byte1 [byte2 ...]",
89 .synopsis =
"Write bytes to a device on an I2C Bus.",
92 "The %P command write the bytes arguments to an I2C Bus for a device " 93 "with the specified address.",
96 "Exit status is 0 if the bytes are successfully written. " 97 "Exist status is >= 2 if error(s) are encountered.",
107 .long_opt =
"address",
109 .has_arg = required_argument,
111 .fn_cvt = OptsCvtArgInt,
112 .fn_fmt = OptsFmtInt,
113 .arg_name =
"<addr>",
114 .opt_desc =
"I2C device address. REQUIRED" 119 .long_opt =
"device",
121 .has_arg = required_argument,
124 .fn_cvt = OptsCvtArgStr,
125 .fn_fmt = OptsFmtStr,
126 .arg_name =
"<device>",
127 .opt_desc =
"I2C device." 133 .short_opt = OPTS_NO_SHORT,
134 .has_arg = required_argument,
136 .fn_cvt = OptsCvtArgInt,
137 .fn_fmt = OptsFmtInt,
139 .opt_desc =
"Opened I2C device file descriptor." 144 .long_opt =
"verbose",
146 .has_arg = no_argument,
149 .fn_fmt = OptsFmtBool,
150 .opt_desc =
"Set print verbosity." 172 printf(
"%s: i2cdev 0x%02x: writting %u bytes...\n",
173 Argv0, (byte_t)addr, writelen);
180 LOGSYSERROR(
"i2c_write()");
186 printf(
"Wrote %d bytes\n", n);
191 LOGERROR(
"wrote only %d bytes of %d", n, writelen);
210 if( (s == NULL) || (*s == 0) )
215 lVal = strtol(s, &sEnd, 0);
227 *pVal = (uint_t)lVal;
248 else if( uVal > 0xff )
254 *pByte = (byte_t)uVal;
274 printf(
"%s Error: At least one byte must be specified\n",
Argv0);
277 else if( argc > max )
280 "%s: Warning: %d: I2C write. Only %u bytes will be written.\n",
281 Argv0, argc, (uint_t)max);
284 for(i=0; i<argc && i<max; ++i)
288 printf(
"%s Error: byte %d = '%s': bad byte value\n",
Argv0, i, argv[i]);
306 Argv0 = basename(argv[0]);
318 fprintf(stderr,
"%s: Address option required\n",
Argv0);
323 fprintf(stderr,
"%s: Address out of range: 0x%x\n",
Argv0,
OptI2CAddr);
334 pI2C->
addr = (ushort_t)(-1);
352 LOGSYSERROR(
"%s: Failed to open.",
OptDevName);
366 int main(
int argc,
char *argv[])
i2c_addr_t addr
address of the currently selected attached I2C device
static int StrToUnsigned(const char *s, uint_t *pVal)
Convert string to unsigned integer.
int i2c_write(i2c_t *i2c, i2c_addr_t addr, const byte_t *buf, uint_t len)
Write to an I2C device.
int fd
opened file descriptor of the I2C bus device
ushort_t i2c_addr_t
I2C Device Address Type.
static void ArgsGet(int argc, char *argv[])
Get write bytes from command-line arguments.
static int OptI2CAddr
slave i2c device address
#define I2C_ADDR_DEV_HIGH
last available device address
static byte_t ArgWriteBuf[1024]
write buffer argument
static int OptVerbose
verbose option
static char * Argv0
command name
I2C character device interface.
static void MainInit(int argc, char *argv[], i2c_t *pI2C)
Command initialization.
static int ArgWriteLen
write byte count arguement
static const PkgInfo_T PkgInfo
static OptsInfo_T I2CWriteOptsInfo[]
Command Line Options Information.
static int StrToByte(const char *s, byte_t *pByte)
Convert string to byte.
Package version information.
static int OptDevFd
opened i2c buse device fd option
int i2c_open(i2c_t *i2c, const char *device)
Open the host I2C Bus device.
int main(int argc, char *argv[])
i2cwrite main()
#define I2C_ADDR_DEV_LOW
first available device address
static OptsPgmInfo_T I2CWritePgmInfo
Program Information.
static char * OptDevName
i2c bus device option
static int execWrite(i2c_t *pI2C, int addr, byte_t writebuf[], int writelen)
Execute I2C slave device write.
Low-level I2C communication level.