summaryrefslogtreecommitdiff
path: root/software
diff options
context:
space:
mode:
authorBernhard Tittelbach <xro@realraum.at>2012-05-17 06:14:24 +0000
committerBernhard Tittelbach <xro@realraum.at>2012-05-17 06:14:24 +0000
commit9bdf2f92bf95c69d668772e1c289ab89868aa738 (patch)
treee55a84d87ea7af7de84e5500221be66f83aa3d94 /software
parenthhd70 spi (diff)
usb lib
git-svn-id: https://svn.spreadspace.org/mur.sat@415 7de4ea59-55d0-425e-a1af-a3118ea81d4c
Diffstat (limited to 'software')
-rw-r--r--software/hhd70dongle/Makefile3
-rw-r--r--software/hhd70dongle/hhd70dongle.c30
-rw-r--r--software/hhd70dongle/pcusbhid/Makefile62
-rw-r--r--software/hhd70dongle/pcusbhid/hid.h6
-rw-r--r--software/hhd70dongle/pcusbhid/hid_LINUX.c381
-rw-r--r--software/hhd70dongle/pcusbhid/hid_MACOSX.c412
-rw-r--r--software/hhd70dongle/pcusbhid/hid_WINDOWS.c328
-rw-r--r--software/hhd70dongle/pcusbhid/rawhid_testinterface.c76
-rw-r--r--software/hhd70dongle/spi.c14
-rw-r--r--software/hhd70dongle/spi.h2
-rw-r--r--software/hhd70dongle/usb_rawhid.c940
-rw-r--r--software/hhd70dongle/usb_rawhid.h85
12 files changed, 2319 insertions, 20 deletions
diff --git a/software/hhd70dongle/Makefile b/software/hhd70dongle/Makefile
index 7a90422..2b9b239 100644
--- a/software/hhd70dongle/Makefile
+++ b/software/hhd70dongle/Makefile
@@ -21,7 +21,8 @@
##
NAME := hhd70dongle
-OBJ := hhd70dongle.o led.o spi.o
+OBJ := hhd70dongle.o led.o spi.o usb_rawhid.o
BOARD_TYPE := teensy2
include ../teensy.include.mk
+CFLAGS += -std=gnu99
diff --git a/software/hhd70dongle/hhd70dongle.c b/software/hhd70dongle/hhd70dongle.c
index 04be5b5..03b4e06 100644
--- a/software/hhd70dongle/hhd70dongle.c
+++ b/software/hhd70dongle/hhd70dongle.c
@@ -34,20 +34,30 @@
#include "led.h"
#include "spi.h"
+#include "usb_rawhid.h"
#define CPU_PRESCALE(n) (CLKPR = 0x80, CLKPR = (n))
+uint8_t read_buffer[64]; // buffer for reading usb signals
+uint8_t write_buffer[64]; // buffer for writing usb signals
int main(void)
{
- CPU_PRESCALE(0);
- led_init();
- spi_init();
+ CPU_PRESCALE(0);
+ led_init();
+ spi_init();
+ usb_init();
+ while (!usb_configured()) /* wait */ ;
- for(;;) {
- _delay_ms(250);
- led_toggle();
- char buf[10];
- unsigned int len;
- spi_read(sizeof(buf),buf,&len);
- }
+ //int8_t r = usb_rawhid_recv(read_buffer, 0);
+ //usb_rawhid_send(write_buffer, 23);
+
+
+ for(;;)
+ {
+ _delay_ms(250);
+ led_toggle();
+ char buf[10];
+ unsigned int len;
+ spi_read(sizeof(buf),buf,&len);
+ }
}
diff --git a/software/hhd70dongle/pcusbhid/Makefile b/software/hhd70dongle/pcusbhid/Makefile
new file mode 100644
index 0000000..c6689ed
--- /dev/null
+++ b/software/hhd70dongle/pcusbhid/Makefile
@@ -0,0 +1,62 @@
+
+OS = LINUX
+#OS = MACOSX
+#OS = WINDOWS
+
+PROG = rawhid_testinterface
+
+# To set up Ubuntu Linux to cross compile for Windows:
+#
+# apt-get install mingw32 mingw32-binutils mingw32-runtime
+#
+# Just edit the variable above for WINDOWS, then use "make" to build rawhid.exe
+
+ifeq ($(OS), LINUX)
+TARGET = $(PROG)
+CC = gcc
+STRIP = strip
+CFLAGS = -Wall -O2 -DOS_$(OS)
+LIBS = -lusb
+else ifeq ($(OS), MACOSX)
+TARGET = $(PROG).dmg
+SDK = /Developer/SDKs/MacOSX10.5.sdk
+ARCH = -mmacosx-version-min=10.5 -arch ppc -arch i386
+CC = gcc
+STRIP = strip
+CFLAGS = -Wall -O2 -DOS_$(OS) -isysroot $(SDK) $(ARCH)
+LIBS = $(ARCH) -Wl,-syslibroot,$(SDK) -framework IOKit -framework CoreFoundation
+else ifeq ($(OS), WINDOWS)
+TARGET = $(PROG).exe
+CC = i586-mingw32msvc-gcc
+STRIP = i586-mingw32msvc-strip
+CFLAGS = -Wall -O2 -DOS_$(OS)
+LIBS = -lhid -lsetupapi
+endif
+
+OBJS = $(PROG).o hid.o
+
+
+all: $(TARGET)
+
+$(PROG): $(OBJS)
+ $(CC) -o $(PROG) $(OBJS) $(LIBS)
+ $(STRIP) $(PROG)
+
+$(PROG).exe: $(PROG)
+ cp $(PROG) $(PROG).exe
+
+$(PROG).dmg: $(PROG)
+ mkdir tmp
+ cp $(PROG) tmp
+ hdiutil create -ov -volname "Raw HID Test" -srcfolder tmp $(PROG).dmg
+
+hid.o: hid_$(OS).c hid.h
+ $(CC) $(CFLAGS) -c -o $@ $<
+
+reset.o: reset.c
+ $(CC) $(CFLAGS) -c -o $@ $<
+
+clean:
+ rm -f *.o $(PROG) $(PROG).exe $(PROG).dmg
+ rm -rf tmp
+
diff --git a/software/hhd70dongle/pcusbhid/hid.h b/software/hhd70dongle/pcusbhid/hid.h
new file mode 100644
index 0000000..4d83703
--- /dev/null
+++ b/software/hhd70dongle/pcusbhid/hid.h
@@ -0,0 +1,6 @@
+
+int rawhid_open(int max, int vid, int pid, int usage_page, int usage);
+int rawhid_recv(int num, void *buf, int len, int timeout);
+int rawhid_send(int num, void *buf, int len, int timeout);
+void rawhid_close(int num);
+
diff --git a/software/hhd70dongle/pcusbhid/hid_LINUX.c b/software/hhd70dongle/pcusbhid/hid_LINUX.c
new file mode 100644
index 0000000..8e98113
--- /dev/null
+++ b/software/hhd70dongle/pcusbhid/hid_LINUX.c
@@ -0,0 +1,381 @@
+/* Simple Raw HID functions for Linux - for use with Teensy RawHID example
+ * http://www.pjrc.com/teensy/rawhid.html
+ * Copyright (c) 2009 PJRC.COM, LLC
+ *
+ * rawhid_open - open 1 or more devices
+ * rawhid_recv - receive a packet
+ * rawhid_send - send a packet
+ * rawhid_close - close a device
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above description, website URL and copyright notice and this permission
+ * notice shall be included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ * Version 1.0: Initial Release
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdint.h>
+#include <usb.h>
+
+#include "hid.h"
+
+
+// On Linux there are several options to access HID devices.
+//
+// libusb 0.1 - the only way that works well on all distributions
+// libusb 1.0 - someday will become standard on most distributions
+// hidraw driver - relatively new, not supported on many distributions (yet)
+// hiddev driver - old, ubuntu, fedora, others dropping support
+// usbfs - low level usb API: http://www.kernel.org/doc/htmldocs/usb.html#usbfs
+//
+// This code uses libusb 0.1, which is well supported on all linux distributions
+// and very stable and widely used by many programs. libusb 0.1 only provides a
+// simple synchronous interface, basically the same as this code needs. However,
+// if you want non-blocking I/O, libusb 0.1 simply does not provide that. There
+// is also no kernel-level buffering, so performance is poor.
+//
+// UPDATE: As of November 2011, hidraw support seems to be working well in all
+// major linux distributions. Embedded and "small" distros, used on ARM boards,
+// routers and embedded hardware stil seem to omit the hidraw driver.
+//
+// The hidraw driver is a great solution. However, it has only been supported
+// by relatively recent (in 2009) kernels. Here is a quick survey of the status
+// of hidraw support in various distributions as of Sept 2009:
+//
+// Fedora 11: works, kernel 2.6.29.4
+// Mandiva 2009.1: works, kernel 2.6.29.1
+// Ubuntu 9.10-alpha6: works, kernel 2.6.31
+// Ubuntu 9.04: sysfs attrs chain broken (hidraw root only), 2.6.28 kernel
+// openSUSE 11.1: sysfs attrs chain broken (hidraw root only), 2.6.27.7 kernel
+// Debian Live, Lenny 5.0.2: sysfs attrs chain broken (hidraw root only), 2.6.26
+// SimplyMEPIS 8.0.10: sysfs attrs chain broken (hidraw root only), 2.6.27
+// Mint 7: sysfs attrs chain broken (hidraw root only), 2.6.28 kernel
+// Gentoo 2008.0-r1: sysfs attrs chain broken (hidraw root only), 2.6.24 kernel
+// Centos 5: no hidraw or hiddev devices, 2.6.18 kernel
+// Slitaz 2.0: no hidraw devices (has hiddev), 2.6.25.5 kernel
+// Puppy 4.3: no hidraw devices (has hiddev), 2.6.30.5 kernel
+// Damn Small 4.4.10: (would not boot)
+// Gentoo 10.0-test20090926: (would not boot)
+// PCLinuxOS 2009.2: (would not boot)
+// Slackware: (no live cd available? www.slackware-live.org dead)
+
+
+
+#define printf(...) // comment this out for lots of info
+
+
+// a list of all opened HID devices, so the caller can
+// simply refer to them by number
+typedef struct hid_struct hid_t;
+static hid_t *first_hid = NULL;
+static hid_t *last_hid = NULL;
+struct hid_struct {
+ usb_dev_handle *usb;
+ int open;
+ int iface;
+ int ep_in;
+ int ep_out;
+ struct hid_struct *prev;
+ struct hid_struct *next;
+};
+
+
+// private functions, not intended to be used from outside this file
+static void add_hid(hid_t *h);
+static hid_t * get_hid(int num);
+static void free_all_hid(void);
+static void hid_close(hid_t *hid);
+static int hid_parse_item(uint32_t *val, uint8_t **data, const uint8_t *end);
+
+
+// rawhid_recv - receive a packet
+// Inputs:
+// num = device to receive from (zero based)
+// buf = buffer to receive packet
+// len = buffer's size
+// timeout = time to wait, in milliseconds
+// Output:
+// number of bytes received, or -1 on error
+//
+int rawhid_recv(int num, void *buf, int len, int timeout)
+{
+ hid_t *hid;
+ int r;
+
+ hid = get_hid(num);
+ if (!hid || !hid->open) return -1;
+ r = usb_interrupt_read(hid->usb, hid->ep_in, buf, len, timeout);
+ if (r >= 0) return r;
+ if (r == -110) return 0; // timeout
+ return -1;
+}
+
+// rawhid_send - send a packet
+// Inputs:
+// num = device to transmit to (zero based)
+// buf = buffer containing packet to send
+// len = number of bytes to transmit
+// timeout = time to wait, in milliseconds
+// Output:
+// number of bytes sent, or -1 on error
+//
+int rawhid_send(int num, void *buf, int len, int timeout)
+{
+ hid_t *hid;
+
+ hid = get_hid(num);
+ if (!hid || !hid->open) return -1;
+ if (hid->ep_out) {
+ return usb_interrupt_write(hid->usb, hid->ep_out, buf, len, timeout);
+ } else {
+ return usb_control_msg(hid->usb, 0x21, 9, 0, hid->iface, buf, len, timeout);
+ }
+}
+
+// rawhid_open - open 1 or more devices
+//
+// Inputs:
+// max = maximum number of devices to open
+// vid = Vendor ID, or -1 if any
+// pid = Product ID, or -1 if any
+// usage_page = top level usage page, or -1 if any
+// usage = top level usage number, or -1 if any
+// Output:
+// actual number of devices opened
+//
+int rawhid_open(int max, int vid, int pid, int usage_page, int usage)
+{
+ struct usb_bus *bus;
+ struct usb_device *dev;
+ struct usb_interface *iface;
+ struct usb_interface_descriptor *desc;
+ struct usb_endpoint_descriptor *ep;
+ usb_dev_handle *u;
+ uint8_t buf[1024], *p;
+ int i, n, len, tag, ep_in, ep_out, count=0, claimed;
+ uint32_t val=0, parsed_usage, parsed_usage_page;
+ hid_t *hid;
+
+ if (first_hid) free_all_hid();
+ printf("rawhid_open, max=%d\n", max);
+ if (max < 1) return 0;
+ 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 (vid > 0 && dev->descriptor.idVendor != vid) continue;
+ if (pid > 0 && dev->descriptor.idProduct != pid) continue;
+ if (!dev->config) continue;
+ if (dev->config->bNumInterfaces < 1) continue;
+ printf("device: vid=%04X, pic=%04X, with %d iface\n",
+ dev->descriptor.idVendor,
+ dev->descriptor.idProduct,
+ dev->config->bNumInterfaces);
+ iface = dev->config->interface;
+ u = NULL;
+ claimed = 0;
+ for (i=0; i<dev->config->bNumInterfaces && iface; i++, iface++) {
+ desc = iface->altsetting;
+ if (!desc) continue;
+ printf(" type %d, %d, %d\n", desc->bInterfaceClass,
+ desc->bInterfaceSubClass, desc->bInterfaceProtocol);
+ if (desc->bInterfaceClass != 3) continue;
+ if (desc->bInterfaceSubClass != 0) continue;
+ if (desc->bInterfaceProtocol != 0) continue;
+ ep = desc->endpoint;
+ ep_in = ep_out = 0;
+ for (n = 0; n < desc->bNumEndpoints; n++, ep++) {
+ if (ep->bEndpointAddress & 0x80) {
+ if (!ep_in) ep_in = ep->bEndpointAddress & 0x7F;
+ printf(" IN endpoint %d\n", ep_in);
+ } else {
+ if (!ep_out) ep_out = ep->bEndpointAddress;
+ printf(" OUT endpoint %d\n", ep_out);
+ }
+ }
+ if (!ep_in) continue;
+ if (!u) {
+ u = usb_open(dev);
+ if (!u) {
+ printf(" unable to open device\n");
+ break;
+ }
+ }
+ printf(" hid interface (generic)\n");
+ if (usb_get_driver_np(u, i, (char *)buf, sizeof(buf)) >= 0) {
+ printf(" in use by driver \"%s\"\n", buf);
+ if (usb_detach_kernel_driver_np(u, i) < 0) {
+ printf(" unable to detach from kernel\n");
+ continue;
+ }
+ }
+ if (usb_claim_interface(u, i) < 0) {
+ printf(" unable claim interface %d\n", i);
+ continue;
+ }
+ len = usb_control_msg(u, 0x81, 6, 0x2200, i, (char *)buf, sizeof(buf), 250);
+ printf(" descriptor, len=%d\n", len);
+ if (len < 2) {
+ usb_release_interface(u, i);
+ continue;
+ }
+ p = buf;
+ parsed_usage_page = parsed_usage = 0;
+ while ((tag = hid_parse_item(&val, &p, buf + len)) >= 0) {
+ printf(" tag: %X, val %X\n", tag, val);
+ if (tag == 4) parsed_usage_page = val;
+ if (tag == 8) parsed_usage = val;
+ if (parsed_usage_page && parsed_usage) break;
+ }
+ if ((!parsed_usage_page) || (!parsed_usage) ||
+ (usage_page > 0 && parsed_usage_page != usage_page) ||
+ (usage > 0 && parsed_usage != usage)) {
+ usb_release_interface(u, i);
+ continue;
+ }
+ hid = (struct hid_struct *)malloc(sizeof(struct hid_struct));
+ if (!hid) {
+ usb_release_interface(u, i);
+ continue;
+ }
+ hid->usb = u;
+ hid->iface = i;
+ hid->ep_in = ep_in;
+ hid->ep_out = ep_out;
+ hid->open = 1;
+ add_hid(hid);
+ claimed++;
+ count++;
+ if (count >= max) return count;
+ }
+ if (u && !claimed) usb_close(u);
+ }
+ }
+ return count;
+}
+
+
+// rawhid_close - close a device
+//
+// Inputs:
+// num = device to close (zero based)
+// Output
+// (nothing)
+//
+void rawhid_close(int num)
+{
+ hid_t *hid;
+
+ hid = get_hid(num);
+ if (!hid || !hid->open) return;
+ hid_close(hid);
+}
+
+// Chuck Robey wrote a real HID report parser
+// (chuckr@telenix.org) chuckr@chuckr.org
+// http://people.freebsd.org/~chuckr/code/python/uhidParser-0.2.tbz
+// this tiny thing only needs to extract the top-level usage page
+// and usage, and even then is may not be truly correct, but it does
+// work with the Teensy Raw HID example.
+static int hid_parse_item(uint32_t *val, uint8_t **data, const uint8_t *end)
+{
+ const uint8_t *p = *data;
+ uint8_t tag;
+ int table[4] = {0, 1, 2, 4};
+ int len;
+
+ if (p >= end) return -1;
+ if (p[0] == 0xFE) {
+ // long item, HID 1.11, 6.2.2.3, page 27
+ if (p + 5 >= end || p + p[1] >= end) return -1;
+ tag = p[2];
+ *val = 0;
+ len = p[1] + 5;
+ } else {
+ // short item, HID 1.11, 6.2.2.2, page 26
+ tag = p[0] & 0xFC;
+ len = table[p[0] & 0x03];
+ if (p + len + 1 >= end) return -1;
+ switch (p[0] & 0x03) {
+ case 3: *val = p[1] | (p[2] << 8) | (p[3] << 16) | (p[4] << 24); break;
+ case 2: *val = p[1] | (p[2] << 8); break;
+ case 1: *val = p[1]; break;
+ case 0: *val = 0; break;
+ }
+ }
+ *data += len + 1;
+ return tag;
+}
+
+
+static void add_hid(hid_t *h)
+{
+ if (!first_hid || !last_hid) {
+ first_hid = last_hid = h;
+ h->next = h->prev = NULL;
+ return;
+ }
+ last_hid->next = h;
+ h->prev = last_hid;
+ h->next = NULL;
+ last_hid = h;
+}
+
+
+static hid_t * get_hid(int num)
+{
+ hid_t *p;
+ for (p = first_hid; p && num > 0; p = p->next, num--) ;
+ return p;
+}
+
+
+static void free_all_hid(void)
+{
+ hid_t *p, *q;
+
+ for (p = first_hid; p; p = p->next) {
+ hid_close(p);
+ }
+ p = first_hid;
+ while (p) {
+ q = p;
+ p = p->next;
+ free(q);
+ }
+ first_hid = last_hid = NULL;
+}
+
+
+static void hid_close(hid_t *hid)
+{
+ hid_t *p;
+ int others=0;
+
+ usb_release_interface(hid->usb, hid->iface);
+ for (p = first_hid; p; p = p->next) {
+ if (p->open && p->usb == hid->usb) others++;
+ }
+ if (!others) usb_close(hid->usb);
+ hid->usb = NULL;
+}
+
+
+
diff --git a/software/hhd70dongle/pcusbhid/hid_MACOSX.c b/software/hhd70dongle/pcusbhid/hid_MACOSX.c
new file mode 100644
index 0000000..c5d5d1b
--- /dev/null
+++ b/software/hhd70dongle/pcusbhid/hid_MACOSX.c
@@ -0,0 +1,412 @@
+/* Simple Raw HID functions for Linux - for use with Teensy RawHID example
+ * http://www.pjrc.com/teensy/rawhid.html
+ * Copyright (c) 2009 PJRC.COM, LLC
+ *
+ * rawhid_open - open 1 or more devices
+ * rawhid_recv - receive a packet
+ * rawhid_send - send a packet
+ * rawhid_close - close a device
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above description, website URL and copyright notice and this permission
+ * notice shall be included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ * Version 1.0: Initial Release
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdint.h>
+#include <unistd.h>
+#include <IOKit/IOKitLib.h>
+#include <IOKit/hid/IOHIDLib.h>
+
+#include "hid.h"
+
+#define BUFFER_SIZE 64
+
+#define printf(...) // comment this out to get lots of info printed
+
+
+// a list of all opened HID devices, so the caller can
+// simply refer to them by number
+typedef struct hid_struct hid_t;
+typedef struct buffer_struct buffer_t;
+static hid_t *first_hid = NULL;
+static hid_t *last_hid = NULL;
+struct hid_struct {
+ IOHIDDeviceRef ref;
+ int open;
+ uint8_t buffer[BUFFER_SIZE];
+ buffer_t *first_buffer;
+ buffer_t *last_buffer;
+ struct hid_struct *prev;
+ struct hid_struct *next;
+};
+struct buffer_struct {
+ struct buffer_struct *next;
+ uint32_t len;
+ uint8_t buf[BUFFER_SIZE];
+};
+
+// private functions, not intended to be used from outside this file
+static void add_hid(hid_t *);
+static hid_t * get_hid(int);
+static void free_all_hid(void);
+static void hid_close(hid_t *);
+static void attach_callback(void *, IOReturn, void *, IOHIDDeviceRef);
+static void detach_callback(void *, IOReturn, void *hid_mgr, IOHIDDeviceRef dev);
+static void timeout_callback(CFRunLoopTimerRef, void *);
+static void input_callback(void *, IOReturn, void *, IOHIDReportType,
+ uint32_t, uint8_t *, CFIndex);
+
+
+
+// rawhid_recv - receive a packet
+// Inputs:
+// num = device to receive from (zero based)
+// buf = buffer to receive packet
+// len = buffer's size
+// timeout = time to wait, in milliseconds
+// Output:
+// number of bytes received, or -1 on error
+//
+int rawhid_recv(int num, void *buf, int len, int timeout)
+{
+ hid_t *hid;
+ buffer_t *b;
+ CFRunLoopTimerRef timer=NULL;
+ CFRunLoopTimerContext context;
+ int ret=0, timeout_occurred=0;
+
+ if (len < 1) return 0;
+ hid = get_hid(num);
+ if (!hid || !hid->open) return -1;
+ if ((b = hid->first_buffer) != NULL) {
+ if (len > b->len) len = b->len;
+ memcpy(buf, b->buf, len);
+ hid->first_buffer = b->next;
+ free(b);
+ return len;
+ }
+ memset(&context, 0, sizeof(context));
+ context.info = &timeout_occurred;
+ timer = CFRunLoopTimerCreate(NULL, CFAbsoluteTimeGetCurrent() +
+ (double)timeout / 1000.0, 0, 0, 0, timeout_callback, &context);
+ CFRunLoopAddTimer(CFRunLoopGetCurrent(), timer, kCFRunLoopDefaultMode);
+ while (1) {
+ CFRunLoopRun();
+ if ((b = hid->first_buffer) != NULL) {
+ if (len > b->len) len = b->len;
+ memcpy(buf, b->buf, len);
+ hid->first_buffer = b->next;
+ free(b);
+ ret = len;
+ break;
+ }
+ if (!hid->open) {
+ printf("rawhid_recv, device not open\n");
+ ret = -1;
+ break;
+ }
+ if (timeout_occurred) break;
+ }
+ CFRunLoopTimerInvalidate(timer);
+ CFRelease(timer);
+ return ret;
+}
+
+static void input_callback(void *context, IOReturn ret, void *sender,
+ IOHIDReportType type, uint32_t id, uint8_t *data, CFIndex len)
+{
+ buffer_t *n;
+ hid_t *hid;
+
+ printf("input_callback\n");
+ if (ret != kIOReturnSuccess || len < 1) return;
+ hid = context;
+ if (!hid || hid->ref != sender) return;
+ n = (buffer_t *)malloc(sizeof(buffer_t));
+ if (!n) return;
+ if (len > BUFFER_SIZE) len = BUFFER_SIZE;
+ memcpy(n->buf, data, len);
+ n->len = len;
+ n->next = NULL;
+ if (!hid->first_buffer || !hid->last_buffer) {
+ hid->first_buffer = hid->last_buffer = n;
+ } else {
+ hid->last_buffer->next = n;
+ hid->last_buffer = n;
+ }
+ CFRunLoopStop(CFRunLoopGetCurrent());
+}
+
+static void timeout_callback(CFRunLoopTimerRef timer, void *info)
+{
+ printf("timeout_callback\n");
+ *(int *)info = 1;
+ CFRunLoopStop(CFRunLoopGetCurrent());
+}
+
+
+void output_callback(void *context, IOReturn ret, void *sender,
+ IOHIDReportType type, uint32_t id, uint8_t *data, CFIndex len)
+{
+ printf("output_callback, r=%d\n", ret);
+ if (ret == kIOReturnSuccess) {
+ *(int *)context = len;
+ } else {
+ // timeout if not success?
+ *(int *)context = 0;
+ }
+ CFRunLoopStop(CFRunLoopGetCurrent());
+}
+
+
+// rawhid_send - send a packet
+// Inputs:
+// num = device to transmit to (zero based)
+// buf = buffer containing packet to send
+// len = number of bytes to transmit
+// timeout = time to wait, in milliseconds
+// Output:
+// number of bytes sent, or -1 on error
+//
+int rawhid_send(int num, void *buf, int len, int timeout)
+{
+ hid_t *hid;
+ int result=-100;
+
+ hid = get_hid(num);
+ if (!hid || !hid->open) return -1;
+#if 1
+ #warning "Send timeout not implemented on MACOSX"
+ IOReturn ret = IOHIDDeviceSetReport(hid->ref, kIOHIDReportTypeOutput, 0, buf, len);
+ result = (ret == kIOReturnSuccess) ? len : -1;
+#endif
+#if 0
+ // No matter what I tried this never actually sends an output
+ // report and output_callback never gets called. Why??
+ // Did I miss something? This is exactly the same params as
+ // the sync call that works. Is it an Apple bug?
+ // (submitted to Apple on 22-sep-2009, problem ID 7245050)
+ //
+ IOHIDDeviceScheduleWithRunLoop(hid->ref, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
+ // should already be scheduled with run loop by attach_callback,
+ // sadly this doesn't make any difference either way
+
+ // could this be related?
+ // http://lists.apple.com/archives/usb/2008/Aug/msg00021.html
+ //
+ IOHIDDeviceSetReportWithCallback(hid->ref, kIOHIDReportTypeOutput,
+ 0, buf, len, (double)timeout / 1000.0, output_callback, &result);
+ while (1) {
+ printf("enter run loop (send)\n");
+ CFRunLoopRun();
+ printf("leave run loop (send)\n");
+ if (result > -100) break;
+ if (!hid->open) {
+ result = -1;
+ break;
+ }
+ }
+#endif
+ return result;
+}
+
+
+// rawhid_open - open 1 or more devices
+//
+// Inputs:
+// max = maximum number of devices to open
+// vid = Vendor ID, or -1 if any
+// pid = Product ID, or -1 if any
+// usage_page = top level usage page, or -1 if any
+// usage = top level usage number, or -1 if any
+// Output:
+// actual number of devices opened
+//
+int rawhid_open(int max, int vid, int pid, int usage_page, int usage)
+{
+ static IOHIDManagerRef hid_manager=NULL;
+ CFMutableDictionaryRef dict;
+ CFNumberRef num;
+ IOReturn ret;
+ hid_t *p;
+ int count=0;
+
+ if (first_hid) free_all_hid();
+ printf("rawhid_open, max=%d\n", max);
+ if (max < 1) return 0;
+ // Start the HID Manager
+ // http://developer.apple.com/technotes/tn2007/tn2187.html
+ if (!hid_manager) {
+ hid_manager = IOHIDManagerCreate(kCFAllocatorDefault, kIOHIDOptionsTypeNone);
+ if (hid_manager == NULL || CFGetTypeID(hid_manager) != IOHIDManagerGetTypeID()) {
+ if (hid_manager) CFRelease(hid_manager);
+ return 0;
+ }
+ }
+ if (vid > 0 || pid > 0 || usage_page > 0 || usage > 0) {
+ // Tell the HID Manager what type of devices we want
+ dict = CFDictionaryCreateMutable(kCFAllocatorDefault, 0,
+ &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
+ if (!dict) return 0;
+ if (vid > 0) {
+ num = CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &vid);
+ CFDictionarySetValue(dict, CFSTR(kIOHIDVendorIDKey), num);
+ CFRelease(num);
+ }
+ if (pid > 0) {
+ num = CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &pid);
+ CFDictionarySetValue(dict, CFSTR(kIOHIDProductIDKey), num);
+ CFRelease(num);
+ }
+ if (usage_page > 0) {
+ num = CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &usage_page);
+ CFDictionarySetValue(dict, CFSTR(kIOHIDPrimaryUsagePageKey), num);
+ CFRelease(num);
+ }
+ if (usage > 0) {
+ num = CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &usage);
+ CFDictionarySetValue(dict, CFSTR(kIOHIDPrimaryUsageKey), num);
+ CFRelease(num);
+ }
+ IOHIDManagerSetDeviceMatching(hid_manager, dict);
+ CFRelease(dict);
+ } else {
+ IOHIDManagerSetDeviceMatching(hid_manager, NULL);
+ }
+ // set up a callbacks for device attach & detach
+ IOHIDManagerScheduleWithRunLoop(hid_manager, CFRunLoopGetCurrent(),
+ kCFRunLoopDefaultMode);
+ IOHIDManagerRegisterDeviceMatchingCallback(hid_manager, attach_callback, NULL);
+ IOHIDManagerRegisterDeviceRemovalCallback(hid_manager, detach_callback, NULL);
+ ret = IOHIDManagerOpen(hid_manager, kIOHIDOptionsTypeNone);
+ if (ret != kIOReturnSuccess) {
+ IOHIDManagerUnscheduleFromRunLoop(hid_manager,
+ CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
+ CFRelease(hid_manager);
+ return 0;
+ }
+ printf("run loop\n");
+ // let it do the callback for all devices
+ while (CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0, true) == kCFRunLoopRunHandledSource) ;
+ // count up how many were added by the callback
+ for (p = first_hid; p; p = p->next) count++;
+ return count;
+}
+
+
+// rawhid_close - close a device
+//
+// Inputs:
+// num = device to close (zero based)
+// Output
+// (nothing)
+//
+void rawhid_close(int num)
+{
+ hid_t *hid;
+
+ hid = get_hid(num);
+ if (!hid || !hid->open) return;
+ hid_close(hid);
+ hid->open = 0;
+}
+
+
+static void add_hid(hid_t *h)
+{
+ if (!first_hid || !last_hid) {
+ first_hid = last_hid = h;
+ h->next = h->prev = NULL;
+ return;
+ }
+ last_hid->next = h;
+ h->prev = last_hid;
+ h->next = NULL;
+ last_hid = h;
+}
+
+
+static hid_t * get_hid(int num)
+{
+ hid_t *p;
+ for (p = first_hid; p && num > 0; p = p->next, num--) ;
+ return p;
+}
+
+
+static void free_all_hid(void)
+{
+ hid_t *p, *q;
+
+ for (p = first_hid; p; p = p->next) {
+ hid_close(p);
+ }
+ p = first_hid;
+ while (p) {
+ q = p;
+ p = p->next;
+ free(q);
+ }
+ first_hid = last_hid = NULL;
+}
+
+
+static void hid_close(hid_t *hid)
+{
+ if (!hid || !hid->open || !hid->ref) return;
+ IOHIDDeviceUnscheduleFromRunLoop(hid->ref, CFRunLoopGetCurrent( ), kCFRunLoopDefaultMode);
+ IOHIDDeviceClose(hid->ref, kIOHIDOptionsTypeNone);
+ hid->ref = NULL;
+}
+
+static void detach_callback(void *context, IOReturn r, void *hid_mgr, IOHIDDeviceRef dev)
+{
+ hid_t *p;
+
+ printf("detach callback\n");
+ for (p = first_hid; p; p = p->next) {
+ if (p->ref == dev) {
+ p->open = 0;
+ CFRunLoopStop(CFRunLoopGetCurrent());
+ return;
+ }
+ }
+}
+
+
+static void attach_callback(void *context, IOReturn r, void *hid_mgr, IOHIDDeviceRef dev)
+{
+ struct hid_struct *h;
+
+ printf("attach callback\n");
+ if (IOHIDDeviceOpen(dev, kIOHIDOptionsTypeNone) != kIOReturnSuccess) return;
+ h = (hid_t *)malloc(sizeof(hid_t));
+ if (!h) return;
+ memset(h, 0, sizeof(hid_t));
+ IOHIDDeviceScheduleWithRunLoop(dev, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
+ IOHIDDeviceRegisterInputReportCallback(dev, h->buffer, sizeof(h->buffer),
+ input_callback, h);
+ h->ref = dev;
+ h->open = 1;
+ add_hid(h);
+}
+
+
diff --git a/software/hhd70dongle/pcusbhid/hid_WINDOWS.c b/software/hhd70dongle/pcusbhid/hid_WINDOWS.c
new file mode 100644
index 0000000..0718773
--- /dev/null
+++ b/software/hhd70dongle/pcusbhid/hid_WINDOWS.c
@@ -0,0 +1,328 @@
+/* Simple Raw HID functions for Windows - for use with Teensy RawHID example
+ * http://www.pjrc.com/teensy/rawhid.html
+ * Copyright (c) 2009 PJRC.COM, LLC
+ *
+ * rawhid_open - open 1 or more devices
+ * rawhid_recv - receive a packet
+ * rawhid_send - send a packet
+ * rawhid_close - close a device
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above description, website URL and copyright notice and this permission
+ * notice shall be included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ * Version 1.0: Initial Release
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdint.h>
+#include <windows.h>
+#include <setupapi.h>
+#include <ddk/hidsdi.h>
+#include <ddk/hidclass.h>
+
+#include "hid.h"
+
+
+// a list of all opened HID devices, so the caller can
+// simply refer to them by number
+typedef struct hid_struct hid_t;
+static hid_t *first_hid = NULL;
+static hid_t *last_hid = NULL;
+struct hid_struct {
+ HANDLE handle;
+ int open;
+ struct hid_struct *prev;
+ struct hid_struct *next;
+};
+static HANDLE rx_event=NULL;
+static HANDLE tx_event=NULL;
+static CRITICAL_SECTION rx_mutex;
+static CRITICAL_SECTION tx_mutex;
+
+
+// private functions, not intended to be used from outside this file
+static void add_hid(hid_t *h);
+static hid_t * get_hid(int num);
+static void free_all_hid(void);
+static void hid_close(hid_t *hid);
+void print_win32_err(void);
+
+
+
+
+// rawhid_recv - receive a packet
+// Inputs:
+// num = device to receive from (zero based)
+// buf = buffer to receive packet
+// len = buffer's size
+// timeout = time to wait, in milliseconds
+// Output:
+// number of bytes received, or -1 on error
+//
+int rawhid_recv(int num, void *buf, int len, int timeout)
+{
+ hid_t *hid;
+ unsigned char tmpbuf[516];
+ OVERLAPPED ov;
+ DWORD n, r;
+
+ if (sizeof(tmpbuf) < len + 1) return -1;
+ hid = get_hid(num);
+ if (!hid || !hid->open) return -1;
+ EnterCriticalSection(&rx_mutex);
+ ResetEvent(&rx_event);
+ memset(&ov, 0, sizeof(ov));
+ ov.hEvent = rx_event;
+ if (!ReadFile(hid->handle, tmpbuf, len + 1, NULL, &ov)) {
+ if (GetLastError() != ERROR_IO_PENDING) goto return_error;
+ r = WaitForSingleObject(rx_event, timeout);
+ if (r == WAIT_TIMEOUT) goto return_timeout;
+ if (r != WAIT_OBJECT_0) goto return_error;
+ }
+ if (!GetOverlappedResult(hid->handle, &ov, &n, FALSE)) goto return_error;
+ LeaveCriticalSection(&rx_mutex);
+ if (n <= 0) return -1;
+ n--;
+ if (n > len) n = len;
+ memcpy(buf, tmpbuf + 1, n);
+ return n;
+return_timeout:
+ CancelIo(hid->handle);
+ LeaveCriticalSection(&rx_mutex);
+ return 0;
+return_error:
+ print_win32_err();
+ LeaveCriticalSection(&rx_mutex);
+ return -1;
+}
+
+// rawhid_send - send a packet
+// Inputs:
+// num = device to transmit to (zero based)
+// buf = buffer containing packet to send
+// len = number of bytes to transmit
+// timeout = time to wait, in milliseconds
+// Output:
+// number of bytes sent, or -1 on error
+//
+int rawhid_send(int num, void *buf, int len, int timeout)
+{
+ hid_t *hid;
+ unsigned char tmpbuf[516];
+ OVERLAPPED ov;
+ DWORD n, r;
+
+ if (sizeof(tmpbuf) < len + 1) return -1;
+ hid = get_hid(num);
+ if (!hid || !hid->open) return -1;
+ EnterCriticalSection(&tx_mutex);
+ ResetEvent(&tx_event);
+ memset(&ov, 0, sizeof(ov));
+ ov.hEvent = tx_event;
+ tmpbuf[0] = 0;
+ memcpy(tmpbuf + 1, buf, len);
+ if (!WriteFile(hid->handle, tmpbuf, len + 1, NULL, &ov)) {
+ if (GetLastError() != ERROR_IO_PENDING) goto return_error;
+ r = WaitForSingleObject(tx_event, timeout);
+ if (r == WAIT_TIMEOUT) goto return_timeout;
+ if (r != WAIT_OBJECT_0) goto return_error;
+ }
+ if (!GetOverlappedResult(hid->handle, &ov, &n, FALSE)) goto return_error;
+ LeaveCriticalSection(&tx_mutex);
+ if (n <= 0) return -1;
+ return n - 1;
+return_timeout:
+ CancelIo(hid->handle);
+ LeaveCriticalSection(&tx_mutex);
+ return 0;
+return_error:
+ print_win32_err();
+ LeaveCriticalSection(&tx_mutex);
+ return -1;
+}
+
+// rawhid_open - open 1 or more devices
+//
+// Inputs:
+// max = maximum number of devices to open
+// vid = Vendor ID, or -1 if any
+// pid = Product ID, or -1 if any
+// usage_page = top level usage page, or -1 if any
+// usage = top level usage number, or -1 if any
+// Output:
+// actual number of devices opened
+//
+int rawhid_open(int max, int vid, int pid, int usage_page, int usage)
+{
+ GUID guid;
+ HDEVINFO info;
+ DWORD index=0, reqd_size;
+ SP_DEVICE_INTERFACE_DATA iface;
+ SP_DEVICE_INTERFACE_DETAIL_DATA *details;
+ HIDD_ATTRIBUTES attrib;
+ PHIDP_PREPARSED_DATA hid_data;
+ HIDP_CAPS capabilities;
+ HANDLE h;
+ BOOL ret;
+ hid_t *hid;
+ int count=0;
+
+ if (first_hid) free_all_hid();
+ if (max < 1) return 0;
+ if (!rx_event) {
+ rx_event = CreateEvent(NULL, TRUE, TRUE, NULL);
+ tx_event = CreateEvent(NULL, TRUE, TRUE, NULL);
+ InitializeCriticalSection(&rx_mutex);
+ InitializeCriticalSection(&tx_mutex);
+ }
+ HidD_GetHidGuid(&guid);
+ info = SetupDiGetClassDevs(&guid, NULL, NULL, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
+ if (info == INVALID_HANDLE_VALUE) return 0;
+ for (index=0; 1 ;index++) {
+ iface.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
+ ret = SetupDiEnumDeviceInterfaces(info, NULL, &guid, index, &iface);
+ if (!ret) return count;
+ SetupDiGetInterfaceDeviceDetail(info, &iface, NULL, 0, &reqd_size, NULL);
+ details = (SP_DEVICE_INTERFACE_DETAIL_DATA *)malloc(reqd_size);
+ if (details == NULL) continue;
+
+ memset(details, 0, reqd_size);
+ details->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
+ ret = SetupDiGetDeviceInterfaceDetail(info, &iface, details,
+ reqd_size, NULL, NULL);
+ if (!ret) {
+ free(details);
+ continue;
+ }
+ h = CreateFile(details->DevicePath, GENERIC_READ|GENERIC_WRITE,
+ FILE_SHARE_READ|FILE_SHARE_WRITE, NULL,
+ OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL);
+ free(details);
+ if (h == INVALID_HANDLE_VALUE) continue;
+ attrib.Size = sizeof(HIDD_ATTRIBUTES);
+ ret = HidD_GetAttributes(h, &attrib);
+ //printf("vid: %4x\n", attrib.VendorID);
+ if (!ret || (vid > 0 && attrib.VendorID != vid) ||
+ (pid > 0 && attrib.ProductID != pid) ||
+ !HidD_GetPreparsedData(h, &hid_data)) {
+ CloseHandle(h);
+ continue;
+ }
+ if (!HidP_GetCaps(hid_data, &capabilities) ||
+ (usage_page > 0 && capabilities.UsagePage != usage_page) ||
+ (usage > 0 && capabilities.Usage != usage)) {
+ HidD_FreePreparsedData(hid_data);
+ CloseHandle(h);
+ continue;
+ }
+ HidD_FreePreparsedData(hid_data);
+ hid = (struct hid_struct *)malloc(sizeof(struct hid_struct));
+ if (!hid) {
+ CloseHandle(h);
+ continue;
+ }
+ hid->handle = h;
+ hid->open = 1;
+ add_hid(hid);
+ count++;
+ if (count >= max) return count;
+ }
+ return count;
+}
+
+
+// rawhid_close - close a device
+//
+// Inputs:
+// num = device to close (zero based)
+// Output
+// (nothing)
+//
+void rawhid_close(int num)
+{
+ hid_t *hid;
+
+ hid = get_hid(num);
+ if (!hid || !hid->open) return;
+ hid_close(hid);
+}
+
+
+
+static void add_hid(hid_t *h)
+{
+ if (!first_hid || !last_hid) {
+ first_hid = last_hid = h;
+ h->next = h->prev = NULL;
+ return;
+ }
+ last_hid->next = h;
+ h->prev = last_hid;
+ h->next = NULL;
+ last_hid = h;
+}
+
+
+static hid_t * get_hid(int num)
+{
+ hid_t *p;
+ for (p = first_hid; p && num > 0; p = p->next, num--) ;
+ return p;
+}
+
+
+static void free_all_hid(void)
+{
+ hid_t *p, *q;
+
+ for (p = first_hid; p; p = p->next) {
+ hid_close(p);
+ }
+ p = first_hid;
+ while (p) {
+ q = p;
+ p = p->next;
+ free(q);
+ }
+ first_hid = last_hid = NULL;
+}
+
+
+static void hid_close(hid_t *hid)
+{
+ CloseHandle(hid->handle);
+ hid->handle = NULL;
+}
+
+
+void print_win32_err(void)
+{
+ char buf[256];
+ DWORD err;
+
+ err = GetLastError();
+ FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, err,
+ 0, buf, sizeof(buf), NULL);
+ printf("err %ld: %s\n", err, buf);
+}
+
+
+
+
+
diff --git a/software/hhd70dongle/pcusbhid/rawhid_testinterface.c b/software/hhd70dongle/pcusbhid/rawhid_testinterface.c
new file mode 100644
index 0000000..8f19c4f
--- /dev/null
+++ b/software/hhd70dongle/pcusbhid/rawhid_testinterface.c
@@ -0,0 +1,76 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdarg.h>
+#include <string.h>
+#include <sys/time.h>
+#include <unistd.h>
+#if defined(OS_LINUX) || defined(OS_MACOSX)
+#include <sys/ioctl.h>
+#include <termios.h>
+#elif defined(OS_WINDOWS)
+#include <conio.h>
+#endif
+
+#include "hid.h"
+
+
+void sendstr(char * tosend)
+{
+ rawhid_send(0, tosend, strlen(tosend),1000);
+}
+
+int mtime_diff(struct timeval high,struct timeval low)
+{
+ int result=1000*(high.tv_sec-low.tv_sec);
+ result+=high.tv_usec/1000-low.tv_usec/1000;
+ return result;
+}
+
+#define BUF_LEN 64
+
+int main (int argc, char *argv[])
+{
+ int i, r, num;
+ char buf[BUF_LEN];
+ // C-based example is 16C0:0480:FFAB:0200
+ unsigned int attemtps = 100;
+ while(attemtps--)
+ {
+ r = rawhid_open(1, 0x16C0, 0x0481, 0xFFAB, 0x0200);
+ if (r > 0)
+ break;
+ else
+ sleep(1.0);
+ }
+ if (r <= 0)
+ {
+ printf("no rawhid device found\n");
+ return -1;
+ }
+ printf("found rawhid device\n\n");
+ //~ printf("Clearing Buffer\n");
+ //~ sendstr("c"); // clear the buffer
+ size_t len;
+ //~ struct timeval start_time,stop_time;
+ //~ gettimeofday(&start_time,NULL);
+ //~ gettimeofday(&stop_time,NULL);
+ //~ while (mtime_diff(stop_time,start_time)<1000) {
+ // check if any Raw HID packet has arrived
+
+ while (1)
+ {
+ num = rawhid_recv(0, buf, BUF_LEN, 220);
+ if (num < 0)
+ {
+ printf("\nerror reading, device went offline\n");
+ rawhid_close(0);
+ return 0;
+ }
+ else if (num == 0)
+ {
+ sleep(1.0);
+ }
+ buf[num]='\0';
+ printf("%s\n",buf);
+ }
+}
diff --git a/software/hhd70dongle/spi.c b/software/hhd70dongle/spi.c
index 618c581..ab5d5b2 100644
--- a/software/hhd70dongle/spi.c
+++ b/software/hhd70dongle/spi.c
@@ -69,25 +69,23 @@ void spi_write(char* data, unsigned int len)
//enable SS of CC1101
PORTB |= (1<<CS);
- for (unsigned int c=0; c++; c<len)
- spi_write_byte(data[c])
+ for (unsigned int c=0; c<len; c++)
+ spi_write_byte(data[c]);
//disable SS of CC1101
PORTB &= ~(1<<CS);
}
-void spi_read(unsigned int maxlen, char* data)
+void spi_read(unsigned int maxlen, char *data, unsigned int *len)
{
PORTB |= (1<<CS);
while(SPI_PINB_REG & (1<<MISO)); /* wait for CC1101 to get ready... */
SPDR = 0x80;
- unsigned int len = 0;
- while(len < maxlen && !(SPSR & (1<<SPIF)))
- {
- data[len++]=SPSR
- }
+ len = 0;
+ while(*len < maxlen && !(SPSR & (1<<SPIF)))
+ data[*len++]=SPSR;
PORTB &= ~(1<<CS);
}
diff --git a/software/hhd70dongle/spi.h b/software/hhd70dongle/spi.h
index 1243122..6346c05 100644
--- a/software/hhd70dongle/spi.h
+++ b/software/hhd70dongle/spi.h
@@ -36,6 +36,6 @@
void spi_init(void);
void spi_write_byte(char byte);
void spi_write(char* data,unsigned int len);
-void spi_read(unsigned int maxlen, char* data, unsigned int* len);
+void spi_read(unsigned int maxlen, char *data, unsigned int *len);
#endif
diff --git a/software/hhd70dongle/usb_rawhid.c b/software/hhd70dongle/usb_rawhid.c
new file mode 100644
index 0000000..1c94ca6
--- /dev/null
+++ b/software/hhd70dongle/usb_rawhid.c
@@ -0,0 +1,940 @@
+/* Teensy RawHID example
+ * http://www.pjrc.com/teensy/rawhid.html
+ * Copyright (c) 2009 PJRC.COM, LLC
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above description, website URL and copyright notice and this permission
+ * notice shall be included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+// Version 1.0: Initial Release
+// Version 1.1: fixed bug in analog
+
+#define USB_PRIVATE_INCLUDE
+#include "usb_rawhid.h"
+
+/**************************************************************************
+ *
+ * Configurable Options
+ *
+ **************************************************************************/
+
+// You can change these to give your code its own name.
+#define STR_MANUFACTURER L"realraum"
+#define STR_PRODUCT L"rf433rxtx"
+
+// These 4 numbers identify your device. Set these to
+// something that is (hopefully) not used by any others!
+#define VENDOR_ID 0x16C0
+#define PRODUCT_ID 0x0481
+#define RAWHID_USAGE_PAGE 0xFFAB // recommended: 0xFF00 to 0xFFFF
+#define RAWHID_USAGE 0x0200 // recommended: 0x0100 to 0xFFFF
+
+// These determine the bandwidth that will be allocated
+// for your communication. You do not need to use it
+// all, but allocating more than necessary means reserved
+// bandwidth is no longer available to other USB devices.
+#define RAWHID_TX_SIZE 64 // transmit packet size
+#define RAWHID_TX_INTERVAL 2 // max # of ms between transmit packets
+#define RAWHID_RX_SIZE 64 // receive packet size
+#define RAWHID_RX_INTERVAL 8 // max # of ms between receive packets
+
+
+/**************************************************************************
+ *
+ * Endpoint Buffer Configuration
+ *
+ **************************************************************************/
+
+#define ENDPOINT0_SIZE 32
+#define RAWHID_INTERFACE 0
+#define RAWHID_TX_ENDPOINT 1
+#define RAWHID_RX_ENDPOINT 2
+
+#if defined(__AVR_AT90USB162__)
+#define RAWHID_TX_BUFFER EP_SINGLE_BUFFER
+#define RAWHID_RX_BUFFER EP_SINGLE_BUFFER
+#else
+#define RAWHID_TX_BUFFER EP_DOUBLE_BUFFER
+#define RAWHID_RX_BUFFER EP_DOUBLE_BUFFER
+#endif
+
+static const uint8_t PROGMEM endpoint_config_table[] = {
+ 1, EP_TYPE_INTERRUPT_IN, EP_SIZE(RAWHID_TX_SIZE) | RAWHID_TX_BUFFER,
+ 1, EP_TYPE_INTERRUPT_OUT, EP_SIZE(RAWHID_RX_SIZE) | RAWHID_RX_BUFFER,
+ 0,
+ 0
+};
+
+
+/**************************************************************************
+ *
+ * Descriptor Data
+ *
+ **************************************************************************/
+
+// Descriptors are the data that your computer reads when it auto-detects
+// this USB device (called "enumeration" in USB lingo). The most commonly
+// changed items are editable at the top of this file. Changing things
+// in here should only be done by those who've read chapter 9 of the USB
+// spec and relevant portions of any USB class specifications!
+
+
+static uint8_t PROGMEM device_descriptor[] = {
+ 18, // bLength
+ 1, // bDescriptorType
+ 0x00, 0x02, // bcdUSB
+ 0, // bDeviceClass
+ 0, // bDeviceSubClass
+ 0, // bDeviceProtocol
+ ENDPOINT0_SIZE, // bMaxPacketSize0
+ LSB(VENDOR_ID), MSB(VENDOR_ID), // idVendor
+ LSB(PRODUCT_ID), MSB(PRODUCT_ID), // idProduct
+ 0x00, 0x01, // bcdDevice
+ 1, // iManufacturer
+ 2, // iProduct
+ 0, // iSerialNumber
+ 1 // bNumConfigurations
+};
+
+static uint8_t PROGMEM rawhid_hid_report_desc[] = {
+ 0x06, LSB(RAWHID_USAGE_PAGE), MSB(RAWHID_USAGE_PAGE),
+ 0x0A, LSB(RAWHID_USAGE), MSB(RAWHID_USAGE),
+ 0xA1, 0x01, // Collection 0x01
+ 0x75, 0x08, // report size = 8 bits
+ 0x15, 0x00, // logical minimum = 0
+ 0x26, 0xFF, 0x00, // logical maximum = 255
+ 0x95, RAWHID_TX_SIZE, // report count
+ 0x09, 0x01, // usage
+ 0x81, 0x02, // Input (array)
+ 0x95, RAWHID_RX_SIZE, // report count
+ 0x09, 0x02, // usage
+ 0x91, 0x02, // Output (array)
+ 0xC0 // end collection
+};
+
+
+#define CONFIG1_DESC_SIZE (9+9+9+7+7)
+#define RAWHID_HID_DESC_OFFSET (9+9)
+static uint8_t PROGMEM config1_descriptor[CONFIG1_DESC_SIZE] = {
+ // configuration descriptor, USB spec 9.6.3, page 264-266, Table 9-10
+ 9, // bLength;
+ 2, // bDescriptorType;
+ LSB(CONFIG1_DESC_SIZE), // wTotalLength
+ MSB(CONFIG1_DESC_SIZE),
+ 1, // bNumInterfaces
+ 1, // bConfigurationValue
+ 0, // iConfiguration
+ 0xC0, // bmAttributes
+ 50, // bMaxPower
+
+ // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
+ 9, // bLength
+ 4, // bDescriptorType
+ RAWHID_INTERFACE, // bInterfaceNumber
+ 0, // bAlternateSetting
+ 2, // bNumEndpoints
+ 0x03, // bInterfaceClass (0x03 = HID)
+ 0x00, // bInterfaceSubClass (0x01 = Boot)
+ 0x00, // bInterfaceProtocol (0x01 = Keyboard)
+ 0, // iInterface
+ // HID interface descriptor, HID 1.11 spec, section 6.2.1
+ 9, // bLength
+ 0x21, // bDescriptorType
+ 0x11, 0x01, // bcdHID
+ 0, // bCountryCode
+ 1, // bNumDescriptors
+ 0x22, // bDescriptorType
+ sizeof(rawhid_hid_report_desc), // wDescriptorLength
+ 0,
+ // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
+ 7, // bLength
+ 5, // bDescriptorType
+ RAWHID_TX_ENDPOINT | 0x80, // bEndpointAddress
+ 0x03, // bmAttributes (0x03=intr)
+ RAWHID_TX_SIZE, 0, // wMaxPacketSize
+ RAWHID_TX_INTERVAL, // bInterval
+ // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
+ 7, // bLength
+ 5, // bDescriptorType
+ RAWHID_RX_ENDPOINT, // bEndpointAddress
+ 0x03, // bmAttributes (0x03=intr)
+ RAWHID_RX_SIZE, 0, // wMaxPacketSize
+ RAWHID_RX_INTERVAL // bInterval
+};
+
+// If you're desperate for a little extra code memory, these strings
+// can be completely removed if iManufacturer, iProduct, iSerialNumber
+// in the device desciptor are changed to zeros.
+struct usb_string_descriptor_struct {
+ uint8_t bLength;
+ uint8_t bDescriptorType;
+ int16_t wString[];
+};
+static struct usb_string_descriptor_struct PROGMEM string0 = {
+ 4,
+ 3,
+ {0x0409}
+};
+static struct usb_string_descriptor_struct PROGMEM string1 = {
+ sizeof(STR_MANUFACTURER),
+ 3,
+ STR_MANUFACTURER
+};
+static struct usb_string_descriptor_struct PROGMEM string2 = {
+ sizeof(STR_PRODUCT),
+ 3,
+ STR_PRODUCT
+};
+
+// This table defines which descriptor data is sent for each specific
+// request from the host (in wValue and wIndex).
+static struct descriptor_list_struct {
+ uint16_t wValue;
+ uint16_t wIndex;
+ const uint8_t *addr;
+ uint8_t length;
+} PROGMEM descriptor_list[] = {
+ {0x0100, 0x0000, device_descriptor, sizeof(device_descriptor)},
+ {0x0200, 0x0000, config1_descriptor, sizeof(config1_descriptor)},
+ {0x2200, RAWHID_INTERFACE, rawhid_hid_report_desc, sizeof(rawhid_hid_report_desc)},
+ {0x2100, RAWHID_INTERFACE, config1_descriptor+RAWHID_HID_DESC_OFFSET, 9},
+ {0x0300, 0x0000, (const uint8_t *)&string0, 4},
+ {0x0301, 0x0409, (const uint8_t *)&string1, sizeof(STR_MANUFACTURER)},
+ {0x0302, 0x0409, (const uint8_t *)&string2, sizeof(STR_PRODUCT)}
+};
+#define NUM_DESC_LIST (sizeof(descriptor_list)/sizeof(struct descriptor_list_struct))
+
+
+/**************************************************************************
+ *
+ * Variables - these are the only non-stack RAM usage
+ *
+ **************************************************************************/
+
+// zero when we are not configured, non-zero when enumerated
+static volatile uint8_t usb_configuration=0;
+
+// these are a more reliable timeout than polling the
+// frame counter (UDFNUML)
+static volatile uint8_t rx_timeout_count=0;
+static volatile uint8_t tx_timeout_count=0;
+
+
+
+/**************************************************************************
+ *
+ * Public Functions - these are the API intended for the user
+ *
+ **************************************************************************/
+
+
+// initialize USB
+void usb_init(void)
+{
+ HW_CONFIG();
+ USB_FREEZE(); // enable USB
+ PLL_CONFIG(); // config PLL
+ while (!(PLLCSR & (1<<PLOCK))) ; // wait for PLL lock
+ USB_CONFIG(); // start USB clock
+ UDCON = 0; // enable attach resistor
+ usb_configuration = 0;
+ UDIEN = (1<<EORSTE)|(1<<SOFE);
+ sei();
+}
+
+// return 0 if the USB is not configured, or the configuration
+// number selected by the HOST
+uint8_t usb_configured(void)
+{
+ return usb_configuration;
+}
+
+
+// receive a packet, with timeout
+int8_t usb_rawhid_recv(uint8_t *buffer, uint8_t timeout)
+{
+ uint8_t intr_state;
+
+ // if we're not online (enumerated and configured), error
+ if (!usb_configuration) return -1;
+ intr_state = SREG;
+ cli();
+ rx_timeout_count = timeout;
+ UENUM = RAWHID_RX_ENDPOINT;
+ // wait for data to be available in the FIFO
+ while (1) {
+ if (UEINTX & (1<<RWAL)) break;
+ SREG = intr_state;
+ if (rx_timeout_count == 0) return 0;
+ if (!usb_configuration) return -1;
+ intr_state = SREG;
+ cli();
+ UENUM = RAWHID_RX_ENDPOINT;
+ }
+ // read bytes from the FIFO
+ #if (RAWHID_RX_SIZE >= 64)
+ *buffer++ = UEDATX;
+ #endif
+ #if (RAWHID_RX_SIZE >= 63)
+ *buffer++ = UEDATX;
+ #endif
+ #if (RAWHID_RX_SIZE >= 62)
+ *buffer++ = UEDATX;
+ #endif
+ #if (RAWHID_RX_SIZE >= 61)
+ *buffer++ = UEDATX;
+ #endif
+ #if (RAWHID_RX_SIZE >= 60)
+ *buffer++ = UEDATX;
+ #endif
+ #if (RAWHID_RX_SIZE >= 59)
+ *buffer++ = UEDATX;
+ #endif
+ #if (RAWHID_RX_SIZE >= 58)
+ *buffer++ = UEDATX;
+ #endif
+ #if (RAWHID_RX_SIZE >= 57)
+ *buffer++ = UEDATX;
+ #endif
+ #if (RAWHID_RX_SIZE >= 56)
+ *buffer++ = UEDATX;
+ #endif
+ #if (RAWHID_RX_SIZE >= 55)
+ *buffer++ = UEDATX;
+ #endif
+ #if (RAWHID_RX_SIZE >= 54)
+ *buffer++ = UEDATX;
+ #endif
+ #if (RAWHID_RX_SIZE >= 53)
+ *buffer++ = UEDATX;
+ #endif
+ #if (RAWHID_RX_SIZE >= 52)
+ *buffer++ = UEDATX;
+ #endif
+ #if (RAWHID_RX_SIZE >= 51)
+ *buffer++ = UEDATX;
+ #endif
+ #if (RAWHID_RX_SIZE >= 50)
+ *buffer++ = UEDATX;
+ #endif
+ #if (RAWHID_RX_SIZE >= 49)
+ *buffer++ = UEDATX;
+ #endif
+ #if (RAWHID_RX_SIZE >= 48)
+ *buffer++ = UEDATX;
+ #endif
+ #if (RAWHID_RX_SIZE >= 47)
+ *buffer++ = UEDATX;
+ #endif
+ #if (RAWHID_RX_SIZE >= 46)
+ *buffer++ = UEDATX;
+ #endif
+ #if (RAWHID_RX_SIZE >= 45)
+ *buffer++ = UEDATX;
+ #endif
+ #if (RAWHID_RX_SIZE >= 44)
+ *buffer++ = UEDATX;
+ #endif
+ #if (RAWHID_RX_SIZE >= 43)
+ *buffer++ = UEDATX;
+ #endif
+ #if (RAWHID_RX_SIZE >= 42)
+ *buffer++ = UEDATX;
+ #endif
+ #if (RAWHID_RX_SIZE >= 41)
+ *buffer++ = UEDATX;
+ #endif
+ #if (RAWHID_RX_SIZE >= 40)
+ *buffer++ = UEDATX;
+ #endif
+ #if (RAWHID_RX_SIZE >= 39)
+ *buffer++ = UEDATX;
+ #endif
+ #if (RAWHID_RX_SIZE >= 38)
+ *buffer++ = UEDATX;
+ #endif
+ #if (RAWHID_RX_SIZE >= 37)
+ *buffer++ = UEDATX;
+ #endif
+ #if (RAWHID_RX_SIZE >= 36)
+ *buffer++ = UEDATX;
+ #endif
+ #if (RAWHID_RX_SIZE >= 35)
+ *buffer++ = UEDATX;
+ #endif
+ #if (RAWHID_RX_SIZE >= 34)
+ *buffer++ = UEDATX;
+ #endif
+ #if (RAWHID_RX_SIZE >= 33)
+ *buffer++ = UEDATX;
+ #endif
+ #if (RAWHID_RX_SIZE >= 32)
+ *buffer++ = UEDATX;
+ #endif
+ #if (RAWHID_RX_SIZE >= 31)
+ *buffer++ = UEDATX;
+ #endif
+ #if (RAWHID_RX_SIZE >= 30)
+ *buffer++ = UEDATX;
+ #endif
+ #if (RAWHID_RX_SIZE >= 29)
+ *buffer++ = UEDATX;
+ #endif
+ #if (RAWHID_RX_SIZE >= 28)
+ *buffer++ = UEDATX;
+ #endif
+ #if (RAWHID_RX_SIZE >= 27)
+ *buffer++ = UEDATX;
+ #endif
+ #if (RAWHID_RX_SIZE >= 26)
+ *buffer++ = UEDATX;
+ #endif
+ #if (RAWHID_RX_SIZE >= 25)
+ *buffer++ = UEDATX;
+ #endif
+ #if (RAWHID_RX_SIZE >= 24)
+ *buffer++ = UEDATX;
+ #endif
+ #if (RAWHID_RX_SIZE >= 23)
+ *buffer++ = UEDATX;
+ #endif
+ #if (RAWHID_RX_SIZE >= 22)
+ *buffer++ = UEDATX;
+ #endif
+ #if (RAWHID_RX_SIZE >= 21)
+ *buffer++ = UEDATX;
+ #endif
+ #if (RAWHID_RX_SIZE >= 20)
+ *buffer++ = UEDATX;
+ #endif
+ #if (RAWHID_RX_SIZE >= 19)
+ *buffer++ = UEDATX;
+ #endif
+ #if (RAWHID_RX_SIZE >= 18)
+ *buffer++ = UEDATX;
+ #endif
+ #if (RAWHID_RX_SIZE >= 17)
+ *buffer++ = UEDATX;
+ #endif
+ #if (RAWHID_RX_SIZE >= 16)
+ *buffer++ = UEDATX;
+ #endif
+ #if (RAWHID_RX_SIZE >= 15)
+ *buffer++ = UEDATX;
+ #endif
+ #if (RAWHID_RX_SIZE >= 14)
+ *buffer++ = UEDATX;
+ #endif
+ #if (RAWHID_RX_SIZE >= 13)
+ *buffer++ = UEDATX;
+ #endif
+ #if (RAWHID_RX_SIZE >= 12)
+ *buffer++ = UEDATX;
+ #endif
+ #if (RAWHID_RX_SIZE >= 11)
+ *buffer++ = UEDATX;
+ #endif
+ #if (RAWHID_RX_SIZE >= 10)
+ *buffer++ = UEDATX;
+ #endif
+ #if (RAWHID_RX_SIZE >= 9)
+ *buffer++ = UEDATX;
+ #endif
+ #if (RAWHID_RX_SIZE >= 8)
+ *buffer++ = UEDATX;
+ #endif
+ #if (RAWHID_RX_SIZE >= 7)
+ *buffer++ = UEDATX;
+ #endif
+ #if (RAWHID_RX_SIZE >= 6)
+ *buffer++ = UEDATX;
+ #endif
+ #if (RAWHID_RX_SIZE >= 5)
+ *buffer++ = UEDATX;
+ #endif
+ #if (RAWHID_RX_SIZE >= 4)
+ *buffer++ = UEDATX;
+ #endif
+ #if (RAWHID_RX_SIZE >= 3)
+ *buffer++ = UEDATX;
+ #endif
+ #if (RAWHID_RX_SIZE >= 2)
+ *buffer++ = UEDATX;
+ #endif
+ #if (RAWHID_RX_SIZE >= 1)
+ *buffer++ = UEDATX;
+ #endif
+ // release the buffer
+ UEINTX = 0x6B;
+ SREG = intr_state;
+ return RAWHID_RX_SIZE;
+}
+
+// send a packet, with timeout
+int8_t usb_rawhid_send(const uint8_t *buffer, uint8_t timeout)
+{
+ uint8_t intr_state;
+
+ // if we're not online (enumerated and configured), error
+ if (!usb_configuration) return -1;
+ intr_state = SREG;
+ cli();
+ tx_timeout_count = timeout;
+ UENUM = RAWHID_TX_ENDPOINT;
+ // wait for the FIFO to be ready to accept data
+ while (1) {
+ if (UEINTX & (1<<RWAL)) break;
+ SREG = intr_state;
+ if (tx_timeout_count == 0) return 0;
+ if (!usb_configuration) return -1;
+ intr_state = SREG;
+ cli();
+ UENUM = RAWHID_TX_ENDPOINT;
+ }
+ // write bytes from the FIFO
+ #if (RAWHID_TX_SIZE >= 64)
+ UEDATX = *buffer++;
+ #endif
+ #if (RAWHID_TX_SIZE >= 63)
+ UEDATX = *buffer++;
+ #endif
+ #if (RAWHID_TX_SIZE >= 62)
+ UEDATX = *buffer++;
+ #endif
+ #if (RAWHID_TX_SIZE >= 61)
+ UEDATX = *buffer++;
+ #endif
+ #if (RAWHID_TX_SIZE >= 60)
+ UEDATX = *buffer++;
+ #endif
+ #if (RAWHID_TX_SIZE >= 59)
+ UEDATX = *buffer++;
+ #endif
+ #if (RAWHID_TX_SIZE >= 58)
+ UEDATX = *buffer++;
+ #endif
+ #if (RAWHID_TX_SIZE >= 57)
+ UEDATX = *buffer++;
+ #endif
+ #if (RAWHID_TX_SIZE >= 56)
+ UEDATX = *buffer++;
+ #endif
+ #if (RAWHID_TX_SIZE >= 55)
+ UEDATX = *buffer++;
+ #endif
+ #if (RAWHID_TX_SIZE >= 54)
+ UEDATX = *buffer++;
+ #endif
+ #if (RAWHID_TX_SIZE >= 53)
+ UEDATX = *buffer++;
+ #endif
+ #if (RAWHID_TX_SIZE >= 52)
+ UEDATX = *buffer++;
+ #endif
+ #if (RAWHID_TX_SIZE >= 51)
+ UEDATX = *buffer++;
+ #endif
+ #if (RAWHID_TX_SIZE >= 50)
+ UEDATX = *buffer++;
+ #endif
+ #if (RAWHID_TX_SIZE >= 49)
+ UEDATX = *buffer++;
+ #endif
+ #if (RAWHID_TX_SIZE >= 48)
+ UEDATX = *buffer++;
+ #endif
+ #if (RAWHID_TX_SIZE >= 47)
+ UEDATX = *buffer++;
+ #endif
+ #if (RAWHID_TX_SIZE >= 46)
+ UEDATX = *buffer++;
+ #endif
+ #if (RAWHID_TX_SIZE >= 45)
+ UEDATX = *buffer++;
+ #endif
+ #if (RAWHID_TX_SIZE >= 44)
+ UEDATX = *buffer++;
+ #endif
+ #if (RAWHID_TX_SIZE >= 43)
+ UEDATX = *buffer++;
+ #endif
+ #if (RAWHID_TX_SIZE >= 42)
+ UEDATX = *buffer++;
+ #endif
+ #if (RAWHID_TX_SIZE >= 41)
+ UEDATX = *buffer++;
+ #endif
+ #if (RAWHID_TX_SIZE >= 40)
+ UEDATX = *buffer++;
+ #endif
+ #if (RAWHID_TX_SIZE >= 39)
+ UEDATX = *buffer++;
+ #endif
+ #if (RAWHID_TX_SIZE >= 38)
+ UEDATX = *buffer++;
+ #endif
+ #if (RAWHID_TX_SIZE >= 37)
+ UEDATX = *buffer++;
+ #endif
+ #if (RAWHID_TX_SIZE >= 36)
+ UEDATX = *buffer++;
+ #endif
+ #if (RAWHID_TX_SIZE >= 35)
+ UEDATX = *buffer++;
+ #endif
+ #if (RAWHID_TX_SIZE >= 34)
+ UEDATX = *buffer++;
+ #endif
+ #if (RAWHID_TX_SIZE >= 33)
+ UEDATX = *buffer++;
+ #endif
+ #if (RAWHID_TX_SIZE >= 32)
+ UEDATX = *buffer++;
+ #endif
+ #if (RAWHID_TX_SIZE >= 31)
+ UEDATX = *buffer++;
+ #endif
+ #if (RAWHID_TX_SIZE >= 30)
+ UEDATX = *buffer++;
+ #endif
+ #if (RAWHID_TX_SIZE >= 29)
+ UEDATX = *buffer++;
+ #endif
+ #if (RAWHID_TX_SIZE >= 28)
+ UEDATX = *buffer++;
+ #endif
+ #if (RAWHID_TX_SIZE >= 27)
+ UEDATX = *buffer++;
+ #endif
+ #if (RAWHID_TX_SIZE >= 26)
+ UEDATX = *buffer++;
+ #endif
+ #if (RAWHID_TX_SIZE >= 25)
+ UEDATX = *buffer++;
+ #endif
+ #if (RAWHID_TX_SIZE >= 24)
+ UEDATX = *buffer++;
+ #endif
+ #if (RAWHID_TX_SIZE >= 23)
+ UEDATX = *buffer++;
+ #endif
+ #if (RAWHID_TX_SIZE >= 22)
+ UEDATX = *buffer++;
+ #endif
+ #if (RAWHID_TX_SIZE >= 21)
+ UEDATX = *buffer++;
+ #endif
+ #if (RAWHID_TX_SIZE >= 20)
+ UEDATX = *buffer++;
+ #endif
+ #if (RAWHID_TX_SIZE >= 19)
+ UEDATX = *buffer++;
+ #endif
+ #if (RAWHID_TX_SIZE >= 18)
+ UEDATX = *buffer++;
+ #endif
+ #if (RAWHID_TX_SIZE >= 17)
+ UEDATX = *buffer++;
+ #endif
+ #if (RAWHID_TX_SIZE >= 16)
+ UEDATX = *buffer++;
+ #endif
+ #if (RAWHID_TX_SIZE >= 15)
+ UEDATX = *buffer++;
+ #endif
+ #if (RAWHID_TX_SIZE >= 14)
+ UEDATX = *buffer++;
+ #endif
+ #if (RAWHID_TX_SIZE >= 13)
+ UEDATX = *buffer++;
+ #endif
+ #if (RAWHID_TX_SIZE >= 12)
+ UEDATX = *buffer++;
+ #endif
+ #if (RAWHID_TX_SIZE >= 11)
+ UEDATX = *buffer++;
+ #endif
+ #if (RAWHID_TX_SIZE >= 10)
+ UEDATX = *buffer++;
+ #endif
+ #if (RAWHID_TX_SIZE >= 9)
+ UEDATX = *buffer++;
+ #endif
+ #if (RAWHID_TX_SIZE >= 8)
+ UEDATX = *buffer++;
+ #endif
+ #if (RAWHID_TX_SIZE >= 7)
+ UEDATX = *buffer++;
+ #endif
+ #if (RAWHID_TX_SIZE >= 6)
+ UEDATX = *buffer++;
+ #endif
+ #if (RAWHID_TX_SIZE >= 5)
+ UEDATX = *buffer++;
+ #endif
+ #if (RAWHID_TX_SIZE >= 4)
+ UEDATX = *buffer++;
+ #endif
+ #if (RAWHID_TX_SIZE >= 3)
+ UEDATX = *buffer++;
+ #endif
+ #if (RAWHID_TX_SIZE >= 2)
+ UEDATX = *buffer++;
+ #endif
+ #if (RAWHID_TX_SIZE >= 1)
+ UEDATX = *buffer++;
+ #endif
+ // transmit it now
+ UEINTX = 0x3A;
+ SREG = intr_state;
+ return RAWHID_TX_SIZE;
+}
+
+
+
+
+/**************************************************************************
+ *
+ * Private Functions - not intended for general user consumption....
+ *
+ **************************************************************************/
+
+
+#if (GCC_VERSION >= 40300) && (GCC_VERSION < 40302)
+#error "Buggy GCC 4.3.0 compiler, please upgrade!"
+#endif
+
+
+// USB Device Interrupt - handle all device-level events
+// the transmit buffer flushing is triggered by the start of frame
+//
+ISR(USB_GEN_vect)
+{
+ uint8_t intbits, t;
+
+ intbits = UDINT;
+ UDINT = 0;
+ if (intbits & (1<<EORSTI)) {
+ UENUM = 0;
+ UECONX = 1;
+ UECFG0X = EP_TYPE_CONTROL;
+ UECFG1X = EP_SIZE(ENDPOINT0_SIZE) | EP_SINGLE_BUFFER;
+ UEIENX = (1<<RXSTPE);
+ usb_configuration = 0;
+ }
+ if ((intbits & (1<<SOFI)) && usb_configuration) {
+ t = rx_timeout_count;
+ if (t) rx_timeout_count = --t;
+ t = tx_timeout_count;
+ if (t) tx_timeout_count = --t;
+ }
+}
+
+
+
+// Misc functions to wait for ready and send/receive packets
+static inline void usb_wait_in_ready(void)
+{
+ while (!(UEINTX & (1<<TXINI))) ;
+}
+static inline void usb_send_in(void)
+{
+ UEINTX = ~(1<<TXINI);
+}
+static inline void usb_wait_receive_out(void)
+{
+ while (!(UEINTX & (1<<RXOUTI))) ;
+}
+static inline void usb_ack_out(void)
+{
+ UEINTX = ~(1<<RXOUTI);
+}
+
+
+
+// USB Endpoint Interrupt - endpoint 0 is handled here. The
+// other endpoints are manipulated by the user-callable
+// functions, and the start-of-frame interrupt.
+//
+ISR(USB_COM_vect)
+{
+ uint8_t intbits;
+ const uint8_t *list;
+ const uint8_t *cfg;
+ uint8_t i, n, len, en;
+ uint8_t bmRequestType;
+ uint8_t bRequest;
+ uint16_t wValue;
+ uint16_t wIndex;
+ uint16_t wLength;
+ uint16_t desc_val;
+ const uint8_t *desc_addr;
+ uint8_t desc_length;
+
+ UENUM = 0;
+ intbits = UEINTX;
+ if (intbits & (1<<RXSTPI)) {
+ bmRequestType = UEDATX;
+ bRequest = UEDATX;
+ wValue = UEDATX;
+ wValue |= (UEDATX << 8);
+ wIndex = UEDATX;
+ wIndex |= (UEDATX << 8);
+ wLength = UEDATX;
+ wLength |= (UEDATX << 8);
+ UEINTX = ~((1<<RXSTPI) | (1<<RXOUTI) | (1<<TXINI));
+ if (bRequest == GET_DESCRIPTOR) {
+ list = (const uint8_t *)descriptor_list;
+ for (i=0; ; i++) {
+ if (i >= NUM_DESC_LIST) {
+ UECONX = (1<<STALLRQ)|(1<<EPEN); //stall
+ return;
+ }
+ desc_val = pgm_read_word(list);
+ if (desc_val != wValue) {
+ list += sizeof(struct descriptor_list_struct);
+ continue;
+ }
+ list += 2;
+ desc_val = pgm_read_word(list);
+ if (desc_val != wIndex) {
+ list += sizeof(struct descriptor_list_struct)-2;
+ continue;
+ }
+ list += 2;
+ desc_addr = (const uint8_t *)pgm_read_word(list);
+ list += 2;
+ desc_length = pgm_read_byte(list);
+ break;
+ }
+ len = (wLength < 256) ? wLength : 255;
+ if (len > desc_length) len = desc_length;
+ do {
+ // wait for host ready for IN packet
+ do {
+ i = UEINTX;
+ } while (!(i & ((1<<TXINI)|(1<<RXOUTI))));
+ if (i & (1<<RXOUTI)) return; // abort
+ // send IN packet
+ n = len < ENDPOINT0_SIZE ? len : ENDPOINT0_SIZE;
+ for (i = n; i; i--) {
+ UEDATX = pgm_read_byte(desc_addr++);
+ }
+ len -= n;
+ usb_send_in();
+ } while (len || n == ENDPOINT0_SIZE);
+ return;
+ }
+ if (bRequest == SET_ADDRESS) {
+ usb_send_in();
+ usb_wait_in_ready();
+ UDADDR = wValue | (1<<ADDEN);
+ return;
+ }
+ if (bRequest == SET_CONFIGURATION && bmRequestType == 0) {
+ usb_configuration = wValue;
+ usb_send_in();
+ cfg = endpoint_config_table;
+ for (i=1; i<5; i++) {
+ UENUM = i;
+ en = pgm_read_byte(cfg++);
+ UECONX = en;
+ if (en) {
+ UECFG0X = pgm_read_byte(cfg++);
+ UECFG1X = pgm_read_byte(cfg++);
+ }
+ }
+ UERST = 0x1E;
+ UERST = 0;
+ return;
+ }
+ if (bRequest == GET_CONFIGURATION && bmRequestType == 0x80) {
+ usb_wait_in_ready();
+ UEDATX = usb_configuration;
+ usb_send_in();
+ return;
+ }
+
+ if (bRequest == GET_STATUS) {
+ usb_wait_in_ready();
+ i = 0;
+ if (bmRequestType == 0x82) {
+ UENUM = wIndex;
+ if (UECONX & (1<<STALLRQ)) i = 1;
+ UENUM = 0;
+ }
+ UEDATX = i;
+ UEDATX = 0;
+ usb_send_in();
+ return;
+ }
+ if ((bRequest == CLEAR_FEATURE || bRequest == SET_FEATURE)
+ && bmRequestType == 0x02 && wValue == 0) {
+ i = wIndex & 0x7F;
+ if (i >= 1 && i <= MAX_ENDPOINT) {
+ usb_send_in();
+ UENUM = i;
+ if (bRequest == SET_FEATURE) {
+ UECONX = (1<<STALLRQ)|(1<<EPEN);
+ } else {
+ UECONX = (1<<STALLRQC)|(1<<RSTDT)|(1<<EPEN);
+ UERST = (1 << i);
+ UERST = 0;
+ }
+ return;
+ }
+ }
+ if (wIndex == RAWHID_INTERFACE) {
+ if (bmRequestType == 0xA1 && bRequest == HID_GET_REPORT) {
+ len = RAWHID_TX_SIZE;
+ do {
+ // wait for host ready for IN packet
+ do {
+ i = UEINTX;
+ } while (!(i & ((1<<TXINI)|(1<<RXOUTI))));
+ if (i & (1<<RXOUTI)) return; // abort
+ // send IN packet
+ n = len < ENDPOINT0_SIZE ? len : ENDPOINT0_SIZE;
+ for (i = n; i; i--) {
+ // just send zeros
+ UEDATX = 0;
+ }
+ len -= n;
+ usb_send_in();
+ } while (len || n == ENDPOINT0_SIZE);
+ return;
+ }
+ if (bmRequestType == 0x21 && bRequest == HID_SET_REPORT) {
+ len = RAWHID_RX_SIZE;
+ do {
+ n = len < ENDPOINT0_SIZE ? len : ENDPOINT0_SIZE;
+ usb_wait_receive_out();
+ // ignore incoming bytes
+ usb_ack_out();
+ len -= n;
+ } while (len);
+ usb_wait_in_ready();
+ usb_send_in();
+ return;
+ }
+ }
+ }
+ UECONX = (1<<STALLRQ) | (1<<EPEN); // stall
+}
+
+
diff --git a/software/hhd70dongle/usb_rawhid.h b/software/hhd70dongle/usb_rawhid.h
new file mode 100644
index 0000000..65bc4de
--- /dev/null
+++ b/software/hhd70dongle/usb_rawhid.h
@@ -0,0 +1,85 @@
+#ifndef usb_serial_h__
+#define usb_serial_h__
+
+#include <stdint.h>
+
+void usb_init(void); // initialize everything
+uint8_t usb_configured(void); // is the USB port configured
+int8_t usb_rawhid_recv(uint8_t *buffer, uint8_t timeout); // receive a packet, with timeout
+int8_t usb_rawhid_send(const uint8_t *buffer, uint8_t timeout); // send a packet, with timeout
+
+// This file does not include the HID debug functions, so these empty
+// macros replace them with nothing, so users can compile code that
+// has calls to these functions.
+#define usb_debug_putchar(c)
+#define usb_debug_flush_output()
+
+
+// Everything below this point is only intended for usb_serial.c
+#ifdef USB_PRIVATE_INCLUDE
+#include <avr/io.h>
+#include <avr/pgmspace.h>
+#include <avr/interrupt.h>
+
+#define EP_TYPE_CONTROL 0x00
+#define EP_TYPE_BULK_IN 0x81
+#define EP_TYPE_BULK_OUT 0x80
+#define EP_TYPE_INTERRUPT_IN 0xC1
+#define EP_TYPE_INTERRUPT_OUT 0xC0
+#define EP_TYPE_ISOCHRONOUS_IN 0x41
+#define EP_TYPE_ISOCHRONOUS_OUT 0x40
+
+#define EP_SINGLE_BUFFER 0x02
+#define EP_DOUBLE_BUFFER 0x06
+
+#define EP_SIZE(s) ((s) > 32 ? 0x30 : \
+ ((s) > 16 ? 0x20 : \
+ ((s) > 8 ? 0x10 : \
+ 0x00)))
+
+#define MAX_ENDPOINT 4
+
+#define LSB(n) (n & 255)
+#define MSB(n) ((n >> 8) & 255)
+
+#if defined(__AVR_AT90USB162__)
+#define HW_CONFIG()
+#define PLL_CONFIG() (PLLCSR = ((1<<PLLE)|(1<<PLLP0)))
+#define USB_CONFIG() (USBCON = (1<<USBE))
+#define USB_FREEZE() (USBCON = ((1<<USBE)|(1<<FRZCLK)))
+#elif defined(__AVR_ATmega32U4__)
+#define HW_CONFIG() (UHWCON = 0x01)
+#define PLL_CONFIG() (PLLCSR = 0x12)
+#define USB_CONFIG() (USBCON = ((1<<USBE)|(1<<OTGPADE)))
+#define USB_FREEZE() (USBCON = ((1<<USBE)|(1<<FRZCLK)))
+#elif defined(__AVR_AT90USB646__)
+#define HW_CONFIG() (UHWCON = 0x81)
+#define PLL_CONFIG() (PLLCSR = 0x1A)
+#define USB_CONFIG() (USBCON = ((1<<USBE)|(1<<OTGPADE)))
+#define USB_FREEZE() (USBCON = ((1<<USBE)|(1<<FRZCLK)))
+#elif defined(__AVR_AT90USB1286__)
+#define HW_CONFIG() (UHWCON = 0x81)
+#define PLL_CONFIG() (PLLCSR = 0x16)
+#define USB_CONFIG() (USBCON = ((1<<USBE)|(1<<OTGPADE)))
+#define USB_FREEZE() (USBCON = ((1<<USBE)|(1<<FRZCLK)))
+#endif
+
+// standard control endpoint request types
+#define GET_STATUS 0
+#define CLEAR_FEATURE 1
+#define SET_FEATURE 3
+#define SET_ADDRESS 5
+#define GET_DESCRIPTOR 6
+#define GET_CONFIGURATION 8
+#define SET_CONFIGURATION 9
+#define GET_INTERFACE 10
+#define SET_INTERFACE 11
+// HID (human interface device)
+#define HID_GET_REPORT 1
+#define HID_GET_IDLE 2
+#define HID_GET_PROTOCOL 3
+#define HID_SET_REPORT 9
+#define HID_SET_IDLE 10
+#define HID_SET_PROTOCOL 11
+#endif
+#endif