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.
|
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...
|
|
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)
- Copyright
- © 2007-2017. RoadNarrows LLC.
http://www.roadnarrows.com
All Rights Reserved
Definition in file i2ctrans.c.
static void ArgsGet |
( |
int |
argc, |
|
|
char * |
argv[] |
|
) |
| |
|
static |
Get write bytes from command-line arguments.
- Parameters
-
argc | Command-line argument count. |
argv | Command-line arguments. |
- Returns
- Exits on error.
Definition at line 303 of file i2ctrans.c.
References Argv0, ArgWriteBuf, ArgWriteLen, and StrToByte().
Referenced by MainInit().
310 printf(
"%s Error: At least one byte must be specified\n",
Argv0);
313 else if( argc > max )
316 "%s: Warning: %d: I2C write. Only %u bytes will be written.\n",
317 Argv0, argc, (uint_t)max);
320 for(i=0; i<argc && i<max; ++i)
324 printf(
"%s Error: byte %d = '%s': bad byte value\n",
Argv0, i, argv[i]);
static int StrToByte(const char *s, byte_t *pByte)
Convert string to byte.
static byte_t ArgWriteBuf[1024]
write buffer argument
static int ArgWriteLen
write byte count argument
static char * Argv0
command name
static int execTrans |
( |
i2c_t * |
pI2C, |
|
|
int |
addr, |
|
|
byte_t |
writebuf[], |
|
|
int |
writelen, |
|
|
int |
readlen |
|
) |
| |
|
static |
Execute I2C slave device write/read transaction.
- Parameters
-
pI2C | Pointer to I2C handle. |
addr | Slave device address. |
writebuf | Buffer to write. |
writelen | Number of write bytes. |
readlen | Number 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().
187 byte_t readbuf[1024];
191 if( readlen >
sizeof(readbuf) )
193 readlen =
sizeof(readbuf);
195 "%s: Warning: %d: I2C read length too long. Resetting to %u\n",
196 Argv0, readlen, (uint_t)
sizeof(readbuf));
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);
206 readbuf, (uint_t)readlen);
210 LOGSYSERROR(
"i2c_transfer()");
218 printf(
"Response %d bytes: ", readlen);
220 for(i=0; i<readlen; ++i)
222 printf(
"0x%02x ", readbuf[i]);
ushort_t i2c_addr_t
I2C Device Address Type.
static int OptVerbose
verbose option
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.
static char * Argv0
command name
int main |
( |
int |
argc, |
|
|
char * |
argv[] |
|
) |
| |
i2ctrans main()
- Parameters
-
argc | Count of command-line options and arguments. |
argv | Array 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.
static byte_t ArgWriteBuf[1024]
write buffer argument
static void MainInit(int argc, char *argv[], i2c_t *pI2C)
Command initialization.
static int ArgWriteLen
write byte count argument
static int OptReadCount
read byte count option
static int OptI2CAddr
slave i2c device address option
static int execTrans(i2c_t *pI2C, int addr, byte_t writebuf[], int writelen, int readlen)
Execute I2C slave device write/read transaction.
static void MainInit |
( |
int |
argc, |
|
|
char * |
argv[], |
|
|
i2c_t * |
pI2C |
|
) |
| |
|
static |
Command initialization.
- Parameters
-
argc | Command-line argument count. |
argv | Command-line arguments. |
pI2C | Pointer 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().
342 Argv0 = basename(argv[0]);
354 fprintf(stderr,
"%s: Address option required\n",
Argv0);
359 fprintf(stderr,
"%s: Address out of range: 0x%x\n",
Argv0,
OptI2CAddr);
370 pI2C->
addr = (ushort_t)(-1);
388 LOGSYSERROR(
"%s: Failed to open.",
OptDevName);
i2c_addr_t addr
address of the currently selected attached I2C device
int fd
opened file descriptor of the I2C bus device
#define I2C_ADDR_DEV_HIGH
last available device address
static OptsInfo_T I2CTransOptsInfo[]
Command Line Options Information.
static void ArgsGet(int argc, char *argv[])
Get write bytes from command-line arguments.
static int OptVerbose
verbose option
static char * OptDevName
i2c bus device option
static const PkgInfo_T PkgInfo
static int OptDevFd
opened i2c bus device fd option
static char * Argv0
command name
static OptsPgmInfo_T I2CTransPgmInfo
Program Information.
int i2c_open(i2c_t *i2c, const char *device)
Open the host I2C Bus device.
#define I2C_ADDR_DEV_LOW
first available device address
static int OptI2CAddr
slave i2c device address option
static int StrToByte |
( |
const char * |
s, |
|
|
byte_t * |
pByte |
|
) |
| |
|
static |
Convert string to byte.
- Parameters
-
| s | String. |
[out] | pByte | Pointer 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().
284 else if( uVal > 0xff )
290 *pByte = (byte_t)uVal;
static int StrToUnsigned(const char *s, uint_t *pVal)
Convert string to unsigned integer.
static int StrToUnsigned |
( |
const char * |
s, |
|
|
uint_t * |
pVal |
|
) |
| |
|
static |
Convert string to unsigned integer.
- Parameters
-
| s | String. |
[out] | pVal | Pointer to converted value. |
- Returns
- Returns OK on success, RC_ERROR on error.
Definition at line 241 of file i2ctrans.c.
Referenced by StrToByte().
246 if( (s == NULL) || (*s == 0) )
251 lVal = strtol(s, &sEnd, 0);
263 *pVal = (uint_t)lVal;
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().