diff options
author | Daniel Friesel <derf@finalrewind.org> | 2013-07-28 19:17:18 +0200 |
---|---|---|
committer | Daniel Friesel <derf@finalrewind.org> | 2013-07-28 19:17:18 +0200 |
commit | c3521ef27bb1fd049f87b1bb0da158842565e8c6 (patch) | |
tree | a56109ae420c2e7bd46120a9deddf4f9970bbb53 /commandline | |
parent | 6011c99aca409a4d78c773cbdbe24e09d3286b96 (diff) |
move common code to i2c-util.c
Diffstat (limited to 'commandline')
-rw-r--r-- | commandline/Makefile | 53 | ||||
-rw-r--r-- | commandline/i2c-util.c | 257 | ||||
-rw-r--r-- | commandline/i2c-util.h | 15 | ||||
-rw-r--r-- | commandline/i2c.c | 236 | ||||
-rw-r--r-- | commandline/i2cdetect.c | 235 |
5 files changed, 299 insertions, 497 deletions
diff --git a/commandline/Makefile b/commandline/Makefile index 1f33ff0..3870114 100644 --- a/commandline/Makefile +++ b/commandline/Makefile @@ -1,59 +1,38 @@ -# Name: Makefile -# Project: PowerSwitch -# Author: Christian Starkjohann -# Creation Date: 2005-01-16 -# Tabsize: 4 -# Copyright: (c) 2005 by OBJECTIVE DEVELOPMENT Software GmbH -# License: GNU GPL v2 (see License.txt) or proprietary (CommercialLicense.txt) -# This Revision: $Id$ - - -# Concigure the following definitions according to your system. The powerSwitch -# tool has been successfully compiled on Mac OS X, Linux and Windows. - # Use the following 3 lines on Unix (uncomment the framework on Mac OS X): USBFLAGS = `libusb-config --cflags` USBLIBS = `libusb-config --libs` #-framework CoreFoundation -EXE_SUFFIX = -# Use the following 3 lines on Windows and comment out the 3 above. You may -# have to change the include paths to where you installed libusb-win32 -#USBFLAGS = -I/usr/local/include -#USBLIBS = -L/usr/local/lib -lusb -#EXE_SUFFIX = .exe - - -CC = gcc -CFLAGS = $(USBFLAGS) -O -Wall -LIBS = $(USBLIBS) +CC ?= gcc +CFLAGS = $(USBFLAGS) -O2 -Wall -Wextra -pedantic +LIBS = $(USBLIBS) PREFIX ?= /usr/local bin_dir = ${DESTDIR}${PREFIX}/bin -PROGRAM = vusb-i2c$(EXE_SUFFIX) - +PROGRAMS = vusb-i2c -all: $(PROGRAM) vusb-i2cdetect +all: $(PROGRAMS) .c.o: $(CC) $(CFLAGS) -c $< -$(PROGRAM): i2c.o - $(CC) -o $(PROGRAM) i2c.o $(LIBS) +i2c.o: i2c.c i2c-util.c i2c-util.h +i2c-util.o: i2c-util.c +i2cdetect.o: i2cdetect.c -vusb-i2cdetect: i2cdetect.o - $(CC) -o vusb-i2cdetect i2cdetect.o $(LIBS) +vusb-i2c: i2c.o i2c-util.o + $(CC) -o $@ $^ $(LIBS) -strip: $(PROGRAM) - strip $(PROGRAM) +vusb-i2cdetect: i2cdetect.o i2c-util.o + $(CC) -o $@ $^ $(LIBS) clean: - rm -f *.o $(PROGRAM) + rm -f *.o $(PROGRAMS) -install: $(PROGRAM) - install -m 0755 $(PROGRAM) $(bin_dir)/$(PROGRAM) +install: + install -m 0755 $(PROGRAMS) $(bin_dir) uninstall: - rm -f $(bin_dir)/$(PROGRAM) + rm -rf $(bin_dir)/vusb-i2c $(bin_dir)/vusb-i2cdetect .PHONY: all strip clean install uninstall diff --git a/commandline/i2c-util.c b/commandline/i2c-util.c new file mode 100644 index 0000000..753160e --- /dev/null +++ b/commandline/i2c-util.c @@ -0,0 +1,257 @@ +/* Name: powerSwitch.c + * Project: PowerSwitch based on AVR USB driver + * Author: Christian Starkjohann + * Creation Date: 2005-01-16 + * Tabsize: 4 + * Copyright: (c) 2005 by OBJECTIVE DEVELOPMENT Software GmbH + * License: GNU GPL v2 (see License.txt) or proprietary (CommercialLicense.txt) + * This Revision: $Id$ + */ + +/* +General Description: +This program controls the PowerSwitch USB device from the command line. +It must be linked with libusb, a library for accessing the USB bus from +Linux, FreeBSD, Mac OS X and other Unix operating systems. Libusb can be +obtained from http://libusb.sourceforge.net/. +*/ + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <usb.h> /* this is libusb, see http://libusb.sourceforge.net/ */ + +#include "i2c-util.h" + +#define USBDEV_SHARED_VENDOR 0x16C0 /* VOTI */ +#define USBDEV_SHARED_PRODUCT 0x05DC /* Obdev's free shared PID */ +/* Use obdev's generic shared VID/PID pair and follow the rules outlined + * in firmware/usbdrv/USBID-License.txt. + */ + +#define PSCMD_ECHO 0 +#define PSCMD_GET 1 +#define PSCMD_ON 2 +#define PSCMD_OFF 3 +/* These are the vendor specific SETUP commands implemented by our USB device */ + +usb_dev_handle *handle = NULL; + +static int usbGetStringAscii(usb_dev_handle * dev, int index, int langid, char + *buf, int buflen) +{ + char buffer[256]; + int rval, i; + + if ((rval = + usb_control_msg(dev, USB_ENDPOINT_IN, USB_REQ_GET_DESCRIPTOR, + (USB_DT_STRING << 8) + index, langid, buffer, + sizeof(buffer), 1000)) < 0) + return rval; + if (buffer[1] != USB_DT_STRING) + return 0; + if ((unsigned char)buffer[0] < rval) + rval = (unsigned char)buffer[0]; + rval /= 2; + /* lossy conversion to ISO Latin1 */ + for (i = 1; i < rval; i++) { + if (i > buflen) /* destination buffer overflow */ + break; + buf[i - 1] = buffer[2 * i]; + if (buffer[2 * i + 1] != 0) /* outside of ISO Latin1 range */ + buf[i - 1] = '?'; + } + buf[i - 1] = 0; + return i - 1; +} + +/* PowerSwitch uses the free shared default VID/PID. If you want to see an + * example device lookup where an individually reserved PID is used, see our + * RemoteSensor reference implementation. + */ + +#define USB_ERROR_NOTFOUND 1 +#define USB_ERROR_ACCESS 2 +#define USB_ERROR_IO 3 + +static int usbOpenDevice(usb_dev_handle ** device, int vendor, char *vendorName, + int product, char *productName) +{ + struct usb_bus *bus; + struct usb_device *dev; + usb_dev_handle *handle = NULL; + int errorCode = USB_ERROR_NOTFOUND; + static int didUsbInit = 0; + + if (!didUsbInit) { + didUsbInit = 1; + usb_init(); + } + usb_find_busses(); + usb_find_devices(); + for (bus = usb_get_busses(); bus; bus = bus->next) { + for (dev = bus->devices; dev; dev = dev->next) { + if (dev->descriptor.idVendor == vendor + && dev->descriptor.idProduct == product) { + char string[256]; + int len; + handle = usb_open(dev); /* we need to open the device in order to query strings */ + if (!handle) { + errorCode = USB_ERROR_ACCESS; + fprintf(stderr, + "Warning: cannot open USB device: %s\n", + usb_strerror()); + continue; + } + if (vendorName == NULL && productName == NULL) { /* name does not matter */ + break; + } + /* now check whether the names match: */ + len = + usbGetStringAscii(handle, + dev->descriptor.iManufacturer, 0x0409, + string, sizeof(string)); + if (len < 0) { + errorCode = USB_ERROR_IO; + fprintf(stderr, + "Warning: cannot query manufacturer for device: %s\n", + usb_strerror()); + } else { + errorCode = USB_ERROR_NOTFOUND; + /* fprintf(stderr, "seen device from vendor ->%s<-\n", string); */ + if (strcmp(string, vendorName) == 0) { + len = + usbGetStringAscii(handle, + dev->descriptor.iProduct, + 0x0409, + string, sizeof(string)); + if (len < 0) { + errorCode = USB_ERROR_IO; + fprintf(stderr, + "Warning: cannot query product for device: %s\n", + usb_strerror()); + } else { + errorCode = USB_ERROR_NOTFOUND; + /* fprintf(stderr, "seen product ->%s<-\n", string); */ + if (strcmp(string, productName) == 0) + break; + } + } + } + usb_close(handle); + handle = NULL; + } + } + if (handle) + break; + } + if (handle != NULL) { + errorCode = 0; + *device = handle; + } + return errorCode; +} + +unsigned char get_status() +{ + unsigned char buffer[8]; + int nBytes = usb_control_msg(handle, + USB_TYPE_VENDOR | USB_RECIP_DEVICE | + USB_ENDPOINT_IN, PSCMD_GET, 0, 0, (char *)buffer, + sizeof(buffer), 5000); + + if (nBytes < 2) { + fprintf(stderr, "ERR: read status: got %d bytes, expected 2\n", + nBytes); + exit(1); + } + return buffer[0]; +} + +static void verify_low(unsigned char bit, char *bitname) +{ + int i; + for (i = 0; i < 10; i++) { + if (~get_status() & (1 << bit)) + return; + usleep(10); + } + fprintf(stderr, "%s stuck high for >1ms\n", bitname); +} + +static void verify_high(unsigned char bit, char *bitname) +{ + int i; + for (i = 0; i < 100; i++) { + if (get_status() & (1 << bit)) + return; + usleep(10); + } + fprintf(stderr, "%s stuck low for >1ms\n", bitname); +} + +void verify_sda_low() +{ + verify_low(BIT_SDA, "SDA"); +} + +void verify_sda_high() +{ + verify_high(BIT_SDA, "SDA"); +} + +void verify_scl_low() +{ + verify_low(BIT_SCL, "SCL"); +} + +void verify_scl_high() +{ + verify_high(BIT_SCL, "SCL"); +} + +/* + * Firmware: DDRB = ~b (with hardware pull-ups and PORTB = 0) + * So to pull an output high (1), we turn it on, which sets it as input + * -> pull-up works. for low (0): turn it off -> output -> pulled low + */ + +void set_sda(char value) +{ + // discarded + unsigned char buffer[8]; + + usb_control_msg(handle, + USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, + (value ? PSCMD_ON : PSCMD_OFF), 0, BIT_SDA, + (char *)buffer, sizeof(buffer), 5000); +} + +void set_scl(char value) +{ + // discarded + unsigned char buffer[8]; + + usb_control_msg(handle, + USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, + (value ? PSCMD_ON : PSCMD_OFF), 0, BIT_SCL, + (char *)buffer, sizeof(buffer), 5000); +} + +void i2c_init() +{ + usb_init(); + if (usbOpenDevice + (&handle, USBDEV_SHARED_VENDOR, "www.obdev.at", + USBDEV_SHARED_PRODUCT, "PowerSwitch") != 0) { + fprintf(stderr, + "Could not find USB device \"PowerSwitch\" with vid=0x%x pid=0x%x\n", + USBDEV_SHARED_VENDOR, USBDEV_SHARED_PRODUCT); + exit(1); + } +} + +void i2c_deinit() +{ + usb_close(handle); +} diff --git a/commandline/i2c-util.h b/commandline/i2c-util.h new file mode 100644 index 0000000..bd5fe34 --- /dev/null +++ b/commandline/i2c-util.h @@ -0,0 +1,15 @@ +#define BIT_SDA 6 +#define BIT_SCL 7 + +void verify_sda_low(); +void verify_sda_high(); +void verify_scl_low(); +void verify_scl_high(); + +void set_sda(char value); +void set_scl(char value); + +unsigned char get_status(); + +void i2c_init(); +void i2c_deinit(); diff --git a/commandline/i2c.c b/commandline/i2c.c index db88bac..ee0b264 100644 --- a/commandline/i2c.c +++ b/commandline/i2c.c @@ -1,239 +1,13 @@ -/* Name: powerSwitch.c - * Project: PowerSwitch based on AVR USB driver - * Author: Christian Starkjohann - * Creation Date: 2005-01-16 - * Tabsize: 4 - * Copyright: (c) 2005 by OBJECTIVE DEVELOPMENT Software GmbH - * License: GNU GPL v2 (see License.txt) or proprietary (CommercialLicense.txt) - * This Revision: $Id$ - */ - -/* -General Description: -This program controls the PowerSwitch USB device from the command line. -It must be linked with libusb, a library for accessing the USB bus from -Linux, FreeBSD, Mac OS X and other Unix operating systems. Libusb can be -obtained from http://libusb.sourceforge.net/. -*/ - +#include <unistd.h> #include <stdio.h> #include <stdlib.h> #include <string.h> -#include <usb.h> /* this is libusb, see http://libusb.sourceforge.net/ */ - - -#define BIT_SDA 6 -#define BIT_SCL 7 - - -#define USBDEV_SHARED_VENDOR 0x16C0 /* VOTI */ -#define USBDEV_SHARED_PRODUCT 0x05DC /* Obdev's free shared PID */ -/* Use obdev's generic shared VID/PID pair and follow the rules outlined - * in firmware/usbdrv/USBID-License.txt. - */ - -#define PSCMD_ECHO 0 -#define PSCMD_GET 1 -#define PSCMD_ON 2 -#define PSCMD_OFF 3 -/* These are the vendor specific SETUP commands implemented by our USB device */ - - -usb_dev_handle *handle = NULL; - -static int usbGetStringAscii(usb_dev_handle *dev, int index, int langid, char *buf, int buflen) -{ -char buffer[256]; -int rval, i; - - if((rval = usb_control_msg(dev, USB_ENDPOINT_IN, USB_REQ_GET_DESCRIPTOR, (USB_DT_STRING << 8) + index, langid, buffer, sizeof(buffer), 1000)) < 0) - return rval; - if(buffer[1] != USB_DT_STRING) - return 0; - if((unsigned char)buffer[0] < rval) - rval = (unsigned char)buffer[0]; - rval /= 2; - /* lossy conversion to ISO Latin1 */ - for(i=1;i<rval;i++){ - if(i > buflen) /* destination buffer overflow */ - break; - buf[i-1] = buffer[2 * i]; - if(buffer[2 * i + 1] != 0) /* outside of ISO Latin1 range */ - buf[i-1] = '?'; - } - buf[i-1] = 0; - return i-1; -} - - -/* PowerSwitch uses the free shared default VID/PID. If you want to see an - * example device lookup where an individually reserved PID is used, see our - * RemoteSensor reference implementation. - */ - -#define USB_ERROR_NOTFOUND 1 -#define USB_ERROR_ACCESS 2 -#define USB_ERROR_IO 3 - -static int usbOpenDevice(usb_dev_handle **device, int vendor, char *vendorName, int product, char *productName) -{ -struct usb_bus *bus; -struct usb_device *dev; -usb_dev_handle *handle = NULL; -int errorCode = USB_ERROR_NOTFOUND; -static int didUsbInit = 0; - - if(!didUsbInit){ - didUsbInit = 1; - usb_init(); - } - usb_find_busses(); - usb_find_devices(); - for(bus=usb_get_busses(); bus; bus=bus->next){ - for(dev=bus->devices; dev; dev=dev->next){ - if(dev->descriptor.idVendor == vendor && dev->descriptor.idProduct == product){ - char string[256]; - int len; - handle = usb_open(dev); /* we need to open the device in order to query strings */ - if(!handle){ - errorCode = USB_ERROR_ACCESS; - fprintf(stderr, "Warning: cannot open USB device: %s\n", usb_strerror()); - continue; - } - if(vendorName == NULL && productName == NULL){ /* name does not matter */ - break; - } - /* now check whether the names match: */ - len = usbGetStringAscii(handle, dev->descriptor.iManufacturer, 0x0409, string, sizeof(string)); - if(len < 0){ - errorCode = USB_ERROR_IO; - fprintf(stderr, "Warning: cannot query manufacturer for device: %s\n", usb_strerror()); - }else{ - errorCode = USB_ERROR_NOTFOUND; - /* fprintf(stderr, "seen device from vendor ->%s<-\n", string); */ - if(strcmp(string, vendorName) == 0){ - len = usbGetStringAscii(handle, dev->descriptor.iProduct, 0x0409, string, sizeof(string)); - if(len < 0){ - errorCode = USB_ERROR_IO; - fprintf(stderr, "Warning: cannot query product for device: %s\n", usb_strerror()); - }else{ - errorCode = USB_ERROR_NOTFOUND; - /* fprintf(stderr, "seen product ->%s<-\n", string); */ - if(strcmp(string, productName) == 0) - break; - } - } - } - usb_close(handle); - handle = NULL; - } - } - if(handle) - break; - } - if(handle != NULL){ - errorCode = 0; - *device = handle; - } - return errorCode; -} - - -static unsigned char get_status() -{ - unsigned char buffer[8]; - int nBytes = usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | - USB_ENDPOINT_IN, PSCMD_GET, 0, 0, (char *)buffer, sizeof(buffer), 5000); - - if (nBytes < 2) { - fprintf(stderr, "ERR: read status: got %d bytes, expected 2\n", nBytes); - exit(1); - } - return buffer[0]; -} - -static void verify_low(unsigned char bit, char *bitname) -{ - int i; - for (i = 0; i < 10; i++) { - if (~get_status() & (1 << bit)) - return; - usleep(10); - } - fprintf(stderr, "%s stuck high for >1ms\n", bitname); -} - -static void verify_high(unsigned char bit, char *bitname) -{ - int i; - for (i = 0; i < 100; i++) { - if (get_status() & (1 << bit)) - return; - usleep(10); - } - fprintf(stderr, "%s stuck low for >1ms\n", bitname); -} - -static void verify_sda_low() -{ - verify_low(BIT_SDA, "SDA"); -} - -static void verify_sda_high() -{ - verify_high(BIT_SDA, "SDA"); -} - -static void verify_scl_low() -{ - verify_low(BIT_SCL, "SCL"); -} - -static void verify_scl_high() -{ - verify_high(BIT_SCL, "SCL"); -} - - - -/* - * Firmware: DDRB = ~b (with hardware pull-ups and PORTB = 0) - * So to pull an output high (1), we turn it on, which sets it as input - * -> pull-up works. for low (0): turn it off -> output -> pulled low - */ - -void set_sda(char value) { - // discarded - unsigned char buffer[8]; - - usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, - (value ? PSCMD_ON : PSCMD_OFF), 0, BIT_SDA, - (char *)buffer, sizeof(buffer), 5000); -} - -void set_scl(char value) { - // discarded - unsigned char buffer[8]; - - usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, - (value ? PSCMD_ON : PSCMD_OFF), 0, BIT_SCL, - (char *)buffer, sizeof(buffer), 5000); -} +#include "i2c-util.h" int main(int argc, char **argv) { -unsigned char buffer[8]; + i2c_init(); - usb_init(); - if(usbOpenDevice(&handle, USBDEV_SHARED_VENDOR, "www.obdev.at", USBDEV_SHARED_PRODUCT, "PowerSwitch") != 0){ - fprintf(stderr, "Could not find USB device \"PowerSwitch\" with vid=0x%x pid=0x%x\n", USBDEV_SHARED_VENDOR, USBDEV_SHARED_PRODUCT); - exit(1); - } -/* We have searched all devices on all busses for our USB device above. Now - * try to open it and perform the vendor specific control operations for the - * function requested by the user. - */ - char line[8]; signed char i; short int number; @@ -303,6 +77,6 @@ unsigned char buffer[8]; usleep(10); verify_sda_high(); - usb_close(handle); - return 0; + i2c_deinit(); + return 0; } diff --git a/commandline/i2cdetect.c b/commandline/i2cdetect.c index cf31e15..ee55528 100644 --- a/commandline/i2cdetect.c +++ b/commandline/i2cdetect.c @@ -1,244 +1,21 @@ -/* Name: powerSwitch.c - * Project: PowerSwitch based on AVR USB driver - * Author: Christian Starkjohann - * Creation Date: 2005-01-16 - * Tabsize: 4 - * Copyright: (c) 2005 by OBJECTIVE DEVELOPMENT Software GmbH - * License: GNU GPL v2 (see License.txt) or proprietary (CommercialLicense.txt) - * This Revision: $Id$ - */ - -/* -General Description: -This program controls the PowerSwitch USB device from the command line. -It must be linked with libusb, a library for accessing the USB bus from -Linux, FreeBSD, Mac OS X and other Unix operating systems. Libusb can be -obtained from http://libusb.sourceforge.net/. -*/ - +#include <unistd.h> #include <stdio.h> #include <stdlib.h> #include <string.h> -#include <usb.h> /* this is libusb, see http://libusb.sourceforge.net/ */ - +#include "i2c-util.h" #define BIT_SDA 6 #define BIT_SCL 7 -#define USBDEV_SHARED_VENDOR 0x16C0 /* VOTI */ -#define USBDEV_SHARED_PRODUCT 0x05DC /* Obdev's free shared PID */ -/* Use obdev's generic shared VID/PID pair and follow the rules outlined - * in firmware/usbdrv/USBID-License.txt. - */ - -#define PSCMD_ECHO 0 -#define PSCMD_GET 1 -#define PSCMD_ON 2 -#define PSCMD_OFF 3 -/* These are the vendor specific SETUP commands implemented by our USB device */ - - -usb_dev_handle *handle = NULL; - -static int usbGetStringAscii(usb_dev_handle *dev, int index, int langid, char *buf, int buflen) -{ -char buffer[256]; -int rval, i; - - if((rval = usb_control_msg(dev, USB_ENDPOINT_IN, USB_REQ_GET_DESCRIPTOR, (USB_DT_STRING << 8) + index, langid, buffer, sizeof(buffer), 1000)) < 0) - return rval; - if(buffer[1] != USB_DT_STRING) - return 0; - if((unsigned char)buffer[0] < rval) - rval = (unsigned char)buffer[0]; - rval /= 2; - /* lossy conversion to ISO Latin1 */ - for(i=1;i<rval;i++){ - if(i > buflen) /* destination buffer overflow */ - break; - buf[i-1] = buffer[2 * i]; - if(buffer[2 * i + 1] != 0) /* outside of ISO Latin1 range */ - buf[i-1] = '?'; - } - buf[i-1] = 0; - return i-1; -} - - -/* PowerSwitch uses the free shared default VID/PID. If you want to see an - * example device lookup where an individually reserved PID is used, see our - * RemoteSensor reference implementation. - */ - -#define USB_ERROR_NOTFOUND 1 -#define USB_ERROR_ACCESS 2 -#define USB_ERROR_IO 3 - -static int usbOpenDevice(usb_dev_handle **device, int vendor, char *vendorName, int product, char *productName) -{ -struct usb_bus *bus; -struct usb_device *dev; -usb_dev_handle *handle = NULL; -int errorCode = USB_ERROR_NOTFOUND; -static int didUsbInit = 0; - - if(!didUsbInit){ - didUsbInit = 1; - usb_init(); - } - usb_find_busses(); - usb_find_devices(); - for(bus=usb_get_busses(); bus; bus=bus->next){ - for(dev=bus->devices; dev; dev=dev->next){ - if(dev->descriptor.idVendor == vendor && dev->descriptor.idProduct == product){ - char string[256]; - int len; - handle = usb_open(dev); /* we need to open the device in order to query strings */ - if(!handle){ - errorCode = USB_ERROR_ACCESS; - fprintf(stderr, "Warning: cannot open USB device: %s\n", usb_strerror()); - continue; - } - if(vendorName == NULL && productName == NULL){ /* name does not matter */ - break; - } - /* now check whether the names match: */ - len = usbGetStringAscii(handle, dev->descriptor.iManufacturer, 0x0409, string, sizeof(string)); - if(len < 0){ - errorCode = USB_ERROR_IO; - fprintf(stderr, "Warning: cannot query manufacturer for device: %s\n", usb_strerror()); - }else{ - errorCode = USB_ERROR_NOTFOUND; - /* fprintf(stderr, "seen device from vendor ->%s<-\n", string); */ - if(strcmp(string, vendorName) == 0){ - len = usbGetStringAscii(handle, dev->descriptor.iProduct, 0x0409, string, sizeof(string)); - if(len < 0){ - errorCode = USB_ERROR_IO; - fprintf(stderr, "Warning: cannot query product for device: %s\n", usb_strerror()); - }else{ - errorCode = USB_ERROR_NOTFOUND; - /* fprintf(stderr, "seen product ->%s<-\n", string); */ - if(strcmp(string, productName) == 0) - break; - } - } - } - usb_close(handle); - handle = NULL; - } - } - if(handle) - break; - } - if(handle != NULL){ - errorCode = 0; - *device = handle; - } - return errorCode; -} - - -static unsigned char get_status() -{ - unsigned char buffer[8]; - int nBytes = usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | - USB_ENDPOINT_IN, PSCMD_GET, 0, 0, (char *)buffer, sizeof(buffer), 5000); - - if (nBytes < 2) { - fprintf(stderr, "ERR: read status: got %d bytes, expected 2\n", nBytes); - exit(1); - } - return buffer[0]; -} - -static void verify_low(unsigned char bit, char *bitname) -{ - int i; - for (i = 0; i < 10; i++) { - if (~get_status() & (1 << bit)) - return; - usleep(10); - } - fprintf(stderr, "%s stuck high for >1ms\n", bitname); -} - -static void verify_high(unsigned char bit, char *bitname) -{ - int i; - for (i = 0; i < 100; i++) { - if (get_status() & (1 << bit)) - return; - usleep(10); - } - fprintf(stderr, "%s stuck low for >1ms\n", bitname); -} - -static void verify_sda_low() -{ - verify_low(BIT_SDA, "SDA"); -} - -static void verify_sda_high() -{ - verify_high(BIT_SDA, "SDA"); -} - -static void verify_scl_low() -{ - verify_low(BIT_SCL, "SCL"); -} - -static void verify_scl_high() -{ - verify_high(BIT_SCL, "SCL"); -} - - - -/* - * Firmware: DDRB = ~b (with hardware pull-ups and PORTB = 0) - * So to pull an output high (1), we turn it on, which sets it as input - * -> pull-up works. for low (0): turn it off -> output -> pulled low - */ - -void set_sda(char value) { - // discarded - unsigned char buffer[8]; - - usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, - (value ? PSCMD_ON : PSCMD_OFF), 0, BIT_SDA, - (char *)buffer, sizeof(buffer), 5000); -} - -void set_scl(char value) { - // discarded - unsigned char buffer[8]; - - usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, - (value ? PSCMD_ON : PSCMD_OFF), 0, BIT_SCL, - (char *)buffer, sizeof(buffer), 5000); -} - int main(int argc, char **argv) { -unsigned char buffer[8]; - usb_init(); - if(usbOpenDevice(&handle, USBDEV_SHARED_VENDOR, "www.obdev.at", USBDEV_SHARED_PRODUCT, "PowerSwitch") != 0){ - fprintf(stderr, "Could not find USB device \"PowerSwitch\" with vid=0x%x pid=0x%x\n", USBDEV_SHARED_VENDOR, USBDEV_SHARED_PRODUCT); - exit(1); - } -/* We have searched all devices on all busses for our USB device above. Now - * try to open it and perform the vendor specific control operations for the - * function requested by the user. - */ - - char line[8]; + i2c_init(); + signed char i; unsigned char id, i2cid; - fputs(" 0 1 2 3 4 5 6 7 8 9 a b c d e f", stdout); for (id = 0; id < 128; id++) { @@ -291,6 +68,6 @@ unsigned char buffer[8]; fputs("\n", stdout); - usb_close(handle); - return 0; + i2c_deinit(); + return 0; } |