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.
USBDevice.cpp
00001 /* USBDevice.c */ 00002 /* Generic USB device */ 00003 /* Copyright (c) 2011 ARM Limited. All rights reserved. */ 00004 00005 /* Reference: */ 00006 /* Universal Serial Bus Specification Revision 2.0, Chapter 9 "USB Device Framework" */ 00007 00008 #include "stdint.h" 00009 00010 #include "USBEndpoints.h" 00011 #include "USBDevice.h" 00012 #include "USBDescriptor.h" 00013 #include "USBHID_Types.h" 00014 00015 00016 /* Device status */ 00017 #define DEVICE_STATUS_SELF_POWERED (1U<<0) 00018 #define DEVICE_STATUS_REMOTE_WAKEUP (1U<<1) 00019 00020 /* Endpoint status */ 00021 #define ENDPOINT_STATUS_HALT (1U<<0) 00022 00023 /* Standard feature selectors */ 00024 #define DEVICE_REMOTE_WAKEUP (1) 00025 #define ENDPOINT_HALT (0) 00026 00027 /* Macro to convert wIndex endpoint number to physical endpoint number */ 00028 #define WINDEX_TO_PHYSICAL(endpoint) (((endpoint & 0x0f) << 1) + \ 00029 ((endpoint & 0x80) ? 1 : 0)) 00030 00031 00032 bool USBDevice::requestGetDescriptor(void) 00033 { 00034 bool success = false; 00035 00036 switch (DESCRIPTOR_TYPE(transfer.setup.wValue)) 00037 { 00038 case DEVICE_DESCRIPTOR: 00039 if (deviceDesc() != NULL) 00040 { 00041 if ((deviceDesc()[0] == DEVICE_DESCRIPTOR_LENGTH) \ 00042 && (deviceDesc()[1] == DEVICE_DESCRIPTOR)) 00043 { 00044 transfer.remaining = DEVICE_DESCRIPTOR_LENGTH; 00045 transfer.ptr = deviceDesc(); 00046 transfer.direction = DEVICE_TO_HOST; 00047 success = true; 00048 } 00049 } 00050 break; 00051 case CONFIGURATION_DESCRIPTOR: 00052 if (configurationDesc() != NULL) 00053 { 00054 if ((configurationDesc()[0] == CONFIGURATION_DESCRIPTOR_LENGTH) \ 00055 && (configurationDesc()[1] == CONFIGURATION_DESCRIPTOR)) 00056 { 00057 /* Get wTotalLength */ 00058 transfer.remaining = configurationDesc()[2] \ 00059 | (configurationDesc()[3] << 8); 00060 00061 transfer.ptr = configurationDesc(); 00062 transfer.direction = DEVICE_TO_HOST; 00063 success = true; 00064 } 00065 } 00066 break; 00067 case STRING_DESCRIPTOR: 00068 switch (DESCRIPTOR_INDEX(transfer.setup.wValue)) 00069 { 00070 case STRING_OFFSET_LANGID: 00071 transfer.remaining = stringLangidDesc()[0]; 00072 transfer.ptr = stringLangidDesc(); 00073 transfer.direction = DEVICE_TO_HOST; 00074 success = true; 00075 break; 00076 case STRING_OFFSET_IMANUFACTURER: 00077 transfer.remaining = stringImanufacturerDesc()[0]; 00078 transfer.ptr = stringImanufacturerDesc(); 00079 transfer.direction = DEVICE_TO_HOST; 00080 success = true; 00081 break; 00082 case STRING_OFFSET_IPRODUCT: 00083 transfer.remaining = stringIproductDesc()[0]; 00084 transfer.ptr = stringIproductDesc(); 00085 transfer.direction = DEVICE_TO_HOST; 00086 success = true; 00087 break; 00088 case STRING_OFFSET_ISERIAL: 00089 transfer.remaining = stringIserialDesc()[0]; 00090 transfer.ptr = stringIserialDesc(); 00091 transfer.direction = DEVICE_TO_HOST; 00092 success = true; 00093 break; 00094 case STRING_OFFSET_ICONFIGURATION: 00095 transfer.remaining = stringIConfigurationDesc()[0]; 00096 transfer.ptr = stringIConfigurationDesc(); 00097 transfer.direction = DEVICE_TO_HOST; 00098 success = true; 00099 break; 00100 case STRING_OFFSET_IINTERFACE: 00101 transfer.remaining = stringIinterfaceDesc()[0]; 00102 transfer.ptr = stringIinterfaceDesc(); 00103 transfer.direction = DEVICE_TO_HOST; 00104 success = true; 00105 break; 00106 } 00107 break; 00108 case INTERFACE_DESCRIPTOR: 00109 case ENDPOINT_DESCRIPTOR: 00110 /* TODO: Support is optional, not implemented here */ 00111 break; 00112 default: 00113 break; 00114 } 00115 00116 return success; 00117 } 00118 00119 void USBDevice::decodeSetupPacket(uint8_t *data, SETUP_PACKET *packet) 00120 { 00121 /* Fill in the elements of a SETUP_PACKET structure from raw data */ 00122 packet->bmRequestType.dataTransferDirection = (data[0] & 0x80) >> 7; 00123 packet->bmRequestType.Type = (data[0] & 0x60) >> 5; 00124 packet->bmRequestType.Recipient = data[0] & 0x1f; 00125 packet->bRequest = data[1]; 00126 packet->wValue = (data[2] | (uint16_t)data[3] << 8); 00127 packet->wIndex = (data[4] | (uint16_t)data[5] << 8); 00128 packet->wLength = (data[6] | (uint16_t)data[7] << 8); 00129 } 00130 00131 00132 bool USBDevice::controlOut(void) 00133 { 00134 /* Control transfer data OUT stage */ 00135 uint8_t buffer[MAX_PACKET_SIZE_EP0]; 00136 uint32_t packetSize; 00137 00138 /* Check we should be transferring data OUT */ 00139 if (transfer.direction != HOST_TO_DEVICE) 00140 { 00141 return false; 00142 } 00143 00144 /* Read from endpoint */ 00145 packetSize = EP0getReadResult(buffer); 00146 00147 /* Check if transfer size is valid */ 00148 if (packetSize > transfer.remaining) 00149 { 00150 /* Too big */ 00151 return false; 00152 } 00153 00154 /* Update transfer */ 00155 transfer.ptr += packetSize; 00156 transfer.remaining -= packetSize; 00157 00158 /* Check if transfer has completed */ 00159 if (transfer.remaining == 0) 00160 { 00161 /* Transfer completed */ 00162 if (transfer.notify) 00163 { 00164 /* Notify class layer. */ 00165 USBCallback_requestCompleted(buffer, packetSize); 00166 transfer.notify = false; 00167 } 00168 /* Status stage */ 00169 EP0write(NULL, 0); 00170 } 00171 else 00172 { 00173 EP0read(); 00174 } 00175 00176 return true; 00177 } 00178 00179 bool USBDevice::controlIn(void) 00180 { 00181 /* Control transfer data IN stage */ 00182 uint32_t packetSize; 00183 00184 /* Check if transfer has completed (status stage transactions */ 00185 /* also have transfer.remaining == 0) */ 00186 if (transfer.remaining == 0) 00187 { 00188 if (transfer.zlp) 00189 { 00190 /* Send zero length packet */ 00191 EP0write(NULL, 0); 00192 transfer.zlp = false; 00193 } 00194 00195 /* Transfer completed */ 00196 if (transfer.notify) 00197 { 00198 /* Notify class layer. */ 00199 USBCallback_requestCompleted(NULL, 0); 00200 transfer.notify = false; 00201 } 00202 00203 EP0read(); 00204 00205 /* Completed */ 00206 return true; 00207 } 00208 00209 /* Check we should be transferring data IN */ 00210 if (transfer.direction != DEVICE_TO_HOST) 00211 { 00212 return false; 00213 } 00214 00215 packetSize = transfer.remaining; 00216 00217 if (packetSize > MAX_PACKET_SIZE_EP0) 00218 { 00219 packetSize = MAX_PACKET_SIZE_EP0; 00220 } 00221 00222 /* Write to endpoint */ 00223 EP0write(transfer.ptr, packetSize); 00224 00225 /* Update transfer */ 00226 transfer.ptr += packetSize; 00227 transfer.remaining -= packetSize; 00228 00229 return true; 00230 } 00231 00232 bool USBDevice::requestSetAddress(void) 00233 { 00234 /* Set the device address */ 00235 setAddress(transfer.setup.wValue); 00236 00237 if (transfer.setup.wValue == 0) 00238 { 00239 device.state = DEFAULT; 00240 } 00241 else 00242 { 00243 device.state = ADDRESS; 00244 } 00245 00246 return true; 00247 } 00248 00249 bool USBDevice::requestSetConfiguration(void) 00250 { 00251 00252 device.configuration = transfer.setup.wValue; 00253 /* Set the device configuration */ 00254 if (device.configuration == 0) 00255 { 00256 /* Not configured */ 00257 unconfigureDevice(); 00258 device.state = ADDRESS; 00259 } 00260 else 00261 { 00262 if (USBCallback_setConfiguration(device.configuration)) 00263 { 00264 /* Valid configuration */ 00265 configureDevice(); 00266 device.state = CONFIGURED; 00267 } 00268 else 00269 { 00270 return false; 00271 } 00272 } 00273 00274 return true; 00275 } 00276 00277 bool USBDevice::requestGetConfiguration(void) 00278 { 00279 /* Send the device configuration */ 00280 transfer.ptr = &device.configuration; 00281 transfer.remaining = sizeof(device.configuration); 00282 transfer.direction = DEVICE_TO_HOST; 00283 return true; 00284 } 00285 00286 bool USBDevice::requestGetInterface(void) 00287 { 00288 /* Return the selected alternate setting for an interface */ 00289 00290 if (device.state != CONFIGURED) 00291 { 00292 return false; 00293 } 00294 00295 /* Send the alternate setting */ 00296 transfer.setup.wIndex = currentInterface; 00297 transfer.ptr = ¤tAlternate; 00298 transfer.remaining = sizeof(currentAlternate); 00299 transfer.direction = DEVICE_TO_HOST; 00300 return true; 00301 } 00302 00303 bool USBDevice::requestSetInterface(void) 00304 { 00305 bool success = false; 00306 if(USBCallback_setInterface(transfer.setup.wIndex, transfer.setup.wValue)) 00307 { 00308 success = true; 00309 currentInterface = transfer.setup.wIndex; 00310 currentAlternate = transfer.setup.wValue; 00311 } 00312 return success; 00313 } 00314 00315 bool USBDevice::requestSetFeature() 00316 { 00317 bool success = false; 00318 00319 if (device.state != CONFIGURED) 00320 { 00321 /* Endpoint or interface must be zero */ 00322 if (transfer.setup.wIndex != 0) 00323 { 00324 return false; 00325 } 00326 } 00327 00328 switch (transfer.setup.bmRequestType.Recipient) 00329 { 00330 case DEVICE_RECIPIENT: 00331 /* TODO: Remote wakeup feature not supported */ 00332 break; 00333 case ENDPOINT_RECIPIENT: 00334 if (transfer.setup.wValue == ENDPOINT_HALT) 00335 { 00336 /* TODO: We should check that the endpoint number is valid */ 00337 stallEndpoint( 00338 WINDEX_TO_PHYSICAL(transfer.setup.wIndex)); 00339 success = true; 00340 } 00341 break; 00342 default: 00343 break; 00344 } 00345 00346 return success; 00347 } 00348 00349 bool USBDevice::requestClearFeature() 00350 { 00351 bool success = false; 00352 00353 if (device.state != CONFIGURED) 00354 { 00355 /* Endpoint or interface must be zero */ 00356 if (transfer.setup.wIndex != 0) 00357 { 00358 return false; 00359 } 00360 } 00361 00362 switch (transfer.setup.bmRequestType.Recipient) 00363 { 00364 case DEVICE_RECIPIENT: 00365 /* TODO: Remote wakeup feature not supported */ 00366 break; 00367 case ENDPOINT_RECIPIENT: 00368 /* TODO: We should check that the endpoint number is valid */ 00369 if (transfer.setup.wValue == ENDPOINT_HALT) 00370 { 00371 unstallEndpoint( WINDEX_TO_PHYSICAL(transfer.setup.wIndex)); 00372 success = true; 00373 } 00374 break; 00375 default: 00376 break; 00377 } 00378 00379 return success; 00380 } 00381 00382 bool USBDevice::requestGetStatus(void) 00383 { 00384 static uint16_t status; 00385 bool success = false; 00386 00387 if (device.state != CONFIGURED) 00388 { 00389 /* Endpoint or interface must be zero */ 00390 if (transfer.setup.wIndex != 0) 00391 { 00392 return false; 00393 } 00394 } 00395 00396 switch (transfer.setup.bmRequestType.Recipient) 00397 { 00398 case DEVICE_RECIPIENT: 00399 /* TODO: Currently only supports self powered devices */ 00400 status = DEVICE_STATUS_SELF_POWERED; 00401 success = true; 00402 break; 00403 case INTERFACE_RECIPIENT: 00404 status = 0; 00405 success = true; 00406 break; 00407 case ENDPOINT_RECIPIENT: 00408 /* TODO: We should check that the endpoint number is valid */ 00409 if (getEndpointStallState( 00410 WINDEX_TO_PHYSICAL(transfer.setup.wIndex))) 00411 { 00412 status = ENDPOINT_STATUS_HALT; 00413 } 00414 else 00415 { 00416 status = 0; 00417 } 00418 success = true; 00419 break; 00420 default: 00421 break; 00422 } 00423 00424 if (success) 00425 { 00426 /* Send the status */ 00427 transfer.ptr = (uint8_t *)&status; /* Assumes little endian */ 00428 transfer.remaining = sizeof(status); 00429 transfer.direction = DEVICE_TO_HOST; 00430 } 00431 00432 return success; 00433 } 00434 00435 bool USBDevice::requestSetup(void) 00436 { 00437 bool success = false; 00438 00439 /* Process standard requests */ 00440 if ((transfer.setup.bmRequestType.Type == STANDARD_TYPE)) 00441 { 00442 switch (transfer.setup.bRequest) 00443 { 00444 case GET_STATUS: 00445 success = requestGetStatus(); 00446 break; 00447 case CLEAR_FEATURE: 00448 success = requestClearFeature(); 00449 break; 00450 case SET_FEATURE: 00451 success = requestSetFeature(); 00452 break; 00453 case SET_ADDRESS: 00454 success = requestSetAddress(); 00455 break; 00456 case GET_DESCRIPTOR: 00457 success = requestGetDescriptor(); 00458 break; 00459 case SET_DESCRIPTOR: 00460 /* TODO: Support is optional, not implemented here */ 00461 success = false; 00462 break; 00463 case GET_CONFIGURATION: 00464 success = requestGetConfiguration(); 00465 break; 00466 case SET_CONFIGURATION: 00467 success = requestSetConfiguration(); 00468 break; 00469 case GET_INTERFACE: 00470 success = requestGetInterface(); 00471 break; 00472 case SET_INTERFACE: 00473 success = requestSetInterface(); 00474 break; 00475 default: 00476 break; 00477 } 00478 } 00479 00480 return success; 00481 } 00482 00483 bool USBDevice::controlSetup(void) 00484 { 00485 bool success = false; 00486 00487 /* Control transfer setup stage */ 00488 uint8_t buffer[MAX_PACKET_SIZE_EP0]; 00489 00490 EP0setup(buffer); 00491 00492 /* Initialise control transfer state */ 00493 decodeSetupPacket(buffer, &transfer.setup); 00494 transfer.ptr = NULL; 00495 transfer.remaining = 0; 00496 transfer.direction = 0; 00497 transfer.zlp = false; 00498 transfer.notify = false; 00499 00500 /* Class / vendor specific */ 00501 success = USBCallback_request(); 00502 00503 if (!success) 00504 { 00505 /* Standard requests */ 00506 if (!requestSetup()) 00507 { 00508 return false; 00509 } 00510 } 00511 00512 /* Check transfer size and direction */ 00513 if (transfer.setup.wLength>0) 00514 { 00515 if (transfer.setup.bmRequestType.dataTransferDirection \ 00516 == DEVICE_TO_HOST) 00517 { 00518 /* IN data stage is required */ 00519 if (transfer.direction != DEVICE_TO_HOST) 00520 { 00521 return false; 00522 } 00523 00524 /* Transfer must be less than or equal to the size */ 00525 /* requested by the host */ 00526 if (transfer.remaining > transfer.setup.wLength) 00527 { 00528 transfer.remaining = transfer.setup.wLength; 00529 } 00530 } 00531 else 00532 { 00533 00534 /* OUT data stage is required */ 00535 if (transfer.direction != HOST_TO_DEVICE) 00536 { 00537 return false; 00538 } 00539 00540 /* Transfer must be equal to the size requested by the host */ 00541 if (transfer.remaining != transfer.setup.wLength) 00542 { 00543 return false; 00544 } 00545 } 00546 } 00547 else 00548 { 00549 /* No data stage; transfer size must be zero */ 00550 if (transfer.remaining != 0) 00551 { 00552 return false; 00553 } 00554 } 00555 00556 /* Data or status stage if applicable */ 00557 if (transfer.setup.wLength>0) 00558 { 00559 if (transfer.setup.bmRequestType.dataTransferDirection \ 00560 == DEVICE_TO_HOST) 00561 { 00562 /* Check if we'll need to send a zero length packet at */ 00563 /* the end of this transfer */ 00564 if (transfer.setup.wLength > transfer.remaining) 00565 { 00566 /* Device wishes to transfer less than host requested */ 00567 if ((transfer.remaining % MAX_PACKET_SIZE_EP0) == 0) 00568 { 00569 /* Transfer is a multiple of EP0 max packet size */ 00570 transfer.zlp = true; 00571 } 00572 } 00573 00574 /* IN stage */ 00575 controlIn(); 00576 } 00577 else 00578 { 00579 /* OUT stage */ 00580 EP0read(); 00581 } 00582 } 00583 else 00584 { 00585 /* Status stage */ 00586 EP0write(NULL, 0); 00587 } 00588 00589 return true; 00590 } 00591 00592 void USBDevice::busReset(void) 00593 { 00594 device.state = DEFAULT; 00595 device.configuration = 0; 00596 device.suspended = false; 00597 00598 /* Call class / vendor specific busReset function */ 00599 USBCallback_busReset(); 00600 } 00601 00602 void USBDevice::EP0setupCallback(void) 00603 { 00604 /* Endpoint 0 setup event */ 00605 if (!controlSetup()) 00606 { 00607 /* Protocol stall */ 00608 EP0stall(); 00609 } 00610 00611 /* Return true if an OUT data stage is expected */ 00612 } 00613 00614 void USBDevice::EP0out(void) 00615 { 00616 /* Endpoint 0 OUT data event */ 00617 if (!controlOut()) 00618 { 00619 /* Protocol stall; this will stall both endpoints */ 00620 EP0stall(); 00621 } 00622 } 00623 00624 void USBDevice::EP0in(void) 00625 { 00626 /* Endpoint 0 IN data event */ 00627 if (!controlIn()) 00628 { 00629 /* Protocol stall; this will stall both endpoints */ 00630 EP0stall(); 00631 } 00632 } 00633 00634 bool USBDevice::configured(void) 00635 { 00636 /* Returns true if device is in the CONFIGURED state */ 00637 return (device.state == CONFIGURED); 00638 } 00639 00640 void USBDevice::connect(void) 00641 { 00642 /* Connect device */ 00643 USBHAL::connect(); 00644 /* Block if not configured */ 00645 while (!configured()); 00646 } 00647 00648 void USBDevice::disconnect(void) 00649 { 00650 /* Disconnect device */ 00651 USBHAL::disconnect(); 00652 } 00653 00654 CONTROL_TRANSFER * USBDevice::getTransferPtr(void) 00655 { 00656 return &transfer; 00657 } 00658 00659 bool USBDevice::addEndpoint(uint8_t endpoint, uint32_t maxPacket) 00660 { 00661 return realiseEndpoint(endpoint, maxPacket, 0); 00662 } 00663 00664 bool USBDevice::addRateFeedbackEndpoint(uint8_t endpoint, uint32_t maxPacket) 00665 { 00666 /* For interrupt endpoints only */ 00667 return realiseEndpoint(endpoint, maxPacket, RATE_FEEDBACK_MODE); 00668 } 00669 00670 uint8_t * USBDevice::findDescriptor(uint8_t descriptorType) 00671 { 00672 /* Find a descriptor within the list of descriptors */ 00673 /* following a configuration descriptor. */ 00674 uint16_t wTotalLength; 00675 uint8_t *ptr; 00676 00677 if (configurationDesc() == NULL) 00678 { 00679 return NULL; 00680 } 00681 00682 /* Check this is a configuration descriptor */ 00683 if ((configurationDesc()[0] != CONFIGURATION_DESCRIPTOR_LENGTH) \ 00684 || (configurationDesc()[1] != CONFIGURATION_DESCRIPTOR)) 00685 { 00686 return NULL; 00687 } 00688 00689 wTotalLength = configurationDesc()[2] | (configurationDesc()[3] << 8); 00690 00691 /* Check there are some more descriptors to follow */ 00692 if (wTotalLength <= (CONFIGURATION_DESCRIPTOR_LENGTH+2)) 00693 /* +2 is for bLength and bDescriptorType of next descriptor */ 00694 { 00695 return false; 00696 } 00697 00698 /* Start at first descriptor after the configuration descriptor */ 00699 ptr = &(configurationDesc()[CONFIGURATION_DESCRIPTOR_LENGTH]); 00700 00701 do { 00702 if (ptr[1] /* bDescriptorType */ == descriptorType) 00703 { 00704 /* Found */ 00705 return ptr; 00706 } 00707 00708 /* Skip to next descriptor */ 00709 ptr += ptr[0]; /* bLength */ 00710 } while (ptr < (configurationDesc() + wTotalLength)); 00711 00712 /* Reached end of the descriptors - not found */ 00713 return NULL; 00714 } 00715 00716 00717 void USBDevice::connectStateChanged(unsigned int connected) 00718 { 00719 } 00720 00721 void USBDevice::suspendStateChanged(unsigned int suspended) 00722 { 00723 } 00724 00725 00726 USBDevice::USBDevice(uint16_t vendor_id, uint16_t product_id, uint16_t product_release){ 00727 VENDOR_ID = vendor_id; 00728 PRODUCT_ID = product_id; 00729 PRODUCT_RELEASE = product_release; 00730 00731 /* Set initial device state */ 00732 device.state = POWERED; 00733 device.configuration = 0; 00734 device.suspended = false; 00735 }; 00736 00737 00738 bool USBDevice::readStart(uint8_t endpoint, uint16_t maxSize) 00739 { 00740 return endpointRead(endpoint, maxSize) == EP_PENDING; 00741 } 00742 00743 00744 bool USBDevice::write(uint8_t endpoint, uint8_t * buffer, uint16_t size, uint16_t maxSize) 00745 { 00746 EP_STATUS result; 00747 00748 if (size > maxSize) 00749 { 00750 return false; 00751 } 00752 00753 00754 if(!configured()) { 00755 return false; 00756 } 00757 00758 /* Send report */ 00759 result = endpointWrite(endpoint, buffer, size); 00760 00761 if (result != EP_PENDING) 00762 { 00763 return false; 00764 } 00765 00766 /* Wait for completion */ 00767 do { 00768 result = endpointWriteResult(endpoint); 00769 } while ((result == EP_PENDING) && configured()); 00770 00771 return (result == EP_COMPLETED); 00772 } 00773 00774 00775 bool USBDevice::writeNB(uint8_t endpoint, uint8_t * buffer, uint16_t size, uint16_t maxSize) 00776 { 00777 EP_STATUS result; 00778 00779 if (size > maxSize) 00780 { 00781 return false; 00782 } 00783 00784 if(!configured()) { 00785 return false; 00786 } 00787 00788 /* Send report */ 00789 result = endpointWrite(endpoint, buffer, size); 00790 00791 if (result != EP_PENDING) 00792 { 00793 return false; 00794 } 00795 00796 result = endpointWriteResult(endpoint); 00797 00798 return (result == EP_COMPLETED); 00799 } 00800 00801 00802 00803 bool USBDevice::readEP(uint8_t endpoint, uint8_t * buffer, uint16_t * size, uint16_t maxSize) 00804 { 00805 EP_STATUS result; 00806 00807 if(!configured()) { 00808 return false; 00809 } 00810 00811 /* Wait for completion */ 00812 do { 00813 result = endpointReadResult(endpoint, buffer, (uint32_t *)size); 00814 } while ((result == EP_PENDING) && configured()); 00815 00816 return (result == EP_COMPLETED); 00817 } 00818 00819 00820 bool USBDevice::readEP_NB(uint8_t endpoint, uint8_t * buffer, uint16_t * size, uint16_t maxSize) 00821 { 00822 EP_STATUS result; 00823 00824 if(!configured()) { 00825 return false; 00826 } 00827 00828 result = endpointReadResult(endpoint, buffer, (uint32_t *)size); 00829 00830 return (result == EP_COMPLETED); 00831 } 00832 00833 00834 00835 uint8_t * USBDevice::deviceDesc() { 00836 static uint8_t deviceDescriptor[] = { 00837 DEVICE_DESCRIPTOR_LENGTH, /* bLength */ 00838 DEVICE_DESCRIPTOR, /* bDescriptorType */ 00839 LSB(USB_VERSION_2_0), /* bcdUSB (LSB) */ 00840 MSB(USB_VERSION_2_0), /* bcdUSB (MSB) */ 00841 0x00, /* bDeviceClass */ 00842 0x00, /* bDeviceSubClass */ 00843 0x00, /* bDeviceprotocol */ 00844 MAX_PACKET_SIZE_EP0, /* bMaxPacketSize0 */ 00845 LSB(VENDOR_ID), /* idVendor (LSB) */ 00846 MSB(VENDOR_ID), /* idVendor (MSB) */ 00847 LSB(PRODUCT_ID), /* idProduct (LSB) */ 00848 MSB(PRODUCT_ID), /* idProduct (MSB) */ 00849 LSB(PRODUCT_RELEASE), /* bcdDevice (LSB) */ 00850 MSB(PRODUCT_RELEASE), /* bcdDevice (MSB) */ 00851 STRING_OFFSET_IMANUFACTURER, /* iManufacturer */ 00852 STRING_OFFSET_IPRODUCT, /* iProduct */ 00853 STRING_OFFSET_ISERIAL, /* iSerialNumber */ 00854 0x01 /* bNumConfigurations */ 00855 }; 00856 return deviceDescriptor; 00857 } 00858 00859 uint8_t * USBDevice::stringLangidDesc() { 00860 static uint8_t stringLangidDescriptor[] = { 00861 0x04, /*bLength*/ 00862 STRING_DESCRIPTOR, /*bDescriptorType 0x03*/ 00863 0x09,0x00, /*bString Lang ID - 0x009 - English*/ 00864 }; 00865 return stringLangidDescriptor; 00866 } 00867 00868 uint8_t * USBDevice::stringImanufacturerDesc() { 00869 static uint8_t stringImanufacturerDescriptor[] = { 00870 0x12, /*bLength*/ 00871 STRING_DESCRIPTOR, /*bDescriptorType 0x03*/ 00872 'm',0,'b',0,'e',0,'d',0,'.',0,'o',0,'r',0,'g',0, /*bString iManufacturer - mbed.org*/ 00873 }; 00874 return stringImanufacturerDescriptor; 00875 } 00876 00877 uint8_t * USBDevice::stringIserialDesc() { 00878 static uint8_t stringIserialDescriptor[] = { 00879 0x16, /*bLength*/ 00880 STRING_DESCRIPTOR, /*bDescriptorType 0x03*/ 00881 '0',0,'1',0,'2',0,'3',0,'4',0,'5',0,'6',0,'7',0,'8',0,'9',0, /*bString iSerial - 0123456789*/ 00882 }; 00883 return stringIserialDescriptor; 00884 } 00885 00886 uint8_t * USBDevice::stringIConfigurationDesc() { 00887 static uint8_t stringIconfigurationDescriptor[] = { 00888 0x06, /*bLength*/ 00889 STRING_DESCRIPTOR, /*bDescriptorType 0x03*/ 00890 '0',0,'1',0, /*bString iConfiguration - 01*/ 00891 }; 00892 return stringIconfigurationDescriptor; 00893 } 00894 00895 uint8_t * USBDevice::stringIinterfaceDesc() { 00896 static uint8_t stringIinterfaceDescriptor[] = { 00897 0x08, /*bLength*/ 00898 STRING_DESCRIPTOR, /*bDescriptorType 0x03*/ 00899 'U',0,'S',0,'B',0, /*bString iInterface - USB*/ 00900 }; 00901 return stringIinterfaceDescriptor; 00902 } 00903 00904 uint8_t * USBDevice::stringIproductDesc() { 00905 static uint8_t stringIproductDescriptor[] = { 00906 0x16, /*bLength*/ 00907 STRING_DESCRIPTOR, /*bDescriptorType 0x03*/ 00908 'U',0,'S',0,'B',0,' ',0,'D',0,'E',0,'V',0,'I',0,'C',0,'E',0 /*bString iProduct - USB DEVICE*/ 00909 }; 00910 return stringIproductDescriptor; 00911 }
Generated on Wed Jul 13 2022 13:59:01 by
1.7.2