Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed UsbHostMAX3421E millis
main.cpp
00001 #include "mbed.h" 00002 00003 #include "Usb.h" 00004 #include "usbhub.h" 00005 #include "pgm_strings.h" 00006 00007 Usb usb(D11, D12, D13, D10, D9); // mosi, miso, sck, ss, intr 00008 00009 uint8_t getConfDescr(uint8_t addr, uint8_t conf); 00010 00011 void printAllAddresses(UsbDevice* pdev) 00012 { 00013 UsbDeviceAddress adr; 00014 00015 adr.devAddress = pdev->address.devAddress; 00016 printf("\r\nAddr: 0x%x (0x%x.0x%x.0x%x)\r\n", adr.devAddress, adr.bmHub, adr.bmParent, adr.bmAddress); 00017 } 00018 00019 /** 00020 * @brief 00021 * @note 00022 * @param 00023 * @retval 00024 */ 00025 void printAddress(uint8_t addr) 00026 { 00027 UsbDeviceAddress adr; 00028 00029 adr.devAddress = addr; 00030 printf("\r\nADDR:\t0x%x\r\n", adr.devAddress); 00031 printf("DEV:\t0x%x\r\n", adr.bmAddress); 00032 printf("PRNT:\t0x%x\r\n", adr.bmParent); 00033 printf("HUB:\t0x%x\r\n", adr.bmHub); 00034 } 00035 00036 /** 00037 * @brief 00038 * @note 00039 * @param 00040 * @retval 00041 */ 00042 uint8_t getDevDescr(uint8_t addr, uint8_t& num_conf) 00043 { 00044 USB_DEVICE_DESCRIPTOR buf; 00045 uint8_t rcode; 00046 00047 rcode = usb.getDevDescr(addr, 0, 0x12, (uint8_t*) &buf); 00048 if (rcode) { 00049 return(rcode); 00050 } 00051 00052 printf("%s", Dev_Header_str); 00053 printf("%s0x%.8x", Dev_Length_str, buf.bLength); 00054 printf("%s0x%.8x", Dev_Type_str, buf.bDescriptorType); 00055 printf("%s0x%.16x", Dev_Version_str, buf.bcdUSB); 00056 printf("%s0x%.8x", Dev_Class_str, buf.bDeviceClass); 00057 printf("%s0x%.8x", Dev_Subclass_str, buf.bDeviceSubClass); 00058 printf("%s0x%.8x", Dev_Protocol_str, buf.bDeviceProtocol); 00059 printf("%s0x%.8x", Dev_Pktsize_str, buf.bMaxPacketSize0); 00060 printf("%s0x%.16x", Dev_Vendor_str, buf.idVendor); 00061 printf("%s0x%.16x", Dev_Product_str, buf.idProduct); 00062 printf("%s0x%.16x", Dev_Revision_str, buf.bcdDevice); 00063 printf("%s0x%.8x", Dev_Mfg_str, buf.iManufacturer); 00064 printf("%s0x%.8x", Dev_Prod_str, buf.iProduct); 00065 printf("%s0x%.8x", Dev_Serial_str, buf.iSerialNumber); 00066 printf("%s0x%.8x", Dev_Nconf_str, buf.bNumConfigurations); 00067 num_conf = buf.bNumConfigurations; 00068 return(0); 00069 } 00070 00071 /* function to print configuration descriptor */ 00072 void printConfDescr(uint8_t* descr_ptr) 00073 { 00074 USB_CONFIGURATION_DESCRIPTOR* conf_ptr = (USB_CONFIGURATION_DESCRIPTOR*)descr_ptr; 00075 00076 printf("%s", Conf_Header_str); 00077 printf("%s0x%.16x", Conf_Totlen_str, conf_ptr->wTotalLength); 00078 printf("%s0x%.8x", Conf_Nint_str, conf_ptr->bNumInterfaces); 00079 printf("%s0x%.8x", Conf_Value_str, conf_ptr->bConfigurationValue); 00080 printf("%s0x%.8x", Conf_String_str, conf_ptr->iConfiguration); 00081 printf("%s0x%.8x", Conf_Attr_str, conf_ptr->bmAttributes); 00082 printf("%s0x%.8x", Conf_Pwr_str, conf_ptr->bMaxPower); 00083 return; 00084 } 00085 00086 /* function to print interface descriptor */ 00087 void printIntfDescr(uint8_t* descr_ptr) 00088 { 00089 USB_INTERFACE_DESCRIPTOR* intf_ptr = (USB_INTERFACE_DESCRIPTOR*)descr_ptr; 00090 00091 printf("%s", Int_Header_str); 00092 printf("%s0x%.8x", Int_Number_str, intf_ptr->bInterfaceNumber); 00093 printf("%s0x%.8x", Int_Alt_str, intf_ptr->bAlternateSetting); 00094 printf("%s0x%.8x", Int_Endpoints_str, intf_ptr->bNumEndpoints); 00095 printf("%s0x%.8x", Int_Class_str, intf_ptr->bInterfaceClass); 00096 printf("%s0x%.8x", Int_Subclass_str, intf_ptr->bInterfaceSubClass); 00097 printf("%s0x%.8x", Int_Protocol_str, intf_ptr->bInterfaceProtocol); 00098 printf("%s0x%.8x", Int_String_str, intf_ptr->iInterface); 00099 return; 00100 } 00101 00102 /* function to print endpoint descriptor */ 00103 void printEpDescr(uint8_t* descr_ptr) 00104 { 00105 USB_ENDPOINT_DESCRIPTOR* ep_ptr = (USB_ENDPOINT_DESCRIPTOR*)descr_ptr; 00106 00107 printf("%s", End_Header_str); 00108 printf("%s0x%.8x", End_Address_str, ep_ptr->bEndpointAddress); 00109 printf("%s0x%.8x", End_Attr_str, ep_ptr->bmAttributes); 00110 printf("%s0x%.16x", End_Pktsize_str, ep_ptr->wMaxPacketSize); 00111 printf("%s0x%.8x", End_Interval_str, ep_ptr->bInterval); 00112 00113 return; 00114 } 00115 00116 /** 00117 * @brief 00118 * @note 00119 * @param 00120 * @retval 00121 */ 00122 void printHubDescr(uint8_t* descrptr, uint8_t addr) 00123 { 00124 HubDescriptor* pHub = (HubDescriptor*)descrptr; 00125 uint8_t len = *((uint8_t*)descrptr); 00126 00127 printf("\r\n\r\nHub Descriptor:\r\n"); 00128 printf("bDescLength:\t\t0x%x\r\n", pHub->bDescLength); 00129 printf("bDescriptorType:\t0x%x\r\n", pHub->bDescriptorType); 00130 printf("bNbrPorts:\t\t0x%x\r\n", pHub->bNbrPorts); 00131 printf("LogPwrSwitchMode:\t0x%x\r\n", pHub->LogPwrSwitchMode); 00132 printf("CompoundDevice:\t\t0x%x\r\n", pHub->CompoundDevice); 00133 printf("OverCurrentProtectMode:\t0x%x", pHub->OverCurrentProtectMode); 00134 printf("TTThinkTime:\t\t0x%x", pHub->TTThinkTime); 00135 printf("PortIndicatorsSupported:0x%x", pHub->PortIndicatorsSupported); 00136 printf("Reserved:\t\t0x%x", pHub->Reserved); 00137 printf("bPwrOn2PwrGood:\t\t0x%x", pHub->bPwrOn2PwrGood); 00138 printf("bHubContrCurrent:\t0x%x", pHub->bHubContrCurrent); 00139 00140 for (uint8_t i = 7; i < len; i++) 00141 printf("0x%.8x ", descrptr[i]); 00142 00143 printf("\r\n"); 00144 00145 //for (uint8_t i=1; i<=pHub->bNbrPorts; i++) 00146 // PrintHubPortStatus(&Usb, addr, i, 1); 00147 } 00148 00149 /** 00150 * @brief 00151 * @note 00152 * @param 00153 * @retval 00154 */ 00155 void printDescriptors(uint8_t addr) 00156 { 00157 uint8_t rcode = 0; 00158 uint8_t num_conf = 0; 00159 00160 rcode = getDevDescr((uint8_t) addr, num_conf); 00161 if (rcode) { 00162 printf("%s0x%.8x", Gen_Error_str, rcode); 00163 } 00164 00165 printf("\r\n"); 00166 00167 for (int i = 0; i < num_conf; i++) { 00168 rcode = getConfDescr(addr, i); // get configuration descriptor 00169 if (rcode) { 00170 printf("%s0x%.8x", Gen_Error_str, rcode); 00171 } 00172 00173 printf("\r\n"); 00174 } 00175 } 00176 00177 /** 00178 * @brief 00179 * @note 00180 * @param 00181 * @retval 00182 */ 00183 void printAllDescriptors(UsbDevice* pdev) 00184 { 00185 printf("\r\n"); 00186 printf("0x%.8x\r\n--", pdev->address.devAddress); 00187 printDescriptors(pdev->address.devAddress); 00188 } 00189 00190 /*function to print unknown descriptor */ 00191 void printUnknownDescr(uint8_t* descr_ptr) 00192 { 00193 uint8_t length = *descr_ptr; 00194 uint8_t i; 00195 00196 printf("%s", Unk_Header_str); 00197 printf("%s0x%.8x", Unk_Length_str, *descr_ptr); 00198 printf("%s0x%.8x", Unk_Type_str, *(descr_ptr + 1 )); 00199 printf("%s", Unk_Contents_str); 00200 descr_ptr += 2; 00201 for (i = 0; i < length; i++) { 00202 printf("0x%.8x", *descr_ptr); 00203 descr_ptr++; 00204 } 00205 } 00206 00207 /** 00208 * @brief 00209 * @note 00210 * @param 00211 * @retval 00212 */ 00213 uint8_t getConfDescr(uint8_t addr, uint8_t conf) 00214 { 00215 uint8_t buf[BUFSIZE]; 00216 uint8_t* buf_ptr = buf; 00217 uint8_t rcode; 00218 uint8_t descr_length; 00219 uint8_t descr_type; 00220 uint16_t total_length; 00221 00222 rcode = usb.getConfDescr(addr, 0, 4, conf, buf); //get total length 00223 LOBYTE(total_length) = buf[2]; 00224 HIBYTE(total_length) = buf[3]; 00225 if (total_length > 256) { 00226 00227 //check if total length is larger than buffer 00228 printf("%s", Conf_Trunc_str); 00229 total_length = 256; 00230 } 00231 00232 rcode = usb.getConfDescr(addr, 0, total_length, conf, buf); //get the whole descriptor 00233 while (buf_ptr < buf + total_length) { 00234 00235 //parsing descriptors 00236 descr_length = *(buf_ptr); 00237 descr_type = *(buf_ptr + 1); 00238 switch (descr_type) { 00239 case (USB_DESCRIPTOR_CONFIGURATION): 00240 printConfDescr(buf_ptr); 00241 break; 00242 00243 case (USB_DESCRIPTOR_INTERFACE): 00244 printIntfDescr(buf_ptr); 00245 break; 00246 00247 case (USB_DESCRIPTOR_ENDPOINT): 00248 printEpDescr(buf_ptr); 00249 break; 00250 00251 case 0x29: 00252 printHubDescr(buf_ptr, addr); 00253 break; 00254 00255 default: 00256 printUnknownDescr(buf_ptr); 00257 break; 00258 } //switch( descr_type 00259 00260 buf_ptr = (buf_ptr + descr_length); //advance buffer pointer 00261 } //while( buf_ptr <=... 00262 00263 return(rcode); 00264 } 00265 00266 00267 /** 00268 * @brief 00269 * @note 00270 * @param 00271 * @retval 00272 */ 00273 int main() 00274 { 00275 printf("Starting..\r\n"); 00276 00277 if (usb.init() == -1) 00278 printf("OSC did not start.\r\n"); 00279 00280 wait_ms(200); 00281 00282 while (true) { 00283 usb.task(); 00284 00285 if (usb.getUsbTaskState() == USB_STATE_RUNNING) { 00286 usb.ForEachUsbDevice(&printAllDescriptors); 00287 usb.ForEachUsbDevice(&printAllAddresses); 00288 00289 while (1) {} 00290 } 00291 } 00292 }
Generated on Thu Jul 14 2022 12:16:23 by
 1.7.2
 1.7.2