Keyboard firmwares for Atmel AVR and Cortex-M
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

USB_desc.ino 9.3KB

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