board corrections + atmega firmware

This commit is contained in:
Nephirus 2012-09-18 22:27:49 +02:00
parent 9e599e4383
commit 8f0ee9923c
38 changed files with 9364 additions and 3 deletions

View file

@ -0,0 +1,42 @@
# Name: Makefile
# Project: hid-data example
# Author: Christian Starkjohann
# Creation Date: 2008-04-11
# Tabsize: 4
# Copyright: (c) 2008 by OBJECTIVE DEVELOPMENT Software GmbH
# License: GNU GPL v2 (see License.txt), GNU GPL v3 or proprietary (CommercialLicense.txt)
# This Revision: $Id$
# Please read the definitions below and edit them as appropriate for your
# system:
# Use the following 3 lines on Unix and Mac OS X:
USBFLAGS= `libusb-config --cflags`
USBLIBS= `libusb-config --libs`
EXE_SUFFIX=
# Use the following 3 lines on Windows and comment out the 3 above:
#USBFLAGS=
#USBLIBS= -lhid -lusb -lsetupapi
#EXE_SUFFIX= .exe
CC= gcc
CFLAGS= -O -Wall $(USBFLAGS)
LIBS= $(USBLIBS)
OBJ= hidtool.o hiddata.o
PROGRAM= hidtool$(EXE_SUFFIX)
all: $(PROGRAM)
$(PROGRAM): $(OBJ)
$(CC) -o $(PROGRAM) $(OBJ) $(LIBS)
strip: $(PROGRAM)
strip $(PROGRAM)
clean:
rm -f $(OBJ) $(PROGRAM)
.c.o:
$(CC) $(ARCH_COMPILE) $(CFLAGS) -c $*.c -o $*.o

View file

@ -0,0 +1,18 @@
# Name: Makefile.windows
# Project: hid-data example
# Author: Christian Starkjohann
# Creation Date: 2008-04-11
# Tabsize: 4
# Copyright: (c) 2008 by OBJECTIVE DEVELOPMENT Software GmbH
# License: GNU GPL v2 (see License.txt), GNU GPL v3 or proprietary (CommercialLicense.txt)
# This Revision: $Id$
# You may use this file with
# make -f Makefile.windows
# on Windows with MinGW instead of editing the main Makefile.
include Makefile
USBFLAGS=
USBLIBS= -lhid -lsetupapi
EXE_SUFFIX= .exe

View file

