i2c  1.4.2
RoadNarrows Robotics I2C Package
i2cscan.c File Reference

I2C Bus Scan. 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 scanCallback (i2c_t *pI2C, i2c_addr_t addr, void *context)
 Found scanned device callback. More...
 
static int execScan (i2c_t *pI2C)
 Execute I2C slave device scan. More...
 
static void MainInit (int argc, char *argv[], i2c_t *pI2C)
 Command initialization. More...
 
int main (int argc, char *argv[])
 i2cscan 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 OptsPgmInfo_T I2CScanPgmInfo
 Program Information. More...
 
static OptsInfo_T I2CScanOptsInfo []
 Command Line Options Information.
 

Detailed Description

I2C Bus Scan.

LastChangedDate
2009-09-09 09:44:12 -0600 (Wed, 09 Sep 2009)
Rev
130
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 i2cscan.c.

Function Documentation

static int execScan ( i2c_t pI2C)
static

Execute I2C slave device scan.

Parameters
pI2CPointer to I2C handle.
Returns
Returns number of devices found ≥ 0 on success, -1 on failure.

Definition at line 160 of file i2cscan.c.

References Argv0, i2c_scan(), OptVerbose, and scanCallback().

Referenced by main().

161 {
162  int n = 0;
163 
164  if( OptVerbose )
165  {
166  printf("%s scanning...\n", Argv0);
167  printf(" scanned devices: ");
168  }
169  n = i2c_scan(pI2C, scanCallback, NULL);
170 
171  if(n > 0)
172  {
173  printf("\n");
174  }
175 
176  if( OptVerbose )
177  {
178  printf(" number found: %d\n", n);
179  }
180 
181  return n;
182 }
int i2c_scan(i2c_t *i2c, int(*callback)(i2c_t *i2c, i2c_addr_t addr, void *context), void *context)
Scans the given I2C Bus to find all connected devices.
Definition: i2ccom.c:219
static int scanCallback(i2c_t *pI2C, i2c_addr_t addr, void *context)
Found scanned device callback.
Definition: i2cscan.c:147
static int OptVerbose
verbose option
Definition: i2cscan.c:71
static char * Argv0
command name
Definition: i2cscan.c:66
int main ( int  argc,
char *  argv[] 
)

i2cscan main()

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

Definition at line 239 of file i2cscan.c.

References execScan(), and MainInit().

240 {
241  i2c_t i2c;
242  int rc;
243 
244  MainInit(argc, argv, &i2c);
245 
246  rc = execScan(&i2c);
247 
248  return rc > 0? 0: 1;
249 }
I2C python modules.
I2C Bus Handle Type.
Definition: i2c.h:79
static int execScan(i2c_t *pI2C)
Execute I2C slave device scan.
Definition: i2cscan.c:160
static void MainInit(int argc, char *argv[], i2c_t *pI2C)
Command initialization.
Definition: i2cscan.c:191
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 191 of file i2cscan.c.

References i2c_struct::addr, Argv0, i2c_struct::fd, i2c_open(), I2CScanOptsInfo, I2CScanPgmInfo, OptDevFd, OptDevName, OptVerbose, and PkgInfo.

Referenced by main().

192 {
193  // Name of this process
194  Argv0 = basename(argv[0]);
195 
196  // Get the environment
197  //EnvGet();
198 
199  // Parse input arguments
200  argv = OptsGet(Argv0, &PkgInfo, &I2CScanPgmInfo, I2CScanOptsInfo, true,
201  &argc, argv);
202 
203  // Opened device specified
204  if( OptDevFd >= 0 )
205  {
206  pI2C->fd = OptDevFd;
207  pI2C->addr = (ushort_t)(-1);
208  }
209 
210  // I2C Bus device specified
211  else
212  {
213  if( OptDevName == NULL || OptDevName[0] == 0 )
214  {
215  OptDevName = "/dev/i2c/0";
216  }
217 
218  if( OptVerbose )
219  {
220  printf("I2C device: %s\n\n", OptDevName);
221  }
222 
223  if( i2c_open(pI2C, OptDevName) < 0 )
224  {
225  LOGSYSERROR("%s: Failed to open.", OptDevName);
226  exit(EC_ERROR);
227  }
228  }
229 }
i2c_addr_t addr
address of the currently selected attached I2C device
Definition: i2c.h:82
static OptsPgmInfo_T I2CScanPgmInfo
Program Information.
Definition: i2cscan.c:78
int fd
opened file descriptor of the I2C bus device
Definition: i2c.h:81
static char * OptDevName
i2c bus device option
Definition: i2cscan.c:72
static const PkgInfo_T PkgInfo
Definition: version.h:45
static int OptVerbose
verbose option
Definition: i2cscan.c:71
static int OptDevFd
opened i2c bus device fd option
Definition: i2cscan.c:73
int i2c_open(i2c_t *i2c, const char *device)
Open the host I2C Bus device.
Definition: i2ccom.c:118
static char * Argv0
command name
Definition: i2cscan.c:66
static OptsInfo_T I2CScanOptsInfo[]
Command Line Options Information.
Definition: i2cscan.c:97
static int scanCallback ( i2c_t pI2C,
i2c_addr_t  addr,
void *  context 
)
static

Found scanned device callback.

Parameters
pI2CPointer to I2C handle.
addrSlave device address.
contextUser provided context.
Returns
Returns 1.

Definition at line 147 of file i2cscan.c.

Referenced by execScan().

148 {
149  printf("0x%02x ", addr);
150  return 1;
151 }

Variable Documentation

OptsPgmInfo_T I2CScanPgmInfo
static
Initial value:
=
{
NULL,
.synopsis = "I2C Bus Scan",
.long_desc =
"The %P command scans the specified I2C Bus for all attached devices. "
"The I2C addresses of all found devices are written to stdout in ASCII hex "
"format.",
.diagnostics =
"Exit status is 0 if devices are found, 1 otherwise. Exit tatus is >= 2 if "
"error(s) are encountered."
}

Program Information.

Definition at line 78 of file i2cscan.c.

Referenced by MainInit().