i2c  1.4.2
RoadNarrows Robotics I2C Package
i2cwrite.c File Reference

Write bytes to 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 execWrite (i2c_t *pI2C, int addr, byte_t writebuf[], int writelen)
 Execute I2C slave device write. 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[])
 i2cwrite 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 buse device fd option
 
static int OptI2CAddr = -1
 slave i2c device address
 
static byte_t ArgWriteBuf [1024]
 write buffer argument
 
static int ArgWriteLen = 0
 write byte count arguement
 
static OptsPgmInfo_T I2CWritePgmInfo
 Program Information. More...
 
static OptsInfo_T I2CWriteOptsInfo []
 Command Line Options Information.
 

Detailed Description

Write bytes to 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 i2cwrite.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 267 of file i2cwrite.c.

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

Referenced by MainInit().

268 {
269  int i;
270  size_t max = sizeof(ArgWriteBuf);
271 
272  if( argc == 0 )
273  {
274  printf("%s Error: At least one byte must be specified\n", Argv0);
275  exit(EC_ERROR);
276  }
277  else if( argc > max )
278  {
279  fprintf(stderr,
280  "%s: Warning: %d: I2C write. Only %u bytes will be written.\n",
281  Argv0, argc, (uint_t)max);
282  }
283 
284  for(i=0; i<argc && i<max; ++i)
285  {
286  if( StrToByte(argv[i], ArgWriteBuf+i) != OK )
287  {
288  printf("%s Error: byte %d = '%s': bad byte value\n", Argv0, i, argv[i]);
289  exit(EC_ERROR);
290  }
291  }
292 
293  ArgWriteLen = i;
294 }
static byte_t ArgWriteBuf[1024]
write buffer argument
Definition: i2cwrite.c:79
static char * Argv0
command name
Definition: i2cwrite.c:70
static int ArgWriteLen
write byte count arguement
Definition: i2cwrite.c:80
static int StrToByte(const char *s, byte_t *pByte)
Convert string to byte.
Definition: i2cwrite.c:240
static int execWrite ( i2c_t pI2C,
int  addr,
byte_t  writebuf[],
int  writelen 
)
static

Execute I2C slave device write.

Parameters
pI2CPointer to I2C handle.
addrSlave device address.
writebufBuffer to write.
writelenNumber of write bytes.
Returns
Returns ≥ 0 on success, -1 on failure.

Definition at line 166 of file i2cwrite.c.

References Argv0, i2c_write(), and OptVerbose.

Referenced by main().

167 {
168  int n;
169 
170  if( OptVerbose )
171  {
172  printf("%s: i2cdev 0x%02x: writting %u bytes...\n",
173  Argv0, (byte_t)addr, writelen);
174  }
175 
176  n = i2c_write(pI2C, (i2c_addr_t)addr, writebuf, (uint_t)writelen);
177 
178  if( n < 0 )
179  {
180  LOGSYSERROR("i2c_write()");
181  return RC_ERROR;
182  }
183 
184  if( OptVerbose )
185  {
186  printf("Wrote %d bytes\n", n);
187  }
188 
189  if( n < writelen )
190  {
191  LOGERROR("wrote only %d bytes of %d", n, writelen);
192  }
193 
194  return n;
195 }
int i2c_write(i2c_t *i2c, i2c_addr_t addr, const byte_t *buf, uint_t len)
Write to an I2C device.
Definition: i2ccom.c:159
ushort_t i2c_addr_t
I2C Device Address Type.
Definition: i2c.h:72
static int OptVerbose
verbose option
Definition: i2cwrite.c:75
static char * Argv0
command name
Definition: i2cwrite.c:70
int main ( int  argc,
char *  argv[] 
)

i2cwrite main()

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

Definition at line 366 of file i2cwrite.c.

References ArgWriteBuf, ArgWriteLen, execWrite(), MainInit(), and OptI2CAddr.

367 {
368  i2c_t i2c;
369  int rc;
370 
371  MainInit(argc, argv, &i2c);
372 
374 
375  return rc == ArgWriteLen? 0: (rc >= 0? 1: 2);
376 }
I2C python modules.
I2C Bus Handle Type.
Definition: i2c.h:79
static int OptI2CAddr
slave i2c device address
Definition: i2cwrite.c:78
static byte_t ArgWriteBuf[1024]
write buffer argument
Definition: i2cwrite.c:79
static void MainInit(int argc, char *argv[], i2c_t *pI2C)
Command initialization.
Definition: i2cwrite.c:303
static int ArgWriteLen
write byte count arguement
Definition: i2cwrite.c:80
static int execWrite(i2c_t *pI2C, int addr, byte_t writebuf[], int writelen)
Execute I2C slave device write.
Definition: i2cwrite.c:166
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 303 of file i2cwrite.c.

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

