i2c  1.4.2
RoadNarrows Robotics I2C Package
i2cread.c File Reference

Read an I2C device. 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 execRead (i2c_t *pI2C, int addr, int readlen)
 Execute I2C slave device read. More...
 
static void MainInit (int argc, char *argv[], i2c_t *pI2C)
 Command initialization. More...
 
int main (int argc, char *argv[])
 i2cread main() More...
 

Variables

static char * Argv0
 command namve
 
static int OptVerbose = 0
 verbose option
 
static char * OptDevName = "/dev/i2c/0"
 i2c bus device option
 
static int OptDevFd = -1
 opened i2c bus dev option
 
static int OptI2CAddr = -1
 slave device address option
 
static int OptI2CReadCount = 1
 read byte count option
 
static OptsPgmInfo_T I2CReadPgmInfo
 Program Information. More...
 
static OptsInfo_T I2CReadOptsInfo []
 Command Line Options Information.
 

Detailed Description

Read an I2C device.

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 i2cread.c.

Function Documentation

static int execRead ( i2c_t pI2C,
int  addr,
int  readlen 
)
static

Execute I2C slave device read.

Parameters
pI2CPointer to I2C handle.
addrSlave device address.
readlenNumber of bytes to read.
Returns
Returns number of bytes read ≥ 0 on success, -1 on failure.

Definition at line 177 of file i2cread.c.

References Argv0, i2c_read(), and OptVerbose.

Referenced by main().

178 {
179  int i;
180  byte_t readbuf[1024];
181 
182  if( readlen > sizeof(readbuf) )
183  {
184  readlen = sizeof(readbuf);
185  fprintf(stderr,
186  "%s: Warning: %d: I2C read length too long. Resetting to %u]\n",
187  Argv0, readlen, (uint_t)sizeof(readbuf));
188  }
189 
190  if( OptVerbose )
191  {
192  printf("%s Reading 0x%02x for %d bytes...\n", Argv0, (byte_t)addr, readlen);
193  }
194 
195  readlen = i2c_read(pI2C, (i2c_addr_t)addr, readbuf, (uint_t)readlen);
196 
197  if( readlen < 0 )
198  {
199  LOGSYSERROR("i2c_read()");
200  return RC_ERROR;
201  }
202 
203  if( OptVerbose )
204  {
205  printf("Read %d bytes: ", readlen);
206  }
207  for(i=0; i<readlen; ++i)
208  {
209  printf("0x%02x ", readbuf[i]);
210  }
211  if( (readlen > 0) || OptVerbose )
212  {
213  printf("\n");
214  }
215 
216  return readlen;
217 }
static int OptVerbose
verbose option
Definition: i2cread.c:75
ushort_t i2c_addr_t
I2C Device Address Type.
Definition: i2c.h:72
static char * Argv0
command namve
Definition: i2cread.c:70
int i2c_read(i2c_t *i2c, i2c_addr_t addr, byte_t *buf, uint_t len)
Read from an I2C device.
Definition: i2ccom.c:147
int main ( int  argc,
char *  argv[] 
)

i2cread main()

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

Definition at line 286 of file i2cread.c.

References execRead(), MainInit(), OptI2CAddr, and OptI2CReadCount.

287 {
288  i2c_t i2c;
289  int rc;
290 
291  MainInit(argc, argv, &i2c);
292 
293  rc = execRead(&i2c, OptI2CAddr, OptI2CReadCount);
294 
295  return rc > 0? 0: (rc == 0? 1: 2);
296 }
I2C python modules.
I2C Bus Handle Type.
Definition: i2c.h:79
static int OptI2CAddr
slave device address option
Definition: i2cread.c:78
static int OptI2CReadCount
read byte count option
Definition: i2cread.c:79
static int execRead(i2c_t *pI2C, int addr, int readlen)
Execute I2C slave device read.
Definition: i2cread.c:177
static void MainInit(int argc, char *argv[], i2c_t *pI2C)
Command initialization.
Definition: i2cread.c:226
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 226 of file i2cread.c.

References i2c_struct::addr, Argv0, i2c_struct::fd, I2C_ADDR_DEV_HIGH, I2C_ADDR_DEV_LOW, i2c_open(), I2CReadOptsInfo, I2CReadPgmInfo, OptDevFd, OptDevName, OptI2CAddr, OptVerbose, and PkgInfo.

Referenced by main().

227 {
228  // Name of this process
229  Argv0 = basename(argv[0]);
230 
231  // Get the environment
232  //EnvGet();
233 
234  // Parse input arguments
235  argv = OptsGet(Argv0, &PkgInfo, &I2CReadPgmInfo, I2CReadOptsInfo, true,
236  &argc, argv);
237 
238  // Final option checks
239  if( OptI2CAddr < 0x00 )
240  {
241  fprintf(stderr, "%s: Address option required\n", Argv0);
242  exit(EC_BAD_OPT);
243  }
245  {
246  fprintf(stderr, "%s: Address out of range: 0x%x\n", Argv0, OptI2CAddr);
247  exit(EC_BAD_OPT);
248  }
249 
250  // Opened device specified
251  if( OptDevFd >= 0 )
252  {
253  pI2C->fd = OptDevFd;
254  pI2C->addr = (ushort_t)(-1);
255  }
256 
257  // I2C Bus device specified
258  else
259  {
260  if( OptDevName == NULL || OptDevName[0] == 0 )
261  {
262  OptDevName = "/dev/i2c/0";
263  }
264 
265  if( OptVerbose )
266  {
267  printf("I2C device: %s\n\n", OptDevName);
268  }
269 
270  if( i2c_open(pI2C, OptDevName) < 0 )
271  {
272  LOGSYSERROR("%s: Failed to open.", OptDevName);
273  exit(EC_ERROR);
274  }
275  }
276 }
i2c_addr_t addr
address of the currently selected attached I2C device
Definition: i2c.h:82
static OptsInfo_T I2CReadOptsInfo[]
Command Line Options Information.
Definition: i2cread.c:102
static char * OptDevName
i2c bus device option
Definition: i2cread.c:76
int fd
opened file descriptor of the I2C bus device
Definition: i2c.h:81
static int OptVerbose
verbose option
Definition: i2cread.c:75
#define I2C_ADDR_DEV_HIGH
last available device address
Definition: i2c-dev.h:103
static int OptI2CAddr
slave device address option
Definition: i2cread.c:78
static char * Argv0
command namve
Definition: i2cread.c:70
static const PkgInfo_T PkgInfo
Definition: version.h:45
static int OptDevFd
opened i2c bus dev option
Definition: i2cread.c:77
static OptsPgmInfo_T I2CReadPgmInfo
Program Information.
Definition: i2cread.c:84
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

Variable Documentation

OptsPgmInfo_T I2CReadPgmInfo
static
Initial value:
=
{
.usage_args = "--address <addr>",
.synopsis = "Read <count> bytes from an I2C device.",
.long_desc =
"The %P command attempts to read <count> bytes from the specified I2C "
"device. All read bytes are printed to stdout in ASCII hex format.",
.diagnostics =
"Exit status is 0 if <count> bytes are read, 1 otherwise. "
"Exit status is >=2 if error(s) are encountered."
}

Program Information.

Definition at line 84 of file i2cread.c.

Referenced by MainInit().