i2c  1.4.2
RoadNarrows Robotics I2C Package
i2ctrans.c File Reference

Do a write/read transaction for a device on the I2C Bus. More...

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <errno.h>
#include <ctype.h>
#include <libgen.h>
#include <unistd.h>
#include "rnr/rnrconfig.h"
#include "rnr/log.h"
#include "rnr/opts.h"
#include "rnr/i2c-dev.h"
#include "rnr/i2c.h"
#include "version.h"

Go to the source code of this file.

Functions

static int execTrans (i2c_t *pI2C, int addr, byte_t writebuf[], int writelen, int readlen)
 Execute I2C slave device write/read transaction. More...
 
static int StrToUnsigned (const char *s, uint_t *pVal)
 Convert string to unsigned integer. More...
 
static int StrToByte (const char *s, byte_t *pByte)
 Convert string to byte. More...
 
static void ArgsGet (int argc, char *argv[])
 Get write bytes from command-line arguments. More...
 
static void MainInit (int argc, char *argv[], i2c_t *pI2C)
 Command initialization. More...
 
int main (int argc, char *argv[])
 i2ctrans main() More...
 

Variables

static char * Argv0
 command name
 
static int OptVerbose = 0
 verbose option
 
static char * OptDevName = "/dev/i2c/0"
 i2c bus device option
 
static int OptDevFd = -1
 opened i2c bus device fd option
 
static int OptI2CAddr = -1
 slave i2c device address option
 
static int OptReadCount = 1
 read byte count option
 
static byte_t ArgWriteBuf [1024]
 write buffer argument
 
static int ArgWriteLen = 0
 write byte count argument
 
static OptsPgmInfo_T I2CTransPgmInfo
 Program Information. More...
 
static OptsInfo_T I2CTransOptsInfo []
 Command Line Options Information.
 

Detailed Description

Do a write/read transaction for a device on the I2C Bus.

LastChangedDate
2011-09-12 16:08:28 -0600 (Mon, 12 Sep 2011)
Rev
1279
Author
Robin Knight (robin.nosp@m..kni.nosp@m.ght@r.nosp@m.oadn.nosp@m.arrow.nosp@m.s.co.nosp@m.m)

Definition in file i2ctrans.c.

Function Documentation

static void ArgsGet ( int  argc,
char *  argv[] 
)
static

Get write bytes from command-line arguments.

Parameters
argcCommand-line argument count.
argvCommand-line arguments.
Returns
Exits on error.

Definition at line 303 of file i2ctrans.c.

References Argv0, ArgWriteBuf, ArgWriteLen, and StrToByte().

Referenced by MainInit().

304 {
305  int i;
306  size_t max = sizeof(ArgWriteBuf);
307 
308  if( argc == 0 )
309  {
310  printf("%s Error: At least one byte must be specified\n", Argv0);
311  exit(EC_ERROR);
312  }
313  else if( argc > max )
314  {
315  fprintf(stderr,
316  "%s: Warning: %d: I2C write. Only %u bytes will be written.\n",
317  Argv0, argc, (uint_t)max);
318  }
319 
320  for(i=0; i<argc && i<max; ++i)
321  {
322  if( StrToByte(argv[i], ArgWriteBuf+i) != OK )
323  {
324  printf("%s Error: byte %d = '%s': bad byte value\n", Argv0, i, argv[i]);
325  exit(EC_ERROR);
326  }
327  }
328 
329  ArgWriteLen = i;
330 }
static int StrToByte(const char *s, byte_t *pByte)
Convert string to byte.
Definition: i2ctrans.c:276
static byte_t ArgWriteBuf[1024]
write buffer argument
Definition: i2ctrans.c:80
static int ArgWriteLen
write byte count argument
Definition: i2ctrans.c:81
static char * Argv0
command name
Definition: i2ctrans.c:70
static int execTrans ( i2c_t pI2C,
int  addr,
byte_t  writebuf[],
int  writelen,
int  readlen 
)
static

Execute I2C slave device write/read transaction.

Parameters
pI2CPointer to I2C handle.
addrSlave device address.
writebufBuffer to write.
writelenNumber of write bytes.
readlenNumber of bytes to read.
Returns
Returns number of bytes read ≥ 0 on success, RC_ERROR on failure.

Definition at line 184 of file i2ctrans.c.

