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.

hub_demo.ino 9.2KB

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