Referenced by main().

304 {
305  // Name of this process
306  Argv0 = basename(argv[0]);
307 
308  // Get the environment
309  //EnvGet();
310 
311  // Parse input arguments
312  argv = OptsGet(Argv0, &PkgInfo, &I2CWritePgmInfo, I2CWriteOptsInfo, true,
313  &argc, argv);
314 
315  // Final option checks
316  if( OptI2CAddr < 0x00 )
317  {
318  fprintf(stderr, "%s: Address option required\n", Argv0);
319  exit(EC_BAD_OPT);
320  }
322  {
323  fprintf(stderr, "%s: Address out of range: 0x%x\n", Argv0, OptI2CAddr);
324  exit(EC_BAD_OPT);
325  }
326 
327  // Convert command-line arguments
328  ArgsGet(argc, argv);
329 
330  // Opened device specified
331  if( OptDevFd >= 0 )
332  {
333  pI2C->fd = OptDevFd;
334  pI2C->addr = (ushort_t)(-1);
335  }
336 
337  // I2C Bus device specified
338  else
339  {
340  if( OptDevName == NULL || OptDevName[0] == 0 )
341  {
342  OptDevName = "/dev/i2c/0";
343  }
344 
345  if( OptVerbose )
346  {
347  printf("I2C device: %s\n\n", OptDevName);
348  }
349 
350  if( i2c_open(pI2C, OptDevName) < 0 )
351  {
352  LOGSYSERROR("%s: Failed to open.", OptDevName);
353  exit(EC_ERROR);
354  }
355  }
356 }
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
static void ArgsGet(int argc, char *argv[])
Get write bytes from command-line arguments.
Definition: i2cwrite.c:267
static int OptI2CAddr
slave i2c device address
Definition: i2cwrite.c:78
#define I2C_ADDR_DEV_HIGH
last available device address
Definition: i2c-dev.h:103
static int OptVerbose
verbose option
Definition: i2cwrite.c:75
static char * Argv0
command name
Definition: i2cwrite.c:70
static const PkgInfo_T PkgInfo
Definition: version.h:45
static OptsInfo_T I2CWriteOptsInfo[]
Command Line Options Information.
Definition: i2cwrite.c:103
static int OptDevFd
opened i2c buse device fd option
Definition: i2cwrite.c:77
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 OptsPgmInfo_T I2CWritePgmInfo
Program Information.
Definition: i2cwrite.c:85
static char * OptDevName
i2c bus device option
Definition: i2cwrite.c:76
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 240 of file i2cwrite.c.

References StrToUnsigned().

Referenced by ArgsGet().

241 {
242  uint_t uVal;
243 
244  if( StrToUnsigned(s, &uVal) != OK )
245  {
246  return RC_ERROR;
247  }
248  else if( uVal > 0xff )
249  {
250  return RC_ERROR;
251  }
252  else
253  {
254  *pByte = (byte_t)uVal;
255  return OK;
256  }
257 }
static int StrToUnsigned(const char *s, uint_t *pVal)
Convert string to unsigned integer.
Definition: i2cwrite.c:205
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 205 of file i2cwrite.c.

Referenced by StrToByte().

206 {
207  long lVal;
208  char *sEnd;
209 
210  if( (s == NULL) || (*s == 0) )
211  {
212  return RC_ERROR;
213  }
214 
215  lVal = strtol(s, &sEnd, 0);
216 
217  if( *sEnd != 0 )
218  {
219  return RC_ERROR;
220  }
221  else if( lVal < 0 )
222  {
223  return RC_ERROR;
224  }
225  else
226  {
227  *pVal = (uint_t)lVal;
228  return OK;
229  }
230 }

Variable Documentation

OptsPgmInfo_T I2CWritePgmInfo
static
Initial value:
=
{
.usage_args = "byte1 [byte2 ...]",
.synopsis = "Write bytes to a device on an I2C Bus.",
.long_desc =
"The %P command write the bytes arguments to an I2C Bus for a device "
"with the specified address.",
.diagnostics =
"Exit status is 0 if the bytes are successfully written. "
"Exist status is >= 2 if error(s) are encountered.",
}

Program Information.

Definition at line 85 of file i2cwrite.c.

Referenced by MainInit().