]> git.friedersdorff.com Git - max/tmk_keyboard.git/blob - tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/USB_desc/USB_desc.ino
lufa: usb-usb: Use LUFA startup instead of cusotom
[max/tmk_keyboard.git] / tmk_core / protocol / usb_hid / USB_Host_Shield_2.0 / examples / USB_desc / USB_desc.ino
1 #include <usbhub.h>
2
3 #include "pgmstrings.h"
4
5 // Satisfy the IDE, which needs to see the include statment in the ino too.
6 #ifdef dobogusinclude
7 #include <spi4teensy3.h>
8 #include <SPI.h>
9 #endif
10
11 USB     Usb;
12 //USBHub  Hub1(&Usb);
13 //USBHub  Hub2(&Usb);
14 //USBHub  Hub3(&Usb);
15 //USBHub  Hub4(&Usb);
16 //USBHub  Hub5(&Usb);
17 //USBHub  Hub6(&Usb);
18 //USBHub  Hub7(&Usb);
19
20 uint32_t next_time;
21
22 void PrintAllAddresses(UsbDevice *pdev)
23 {
24   UsbDeviceAddress adr;
25   adr.devAddress = pdev->address.devAddress;
26   Serial.print("\r\nAddr:");
27   Serial.print(adr.devAddress, HEX);
28   Serial.print("(");
29   Serial.print(adr.bmHub, HEX);
30   Serial.print(".");
31   Serial.print(adr.bmParent, HEX);
32   Serial.print(".");
33   Serial.print(adr.bmAddress, HEX);
34   Serial.println(")");
35 }
36
37 void PrintAddress(uint8_t addr)
38 {
39   UsbDeviceAddress adr;
40   adr.devAddress = addr;
41   Serial.print("\r\nADDR:\t");
42   Serial.println(adr.devAddress, HEX);
43   Serial.print("DEV:\t");
44   Serial.println(adr.bmAddress, HEX);
45   Serial.print("PRNT:\t");
46   Serial.println(adr.bmParent, HEX);
47   Serial.print("HUB:\t");
48   Serial.println(adr.bmHub, HEX);
49 }
50
51 void setup()
52 {
53   Serial.begin( 115200 );
54 #if !defined(__MIPSEL__)
55   while (!Serial); // Wait for serial port to connect - used on Leonardo, Teensy and other boards with built-in USB CDC serial connection
56 #endif
57   Serial.println("Start");
58
59   if (Usb.Init() == -1)
60     Serial.println("OSC did not start.");
61
62   delay( 200 );
63
64   next_time = millis() + 10000;
65 }
66
67 byte getdevdescr( byte addr, byte &num_conf );
68
69 void PrintDescriptors(uint8_t addr)
70 {
71   uint8_t rcode = 0;
72   byte num_conf = 0;
73
74   rcode = getdevdescr( (byte)addr, num_conf );
75   if ( rcode )
76   {
77     printProgStr(Gen_Error_str);
78     print_hex( rcode, 8 );
79   }
80   Serial.print("\r\n");
81
82   for (int i = 0; i < num_conf; i++)
83   {
84     rcode = getconfdescr( addr, i );                 // get configuration descriptor
85     if ( rcode )
86     {
87       printProgStr(Gen_Error_str);
88       print_hex(rcode, 8);
89     }
90     Serial.println("\r\n");
91   }
92 }
93
94 void PrintAllDescriptors(UsbDevice *pdev)
95 {
96   Serial.println("\r\n");
97   print_hex(pdev->address.devAddress, 8);
98   Serial.println("\r\n--");
99   PrintDescriptors( pdev->address.devAddress );
100 }
101
102 void loop()
103 {
104   Usb.Task();
105
106   if ( Usb.getUsbTaskState() == USB_STATE_RUNNING )
107   {
108     //if (millis() >= next_time)
109     {
110       Usb.ForEachUsbDevice(&PrintAllDescriptors);
111       Usb.ForEachUsbDevice(&PrintAllAddresses);
112
113       while ( 1 );                          //stop
114     }
115   }
116 }
117
118 byte getdevdescr( byte addr, byte &num_conf )
119 {
120   USB_DEVICE_DESCRIPTOR buf;
121   byte rcode;
122   rcode = Usb.getDevDescr( addr, 0, 0x12, ( uint8_t *)&buf );
123   if ( rcode ) {
124     return ( rcode );
125   }
126   printProgStr(Dev_Header_str);
127   printProgStr(Dev_Length_str);
128   print_hex( buf.bLength, 8 );
129   printProgStr(Dev_Type_str);
130   print_hex( buf.bDescriptorType, 8 );
131   printProgStr(Dev_Version_str);
132   print_hex( buf.bcdUSB, 16 );
133   printProgStr(Dev_Class_str);
134   print_hex( buf.bDeviceClass, 8 );
135   printProgStr(Dev_Subclass_str);
136   print_hex( buf.bDeviceSubClass, 8 );
137   printProgStr(Dev_Protocol_str);
138   print_hex( buf.bDeviceProtocol, 8 );
139   printProgStr(Dev_Pktsize_str);
140   print_hex( buf.bMaxPacketSize0, 8 );
141   printProgStr(Dev_Vendor_str);
142   print_hex( buf.idVendor, 16 );
143   printProgStr(Dev_Product_str);
144   print_hex( buf.idProduct, 16 );
145   printProgStr(Dev_Revision_str);
146   print_hex( buf.bcdDevice, 16 );
147   printProgStr(Dev_Mfg_str);
148   print_hex( buf.iManufacturer, 8 );
149   printProgStr(Dev_Prod_str);
150   print_hex( buf.iProduct, 8 );
151   printProgStr(Dev_Serial_str);
152   print_hex( buf.iSerialNumber, 8 );
153   printProgStr(Dev_Nconf_str);
154   print_hex( buf.bNumConfigurations, 8 );
155   num_conf = buf.bNumConfigurations;
156   return ( 0 );
157 }
158
159 void printhubdescr(uint8_t *descrptr, uint8_t addr)
160 {
161   HubDescriptor  *pHub = (HubDescriptor*) descrptr;
162   uint8_t        len = *((uint8_t*)descrptr);
163
164   printProgStr(PSTR("\r\n\r\nHub Descriptor:\r\n"));
165   printProgStr(PSTR("bDescLength:\t\t"));
166   Serial.println(pHub->bDescLength, HEX);
167
168   printProgStr(PSTR("bDescriptorType:\t"));
169   Serial.println(pHub->bDescriptorType, HEX);
170
171   printProgStr(PSTR("bNbrPorts:\t\t"));
172   Serial.println(pHub->bNbrPorts, HEX);
173
174   printProgStr(PSTR("LogPwrSwitchMode:\t"));
175   Serial.println(pHub->LogPwrSwitchMode, BIN);
176
177   printProgStr(PSTR("CompoundDevice:\t\t"));
178   Serial.println(pHub->CompoundDevice, BIN);
179
180   printProgStr(PSTR("OverCurrentProtectMode:\t"));
181   Serial.println(pHub->OverCurrentProtectMode, BIN);
182
183   printProgStr(PSTR("TTThinkTime:\t\t"));
184   Serial.println(pHub->TTThinkTime, BIN);
185
186   printProgStr(PSTR("PortIndicatorsSupported:"));
187   Serial.println(pHub->PortIndicatorsSupported, BIN);
188
189   printProgStr(PSTR("Reserved:\t\t"));
190   Serial.println(pHub->Reserved, HEX);
191
192   printProgStr(PSTR("bPwrOn2PwrGood:\t\t"));
193   Serial.println(pHub->bPwrOn2PwrGood, HEX);
194
195   printProgStr(PSTR("bHubContrCurrent:\t"));
196   Serial.println(pHub->bHubContrCurrent, HEX);
197
198   for (uint8_t i = 7; i < len; i++)
199     print_hex(descrptr[i], 8);
200
201   //for (uint8_t i=1; i<=pHub->bNbrPorts; i++)
202   //    PrintHubPortStatus(&Usb, addr, i, 1);
203 }
204
205 byte getconfdescr( byte addr, byte conf )
206 {
207   uint8_t buf[ BUFSIZE ];
208   uint8_t* buf_ptr = buf;
209   byte rcode;
210   byte descr_length;
211   byte descr_type;
212   unsigned int total_length;
213   rcode = Usb.getConfDescr( addr, 0, 4, conf, buf );  //get total length
214   LOBYTE( total_length ) = buf[ 2 ];
215   HIBYTE( total_length ) = buf[ 3 ];
216   if ( total_length > 256 ) {   //check if total length is larger than buffer
217     printProgStr(Conf_Trunc_str);
218     total_length = 256;
219   }
220   rcode = Usb.getConfDescr( addr, 0, total_length, conf, buf ); //get the whole descriptor
221   while ( buf_ptr < buf + total_length ) { //parsing descriptors
222     descr_length = *( buf_ptr );
223     descr_type = *( buf_ptr + 1 );
224     switch ( descr_type ) {
225       case ( USB_DESCRIPTOR_CONFIGURATION ):
226         printconfdescr( buf_ptr );
227         break;
228       case ( USB_DESCRIPTOR_INTERFACE ):
229         printintfdescr( buf_ptr );
230         break;
231       case ( USB_DESCRIPTOR_ENDPOINT ):
232         printepdescr( buf_ptr );
233         break;
234       case 0x29:
235         printhubdescr( buf_ptr, addr );
236         break;
237       default:
238         printunkdescr( buf_ptr );
239         break;
240     }//switch( descr_type
241     buf_ptr = ( buf_ptr + descr_length );    //advance buffer pointer
242   }//while( buf_ptr <=...
243   return ( rcode );
244 }
245 /* prints hex numbers with leading zeroes */
246 // copyright, Peter H Anderson, Baltimore, MD, Nov, '07
247 // source: http://www.phanderson.com/arduino/arduino_display.html
248 void print_hex(int v, int num_places)
249 {
250   int mask = 0, n, num_nibbles, digit;
251
252   for (n = 1; n <= num_places; n++) {
253     mask = (mask << 1) | 0x0001;
254   }
255   v = v & mask; // truncate v to specified number of places
256
257   num_nibbles = num_places / 4;
258   if ((num_places % 4) != 0) {
259     ++num_nibbles;
260   }
261   do {
262     digit = ((v >> (num_nibbles - 1) * 4)) & 0x0f;
263     Serial.print(digit, HEX);
264   }
265   while (--num_nibbles);
266 }
267 /* function to print configuration descriptor */
268 void printconfdescr( uint8_t* descr_ptr )
269 {
270   USB_CONFIGURATION_DESCRIPTOR* conf_ptr = ( USB_CONFIGURATION_DESCRIPTOR* )descr_ptr;
271   printProgStr(Conf_Header_str);
272   printProgStr(Conf_Totlen_str);
273   print_hex( conf_ptr->wTotalLength, 16 );
274   printProgStr(Conf_Nint_str);
275   print_hex( conf_ptr->bNumInterfaces, 8 );
276   printProgStr(Conf_Value_str);
277   print_hex( conf_ptr->bConfigurationValue, 8 );
278   printProgStr(Conf_String_str);
279   print_hex( conf_ptr->iConfiguration, 8 );
280   printProgStr(Conf_Attr_str);
281   print_hex( conf_ptr->bmAttributes, 8 );
282   printProgStr(Conf_Pwr_str);
283   print_hex( conf_ptr->bMaxPower, 8 );
284   return;
285 }
286 /* function to print interface descriptor */
287 void printintfdescr( uint8_t* descr_ptr )
288 {
289   USB_INTERFACE_DESCRIPTOR* intf_ptr = ( USB_INTERFACE_DESCRIPTOR* )descr_ptr;
290   printProgStr(Int_Header_str);
291   printProgStr(Int_Number_str);
292   print_hex( intf_ptr->bInterfaceNumber, 8 );
293   printProgStr(Int_Alt_str);
294   print_hex( intf_ptr->bAlternateSetting, 8 );
295   printProgStr(Int_Endpoints_str);
296   print_hex( intf_ptr->bNumEndpoints, 8 );
297   printProgStr(Int_Class_str);
298   print_hex( intf_ptr->bInterfaceClass, 8 );
299   printProgStr(Int_Subclass_str);
300   print_hex( intf_ptr->bInterfaceSubClass, 8 );
301   printProgStr(Int_Protocol_str);
302   print_hex( intf_ptr->bInterfaceProtocol, 8 );
303   printProgStr(Int_String_str);
304   print_hex( intf_ptr->iInterface, 8 );
305   return;
306 }
307 /* function to print endpoint descriptor */
308 void printepdescr( uint8_t* descr_ptr )
309 {
310   USB_ENDPOINT_DESCRIPTOR* ep_ptr = ( USB_ENDPOINT_DESCRIPTOR* )descr_ptr;
311   printProgStr(End_Header_str);
312   printProgStr(End_Address_str);
313   print_hex( ep_ptr->bEndpointAddress, 8 );
314   printProgStr(End_Attr_str);
315   print_hex( ep_ptr->bmAttributes, 8 );
316   printProgStr(End_Pktsize_str);
317   print_hex( ep_ptr->wMaxPacketSize, 16 );
318   printProgStr(End_Interval_str);
319   print_hex( ep_ptr->bInterval, 8 );
320
321   return;
322 }
323 /*function to print unknown descriptor */
324 void printunkdescr( uint8_t* descr_ptr )
325 {
326   byte length = *descr_ptr;
327   byte i;
328   printProgStr(Unk_Header_str);
329   printProgStr(Unk_Length_str);
330   print_hex( *descr_ptr, 8 );
331   printProgStr(Unk_Type_str);
332   print_hex( *(descr_ptr + 1 ), 8 );
333   printProgStr(Unk_Contents_str);
334   descr_ptr += 2;
335   for ( i = 0; i < length; i++ ) {
336     print_hex( *descr_ptr, 8 );
337     descr_ptr++;
338   }
339 }
340
341
342 /* Print a string from Program Memory directly to save RAM */
343 void printProgStr(const char* str)
344 {
345   char c;
346   if (!str) return;
347   while ((c = pgm_read_byte(str++)))
348     Serial.print(c);
349 }