summaryrefslogtreecommitdiff
path: root/pc
diff options
context:
space:
mode:
authorFlorian Jung <flo@windfisch.org>2015-11-29 00:30:43 +0100
committerFlorian Jung <flo@windfisch.org>2015-11-29 00:30:43 +0100
commit84b980994934d54b3dad6dd2184830f4464b9933 (patch)
tree9e5eb6fb283e791ff50f85c6d97d28ccc7dca6b6 /pc
parent1923a8c9121788804a4e307688b6c45993f58205 (diff)
usb hello world \o/
Diffstat (limited to 'pc')
-rwxr-xr-xpc/a.outbin0 -> 12696 bytes
-rw-r--r--pc/main.c134
-rw-r--r--pc/usbtest.c199
3 files changed, 333 insertions, 0 deletions
diff --git a/pc/a.out b/pc/a.out
new file mode 100755
index 0000000..f1b6c1b
--- /dev/null
+++ b/pc/a.out
Binary files differ
diff --git a/pc/main.c b/pc/main.c
new file mode 100644
index 0000000..fe0d75d
--- /dev/null
+++ b/pc/main.c
@@ -0,0 +1,134 @@
+/* Name: set-led.c
+ * Project: custom-class, a basic USB example
+ * Author: Christian Starkjohann
+ * Creation Date: 2008-04-10
+ * Tabsize: 4
+ * Copyright: (c) 2008 by OBJECTIVE DEVELOPMENT Software GmbH
+ * License: GNU GPL v2 (see License.txt), GNU GPL v3 or proprietary (CommercialLicense.txt)
+ */
+
+/*
+General Description:
+This is the host-side driver for the custom-class example device. It searches
+the USB for the LEDControl device and sends the requests understood by this
+device.
+This program must be linked with libusb on Unix and libusb-win32 on Windows.
+See http://libusb.sourceforge.net/ or http://libusb-win32.sourceforge.net/
+respectively.
+*/
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <usb.h> /* this is libusb */
+#include "opendevice.h" /* common code moved to separate module */
+
+#include "../firmware/requests.h" /* custom request numbers */
+#include "../firmware/usbconfig.h" /* device's VID/PID and names */
+
+static void usage(char *name)
+{
+ fprintf(stderr, "usage:\n");
+ fprintf(stderr, " %s on ....... turn on LED\n", name);
+ fprintf(stderr, " %s off ...... turn off LED\n", name);
+ fprintf(stderr, " %s status ... ask current status of LED\n", name);
+#if ENABLE_TEST
+ fprintf(stderr, " %s test ..... run driver reliability test\n", name);
+#endif /* ENABLE_TEST */
+}
+
+int main(int argc, char **argv)
+{
+usb_dev_handle *handle = NULL;
+const unsigned char rawVid[2] = {USB_CFG_VENDOR_ID}, rawPid[2] = {USB_CFG_DEVICE_ID};
+char vendor[] = {USB_CFG_VENDOR_NAME, 0}, product[] = {USB_CFG_DEVICE_NAME, 0};
+char buffer[4];
+int cnt, vid, pid, isOn;
+
+ usb_init();
+ if(argc < 2){ /* we need at least one argument */
+ usage(argv[0]);
+ exit(1);
+ }
+ /* compute VID/PID from usbconfig.h so that there is a central source of information */
+ vid = rawVid[1] * 256 + rawVid[0];
+ pid = rawPid[1] * 256 + rawPid[0];
+ /* The following function is in opendevice.c: */
+ if(usbOpenDevice(&handle, vid, vendor, pid, product, NULL, NULL, NULL) != 0){
+ fprintf(stderr, "Could not find USB device \"%s\" with vid=0x%x pid=0x%x\n", product, vid, pid);
+ exit(1);
+ }
+ /* Since we use only control endpoint 0, we don't need to choose a
+ * configuration and interface. Reading device descriptor and setting a
+ * configuration and interface is done through endpoint 0 after all.
+ * However, newer versions of Linux require that we claim an interface
+ * even for endpoint 0. Enable the following code if your operating system
+ * needs it: */
+#if 0
+ int retries = 1, usbConfiguration = 1, usbInterface = 0;
+ if(usb_set_configuration(handle, usbConfiguration) && showWarnings){
+ fprintf(stderr, "Warning: could not set configuration: %s\n", usb_strerror());
+ }
+ /* now try to claim the interface and detach the kernel HID driver on
+ * Linux and other operating systems which support the call. */
+ while((len = usb_claim_interface(handle, usbInterface)) != 0 && retries-- > 0){
+#ifdef LIBUSB_HAS_DETACH_KERNEL_DRIVER_NP
+ if(usb_detach_kernel_driver_np(handle, 0) < 0 && showWarnings){
+ fprintf(stderr, "Warning: could not detach kernel driver: %s\n", usb_strerror());
+ }
+#endif
+ }
+#endif
+
+ if(strcasecmp(argv[1], "status") == 0){
+ cnt = usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, CUSTOM_RQ_GET_STATUS, 0, 0, buffer, sizeof(buffer), 5000);
+ if(cnt < 1){
+ if(cnt < 0){
+ fprintf(stderr, "USB error: %s\n", usb_strerror());
+ }else{
+ fprintf(stderr, "only %d bytes received.\n", cnt);
+ }
+ }else{
+ printf("LED is %s\n", buffer[0] ? "on" : "off");
+ }
+ }else if((isOn = (strcasecmp(argv[1], "on") == 0)) || strcasecmp(argv[1], "off") == 0){
+ cnt = usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_OUT, CUSTOM_RQ_SET_STATUS, isOn, 0, buffer, 0, 5000);
+ if(cnt < 0){
+ fprintf(stderr, "USB error: %s\n", usb_strerror());
+ }
+#if ENABLE_TEST
+ }else if(strcasecmp(argv[1], "test") == 0){
+ int i;
+ srandomdev();
+ for(i = 0; i < 50000; i++){
+ int value = random() & 0xffff, index = random() & 0xffff;
+ int rxValue, rxIndex;
+ if((i+1) % 100 == 0){
+ fprintf(stderr, "\r%05d", i+1);
+ fflush(stderr);
+ }
+ cnt = usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, CUSTOM_RQ_ECHO, value, index, buffer, sizeof(buffer), 5000);
+ if(cnt < 0){
+ fprintf(stderr, "\nUSB error in iteration %d: %s\n", i, usb_strerror());
+ break;
+ }else if(cnt != 4){
+ fprintf(stderr, "\nerror in iteration %d: %d bytes received instead of 4\n", i, cnt);
+ break;
+ }
+ rxValue = ((int)buffer[0] & 0xff) | (((int)buffer[1] & 0xff) << 8);
+ rxIndex = ((int)buffer[2] & 0xff) | (((int)buffer[3] & 0xff) << 8);
+ if(rxValue != value || rxIndex != index){
+ fprintf(stderr, "\ndata error in iteration %d:\n", i);
+ fprintf(stderr, "rxValue = 0x%04x value = 0x%04x\n", rxValue, value);
+ fprintf(stderr, "rxIndex = 0x%04x index = 0x%04x\n", rxIndex, index);
+ }
+ }
+ fprintf(stderr, "\nTest completed.\n");
+#endif /* ENABLE_TEST */
+ }else{
+ usage(argv[0]);
+ exit(1);
+ }
+ usb_close(handle);
+ return 0;
+}
diff --git a/pc/usbtest.c b/pc/usbtest.c
new file mode 100644
index 0000000..002add4
--- /dev/null
+++ b/pc/usbtest.c
@@ -0,0 +1,199 @@
+/**
+ * Project: AVR ATtiny USB Tutorial at http://codeandlife.com/
+ * Author: Joonas Pihlajamaa, joonas.pihlajamaa@iki.fi
+ * Based on V-USB example code by Christian Starkjohann
+ * Copyright: (C) 2012 by Joonas Pihlajamaa
+ * License: GNU GPL v3 (see License.txt)
+ */
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <usb.h>
+
+// used to get descriptor strings for device identification
+static int usbGetDescriptorString(usb_dev_handle *dev, int index, int langid,
+ char *buf, int buflen)
+{
+ char buffer[256];
+ int rval, i;
+
+ // make standard request GET_DESCRIPTOR, type string and given index
+ // (e.g. dev->iProduct)
+ rval = usb_control_msg(dev,
+ USB_TYPE_STANDARD | USB_RECIP_DEVICE | USB_ENDPOINT_IN,
+ USB_REQ_GET_DESCRIPTOR, (USB_DT_STRING << 8) + index, langid,
+ buffer, sizeof(buffer), 1000);
+
+ if(rval < 0) // error
+ return rval;
+
+ // rval should be bytes read, but buffer[0] contains the actual response size
+ if((unsigned char)buffer[0] < rval)
+ rval = (unsigned char)buffer[0]; // string is shorter than bytes read
+
+ if(buffer[1] != USB_DT_STRING) // second byte is the data type
+ return 0; // invalid return type
+
+ // we're dealing with UTF-16LE here so actual chars is half of rval,
+ // and index 0 doesn't count
+ rval /= 2;
+
+ // lossy conversion to ISO Latin1
+ for(i = 1; i < rval && i < buflen; i++)
+ {
+ if(buffer[2 * i + 1] == 0)
+ buf[i-1] = buffer[2 * i];
+ else
+ buf[i-1] = '?'; // outside of ISO Latin1 range
+ }
+ buf[i-1] = 0;
+
+ return i-1;
+}
+
+static usb_dev_handle * usbOpenDevice(int vendor, char *vendorName,
+ int product, char *productName)
+{
+ struct usb_bus *bus;
+ struct usb_device *dev;
+ char devVendor[256], devProduct[256];
+
+ usb_dev_handle * handle = NULL;
+
+ usb_set_debug(3);
+
+ 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)
+ continue;
+
+ // we need to open the device in order to query strings
+ if(!(handle = usb_open(dev)))
+ {
+ fprintf(stderr, "Warning: cannot open USB device: %s\n",
+ usb_strerror());
+ continue;
+ }
+
+ // get vendor name
+ if(usbGetDescriptorString(handle, dev->descriptor.iManufacturer, 0x0409, devVendor, sizeof(devVendor)) < 0)
+ {
+ fprintf(stderr,
+ "Warning: cannot query manufacturer for device: %s\n",
+ usb_strerror());
+ usb_close(handle);
+ continue;
+ }
+
+ // get product name
+ if(usbGetDescriptorString(handle, dev->descriptor.iProduct,
+ 0x0409, devProduct, sizeof(devVendor)) < 0)
+ {
+ fprintf(stderr,
+ "Warning: cannot query product for device: %s\n",
+ usb_strerror());
+ usb_close(handle);
+ continue;
+ }
+
+ if(strcmp(devVendor, vendorName) == 0 &&
+ strcmp(devProduct, productName) == 0)
+ return handle;
+ else
+ usb_close(handle);
+ }
+ }
+
+ return NULL;
+}
+
+int main(int argc, char **argv)
+{
+ usb_dev_handle *handle = NULL;
+ int nBytes = 0;
+ char buffer[256];
+
+ handle = usbOpenDevice(0x16C0, "windfisch.org", 0x05DC, "DS1992 Dumper");
+
+ if(handle == NULL)
+ {
+ fprintf(stderr, "Could not find USB device!\n");
+ exit(1);
+ }
+
+ printf("got device\n");
+
+ // read from device
+ nBytes = usb_control_msg(handle,
+ USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN,
+ 0x21, 0, 0, (char *)buffer, sizeof(buffer), 1000);
+ printf("Got %d bytes: %s;\n", nBytes, buffer);
+
+ // write to device
+ nBytes = usb_control_msg(handle,
+ USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_OUT,
+ 0x42, 0, 0, "Tschuess", strlen("Tschuess"), 1000);
+ printf("Wrote %d bytes\n", nBytes);
+
+
+
+
+ // read from device
+ nBytes = usb_control_msg(handle,
+ USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN,
+ 0x21, 0, 0, (char *)buffer, sizeof(buffer), 1000);
+ printf("Got %d bytes: %s;\n", nBytes, buffer);
+
+/*
+
+ nBytes = usb_control_msg(handle,
+ USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN,
+ 0x42, 0,0,
+ (char *)buffer, sizeof(buffer), 5000);
+ if(strcmp(argv[1], "on") == 0)
+ {
+ nBytes = usb_control_msg(handle,
+ USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN,
+ USB_LED_ON, 0, 0, (char *)buffer, sizeof(buffer), 5000);
+ }
+ else if(strcmp(argv[1], "off") == 0)
+ {
+ nBytes = usb_control_msg(handle,
+ USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN,
+ USB_LED_OFF, 0, 0, (char *)buffer, sizeof(buffer), 5000);
+ }
+ else if(strcmp(argv[1], "out") == 0)
+ {
+ nBytes = usb_control_msg(handle,
+ USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN,
+ USB_DATA_OUT, 0, 0, (char *)buffer, sizeof(buffer), 5000);
+ printf("Got %d bytes: %s\n", nBytes, buffer);
+ }
+ else if(strcmp(argv[1], "write") == 0)
+ {
+ nBytes = usb_control_msg(handle,
+ USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN,
+ USB_DATA_WRITE, 'T' + ('E' << 8), 'S' + ('T' << 8),
+ (char *)buffer, sizeof(buffer), 5000);
+ }
+ else if(strcmp(argv[1], "in") == 0 && argc > 2)
+ {
+ nBytes = usb_control_msg(handle,
+ USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_OUT,
+ USB_DATA_IN, 0, 0, argv[2], strlen(argv[2])+1, 5000);
+ }
+*/
+ if(nBytes < 0)
+ fprintf(stderr, "USB error: %s\n", usb_strerror());
+
+ usb_close(handle);
+
+ return 0;
+}