i2c  1.4.2
RoadNarrows Robotics I2C Package
i2cscan.c
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // Package: RCB3
4 //
5 // File: i2cscan.c
6 //
7 /*! \file
8  *
9  * $LastChangedDate: 2009-09-09 09:44:12 -0600 (Wed, 09 Sep 2009) $
10  * $Rev: 130 $
11  *
12  * \brief I2C Bus Scan.
13  *
14  * \author Robin Knight (robin.knight@roadnarrows.com)
15  *
16  * \copyright
17  * \h_copy 2007-2017. RoadNarrows LLC.\n
18  * http://www.roadnarrows.com\n
19  * All Rights Reserved
20  */
21 // Permission is hereby granted, without written agreement and without
22 // license or royalty fees, to use, copy, modify, and distribute this
23 // software and its documentation for any purpose, provided that
24 // (1) The above copyright notice and the following two paragraphs
25 // appear in all copies of the source code and (2) redistributions
26 // including binaries reproduces these notices in the supporting
27 // documentation. Substantial modifications to this software may be
28 // copyrighted by their authors and need not follow the licensing terms
29 // described here, provided that the new terms are clearly indicated in
30 // all files where they apply.
31 //
32 // IN NO EVENT SHALL THE AUTHOR, ROADNARROWS LLC, OR ANY MEMBERS/EMPLOYEES
33 // OF ROADNARROW LLC OR DISTRIBUTORS OF THIS SOFTWARE BE LIABLE TO ANY
34 // PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
35 // DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION,
36 // EVEN IF THE AUTHORS OR ANY OF THE ABOVE PARTIES HAVE BEEN ADVISED OF
37 // THE POSSIBILITY OF SUCH DAMAGE.
38 //
39 // THE AUTHOR AND ROADNARROWS LLC SPECIFICALLY DISCLAIM ANY WARRANTIES,
40 // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
41 // FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN
42 // "AS IS" BASIS, AND THE AUTHORS AND DISTRIBUTORS HAVE NO OBLIGATION TO
43 // PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
44 //
45 ////////////////////////////////////////////////////////////////////////////////
46 
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <stdarg.h>
50 #include <string.h>
51 #include <errno.h>
52 #include <ctype.h>
53 #include <libgen.h>
54 #include <unistd.h>
55 
56 #include "rnr/rnrconfig.h"
57 #include "rnr/log.h"
58 #include "rnr/opts.h"
59 
60 #include "rnr/i2c-dev.h"
61 #include "rnr/i2c.h"
62 
63 #include "version.h"
64 
65 // The Command
66 static char *Argv0; ///< command name
67 
68 //
69 // Command Option Values
70 //
71 static int OptVerbose = 0; ///< verbose option
72 static char *OptDevName = "/dev/i2c/0"; ///< i2c bus device option
73 static int OptDevFd = -1; ///< opened i2c bus device fd option
74 
75 /*!
76  * \brief Program Information
77  */
78 static OptsPgmInfo_T I2CScanPgmInfo =
79 {
80  NULL,
81 
82  .synopsis = "I2C Bus Scan",
83 
84  .long_desc =
85  "The %P command scans the specified I2C Bus for all attached devices. "
86  "The I2C addresses of all found devices are written to stdout in ASCII hex "
87  "format.",
88 
89  .diagnostics =
90  "Exit status is 0 if devices are found, 1 otherwise. Exit tatus is >= 2 if "
91  "error(s) are encountered."
92 };
93 
94 /*!
95  * \brief Command Line Options Information
96  */
97 static OptsInfo_T I2CScanOptsInfo[] =
98 {
99  // I2C device name
100  {
101  .long_opt = "device",
102  .short_opt = 'd',
103  .has_arg = required_argument,
104  .has_default= true,
105  .opt_addr = &OptDevName,
106  .fn_cvt = OptsCvtArgStr,
107  .fn_fmt = OptsFmtStr,
108  .arg_name = "<device>",
109  .opt_desc = "I2C device."
110  },
111 
112  // opend I2C file descriptor
113  {
114  .long_opt = "fd",
115  .short_opt = OPTS_NO_SHORT,
116  .has_arg = required_argument,
117  .opt_addr = &OptDevFd,
118  .fn_cvt = OptsCvtArgInt,
119  .fn_fmt = OptsFmtInt,
120  .arg_name = "<n>",
121  .opt_desc = "Opened I2C device file descriptor."
122  },
123 
124  // verbose printing
125  {
126  .long_opt = "verbose",
127  .short_opt = 'v',
128  .has_arg = no_argument,
129  .has_default= true,
130  .opt_addr = &OptVerbose,
131  .fn_fmt = OptsFmtBool,
132  .opt_desc = "Set print verbosity."
133  },
134 
135  {NULL, }
136 };
137 
138 /*!
139  * \brief Found scanned device callback.
140  *
141  * \param pI2C Pointer to \h_i2c handle.
142  * \param addr Slave device address.
143  * \param context User provided context.
144  *
145  * \return Returns 1.
146  */
147 static int scanCallback(i2c_t *pI2C, i2c_addr_t addr, void *context)
148 {
149  printf("0x%02x ", addr);
150  return 1;
151 }
152 
153 /*!
154  * \brief Execute \h_i2c slave device scan.
155  *
156  * \param pI2C Pointer to \h_i2c handle.
157  *
158  * \return Returns number of devices found \h_ge 0 on success, -1 on failure.
159  */
160 static int execScan(i2c_t *pI2C)
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 }
183 
184 /*!
185  * \brief Command initialization.
186  *
187  * \param argc Command-line argument count.
188  * \param argv Command-line arguments.
189  * \param pI2C Pointer to \h_i2c handle.
190  */
191 static void MainInit(int argc, char *argv[], i2c_t *pI2C)
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 }
230 
231 /*!
232  * \brief i2cscan main()
233  *
234  * \param argc Count of command-line options and arguments.
235  * \param argv Array of command-line options and arguments.
236  *
237  * \return Exit value.
238  */
239 int main(int argc, char *argv[])
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 }
250 
251 /*!
252 \page i2cscan I2CSCAN(1)
253 
254 \section NAME
255 i2cscan - I<sup>2</sup>C Bus Scan
256 
257 \section SYNOPSIS
258 i2cscan [OPTIONS]
259 
260 \section DESCRIPTION
261 The i2cscan command scans the specified I<sup>2</sup>C Bus for all attached
262 devices. The I<sup>2</sup>C addresses of all found devices are written to
263 \p stdout in ASCII hex format.
264 
265 
266 \section OPTIONS RNR_OPTIONS
267 \verbatim
268  -d, --device=<device> I2C device.
269  DEFAULT: /dev/i2c/0
270  --fd=<n> Opened I2C device file descriptor.
271  -v, --verbose Set print verbosity.
272  DEFAULT: false
273  RNR_OPTIONS Standard set of options provided by librnr.
274 \endverbatim
275 
276 \section DIAGNOSTICS
277 Exit status is 0 if devices are found, 1 otherwise.
278 Exit status is >= 2 if error(s) are encountered.
279 
280 \section SEE_ALSO
281 \ref i2ccheck,
282 \ref i2cread,
283 \ref i2ctrans,
284 \ref i2cwrite
285 
286 \section AUTHOR
287 Robin Knight (robin.knight@roadnarrows.com)
288 
289 \section COPYRIGHT
290 (C) 2007. RoadNarrows LLC.
291 (http://www.roadnarrows.com)
292 \n All Rights Reserved
293 */
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
I2C python modules.
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
ushort_t i2c_addr_t
I2C Device Address Type.
Definition: i2c.h:72
I2C Bus Handle Type.
Definition: i2c.h:79
static int execScan(i2c_t *pI2C)
Execute I2C slave device scan.
Definition: i2cscan.c:160
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
I2C character device interface.
static const PkgInfo_T PkgInfo
Definition: version.h:45
static int OptVerbose
verbose option
Definition: i2cscan.c:71
Package version information.
int main(int argc, char *argv[])
i2cscan main()
Definition: i2cscan.c:239
static int OptDevFd
opened i2c bus device fd option
Definition: i2cscan.c:73
static void MainInit(int argc, char *argv[], i2c_t *pI2C)
Command initialization.
Definition: i2cscan.c:191
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
Low-level I2C communication level.