diff options
author | Daniel Friesel <derf@finalrewind.org> | 2013-07-28 23:25:47 +0200 |
---|---|---|
committer | Daniel Friesel <derf@finalrewind.org> | 2013-07-28 23:25:47 +0200 |
commit | 681e2e2674cf6c0639a403286241bdbd00e24236 (patch) | |
tree | a87f978f126582934c6c801c1f975244acf12cc7 | |
parent | d7b52ba112b3bc5293e3e6c361e8d0cd64fc75bf (diff) |
add vusb-i2cget
-rw-r--r-- | commandline/Makefile | 6 | ||||
-rw-r--r-- | commandline/i2c-util.c | 18 | ||||
-rw-r--r-- | commandline/i2c-util.h | 1 | ||||
-rw-r--r-- | commandline/i2cget.c | 34 |
4 files changed, 58 insertions, 1 deletions
diff --git a/commandline/Makefile b/commandline/Makefile index a4d0792..587f86c 100644 --- a/commandline/Makefile +++ b/commandline/Makefile @@ -9,7 +9,7 @@ LIBS = $(USBLIBS) PREFIX ?= /usr/local bin_dir = ${DESTDIR}${PREFIX}/bin -PROGRAMS = vusb-i2c vusb-i2cdetect vusb-i2cwrite +PROGRAMS = vusb-i2c vusb-i2cdetect vusb-i2cwrite vusb-i2cget all: $(PROGRAMS) @@ -20,6 +20,7 @@ i2c.o: i2c.c i2c-util.c i2c-util.h i2c-util.o: i2c-util.c i2cdetect.o: i2cdetect.c i2c-util.c i2c-util.h i2cwrite.o: i2cwrite.c i2c-util.c i2c-util.h +i2cget.o: i2cget.c i2c-util.c i2c-util.h vusb-i2c: i2c.o i2c-util.o $(CC) -o $@ $^ $(LIBS) @@ -30,6 +31,9 @@ vusb-i2cdetect: i2cdetect.o i2c-util.o vusb-i2cwrite: i2cwrite.o i2c-util.o $(CC) -o $@ $^ $(LIBS) +vusb-i2cget: i2cget.o i2c-util.o + $(CC) -o $@ $^ $(LIBS) + clean: rm -f *.o $(PROGRAMS) diff --git a/commandline/i2c-util.c b/commandline/i2c-util.c index 3971721..2771a4e 100644 --- a/commandline/i2c-util.c +++ b/commandline/i2c-util.c @@ -267,6 +267,24 @@ unsigned char i2c_tx_byte(unsigned char byte) return ack; } +unsigned char i2c_rx_byte() +{ + signed char i; + unsigned char ret = 0; + + set_sda(1); + for (i = 7; i >= -1; i--) { + set_scl(1); + usleep(10); + if ((i >= 0) && ( get_status() & (1 << BIT_SDA))) + ret |= (1 << i); + set_scl(0); + usleep(10); + } + + return ret; +} + void i2c_start() { set_sda(1); diff --git a/commandline/i2c-util.h b/commandline/i2c-util.h index 2b2f57b..4cd4919 100644 --- a/commandline/i2c-util.h +++ b/commandline/i2c-util.h @@ -11,6 +11,7 @@ void set_scl(char value); unsigned char get_status(); unsigned char i2c_tx_byte(unsigned char byte); +unsigned char i2c_rx_byte(); void i2c_start(); void i2c_stop(); diff --git a/commandline/i2cget.c b/commandline/i2cget.c new file mode 100644 index 0000000..ed453ac --- /dev/null +++ b/commandline/i2cget.c @@ -0,0 +1,34 @@ +#include <unistd.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include "i2c-util.h" + +int main(int argc, char **argv) +{ + int address, command; + unsigned char ret; + + i2c_init(); + i2c_start(); + + if (argc < 3) { + fputs("Usage: vusb-i2cget <address> <register>", stderr); + return 1; + } + + address = atoi(argv[1]); + command = atoi(argv[2]); + i2c_tx_byte((address << 1) | 0); + i2c_tx_byte(command); + i2c_start(); + i2c_tx_byte((address << 1) | 1); + ret = i2c_rx_byte(); + + i2c_stop(); + i2c_deinit(); + + printf("%i\n", ret); + + return 0; +} |