Subversion Repositories Projects

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1702 - 1
/* MAX3421E USB Host controller get configuration descriptor */
2
#include <Spi.h>
3
#include <Max3421e.h>
4
#include <Usb.h>
5
 
6
#define LOBYTE(x) ((char*)(&(x)))[0]
7
#define HIBYTE(x) ((char*)(&(x)))[1]
8
#define BUFSIZE 256    //buffer size
9
 
10
void setup();
11
void loop();
12
 
13
MAX3421E Max;
14
USB Usb;
15
 
16
void setup()
17
{
18
  byte tmpdata = 0;
19
  Serial.begin( 9600 );
20
  Serial.println("Start");
21
  Max.powerOn();
22
  delay( 200 );
23
}
24
 
25
void loop()
26
{
27
  byte rcode;
28
  byte tmpbyte;
29
  Max.Task();
30
  Usb.Task();
31
  if( Usb.getUsbTaskState() >= USB_STATE_CONFIGURING ) {  //state configuring or higher
32
    /* entering configuration number to decode. Restricted to first 10 configurations */
33
    Serial.print("Enter configuration number: ");
34
    while( Serial.available() == 0 );      //wait for input
35
    tmpbyte = Serial.read();
36
    Serial.println( tmpbyte );
37
    tmpbyte -= 0x30;                       //convert to number
38
    if( tmpbyte > 9 ) {
39
      Serial.println("Not a number. Assuming configuration 0");
40
      tmpbyte = 0;
41
    }
42
    rcode = getconfdescr( 1, tmpbyte );                 //get configuration descriptor
43
    if( rcode ) {
44
      Serial.println( rcode, HEX );
45
    }
46
//    while( 1 );                          //stop
47
  }
48
}
49
 
