Created
December 9, 2015 11:02
-
-
Save YuuichiAkagawa/ac41557a5dcb7cfa74ba to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* GR-SAKURA Sketch Template V2.06 */ | |
#include <Arduino.h> | |
//USBDesc | |
#include <usbhub.h> | |
#define Serial Serial1 | |
#define BUFSIZE 256 //buffer size | |
#define LOBYTE(x) ((char*)(&(x)))[0] | |
#define HIBYTE(x) ((char*)(&(x)))[1] | |
const char Gen_Error_str[] = "\r\nRequest error. Error code:\t"; | |
const char Dev_Header_str[] ="\r\nDevice descriptor: "; | |
const char Dev_Length_str[] ="\r\nDescriptor Length:\t"; | |
const char Dev_Type_str[] ="\r\nDescriptor type:\t"; | |
const char Dev_Version_str[] ="\r\nUSB version:\t\t"; | |
const char Dev_Class_str[] ="\r\nDevice class:\t\t"; | |
const char Dev_Subclass_str[] ="\r\nDevice Subclass:\t"; | |
const char Dev_Protocol_str[] ="\r\nDevice Protocol:\t"; | |
const char Dev_Pktsize_str[] ="\r\nMax.packet size:\t"; | |
const char Dev_Vendor_str[] ="\r\nVendor ID:\t\t"; | |
const char Dev_Product_str[] ="\r\nProduct ID:\t\t"; | |
const char Dev_Revision_str[] ="\r\nRevision ID:\t\t"; | |
const char Dev_Mfg_str[] ="\r\nMfg.string index:\t"; | |
const char Dev_Prod_str[] ="\r\nProd.string index:\t"; | |
const char Dev_Serial_str[] ="\r\nSerial number index:\t"; | |
const char Dev_Nconf_str[] ="\r\nNumber of conf.:\t"; | |
const char Conf_Trunc_str[] ="Total length truncated to 256 bytes"; | |
const char Conf_Header_str[] ="\r\nConfiguration descriptor:"; | |
const char Conf_Totlen_str[] ="\r\nTotal length:\t\t"; | |
const char Conf_Nint_str[] ="\r\nNum.intf:\t\t"; | |
const char Conf_Value_str[] ="\r\nConf.value:\t\t"; | |
const char Conf_String_str[] ="\r\nConf.string:\t\t"; | |
const char Conf_Attr_str[] ="\r\nAttr.:\t\t\t"; | |
const char Conf_Pwr_str[] ="\r\nMax.pwr:\t\t"; | |
const char Int_Header_str[] ="\r\n\r\nInterface descriptor:"; | |
const char Int_Number_str[] ="\r\nIntf.number:\t\t"; | |
const char Int_Alt_str[] ="\r\nAlt.:\t\t\t"; | |
const char Int_Endpoints_str[] ="\r\nEndpoints:\t\t"; | |
const char Int_Class_str[] ="\r\nIntf. Class:\t\t"; | |
const char Int_Subclass_str[] ="\r\nIntf. Subclass:\t\t"; | |
const char Int_Protocol_str[] ="\r\nIntf. Protocol:\t\t"; | |
const char Int_String_str[] ="\r\nIntf.string:\t\t"; | |
const char End_Header_str[] ="\r\n\r\nEndpoint descriptor:"; | |
const char End_Address_str[] ="\r\nEndpoint address:\t"; | |
const char End_Attr_str[] ="\r\nAttr.:\t\t\t"; | |
const char End_Pktsize_str[] ="\r\nMax.pkt size:\t\t"; | |
const char End_Interval_str[] ="\r\nPolling interval:\t"; | |
const char Unk_Header_str[] = "\r\nUnknown descriptor:"; | |
const char Unk_Length_str[] ="\r\nLength:\t\t"; | |
const char Unk_Type_str[] ="\r\nType:\t\t"; | |
const char Unk_Contents_str[] ="\r\nContents:\t"; | |
void PrintAllAddresses(UsbDevice *pdev); | |
void PrintAddress(uint8_t addr); | |
void PrintDescriptors(uint8_t addr); | |
void PrintAllDescriptors(UsbDevice *pdev); | |
byte getdevdescr( byte addr, byte &num_conf ); | |
void printhubdescr(uint8_t *descrptr, uint8_t addr); | |
byte getconfdescr( byte addr, byte conf ); | |
void print_hex(int v, int num_places); | |
void printconfdescr( uint8_t* descr_ptr ); | |
void printintfdescr( uint8_t* descr_ptr ); | |
void printepdescr( uint8_t* descr_ptr ); | |
void printunkdescr( uint8_t* descr_ptr ); | |
void printhiddescr( uint8_t* descr_ptr ); | |
USB Usb; | |
//USBHub Hub1(&Usb); | |
uint32_t next_time; | |
void PrintAllAddresses(UsbDevice *pdev) | |
{ | |
UsbDeviceAddress adr; | |
adr.devAddress = pdev->address.devAddress; | |
Serial.print("\r\nAddr:"); | |
Serial.print(adr.devAddress, HEX); | |
Serial.print("("); | |
Serial.print(adr.bmHub, HEX); | |
Serial.print("."); | |
Serial.print(adr.bmParent, HEX); | |
Serial.print("."); | |
Serial.print(adr.bmAddress, HEX); | |
Serial.println(")"); | |
} | |
void PrintAddress(uint8_t addr) | |
{ | |
UsbDeviceAddress adr; | |
adr.devAddress = addr; | |
Serial.print("\r\nADDR:\t"); | |
Serial.println(adr.devAddress,HEX); | |
Serial.print("DEV:\t"); | |
Serial.println(adr.bmAddress,HEX); | |
Serial.print("PRNT:\t"); | |
Serial.println(adr.bmParent,HEX); | |
Serial.print("HUB:\t"); | |
Serial.println(adr.bmHub,HEX); | |
} | |
void setup() | |
{ | |
pinMode(PIN_LED0, OUTPUT); | |
pinMode(PIN_LED1, OUTPUT); | |
pinMode(PIN_LED2, OUTPUT); | |
pinMode(PIN_LED3, OUTPUT); | |
digitalWrite(PIN_LED3, HIGH); | |
Serial.begin( 115200 ); | |
while (!Serial); // Wait for serial port to connect - used on Leonardo, Teensy and other boards with built-in USB CDC serial connection | |
Serial.println("Start"); | |
if (Usb.Init() == -1) | |
Serial.println("OSC did not start."); | |
delay( 200 ); | |
next_time = millis() + 10000; | |
} | |
byte getdevdescr( byte addr, byte &num_conf ); | |
void PrintDescriptors(uint8_t addr) | |
{ | |
uint8_t rcode = 0; | |
byte num_conf = 0; | |
rcode = getdevdescr( (byte)addr, num_conf ); | |
if( rcode ) | |
{ | |
Serial.print(Gen_Error_str); | |
print_hex( rcode, 8 ); | |
} | |
Serial.print("\r\n"); | |
for (int i=0; i<num_conf; i++) | |
{ | |
rcode = getconfdescr( addr, i ); // get configuration descriptor | |
if( rcode ) | |
{ | |
Serial.print(Gen_Error_str); | |
print_hex(rcode, 8); | |
} | |
Serial.println("\r\n"); | |
} | |
} | |
void PrintAllDescriptors(UsbDevice *pdev) | |
{ | |
Serial.println("\r\n"); | |
print_hex(pdev->address.devAddress, 8); | |
Serial.println("\r\n--"); | |
PrintDescriptors( pdev->address.devAddress ); | |
} | |
void loop() | |
{ | |
Usb.Task(); | |
digitalWrite(PIN_LED0, HIGH); | |
delay(50); | |
digitalWrite(PIN_LED0, LOW); | |
delay(50); | |
if( Usb.getUsbTaskState() == USB_STATE_RUNNING ) | |
{ | |
//if (millis() >= next_time) | |
{ | |
Usb.ForEachUsbDevice(&PrintAllDescriptors); | |
Usb.ForEachUsbDevice(&PrintAllAddresses); | |
while( 1 ); //stop | |
} | |
} | |
} | |
byte getdevdescr( byte addr, byte &num_conf ) | |
{ | |
USB_DEVICE_DESCRIPTOR buf; | |
byte rcode; | |
rcode = Usb.getDevDescr( addr, 0, 0x12, ( uint8_t *)&buf ); | |
if( rcode ) { | |
return( rcode ); | |
} | |
Serial.print(Dev_Header_str); | |
Serial.print(Dev_Length_str); | |
print_hex( buf.bLength, 8 ); | |
Serial.print(Dev_Type_str); | |
print_hex( buf.bDescriptorType, 8 ); | |
Serial.print(Dev_Version_str); | |
print_hex( buf.bcdUSB, 16 ); | |
Serial.print(Dev_Class_str); | |
print_hex( buf.bDeviceClass, 8 ); | |
Serial.print(Dev_Subclass_str); | |
print_hex( buf.bDeviceSubClass, 8 ); | |
Serial.print(Dev_Protocol_str); | |
print_hex( buf.bDeviceProtocol, 8 ); | |
Serial.print(Dev_Pktsize_str); | |
print_hex( buf.bMaxPacketSize0, 8 ); | |
Serial.print(Dev_Vendor_str); | |
print_hex( buf.idVendor, 16 ); | |
Serial.print(Dev_Product_str); | |
print_hex( buf.idProduct, 16 ); | |
Serial.print(Dev_Revision_str); | |
print_hex( buf.bcdDevice, 16 ); | |
Serial.print(Dev_Mfg_str); | |
print_hex( buf.iManufacturer, 8 ); | |
Serial.print(Dev_Prod_str); | |
print_hex( buf.iProduct, 8 ); | |
Serial.print(Dev_Serial_str); | |
print_hex( buf.iSerialNumber, 8 ); | |
Serial.print(Dev_Nconf_str); | |
print_hex( buf.bNumConfigurations, 8 ); | |
num_conf = buf.bNumConfigurations; | |
return( 0 ); | |
} | |
void printhubdescr(uint8_t *descrptr, uint8_t addr) | |
{ | |
HubDescriptor *pHub = (HubDescriptor*) descrptr; | |
uint8_t len = *((uint8_t*)descrptr); | |
Serial.print(PSTR("\r\n\r\nHub Descriptor:\r\n")); | |
Serial.print(PSTR("bDescLength:\t\t")); | |
Serial.println(pHub->bDescLength, HEX); | |
Serial.print(PSTR("bDescriptorType:\t")); | |
Serial.println(pHub->bDescriptorType, HEX); | |
Serial.print(PSTR("bNbrPorts:\t\t")); | |
Serial.println(pHub->bNbrPorts, HEX); | |
Serial.print(PSTR("LogPwrSwitchMode:\t")); | |
Serial.println(pHub->LogPwrSwitchMode, BIN); | |
Serial.print(PSTR("CompoundDevice:\t\t")); | |
Serial.println(pHub->CompoundDevice, BIN); | |
Serial.print(PSTR("OverCurrentProtectMode:\t")); | |
Serial.println(pHub->OverCurrentProtectMode, BIN); | |
Serial.print(PSTR("TTThinkTime:\t\t")); | |
Serial.println(pHub->TTThinkTime, BIN); | |
Serial.print(PSTR("PortIndicatorsSupported:")); | |
Serial.println(pHub->PortIndicatorsSupported, BIN); | |
Serial.print(PSTR("Reserved:\t\t")); | |
Serial.println(pHub->Reserved, HEX); | |
Serial.print(PSTR("bPwrOn2PwrGood:\t\t")); | |
Serial.println(pHub->bPwrOn2PwrGood, HEX); | |
Serial.print(PSTR("bHubContrCurrent:\t")); | |
Serial.println(pHub->bHubContrCurrent, HEX); | |
for (uint8_t i=7; i<len; i++) | |
print_hex(descrptr[i], 8); | |
//for (uint8_t i=1; i<=pHub->bNbrPorts; i++) | |
// PrintHubPortStatus(&Usb, addr, i, 1); | |
} | |
byte getconfdescr( byte addr, byte conf ) | |
{ | |
uint8_t buf[ BUFSIZE ]; | |
uint8_t* buf_ptr = buf; | |
byte rcode; | |
byte descr_length; | |
byte descr_type; | |
unsigned int total_length; | |
rcode = Usb.getConfDescr( addr, 0, 4, conf, buf ); //get total length | |
LOBYTE( total_length ) = buf[ 2 ]; | |
HIBYTE( total_length ) = buf[ 3 ]; | |
if( total_length > 256 ) { //check if total length is larger than buffer | |
Serial.print(Conf_Trunc_str); | |
total_length = 256; | |
} | |
rcode = Usb.getConfDescr( addr, 0, total_length, conf, buf ); //get the whole descriptor | |
while( buf_ptr < buf + total_length ) { //parsing descriptors | |
descr_length = *( buf_ptr ); | |
descr_type = *( buf_ptr + 1 ); | |
switch( descr_type ) { | |
case( USB_DESCRIPTOR_CONFIGURATION ): | |
printconfdescr( buf_ptr ); | |
break; | |
case( USB_DESCRIPTOR_INTERFACE ): | |
printintfdescr( buf_ptr ); | |
break; | |
case( USB_DESCRIPTOR_ENDPOINT ): | |
printepdescr( buf_ptr ); | |
break; | |
case 0x29: | |
printhubdescr( buf_ptr, addr ); | |
break; | |
case HID_DESCRIPTOR_HID : | |
printhiddescr( buf_ptr ); | |
break; | |
default: | |
printunkdescr( buf_ptr ); | |
break; | |
}//switch( descr_type | |
buf_ptr = ( buf_ptr + descr_length ); //advance buffer pointer | |
}//while( buf_ptr <=... | |
return( 0 ); | |
} | |
/* prints hex numbers with leading zeroes */ | |
// copyright, Peter H Anderson, Baltimore, MD, Nov, '07 | |
// source: http://www.phanderson.com/arduino/arduino_display.html | |
void print_hex(int v, int num_places) | |
{ | |
int mask=0, n, num_nibbles, digit; | |
for (n=1; n<=num_places; n++) { | |
mask = (mask << 1) | 0x0001; | |
} | |
v = v & mask; // truncate v to specified number of places | |
num_nibbles = num_places / 4; | |
if ((num_places % 4) != 0) { | |
++num_nibbles; | |
} | |
do { | |
digit = ((v >> (num_nibbles-1) * 4)) & 0x0f; | |
Serial.print(digit, HEX); | |
} | |
while(--num_nibbles); | |
} | |
/* function to print configuration descriptor */ | |
void printconfdescr( uint8_t* descr_ptr ) | |
{ | |
USB_CONFIGURATION_DESCRIPTOR* conf_ptr = ( USB_CONFIGURATION_DESCRIPTOR* )descr_ptr; | |
Serial.print(Conf_Header_str); | |
Serial.print(Conf_Totlen_str); | |
print_hex( conf_ptr->wTotalLength, 16 ); | |
Serial.print(Conf_Nint_str); | |
print_hex( conf_ptr->bNumInterfaces, 8 ); | |
Serial.print(Conf_Value_str); | |
print_hex( conf_ptr->bConfigurationValue, 8 ); | |
Serial.print(Conf_String_str); | |
print_hex( conf_ptr->iConfiguration, 8 ); | |
Serial.print(Conf_Attr_str); | |
print_hex( conf_ptr->bmAttributes, 8 ); | |
Serial.print(Conf_Pwr_str); | |
print_hex( conf_ptr->bMaxPower, 8 ); | |
return; | |
} | |
/* function to print interface descriptor */ | |
void printintfdescr( uint8_t* descr_ptr ) | |
{ | |
USB_INTERFACE_DESCRIPTOR* intf_ptr = ( USB_INTERFACE_DESCRIPTOR* )descr_ptr; | |
Serial.print(Int_Header_str); | |
Serial.print(Int_Number_str); | |
print_hex( intf_ptr->bInterfaceNumber, 8 ); | |
Serial.print(Int_Alt_str); | |
print_hex( intf_ptr->bAlternateSetting, 8 ); | |
Serial.print(Int_Endpoints_str); | |
print_hex( intf_ptr->bNumEndpoints, 8 ); | |
Serial.print(Int_Class_str); | |
print_hex( intf_ptr->bInterfaceClass, 8 ); | |
Serial.print(Int_Subclass_str); | |
print_hex( intf_ptr->bInterfaceSubClass, 8 ); | |
Serial.print(Int_Protocol_str); | |
print_hex( intf_ptr->bInterfaceProtocol, 8 ); | |
Serial.print(Int_String_str); | |
print_hex( intf_ptr->iInterface, 8 ); | |
return; | |
} | |
/* function to print endpoint descriptor */ | |
void printepdescr( uint8_t* descr_ptr ) | |
{ | |
USB_ENDPOINT_DESCRIPTOR* ep_ptr = ( USB_ENDPOINT_DESCRIPTOR* )descr_ptr; | |
Serial.print(End_Header_str); | |
Serial.print(End_Address_str); | |
print_hex( ep_ptr->bEndpointAddress, 8 ); | |
Serial.print(End_Attr_str); | |
print_hex( ep_ptr->bmAttributes, 8 ); | |
Serial.print(End_Pktsize_str); | |
print_hex( ep_ptr->wMaxPacketSize, 16 ); | |
Serial.print(End_Interval_str); | |
print_hex( ep_ptr->bInterval, 8 ); | |
return; | |
} | |
/*function to print unknown descriptor */ | |
void printunkdescr( uint8_t* descr_ptr ) | |
{ | |
byte length = *descr_ptr; | |
byte i; | |
Serial.print(Unk_Header_str); | |
Serial.print(Unk_Length_str); | |
print_hex( *descr_ptr, 8 ); | |
Serial.print(Unk_Type_str); | |
print_hex( *(descr_ptr + 1 ), 8 ); | |
Serial.print(Unk_Contents_str); | |
descr_ptr += 2; | |
for( i = 0; i < length; i++ ) { | |
print_hex( *descr_ptr, 8 ); | |
descr_ptr++; | |
} | |
} | |
/* function to print hid descriptor */ | |
void printhiddescr( uint8_t* descr_ptr ) | |
{ | |
USB_HID_DESCRIPTOR* hid_ptr = ( USB_HID_DESCRIPTOR* )descr_ptr; | |
Serial.print("\r\n\r\nHID descriptor:"); | |
Serial.print("\r\nLength:\t\t\t"); | |
print_hex( hid_ptr->bLength, 8 ); | |
Serial.print("\r\nbcdHID:\t\t\t"); | |
print_hex( hid_ptr->bcdHID, 16 ); | |
Serial.print("\r\nCountryCode:\t\t"); | |
print_hex( hid_ptr->bCountryCode, 8 ); | |
Serial.print("\r\nNumDescriptors:\t\t"); | |
print_hex( hid_ptr->bNumDescriptors, 8 ); | |
Serial.print("\r\nDescriptorType:\t\t"); | |
print_hex( hid_ptr->bDescrType, 8 ); | |
Serial.print("\r\nDescriptorLength:\t"); | |
print_hex( hid_ptr->wDescriptorLength, 16 ); | |
return; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment