USB_Host_Shield_2.0/examples/USB_desc/USB_desc.ino

347 lines
9.3 KiB
Arduino
Raw Permalink Normal View History

2011-10-11 20:37:54 +02:00
#include <usbhub.h>
#include "pgmstrings.h"
// Satisfy the IDE, which needs to see the include statment in the ino too.
#ifdef dobogusinclude
#include <spi4teensy3.h>
#endif
#include <SPI.h>
2011-10-11 20:37:54 +02:00
USB Usb;
2012-01-10 20:49:42 +01:00
//USBHub Hub1(&Usb);
//USBHub Hub2(&Usb);
//USBHub Hub3(&Usb);
//USBHub Hub4(&Usb);
//USBHub Hub5(&Usb);
//USBHub Hub6(&Usb);
//USBHub Hub7(&Usb);
2011-10-11 20:37:54 +02:00
void PrintAllAddresses(UsbDevice *pdev)
{
2015-04-22 15:48:43 +02:00
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(")");
2011-10-11 20:37:54 +02:00
}
void PrintAddress(uint8_t addr)
{
2015-04-22 15:48:43 +02:00
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);
2011-10-11 20:37:54 +02:00
}
void setup()
{
Serial.begin( 115200 );
#if !defined(__MIPSEL__)
while (!Serial); // Wait for serial port to connect - used on Leonardo, Teensy and other boards with built-in USB CDC serial connection
#endif
2011-10-11 20:37:54 +02:00
Serial.println("Start");
if (Usb.Init() == -1)
2015-04-22 15:48:43 +02:00
Serial.println("OSC did not start.");
2011-10-11 20:37:54 +02:00
delay( 200 );
}
uint8_t getdevdescr( uint8_t addr, uint8_t &num_conf );
2011-10-11 20:37:54 +02:00
void PrintDescriptors(uint8_t addr)
{
2015-04-22 15:48:43 +02:00
uint8_t rcode = 0;
uint8_t num_conf = 0;
rcode = getdevdescr( (uint8_t)addr, num_conf );
2015-04-22 15:48:43 +02:00
if ( rcode )
{
printProgStr(Gen_Error_str);
print_hex( rcode, 8 );
}
Serial.print("\r\n");
2015-04-22 15:48:43 +02:00
for (int i = 0; i < num_conf; i++)
{
rcode = getconfdescr( addr, i ); // get configuration descriptor
if ( rcode )
2011-10-11 20:37:54 +02:00
{
2015-04-22 15:48:43 +02:00
printProgStr(Gen_Error_str);
print_hex(rcode, 8);
2011-10-11 20:37:54 +02:00
}
2015-04-22 15:48:43 +02:00
Serial.println("\r\n");
}
2011-10-11 20:37:54 +02:00
}
void PrintAllDescriptors(UsbDevice *pdev)
{
2015-04-22 15:48:43 +02:00
Serial.println("\r\n");
print_hex(pdev->address.devAddress, 8);
Serial.println("\r\n--");
PrintDescriptors( pdev->address.devAddress );
2011-10-11 20:37:54 +02:00
}
void loop()
{
Usb.Task();
2015-04-22 15:48:43 +02:00
if ( Usb.getUsbTaskState() == USB_STATE_RUNNING )
{
Usb.ForEachUsbDevice(&PrintAllDescriptors);
Usb.ForEachUsbDevice(&PrintAllAddresses);
while ( 1 ) { // stop
#ifdef ESP8266
yield(); // needed in order to reset the watchdog timer on the ESP8266
#endif
}
}
2011-10-11 20:37:54 +02:00
}
uint8_t getdevdescr( uint8_t addr, uint8_t &num_conf )
2011-10-11 20:37:54 +02:00
{
USB_DEVICE_DESCRIPTOR buf;
uint8_t rcode;
2011-10-11 20:37:54 +02:00
rcode = Usb.getDevDescr( addr, 0, 0x12, ( uint8_t *)&buf );
2015-04-22 15:48:43 +02:00
if ( rcode ) {
return ( rcode );
2011-10-11 20:37:54 +02:00
}
printProgStr(Dev_Header_str);
printProgStr(Dev_Length_str);
print_hex( buf.bLength, 8 );
printProgStr(Dev_Type_str);
print_hex( buf.bDescriptorType, 8 );
printProgStr(Dev_Version_str);
print_hex( buf.bcdUSB, 16 );
printProgStr(Dev_Class_str);
2011-10-11 20:37:54 +02:00
print_hex( buf.bDeviceClass, 8 );
printProgStr(Dev_Subclass_str);
print_hex( buf.bDeviceSubClass, 8 );
printProgStr(Dev_Protocol_str);
print_hex( buf.bDeviceProtocol, 8 );
printProgStr(Dev_Pktsize_str);
print_hex( buf.bMaxPacketSize0, 8 );
printProgStr(Dev_Vendor_str);
print_hex( buf.idVendor, 16 );
printProgStr(Dev_Product_str);
print_hex( buf.idProduct, 16 );
printProgStr(Dev_Revision_str);
print_hex( buf.bcdDevice, 16 );
printProgStr(Dev_Mfg_str);
print_hex( buf.iManufacturer, 8 );
printProgStr(Dev_Prod_str);
print_hex( buf.iProduct, 8 );
printProgStr(Dev_Serial_str);
print_hex( buf.iSerialNumber, 8 );
printProgStr(Dev_Nconf_str);
print_hex( buf.bNumConfigurations, 8 );
num_conf = buf.bNumConfigurations;
2015-04-22 15:48:43 +02:00
return ( 0 );
2011-10-11 20:37:54 +02:00
}
2011-10-11 20:37:54 +02:00
void printhubdescr(uint8_t *descrptr, uint8_t addr)
{
2015-04-22 15:48:43 +02:00
HubDescriptor *pHub = (HubDescriptor*) descrptr;
uint8_t len = *((uint8_t*)descrptr);
2015-04-22 15:48:43 +02:00
printProgStr(PSTR("\r\n\r\nHub Descriptor:\r\n"));
printProgStr(PSTR("bDescLength:\t\t"));
Serial.println(pHub->bDescLength, HEX);
2015-04-22 15:48:43 +02:00
printProgStr(PSTR("bDescriptorType:\t"));
Serial.println(pHub->bDescriptorType, HEX);
2015-04-22 15:48:43 +02:00
printProgStr(PSTR("bNbrPorts:\t\t"));
Serial.println(pHub->bNbrPorts, HEX);
2015-04-22 15:48:43 +02:00
printProgStr(PSTR("LogPwrSwitchMode:\t"));
Serial.println(pHub->LogPwrSwitchMode, BIN);
2015-04-22 15:48:43 +02:00
printProgStr(PSTR("CompoundDevice:\t\t"));
Serial.println(pHub->CompoundDevice, BIN);
2015-04-22 15:48:43 +02:00
printProgStr(PSTR("OverCurrentProtectMode:\t"));
Serial.println(pHub->OverCurrentProtectMode, BIN);
2015-04-22 15:48:43 +02:00
printProgStr(PSTR("TTThinkTime:\t\t"));
Serial.println(pHub->TTThinkTime, BIN);
2015-04-22 15:48:43 +02:00
printProgStr(PSTR("PortIndicatorsSupported:"));
Serial.println(pHub->PortIndicatorsSupported, BIN);
2015-04-22 15:48:43 +02:00
printProgStr(PSTR("Reserved:\t\t"));
Serial.println(pHub->Reserved, HEX);
2011-10-11 20:37:54 +02:00
2015-04-22 15:48:43 +02:00
printProgStr(PSTR("bPwrOn2PwrGood:\t\t"));
Serial.println(pHub->bPwrOn2PwrGood, HEX);
2015-04-22 15:48:43 +02:00
printProgStr(PSTR("bHubContrCurrent:\t"));
Serial.println(pHub->bHubContrCurrent, HEX);
2015-04-22 15:48:43 +02:00
for (uint8_t i = 7; i < len; i++)
print_hex(descrptr[i], 8);
2011-10-11 20:37:54 +02:00
2015-04-22 15:48:43 +02:00
//for (uint8_t i=1; i<=pHub->bNbrPorts; i++)
// PrintHubPortStatus(&Usb, addr, i, 1);
2011-10-11 20:37:54 +02:00
}
uint8_t getconfdescr( uint8_t addr, uint8_t conf )
2011-10-11 20:37:54 +02:00
{
uint8_t buf[ BUFSIZE ];
uint8_t* buf_ptr = buf;
uint8_t rcode;
uint8_t descr_length;
uint8_t descr_type;
uint16_t total_length;
2011-10-11 20:37:54 +02:00
rcode = Usb.getConfDescr( addr, 0, 4, conf, buf ); //get total length
LOBYTE( total_length ) = buf[ 2 ];
HIBYTE( total_length ) = buf[ 3 ];
2015-04-22 15:48:43 +02:00
if ( total_length > 256 ) { //check if total length is larger than buffer
2011-10-11 20:37:54 +02:00
printProgStr(Conf_Trunc_str);
total_length = 256;
}
rcode = Usb.getConfDescr( addr, 0, total_length, conf, buf ); //get the whole descriptor
2015-04-22 15:48:43 +02:00
while ( buf_ptr < buf + total_length ) { //parsing descriptors
2011-10-11 20:37:54 +02:00
descr_length = *( buf_ptr );
descr_type = *( buf_ptr + 1 );
2015-04-22 15:48:43 +02:00
switch ( descr_type ) {
case ( USB_DESCRIPTOR_CONFIGURATION ):
2011-10-11 20:37:54 +02:00
printconfdescr( buf_ptr );
break;
2015-04-22 15:48:43 +02:00
case ( USB_DESCRIPTOR_INTERFACE ):
2011-10-11 20:37:54 +02:00
printintfdescr( buf_ptr );
break;
2015-04-22 15:48:43 +02:00
case ( USB_DESCRIPTOR_ENDPOINT ):
2011-10-11 20:37:54 +02:00
printepdescr( buf_ptr );
break;
case 0x29:
printhubdescr( buf_ptr, addr );
break;
default:
printunkdescr( buf_ptr );
break;
2015-04-22 15:48:43 +02:00
}//switch( descr_type
2011-10-11 20:37:54 +02:00
buf_ptr = ( buf_ptr + descr_length ); //advance buffer pointer
}//while( buf_ptr <=...
return ( rcode );
2011-10-11 20:37:54 +02:00
}
/* 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)
{
2015-04-22 15:48:43 +02:00
int mask = 0, n, num_nibbles, digit;
2015-04-22 15:48:43 +02:00
for (n = 1; n <= num_places; n++) {
2011-10-11 20:37:54 +02:00
mask = (mask << 1) | 0x0001;
}
v = v & mask; // truncate v to specified number of places
2011-10-11 20:37:54 +02:00
num_nibbles = num_places / 4;
if ((num_places % 4) != 0) {
++num_nibbles;
}
do {
2015-04-22 15:48:43 +02:00
digit = ((v >> (num_nibbles - 1) * 4)) & 0x0f;
2011-10-11 20:37:54 +02:00
Serial.print(digit, HEX);
}
2015-04-22 15:48:43 +02:00
while (--num_nibbles);
2011-10-11 20:37:54 +02:00
}
/* function to print configuration descriptor */
void printconfdescr( uint8_t* descr_ptr )
{
2015-04-22 15:48:43 +02:00
USB_CONFIGURATION_DESCRIPTOR* conf_ptr = ( USB_CONFIGURATION_DESCRIPTOR* )descr_ptr;
2011-10-11 20:37:54 +02:00
printProgStr(Conf_Header_str);
printProgStr(Conf_Totlen_str);
print_hex( conf_ptr->wTotalLength, 16 );
printProgStr(Conf_Nint_str);
print_hex( conf_ptr->bNumInterfaces, 8 );
printProgStr(Conf_Value_str);
print_hex( conf_ptr->bConfigurationValue, 8 );
printProgStr(Conf_String_str);
print_hex( conf_ptr->iConfiguration, 8 );
printProgStr(Conf_Attr_str);
print_hex( conf_ptr->bmAttributes, 8 );
printProgStr(Conf_Pwr_str);
print_hex( conf_ptr->bMaxPower, 8 );
return;
}
/* function to print interface descriptor */
void printintfdescr( uint8_t* descr_ptr )
{
2015-04-22 15:48:43 +02:00
USB_INTERFACE_DESCRIPTOR* intf_ptr = ( USB_INTERFACE_DESCRIPTOR* )descr_ptr;
2011-10-11 20:37:54 +02:00
printProgStr(Int_Header_str);
printProgStr(Int_Number_str);
print_hex( intf_ptr->bInterfaceNumber, 8 );
printProgStr(Int_Alt_str);
print_hex( intf_ptr->bAlternateSetting, 8 );
printProgStr(Int_Endpoints_str);
print_hex( intf_ptr->bNumEndpoints, 8 );
printProgStr(Int_Class_str);
print_hex( intf_ptr->bInterfaceClass, 8 );
printProgStr(Int_Subclass_str);
print_hex( intf_ptr->bInterfaceSubClass, 8 );
printProgStr(Int_Protocol_str);
print_hex( intf_ptr->bInterfaceProtocol, 8 );
printProgStr(Int_String_str);
print_hex( intf_ptr->iInterface, 8 );
return;
}
/* function to print endpoint descriptor */
void printepdescr( uint8_t* descr_ptr )
{
2015-04-22 15:48:43 +02:00
USB_ENDPOINT_DESCRIPTOR* ep_ptr = ( USB_ENDPOINT_DESCRIPTOR* )descr_ptr;
2011-10-11 20:37:54 +02:00
printProgStr(End_Header_str);
printProgStr(End_Address_str);
print_hex( ep_ptr->bEndpointAddress, 8 );
printProgStr(End_Attr_str);
print_hex( ep_ptr->bmAttributes, 8 );
printProgStr(End_Pktsize_str);
print_hex( ep_ptr->wMaxPacketSize, 16 );
printProgStr(End_Interval_str);
print_hex( ep_ptr->bInterval, 8 );
2011-10-11 20:37:54 +02:00
return;
}
/*function to print unknown descriptor */
void printunkdescr( uint8_t* descr_ptr )
{
uint8_t length = *descr_ptr;
uint8_t i;
2011-10-11 20:37:54 +02:00
printProgStr(Unk_Header_str);
printProgStr(Unk_Length_str);
print_hex( *descr_ptr, 8 );
printProgStr(Unk_Type_str);
print_hex( *(descr_ptr + 1 ), 8 );
printProgStr(Unk_Contents_str);
descr_ptr += 2;
2015-04-22 15:48:43 +02:00
for ( i = 0; i < length; i++ ) {
2011-10-11 20:37:54 +02:00
print_hex( *descr_ptr, 8 );
descr_ptr++;
}
}
2011-10-11 20:37:54 +02:00
/* Print a string from Program Memory directly to save RAM */
void printProgStr(const char* str)
2011-10-11 20:37:54 +02:00
{
char c;
2015-04-22 15:48:43 +02:00
if (!str) return;
while ((c = pgm_read_byte(str++)))
2012-01-10 20:49:42 +01:00
Serial.print(c);
2011-10-11 20:37:54 +02:00
}