References Argv0, i2c_transfer(), and OptVerbose.

Referenced by main().

186 {
187  byte_t readbuf[1024];
188  int n;
189  int i;
190 
191  if( readlen > sizeof(readbuf) )
192  {
193  readlen = sizeof(readbuf);
194  fprintf(stderr,
195  "%s: Warning: %d: I2C read length too long. Resetting to %u\n",
196  Argv0, readlen, (uint_t)sizeof(readbuf));
197  }
198 
199  if( OptVerbose )
200  {
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);
203  }
204 
205  n = i2c_transfer(pI2C, (i2c_addr_t)addr, writebuf, (uint_t)writelen,
206  readbuf, (uint_t)readlen);
207 
208  if( n < 0 )
209  {
210  LOGSYSERROR("i2c_transfer()");
211  return RC_ERROR;
212  }
213 
214  else
215  {
216  if( OptVerbose )
217  {
218  printf("Response %d bytes: ", readlen);
219  }
220  for(i=0; i<readlen; ++i)
221  {
222  printf("0x%02x ", readbuf[i]);
223  }
224  if( (readlen > 0) || OptVerbose )
225  {
226  printf("\n");
227  }
228  }
229 
230  return n;
231 }
ushort_t i2c_addr_t
I2C Device Address Type.
Definition: i2c.h:72
static int OptVerbose
verbose option
Definition: i2ctrans.c:75
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.
Definition: i2ccom.c:171
static char * Argv0
command name
Definition: i2ctrans.c:70
int main ( int  argc,
char *  argv[] 
)

i2ctrans main()

Parameters
argcCount of command-line options and arguments.
argvArray of command-line options and arguments.
Returns
Exit value.

Definition at line 402 of file i2ctrans.c.

References ArgWriteBuf, ArgWriteLen, execTrans(), MainInit(), OptI2CAddr, and OptReadCount.

403 {
404  i2c_t i2c;
405  int rc;
406 
407  MainInit(argc, argv, &i2c);
408 
410 
411  return rc > 0? 0: 2;
412 }
I2C python modules.
static byte_t ArgWriteBuf[1024]
write buffer argument
Definition: i2ctrans.c:80
I2C Bus Handle Type.
Definition: i2c.h:79
static void MainInit(int argc, char *argv[], i2c_t *pI2C)
Command initialization.
Definition: i2ctrans.c:339
static int ArgWriteLen
write byte count argument
Definition: i2ctrans.c:81
static int OptReadCount
read byte count option
Definition: i2ctrans.c:79
static int OptI2CAddr
slave i2c device address option
Definition: i2ctrans.c:78
static int execTrans(i2c_t *pI2C, int addr, byte_t writebuf[], int writelen, int readlen)
Execute I2C slave device write/read transaction.
Definition: i2ctrans.c:184
static void MainInit ( int  argc,
char *  argv[],
i2c_t pI2C 
)
static

Command initialization.

Parameters
argcCommand-line argument count.
argvCommand-line arguments.
pI2CPointer to I2C handle.

Definition at line 339 of file i2ctrans.c.

References i2c_struct::addr, ArgsGet(), Argv0, i2c_struct::fd, I2C_ADDR_DEV_HIGH, I2C_ADDR_DEV_LOW, i2c_open(), I2CTransOptsInfo, I2CTransPgmInfo, OptDevFd, OptDevName, OptI2CAddr, OptVerbose, and PkgInfo.

Referenced by main().

340 {
341  // Name of this process
342  Argv0 = basename(argv[0]);
343 
344  // Get the environment
345  //EnvGet();
346 
347  // Parse input arguments
348  argv = OptsGet(Argv0, &PkgInfo, &I2CTransPgmInfo, I2CTransOptsInfo, true,
349  &argc, argv);
350 
351  // Final option checks
352  if( OptI2CAddr < 0 )
353  {
354  fprintf(stderr, "%s: Address option required\n", Argv0);
355  exit(EC_BAD_OPT);
356  }
358  {
359  fprintf(stderr, "%s: Address out of range: 0x%x\n", Argv0, OptI2CAddr);
360  exit(EC_BAD_OPT);
361  }
362 
363  // Convert command-line arguments
364  ArgsGet(argc, argv);
365 
366  // Opened device specified
367  if( OptDevFd >= 0 )
368  {
369  pI2C->fd = OptDevFd;
370  pI2C->addr = (ushort_t)(-1);
371  }
372 
373  // I2C Bus device specified
374  else
375  {
376  if( OptDevName == NULL || OptDevName[0] == 0 )
377  {
378  OptDevName = "/dev/i2c/0";
379  }
380 
381  if( OptVerbose )
382  {
383  printf("I2C device: %s\n\n", OptDevName);
384  }
385 
386  if( i2c_open(pI2C, OptDevName) < 0 )
387  {
388  LOGSYSERROR("%s: Failed to open.", OptDevName);
389  exit(EC_ERROR);
390  }
391  }
392 }
i2c_addr_t addr
address of the currently selected attached I2C device
Definition: i2c.h:82
int fd
opened file descriptor of the I2C bus device
Definition: i2c.h:81
#define I2C_ADDR_DEV_HIGH
last available device address
Definition: i2c-dev.h:103
static OptsInfo_T I2CTransOptsInfo[]
Command Line Options Information.
Definition: i2ctrans.c:107
static void ArgsGet(int argc, char *argv[])
Get write bytes from command-line arguments.
Definition: i2ctrans.c:303
static int OptVerbose
verbose option
Definition: i2ctrans.c:75
static char * OptDevName
i2c bus device option
Definition: i2ctrans.c:76
static const PkgInfo_T PkgInfo
Definition: version.h:45
static int OptDevFd
opened i2c bus device fd option
Definition: i2ctrans.c:77
static char * Argv0
command name
Definition: i2ctrans.c:70
static OptsPgmInfo_T I2CTransPgmInfo
Program Information.
Definition: i2ctrans.c:86
int i2c_open(i2c_t *i2c, const char *device)
Open the host I2C Bus device.
Definition: i2ccom.c:118
#define I2C_ADDR_DEV_LOW
first available device address
Definition: i2c-dev.h:101
static int OptI2CAddr
slave i2c device address option
Definition: i2ctrans.c:78
static int StrToByte ( const char *  s,
byte_t *  pByte 
)
static

Convert string to byte.

Parameters
sString.
[out]pBytePointer to converted value.
Returns
Returns OK on success, RC_ERROR on error.

Definition at line 276 of file i2ctrans.c.

References StrToUnsigned().

Referenced by ArgsGet().

277 {
278  uint_t uVal;
279 
280  if( StrToUnsigned(s, &uVal) != OK )
281  {
282  return RC_ERROR;
283  }
284  else if( uVal > 0xff )
285  {
286  return RC_ERROR;
287  }
288  else
289  {
290  *pByte = (byte_t)uVal;
291  return OK;
292  }
293 }
static int StrToUnsigned(const char *s, uint_t *pVal)
Convert string to unsigned integer.
Definition: i2ctrans.c:241
static int StrToUnsigned ( const char *  s,
uint_t *  pVal 
)
static

Convert string to unsigned integer.

Parameters
sString.
[out]pValPointer to converted value.
Returns
Returns OK on success, RC_ERROR on error.

Definition at line 241 of file i2ctrans.c.

Referenced by StrToByte().

242 {
243  long lVal;
244  char *sEnd;
245 
246  if( (s == NULL) || (*s == 0) )
247  {
248  return RC_ERROR;
249  }
250 
251  lVal = strtol(s, &sEnd, 0);
252 
253  if( *sEnd != 0 )
254  {
255  return RC_ERROR;
256  }
257  else if( lVal < 0 )
258  {
259  return RC_ERROR;
260  }
261  else
262  {
263  *pVal = (uint_t)lVal;
264  return OK;
265  }
266 }

Variable Documentation

OptsPgmInfo_T I2CTransPgmInfo
static
Initial value:
=
{
.usage_args = "byte1 [byte2 ...]",
.synopsis = "Do a write/read transaction for a device on the I2C Bus.",
.long_desc =
"The %P command performs a message request/response transaction with "
"a device of the specified address on the I2C Bus. "
"The specified bytes are writen to the device. Then <count> bytes are "
"read.\n\n"
"The bytes read are printed to stdout in ASCII hex format.",
.diagnostics =
"Exit status is 0 if the transaction was completed successfully. "
"Exist status is >= 2 if error(s) are encountered.",
}

Program Information.

Definition at line 86 of file i2ctrans.c.

Referenced by MainInit().