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