Keys and LEDs with USB
authorJulien Viard de Galbert <julien@vdg.blogsite.org>
Mon, 20 Dec 2010 16:03:49 +0000 (17:03 +0100)
committerJulien Viard de Galbert <julien@vdg.blogsite.org>
Mon, 20 Dec 2010 16:03:49 +0000 (17:03 +0100)
main.c
test/Makefile [new file with mode: 0644]
test/test.c [new file with mode: 0644]

diff --git a/main.c b/main.c
index 083ea67..f0e146d 100644 (file)
--- a/main.c
+++ b/main.c
 
 #include <avr/io.h>
 
+enum
+{
+       // Generic requests
+       USBTINY_ECHO,           // echo test
+       USBTINY_READ,           // read byte
+       USBTINY_WRITE,          // write byte
+       USBTINY_CLR,            // clear bit 
+       USBTINY_SET,            // set bit
+       //
+       USBTINY_KEY_READ        // read key status
+};
+
 // ----------------------------------------------------------------------
 // Handle a non-standard SETUP packet.
 // ----------------------------------------------------------------------
 extern byte_t  usb_setup ( byte_t data[8] )
 {
-       return 8;       // simply echo back the setup packet
+       byte_t  bit;
+       byte_t  mask;
+       byte_t  req;
+
+       // Generic requests
+       req = data[1];
+       if      ( req == USBTINY_ECHO )
+       {
+               return 8;
+       }
+       // LED PORTC0-3
+       if      ( req == USBTINY_READ )
+       {
+               data[0] = PINC & ((1 << PC0) | (1 << PC1) | (1 << PC2) | (1 << PC3));
+               return 1;
+       }
+       if      ( req == USBTINY_WRITE )
+       {
+               PORTC = data[2] & ((1 << PC0) | (1 << PC1) | (1 << PC2) | (1 << PC3));
+               return 0;
+       }
+       bit = data[2] & 3;
+       mask = 1 << bit;
+       if      ( req == USBTINY_CLR )
+       {
+               PORTC &= ~ mask;
+               return 0;
+       }
+       if      ( req == USBTINY_SET )
+       {
+               PORTC |= mask;
+               return 0;
+       }
+       if      ( req == USBTINY_KEY_READ )
+       {
+               // PD6-7, keys are inverted
+               data[0] = (~PIND & ((1 << PD6) | (1 << PD7))) >> PD6;
+               return 1;
+       }
+
+       return 0;
 }
 
 // ----------------------------------------------------------------------
@@ -51,19 +103,5 @@ extern      int     main ( void )
        for     ( ;; )
        {
                usb_poll();
-               {
-                       // stupid test:
-                       byte_t pin=PIND;
-                       if(!(pin & (1 << PD6))) {
-                               PORTC |= (1 << PC0) | (1 << PC1);
-                       } else {
-                               PORTC &= ~((1 << PC0) | (1 << PC1));
-                       }
-                       if(!(pin & (1 << PD7))) {
-                               PORTC |= (1 << PC2) | (1 << PC3);
-                       } else {
-                               PORTC &= ~((1 << PC2) | (1 << PC3));
-                       }
-               }
        }
 }
diff --git a/test/Makefile b/test/Makefile
new file mode 100644 (file)
index 0000000..4034b40
--- /dev/null
@@ -0,0 +1,11 @@
+# c flags
+CFLAGS:=-Os -g -Wall $(shell pkg-config --cflags libusb-1.0)
+# preprocessor flags
+CPPFLAGS:=
+# link flags
+LDFLAGS:=
+# libs
+LDLIBS:=$(shell pkg-config --libs libusb-1.0)
+
+
+all: test
diff --git a/test/test.c b/test/test.c
new file mode 100644 (file)
index 0000000..f04ae91
--- /dev/null
@@ -0,0 +1,74 @@
+// basic test program
+
+#include <stdio.h>
+#include <libusb.h>
+
+
+enum
+{
+       // Generic requests
+       USBTINY_ECHO,           // echo test
+       USBTINY_READ,           // read byte
+       USBTINY_WRITE,          // write byte
+       USBTINY_CLR,            // clear bit 
+       USBTINY_SET,            // set bit
+       //
+       USBTINY_KEY_READ        // read key status
+};
+
+#define USB_TIMEOUT 500
+
+libusb_device_handle * usb_device;
+
+int main(int argc, char ** argv)
+{
+       libusb_init(NULL);
+
+       printf("USB gadget test program (c) 2010 Julien VdG\n");
+
+       usb_device = libusb_open_device_with_vid_pid (NULL, 0x6666, 0x0001);
+       if(NULL == usb_device) {
+               fprintf(stderr, "ERROR: USB device not found !\n");
+               libusb_exit(NULL);
+               return 2;
+       }
+
+       {
+               unsigned char res[4];
+               int st;
+               st = libusb_control_transfer(usb_device, 
+                       LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE,
+                       USBTINY_READ,
+                       0,
+                       0,
+                       res,
+                       1,
+                       USB_TIMEOUT);
+               printf("Read LEDs: %x (%d)\n",res[0], st);
+
+               st = libusb_control_transfer(usb_device, 
+                       LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE,
+                       USBTINY_WRITE,
+                       0xf & (res[0]+1),
+                       1,
+                       NULL,
+                       0,
+                       USB_TIMEOUT);
+
+               st = libusb_control_transfer(usb_device, 
+                       LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE,
+                       USBTINY_KEY_READ,
+                       0,
+                       0,
+                       res,
+                       1,
+                       USB_TIMEOUT);
+               printf("Read Keys: %x (%d)\n",res[0], st);
+
+       }
+
+
+       libusb_close(usb_device);
+       libusb_exit(NULL);
+       return 0;
+}