@ -0,0 +1,324 @@
/* Name: hiddata.c
* Author: Christian Starkjohann
* Creation Date: 2008-04-11
* Tabsize: 4
* Copyright: (c) 2008 by OBJECTIVE DEVELOPMENT Software GmbH
* License: GNU GPL v2 (see License.txt), GNU GPL v3 or proprietary (CommercialLicense.txt)
* This Revision: $Id$
*/
#include <stdio.h>
#include "hiddata.h"
/* ######################################################################## */
#if defined(WIN32) /* ##################################################### */
/* ######################################################################## */
#include <windows.h>
#include <setupapi.h>
#include "hidsdi.h"
#include <ddk/hidpi.h>
#ifdef DEBUG
#define DEBUG_PRINT(arg) printf arg
#else
#define DEBUG_PRINT(arg)
#endif
/* ------------------------------------------------------------------------ */
static void convertUniToAscii(char *buffer)
{
unsigned short *uni = (void *)buffer;
char *ascii = buffer;
while(*uni != 0){
if(*uni >= 256){
*ascii++ = '?';
}else{
*ascii++ = *uni++;
}
}
*ascii++ = 0;
}
int usbhidOpenDevice(usbDevice_t **device, int vendor, char *vendorName, int product, char *productName, int usesReportIDs)
{
GUID hidGuid; /* GUID for HID driver */
HDEVINFO deviceInfoList;
SP_DEVICE_INTERFACE_DATA deviceInfo;
SP_DEVICE_INTERFACE_DETAIL_DATA *deviceDetails = NULL;
DWORD size;
int i, openFlag = 0; /* may be FILE_FLAG_OVERLAPPED */
int errorCode = USBOPEN_ERR_NOTFOUND;
HANDLE handle = INVALID_HANDLE_VALUE;
HIDD_ATTRIBUTES deviceAttributes;
HidD_GetHidGuid(&hidGuid);
deviceInfoList = SetupDiGetClassDevs(&hidGuid, NULL, NULL, DIGCF_PRESENT | DIGCF_INTERFACEDEVICE);
deviceInfo.cbSize = sizeof(deviceInfo);
for(i=0;;i++){
if(handle != INVALID_HANDLE_VALUE){
CloseHandle(handle);
handle = INVALID_HANDLE_VALUE;
}
if(!SetupDiEnumDeviceInterfaces(deviceInfoList, 0, &hidGuid, i, &deviceInfo))
break; /* no more entries */
/* first do a dummy call just to determine the actual size required */
SetupDiGetDeviceInterfaceDetail(deviceInfoList, &deviceInfo, NULL, 0, &size, NULL);
if(deviceDetails != NULL)
free(deviceDetails);
deviceDetails = malloc(size);
deviceDetails->cbSize = sizeof(*deviceDetails);
/* this call is for real: */
SetupDiGetDeviceInterfaceDetail(deviceInfoList, &deviceInfo, deviceDetails, size, &size, NULL);
DEBUG_PRINT(("checking HID path \"%s\"\n", deviceDetails->DevicePath));
#if 0
/* If we want to access a mouse our keyboard, we can only use feature
* requests as the device is locked by Windows. It must be opened
* with ACCESS_TYPE_NONE.
*/
handle = CreateFile(deviceDetails->DevicePath, ACCESS_TYPE_NONE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, openFlag, NULL);
#endif
/* attempt opening for R/W -- we don't care about devices which can't be accessed */
handle = CreateFile(deviceDetails->DevicePath, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, openFlag, NULL);
if(handle == INVALID_HANDLE_VALUE){
DEBUG_PRINT(("opening failed: %d\n", (int)GetLastError()));
/* errorCode = USBOPEN_ERR_ACCESS; opening will always fail for mouse -- ignore */
continue;
}
deviceAttributes.Size = sizeof(deviceAttributes);
HidD_GetAttributes(handle, &deviceAttributes);
DEBUG_PRINT(("device attributes: vid=%d pid=%d\n", deviceAttributes.VendorID, deviceAttributes.ProductID));
if(deviceAttributes.VendorID != vendor || deviceAttributes.ProductID != product)
continue; /* ignore this device */
errorCode = USBOPEN_ERR_NOTFOUND;
if(vendorName != NULL && productName != NULL){
char buffer[512];
if(!HidD_GetManufacturerString(handle, buffer, sizeof(buffer))){
DEBUG_PRINT(("error obtaining vendor name\n"));
errorCode = USBOPEN_ERR_IO;
continue;
}
convertUniToAscii(buffer);
DEBUG_PRINT(("vendorName = \"%s\"\n", buffer));
if(strcmp(vendorName, buffer) != 0)
continue;
if(!HidD_GetProductString(handle, buffer, sizeof(buffer))){
DEBUG_PRINT(("error obtaining product name\n"));
errorCode = USBOPEN_ERR_IO;
continue;
}
convertUniToAscii(buffer);
DEBUG_PRINT(("productName = \"%s\"\n", buffer));
if(strcmp(productName, buffer) != 0)
continue;
}
break; /* we have found the device we are looking for! */
}
SetupDiDestroyDeviceInfoList(deviceInfoList);
if(deviceDetails != NULL)
free(deviceDetails);
if(handle != INVALID_HANDLE_VALUE){
*device = (usbDevice_t *)handle;
errorCode = 0;
}
return errorCode;
}
/* ------------------------------------------------------------------------ */
void usbhidCloseDevice(usbDevice_t *device)
{
CloseHandle((HANDLE)device);
}
/* ------------------------------------------------------------------------ */
int usbhidSetReport(usbDevice_t *device, char *buffer, int len)
{
BOOLEAN rval;
rval = HidD_SetFeature((HANDLE)device, buffer, len);
return rval == 0 ? USBOPEN_ERR_IO : 0;
}
/* ------------------------------------------------------------------------ */
int usbhidGetReport(usbDevice_t *device, int reportNumber, char *buffer, int *len)
{
BOOLEAN rval = 0;
buffer[0] = reportNumber;
rval = HidD_GetFeature((HANDLE)device, buffer, *len);
return rval == 0 ? USBOPEN_ERR_IO : 0;
}
/* ------------------------------------------------------------------------ */
/* ######################################################################## */
#else /* defined WIN32 #################################################### */
/* ######################################################################## */
#include <string.h>
#include <usb.h>
#define usbDevice usb_dev_handle /* use libusb's device structure */
/* ------------------------------------------------------------------------- */
#define USBRQ_HID_GET_REPORT 0x01
#define USBRQ_HID_SET_REPORT 0x09
#define USB_HID_REPORT_TYPE_FEATURE 3
static int usesReportIDs;
/* ------------------------------------------------------------------------- */
static int usbhidGetStringAscii(usb_dev_handle *dev, int index, char *buf, int buflen)
{
char buffer[256];
int rval, i;
if((rval = usb_get_string_simple(dev, index, buf, buflen)) >= 0) /* use libusb version if it works */
return rval;
if((rval = usb_control_msg(dev, USB_ENDPOINT_IN, USB_REQ_GET_DESCRIPTOR, (USB_DT_STRING << 8) + index, 0x0409, buffer, sizeof(buffer), 5000)) < 0)
return rval;
if(buffer[1] != USB_DT_STRING){
*buf = 0;
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;
}
int usbhidOpenDevice(usbDevice_t **device, int vendor, char *vendorName, int product, char *productName, int _usesReportIDs)
{
struct usb_bus *bus;
struct usb_device *dev;
usb_dev_handle *handle = NULL;
int errorCode = USBOPEN_ERR_NOTFOUND;
static int didUsbInit = 0;
if(!didUsbInit){
usb_init();
didUsbInit = 1;
}
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 = USBOPEN_ERR_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 = usbhidGetStringAscii(handle, dev->descriptor.iManufacturer, string, sizeof(string));
if(len < 0){
errorCode = USBOPEN_ERR_IO;
fprintf(stderr, "Warning: cannot query manufacturer for device: %s\n", usb_strerror());
}else{
errorCode = USBOPEN_ERR_NOTFOUND;
/* fprintf(stderr, "seen device from vendor ->%s<-\n", string); */
if(strcmp(string, vendorName) == 0){
len = usbhidGetStringAscii(handle, dev->descriptor.iProduct, string, sizeof(string));
if(len < 0){
errorCode = USBOPEN_ERR_IO;
fprintf(stderr, "Warning: cannot query product for device: %s\n", usb_strerror());
}else{
errorCode = USBOPEN_ERR_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 = (void *)handle;
usesReportIDs = _usesReportIDs;
}
return errorCode;
}
/* ------------------------------------------------------------------------- */
void usbhidCloseDevice(usbDevice_t *device)
{
if(device != NULL)
usb_close((void *)device);
}
/* ------------------------------------------------------------------------- */
int usbhidSetReport(usbDevice_t *device, char *buffer, int len)
{
int bytesSent, reportId = buffer[0];
if(!usesReportIDs){
buffer++; /* skip dummy report ID */
len--;
}
bytesSent = usb_control_msg((void *)device, USB_TYPE_CLASS | USB_RECIP_DEVICE | USB_ENDPOINT_OUT, USBRQ_HID_SET_REPORT, USB_HID_REPORT_TYPE_FEATURE << 8 | (reportId & 0xff), 0, buffer, len, 5000);
if(bytesSent != len){
if(bytesSent < 0)
fprintf(stderr, "Error sending message: %s\n", usb_strerror());
return USBOPEN_ERR_IO;
}
return 0;
}
/* ------------------------------------------------------------------------- */
int usbhidGetReport(usbDevice_t *device, int reportNumber, char *buffer, int *len)
{
int bytesReceived, maxLen = *len;
if(!usesReportIDs){
buffer++; /* make room for dummy report ID */
maxLen--;
}
bytesReceived = usb_control_msg((void *)device, USB_TYPE_CLASS | USB_RECIP_DEVICE | USB_ENDPOINT_IN, USBRQ_HID_GET_REPORT, USB_HID_REPORT_TYPE_FEATURE << 8 | reportNumber, 0, buffer, maxLen, 5000);
if(bytesReceived < 0){
fprintf(stderr, "Error sending message: %s\n", usb_strerror());
return USBOPEN_ERR_IO;
}
*len = bytesReceived;
if(!usesReportIDs){
buffer[-1] = reportNumber; /* add dummy report ID */
(*len)++;
}
return 0;
}
/* ######################################################################## */
#endif /* defined WIN32 ################################################### */
/* ######################################################################## */

View file

@ -0,0 +1,71 @@
/* Name: hiddata.h
* Author: Christian Starkjohann
* Creation Date: 2008-04-11
* Tabsize: 4
* Copyright: (c) 2008 by OBJECTIVE DEVELOPMENT Software GmbH
* License: GNU GPL v2 (see License.txt), GNU GPL v3 or proprietary (CommercialLicense.txt)
* This Revision: $Id$
*/
#ifndef __HIDDATA_H_INCLUDED__
#define __HIDDATA_H_INCLUDED__
/*
General Description:
This module implements an abstraction layer for data transfer over HID feature
requests. The implementation uses native Windows functions on Windows so that
no driver installation is required and libusb on Unix. You must link the
appropriate libraries in either case: "-lhid -lusb -lsetupapi" on Windows and
`libusb-config --libs` on Unix.
*/
/* ------------------------------------------------------------------------ */
#define USBOPEN_SUCCESS 0 /* no error */
#define USBOPEN_ERR_ACCESS 1 /* not enough permissions to open device */
#define USBOPEN_ERR_IO 2 /* I/O error */
#define USBOPEN_ERR_NOTFOUND 3 /* device not found */
/* ------------------------------------------------------------------------ */
typedef struct usbDevice usbDevice_t;
/* Opaque data type representing the USB device. This can be a Windows handle
* or a libusb handle, depending on the backend implementation.
*/
/* ------------------------------------------------------------------------ */
int usbhidOpenDevice(usbDevice_t **device, int vendorID, char *vendorName, int productID, char *productName, int usesReportIDs);
/* This function opens a USB device. 'vendorID' and 'productID' are the numeric
* Vendor-ID and Product-ID of the device we want to open. If 'vendorName' and
* 'productName' are both not NULL, only devices with matching manufacturer-
* and product name strings are accepted. If the device uses report IDs,
* 'usesReportIDs' must be set to a non-zero value.
* Returns: If a matching device has been found, USBOPEN_SUCCESS is returned
* and '*device' is set to an opaque pointer representing the device. The
* device must be closed with usbhidCloseDevice(). If the device has not been
* found or opening failed, an error code is returned.
*/
void usbhidCloseDevice(usbDevice_t *device);
/* Every device opened with usbhidOpenDevice() must be closed with this function.
*/
int usbhidSetReport(usbDevice_t *device, char *buffer, int len);
/* This function sends a feature report to the device. The report ID must be
* in the first byte of buffer and the length 'len' of the report is specified
* including this report ID. If no report IDs are used, buffer[0] must be set
* to 0 (dummy report ID).
* Returns: 0 on success, an error code otherwise.
*/
int usbhidGetReport(usbDevice_t *device, int reportID, char *buffer, int *len);
/* This function obtains a feature report from the device. The requested
* report-ID is passed in 'reportID'. The caller must pass a buffer of the size
* of the expected report in 'buffer' and initialize the variable pointed to by
* 'len' to the total size of this buffer. Upon successful return, the report
* (prefixed with the report-ID) is in 'buffer' and the actual length of the
* report is returned in '*len'.
* Returns: 0 on success, an error code otherwise.
*/
/* ------------------------------------------------------------------------ */
#endif /* __HIDDATA_H_INCLUDED__ */

Binary file not shown.

View file

@ -0,0 +1,49 @@
/* Name: hidsdi.h
* Author: Christian Starkjohann
* Creation Date: 2006-02-02
* Tabsize: 4
* Copyright: (c) 2006-2008 by OBJECTIVE DEVELOPMENT Software GmbH
* License: GNU GPL v2 (see License.txt), GNU GPL v3 or proprietary (CommercialLicense.txt)
* This Revision: $Id$
*/
/*
General Description
This file is a replacement for hidsdi.h from the Windows DDK. It defines some
of the types and function prototypes of this header for our project. If you
have the Windows DDK version of this file or a version shipped with MinGW, use
that instead.
*/
#ifndef _HIDSDI_H
#define _HIDSDI_H
#include <pshpack4.h>
#include <ddk/hidusage.h>
#include <ddk/hidpi.h>
typedef struct{
ULONG Size;
USHORT VendorID;
USHORT ProductID;
USHORT VersionNumber;
}HIDD_ATTRIBUTES;
void __stdcall HidD_GetHidGuid(OUT LPGUID hidGuid);
BOOLEAN __stdcall HidD_GetAttributes(IN HANDLE device, OUT HIDD_ATTRIBUTES *attributes);
BOOLEAN __stdcall HidD_GetManufacturerString(IN HANDLE device, OUT void *buffer, IN ULONG bufferLen);
BOOLEAN __stdcall HidD_GetProductString(IN HANDLE device, OUT void *buffer, IN ULONG bufferLen);
BOOLEAN __stdcall HidD_GetSerialNumberString(IN HANDLE device, OUT void *buffer, IN ULONG bufferLen);
BOOLEAN __stdcall HidD_GetFeature(IN HANDLE device, OUT void *reportBuffer, IN ULONG bufferLen);
BOOLEAN __stdcall HidD_SetFeature(IN HANDLE device, IN void *reportBuffer, IN ULONG bufferLen);
BOOLEAN __stdcall HidD_GetNumInputBuffers(IN HANDLE device, OUT ULONG *numBuffers);
BOOLEAN __stdcall HidD_SetNumInputBuffers(IN HANDLE device, OUT ULONG numBuffers);
#include <poppack.h>
#endif

BIN
firmware/commandline/hidtool Executable file

Binary file not shown.

View file

@ -0,0 +1,127 @@
/* Name: hidtool.c
* Project: hid-data example
* Author: Christian Starkjohann
* Creation Date: 2008-04-11
* Tabsize: 4
* Copyright: (c) 2008 by OBJECTIVE DEVELOPMENT Software GmbH
* License: GNU GPL v2 (see License.txt), GNU GPL v3 or proprietary (CommercialLicense.txt)
* This Revision: $Id$
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "hiddata.h"
#include "../firmware/usbconfig.h" /* for device VID, PID, vendor name and product name */
/* ------------------------------------------------------------------------- */
static char *usbErrorMessage(int errCode)
{
static char buffer[80];
switch(errCode){
case USBOPEN_ERR_ACCESS: return "Access to device denied";
case USBOPEN_ERR_NOTFOUND: return "The specified device was not found";
case USBOPEN_ERR_IO: return "Communication error with device";
default:
sprintf(buffer, "Unknown USB error %d", errCode);
return buffer;
}
return NULL; /* not reached */
}
static usbDevice_t *openDevice(void)
{
usbDevice_t *dev = NULL;
unsigned char rawVid[2] = {USB_CFG_VENDOR_ID}, rawPid[2] = {USB_CFG_DEVICE_ID};
char vendorName[] = {USB_CFG_VENDOR_NAME, 0}, productName[] = {USB_CFG_DEVICE_NAME, 0};
int vid = rawVid[0] + 256 * rawVid[1];
int pid = rawPid[0] + 256 * rawPid[1];
int err;
if((err = usbhidOpenDevice(&dev, vid, vendorName, pid, productName, 0)) != 0){
fprintf(stderr, "error finding %s: %s\n", productName, usbErrorMessage(err));
return NULL;
}
return dev;
}
/* ------------------------------------------------------------------------- */
static void hexdump(char *buffer, int len)
{
int i;
FILE *fp = stdout;
for(i = 0; i < len; i++){
if(i != 0){
if(i % 16 == 0){
fprintf(fp, "\n");
}else{
fprintf(fp, " ");
}
}
fprintf(fp, "0x%02x", buffer[i] & 0xff);
}
if(i != 0)
fprintf(fp, "\n");
}
static int hexread(char *buffer, char *string, int buflen)
{
char *s;
int pos = 0;
while((s = strtok(string, ", ")) != NULL && pos < buflen){
string = NULL;
buffer[pos++] = (char)strtol(s, NULL, 0);
}
return pos;
}
/* ------------------------------------------------------------------------- */
static void usage(char *myName)
{
fprintf(stderr, "usage:\n");
fprintf(stderr, " %s read\n", myName);
fprintf(stderr, " %s write <listofbytes>\n", myName);
}
int main(int argc, char **argv)
{
usbDevice_t *dev;
char buffer[4]; /* room for dummy report ID */
int err;
if(argc < 2){
usage(argv[0]);
exit(1);
}
if((dev = openDevice()) == NULL)
exit(1);
if(strcasecmp(argv[1], "read") == 0){
int len = sizeof(buffer);
if((err = usbhidGetReport(dev, 0, buffer, &len)) != 0){
fprintf(stderr, "error reading data: %s\n", usbErrorMessage(err));
}else{
hexdump(buffer + 1, sizeof(buffer) - 1);
}
}else if(strcasecmp(argv[1], "write") == 0){
int i, pos;
memset(buffer, 0, sizeof(buffer));
for(pos = 1, i = 2; i < argc && pos < sizeof(buffer); i++){
pos += hexread(buffer + pos, argv[i], sizeof(buffer) - pos);
}
if((err = usbhidSetReport(dev, buffer, sizeof(buffer))) != 0) /* add a dummy report ID */
fprintf(stderr, "error writing data: %s\n", usbErrorMessage(err));
}else{
usage(argv[0]);
exit(1);
}
usbhidCloseDevice(dev);
return 0;
}
/* ------------------------------------------------------------------------- */

Binary file not shown.