50
byte getconfdescr( byte addr, byte conf )
51
{
52
  char buf[ BUFSIZE ];
53
  char* buf_ptr = buf;
54
  byte rcode;
55
  byte descr_length;
56
  byte descr_type;
57
  unsigned int total_length;
58
  rcode = Usb.getConfDescr( addr, 0, 4, conf, buf );  //get total length
59
  LOBYTE( total_length ) = buf[ 2 ];
60
  HIBYTE( total_length ) = buf[ 3 ];
61
  if( total_length > BUFSIZE ) {    //check if total length is larger than buffer
62
    Serial.println("Total length truncated to 256 bytes");
63
    total_length = BUFSIZE;
64
  }
65
  rcode = Usb.getConfDescr( addr, 0, total_length, conf, buf ); //get the whole descriptor
66
  while( buf_ptr < buf + total_length ) {  //parsing descriptors
67
    descr_length = *( buf_ptr );
68
    descr_type = *( buf_ptr + 1 );
69
    switch( descr_type ) {
70
      case( USB_DESCRIPTOR_CONFIGURATION ):
71
        printconfdescr( buf_ptr );
72
        break;
73
      case( USB_DESCRIPTOR_INTERFACE ):
74
        printintfdescr( buf_ptr );
75
        break;
76
      case( USB_DESCRIPTOR_ENDPOINT ):
77
        printepdescr( buf_ptr );
78
        break;
79
      default:
80
        printunkdescr( buf_ptr );
81
        break;
82
        }//switch( descr_type
83
    Serial.println("");
84
    buf_ptr = ( buf_ptr + descr_length );    //advance buffer pointer
85
  }//while( buf_ptr <=...
86
  return( 0 );
87
}
88
/* prints hex numbers with leading zeroes */
89
// copyright, Peter H Anderson, Baltimore, MD, Nov, '07
90
// source: http://www.phanderson.com/arduino/arduino_display.html
91
void print_hex(int v, int num_places)
92
{
93
  int mask=0, n, num_nibbles, digit;
94
 
95
  for (n=1; n<=num_places; n++) {
96
    mask = (mask << 1) | 0x0001;
97
  }
98
  v = v & mask; // truncate v to specified number of places
99
 
100
  num_nibbles = num_places / 4;
101
  if ((num_places % 4) != 0) {
102
    ++num_nibbles;
103
  }
104
  do {
105
    digit = ((v >> (num_nibbles-1) * 4)) & 0x0f;
106
    Serial.print(digit, HEX);
107
  }
108
  while(--num_nibbles);
109
}
110
/* function to print configuration descriptor */
111
void printconfdescr( char* descr_ptr )
112
{
113
 USB_CONFIGURATION_DESCRIPTOR* conf_ptr = ( USB_CONFIGURATION_DESCRIPTOR* )descr_ptr;
114
  Serial.println("Configuration descriptor:");
115
  Serial.print("Total length:\t");
116
  print_hex( conf_ptr->wTotalLength, 16 );
117
  Serial.print("\r\nNum.intf:\t\t");
118
  print_hex( conf_ptr->bNumInterfaces, 8 );
119
  Serial.print("\r\nConf.value:\t");
120
  print_hex( conf_ptr->bConfigurationValue, 8 );
121
  Serial.print("\r\nConf.string:\t");
122
  print_hex( conf_ptr->iConfiguration, 8 );
123
  Serial.print("\r\nAttr.:\t\t");
124
  print_hex( conf_ptr->bmAttributes, 8 );
125
  Serial.print("\r\nMax.pwr:\t\t");
126
  print_hex( conf_ptr->bMaxPower, 8 );
127
  return;
128
}
129
/* function to print interface descriptor */
130
void printintfdescr( char* descr_ptr )
131
{
132
 USB_INTERFACE_DESCRIPTOR* intf_ptr = ( USB_INTERFACE_DESCRIPTOR* )descr_ptr;
133
  Serial.println("\r\nInterface descriptor:");
134
  Serial.print("Intf.number:\t");
135
  print_hex( intf_ptr->bInterfaceNumber, 8 );
136
  Serial.print("\r\nAlt.:\t\t");
137
  print_hex( intf_ptr->bAlternateSetting, 8 );
138
  Serial.print("\r\nEndpoints:\t\t");
139
  print_hex( intf_ptr->bNumEndpoints, 8 );
140
  Serial.print("\r\nClass:\t\t");
141
  print_hex( intf_ptr->bInterfaceClass, 8 );
142
  Serial.print("\r\nSubclass:\t\t");
143
  print_hex( intf_ptr->bInterfaceSubClass, 8 );
144
  Serial.print("\r\nProtocol:\t\t");
145
  print_hex( intf_ptr->bInterfaceProtocol, 8 );
146
  Serial.print("\r\nIntf.string:\t");
147
  print_hex( intf_ptr->iInterface, 8 );
148
  return;
149
}
150
/* function to print endpoint descriptor */
151
void printepdescr( char* descr_ptr )
152
{
153
 USB_ENDPOINT_DESCRIPTOR* ep_ptr = ( USB_ENDPOINT_DESCRIPTOR* )descr_ptr;
154
  Serial.println("\r\nEndpoint descriptor:");
155
  Serial.print("Endpoint address:\t");
156
  print_hex( ep_ptr->bEndpointAddress, 8 );
157
  Serial.print("\r\nAttr.:\t\t");
158
  print_hex( ep_ptr->bmAttributes, 8 );
159
  Serial.print("\r\nMax.pkt size:\t");
160
  print_hex( ep_ptr->wMaxPacketSize, 16 );
161
  Serial.print("\r\nPolling interval:\t");
162
  print_hex( ep_ptr->bInterval, 8 );
163
  return;
164
}
165
/*function to print unknown descriptor */
166
void printunkdescr( char* descr_ptr )
167
{
168
  byte length = *descr_ptr;
169
  byte i;
170
  Serial.println("\r\nUnknown descriptor:");
171
  Serial. print("Length:\t\t");
172
  print_hex( *descr_ptr, 8 );
173
  Serial.print("\r\nType:\t\t");
174
  print_hex( *(descr_ptr + 1 ), 8 );
175
  Serial.print("\r\nContents:\t");
176
  descr_ptr += 2;
177
  for( i = 0; i < length; i++ ) {
178
    print_hex( *descr_ptr, 8 );
179
    descr_ptr++;
180
  }
181
}