summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Friesel <derf@finalrewind.org>2013-10-03 19:43:35 +0200
committerDaniel Friesel <derf@finalrewind.org>2013-10-03 19:43:35 +0200
commitc041a6eae368a11c97e1ce83e02716ab190de0d3 (patch)
treed7b0fa35789aca3de21e4d2b987a92d528110620
parent910e732b05b01eff02283e9a19f405deb1b060b4 (diff)
i2cget / i2cset: Accept hexadecimal input, document exit codes
-rw-r--r--commandline/i2cget.c22
-rw-r--r--commandline/i2cset.c10
-rw-r--r--commandline/man/vusb-i2cget.126
-rw-r--r--commandline/man/vusb-i2cset.111
4 files changed, 62 insertions, 7 deletions
diff --git a/commandline/i2cget.c b/commandline/i2cget.c
index b7c0245..4138722 100644
--- a/commandline/i2cget.c
+++ b/commandline/i2cget.c
@@ -6,9 +6,10 @@
int main(int argc, char **argv)
{
- int address, command;
+ int address, command, got_ack;
unsigned int ret;
char mode = 'c';
+ char *conv_err;
i2c_init();
i2c_start();
@@ -18,8 +19,19 @@ int main(int argc, char **argv)
return 1;
}
- address = atoi(argv[1]);
- command = atoi(argv[2]);
+ address = strtol(argv[1], &conv_err, 0);
+
+ if (conv_err && *conv_err) {
+ fprintf(stderr, "address: Conversion error at '%s'", conv_err);
+ return 2;
+ }
+
+ command = strtol(argv[2], &conv_err, 0);
+
+ if (conv_err && *conv_err) {
+ fprintf(stderr, "command: conversion error at '%s'", conv_err);
+ return 2;
+ }
if (argc == 4)
mode = argv[3][0];
@@ -27,7 +39,7 @@ int main(int argc, char **argv)
i2c_tx_byte((address << 1) | 0);
i2c_tx_byte(command);
i2c_start();
- i2c_tx_byte((address << 1) | 1);
+ got_ack = i2c_tx_byte((address << 1) | 1);
if (mode == 'i') {
ret = i2c_rx_byte(1);
@@ -41,5 +53,5 @@ int main(int argc, char **argv)
printf("%i\n", ret);
- return 0;
+ return got_ack ? 0 : 1;
}
diff --git a/commandline/i2cset.c b/commandline/i2cset.c
index 971783a..c6333c5 100644
--- a/commandline/i2cset.c
+++ b/commandline/i2cset.c
@@ -7,6 +7,7 @@
int main(int argc, char **argv)
{
int i, address, got_ack;
+ char *conv_err;
i2c_init();
i2c_start();
@@ -16,9 +17,14 @@ int main(int argc, char **argv)
return 1;
}
- address = atoi(argv[1]) << 1;
+ address = strtol(argv[1], &conv_err, 0);
- got_ack = i2c_tx_byte(address);
+ if (conv_err && *conv_err) {
+ fprintf(stderr, "Conversion error at '%s'", conv_err);
+ return 2;
+ }
+
+ got_ack = i2c_tx_byte(address << 1);
for (i = 2; i < argc; i++) {
i2c_tx_byte(atoi(argv[i]));
diff --git a/commandline/man/vusb-i2cget.1 b/commandline/man/vusb-i2cget.1
index 0210f18..5125783 100644
--- a/commandline/man/vusb-i2cget.1
+++ b/commandline/man/vusb-i2cget.1
@@ -32,6 +32,32 @@ It first writes
to the device, then starts a new read connection and reads one byte.
This byte is printed to stdout as an unsigned char.
.
+.Ar address
+and
+.Ar register
+are decimal by default, use the
+.Qq 0x
+prefix for hexadecimal numbers
+.
+.
+.Sh EXIT STATUS
+.
+.Bl -tag -width indent
+.
+.It 0
+.
+Device queried successfully (ACK after address byte)
+.
+.It 1
+.
+Device did not respond (NAK after address byte)
+.
+.It 2
+.
+Bad input (not a number)
+.
+.El
+.
.
.Sh AUTHOR
.
diff --git a/commandline/man/vusb-i2cset.1 b/commandline/man/vusb-i2cset.1
index 7f24941..795345b 100644
--- a/commandline/man/vusb-i2cset.1
+++ b/commandline/man/vusb-i2cset.1
@@ -30,6 +30,12 @@ sends bytes to an I2C device. It transmits a START condition, then the
.Ar address
with the R/W bit set to write, and then all specified bytes.
.
+.Ar address
+and
+.Ar data
+are decimal by default, use the
+.Qq 0x
+prefix for hexadecimal numbers
.
.Sh EXIT STATUS
.
@@ -47,9 +53,14 @@ after transmitting the
.Ar address ,
a NAK was received
.
+.It 2
+.
+Bad input (not a number)
+.
.El
.
.
+
.Sh AUTHOR
.
Copyright (C) 2013 by Daniel Friesel