usbd_desc.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. */
  4. /**
  5. ******************************************************************************
  6. * @file USB_Device/CDC_Standalone/Src/usbd_desc.c
  7. * @author MCD Application Team
  8. * @version V1.0.1
  9. * @date 26-February-2014
  10. * @brief This file provides the USBD descriptors and string formating method.
  11. ******************************************************************************
  12. * @attention
  13. *
  14. * <h2><center>&copy; COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
  15. *
  16. * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
  17. * You may not use this file except in compliance with the License.
  18. * You may obtain a copy of the License at:
  19. *
  20. * http://www.st.com/software_license_agreement_liberty_v2
  21. *
  22. * Unless required by applicable law or agreed to in writing, software
  23. * distributed under the License is distributed on an "AS IS" BASIS,
  24. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  25. * See the License for the specific language governing permissions and
  26. * limitations under the License.
  27. *
  28. ******************************************************************************
  29. */
  30. #include "usbd_core.h"
  31. #include "usbd_desc.h"
  32. #include "usbd_conf.h"
  33. // need this header just for MP_HAL_UNIQUE_ID_ADDRESS
  34. #include "py/mphal.h"
  35. // So we don't clash with existing ST boards, we use the unofficial FOSS VID.
  36. // This needs a proper solution.
  37. #define USBD_VID 0xf055
  38. #define USBD_PID 0x9800
  39. #define USBD_LANGID_STRING 0x409
  40. #define USBD_MANUFACTURER_STRING "MicroPython"
  41. #define USBD_PRODUCT_HS_STRING "Pyboard Virtual Comm Port in HS Mode"
  42. #define USBD_PRODUCT_FS_STRING "Pyboard Virtual Comm Port in FS Mode"
  43. #define USBD_CONFIGURATION_HS_STRING "Pyboard Config"
  44. #define USBD_INTERFACE_HS_STRING "Pyboard Interface"
  45. #define USBD_CONFIGURATION_FS_STRING "Pyboard Config"
  46. #define USBD_INTERFACE_FS_STRING "Pyboard Interface"
  47. __ALIGN_BEGIN static const uint8_t USBD_LangIDDesc[USB_LEN_LANGID_STR_DESC] __ALIGN_END = {
  48. USB_LEN_LANGID_STR_DESC,
  49. USB_DESC_TYPE_STRING,
  50. LOBYTE(USBD_LANGID_STRING),
  51. HIBYTE(USBD_LANGID_STRING),
  52. };
  53. // set the VID, PID and device release number
  54. void USBD_SetVIDPIDRelease(usbd_cdc_msc_hid_state_t *usbd, uint16_t vid, uint16_t pid, uint16_t device_release_num, int cdc_only) {
  55. uint8_t *dev_desc = &usbd->usbd_device_desc[0];
  56. dev_desc[0] = USB_LEN_DEV_DESC; // bLength
  57. dev_desc[1] = USB_DESC_TYPE_DEVICE; // bDescriptorType
  58. dev_desc[2] = 0x00; // bcdUSB
  59. dev_desc[3] = 0x02; // bcdUSB
  60. if (cdc_only) {
  61. // Make it look like a Communications device if we're only
  62. // using CDC. Otherwise, windows gets confused when we tell it that
  63. // its a composite device with only a cdc serial interface.
  64. dev_desc[4] = 0x02; // bDeviceClass
  65. dev_desc[5] = 0x00; // bDeviceSubClass
  66. dev_desc[6] = 0x00; // bDeviceProtocol
  67. } else {
  68. // For the other modes, we make this look like a composite device.
  69. dev_desc[4] = 0xef; // bDeviceClass: Miscellaneous Device Class
  70. dev_desc[5] = 0x02; // bDeviceSubClass: Common Class
  71. dev_desc[6] = 0x01; // bDeviceProtocol: Interface Association Descriptor
  72. }
  73. dev_desc[7] = USB_MAX_EP0_SIZE; // bMaxPacketSize
  74. dev_desc[8] = LOBYTE(vid); // idVendor
  75. dev_desc[9] = HIBYTE(vid); // idVendor
  76. dev_desc[10] = LOBYTE(pid); // idVendor
  77. dev_desc[11] = HIBYTE(pid); // idVendor
  78. dev_desc[12] = LOBYTE(device_release_num); // bcdDevice
  79. dev_desc[13] = HIBYTE(device_release_num); // bcdDevice
  80. dev_desc[14] = USBD_IDX_MFC_STR; // Index of manufacturer string
  81. dev_desc[15] = USBD_IDX_PRODUCT_STR; // Index of product string
  82. dev_desc[16] = USBD_IDX_SERIAL_STR; // Index of serial number string
  83. dev_desc[17] = USBD_MAX_NUM_CONFIGURATION; // bNumConfigurations
  84. }
  85. /**
  86. * @brief Returns the device descriptor.
  87. * @param speed: Current device speed
  88. * @param length: Pointer to data length variable
  89. * @retval Pointer to descriptor buffer
  90. */
  91. STATIC uint8_t *USBD_DeviceDescriptor(USBD_HandleTypeDef *pdev, uint16_t *length) {
  92. uint8_t *dev_desc = ((usbd_cdc_msc_hid_state_t*)pdev->pClassData)->usbd_device_desc;
  93. *length = USB_LEN_DEV_DESC;
  94. return dev_desc;
  95. }
  96. /**
  97. * @brief Returns a string descriptor
  98. * @param idx: Index of the string descriptor to retrieve
  99. * @param length: Pointer to data length variable
  100. * @retval Pointer to descriptor buffer, or NULL if idx is invalid
  101. */
  102. STATIC uint8_t *USBD_StrDescriptor(USBD_HandleTypeDef *pdev, uint8_t idx, uint16_t *length) {
  103. char str_buf[16];
  104. const char *str = NULL;
  105. switch (idx) {
  106. case USBD_IDX_LANGID_STR:
  107. *length = sizeof(USBD_LangIDDesc);
  108. return (uint8_t*)USBD_LangIDDesc; // the data should only be read from this buf
  109. case USBD_IDX_MFC_STR:
  110. str = USBD_MANUFACTURER_STRING;
  111. break;
  112. case USBD_IDX_PRODUCT_STR:
  113. if (pdev->dev_speed == USBD_SPEED_HIGH) {
  114. str = USBD_PRODUCT_HS_STRING;
  115. } else {
  116. str = USBD_PRODUCT_FS_STRING;
  117. }
  118. break;
  119. case USBD_IDX_SERIAL_STR: {
  120. // This document: http://www.usb.org/developers/docs/devclass_docs/usbmassbulk_10.pdf
  121. // says that the serial number has to be at least 12 digits long and that
  122. // the last 12 digits need to be unique. It also stipulates that the valid
  123. // character set is that of upper-case hexadecimal digits.
  124. //
  125. // The onboard DFU bootloader produces a 12-digit serial number based on
  126. // the 96-bit unique ID, so for consistency we go with this algorithm.
  127. // You can see the serial number if you use: lsusb -v
  128. //
  129. // See: https://my.st.com/52d187b7 for the algorithim used.
  130. uint8_t *id = (uint8_t *)MP_HAL_UNIQUE_ID_ADDRESS;
  131. snprintf(str_buf, sizeof(str_buf),
  132. "%02X%02X%02X%02X%02X%02X",
  133. id[11], id[10] + id[2], id[9], id[8] + id[0], id[7], id[6]);
  134. str = str_buf;
  135. break;
  136. }
  137. case USBD_IDX_CONFIG_STR:
  138. if (pdev->dev_speed == USBD_SPEED_HIGH) {
  139. str = USBD_CONFIGURATION_HS_STRING;
  140. } else {
  141. str = USBD_CONFIGURATION_FS_STRING;
  142. }
  143. break;
  144. case USBD_IDX_INTERFACE_STR:
  145. if (pdev->dev_speed == USBD_SPEED_HIGH) {
  146. str = USBD_INTERFACE_HS_STRING;
  147. } else {
  148. str = USBD_INTERFACE_FS_STRING;
  149. }
  150. break;
  151. default:
  152. // invalid string index
  153. return NULL;
  154. }
  155. uint8_t *str_desc = ((usbd_cdc_msc_hid_state_t*)pdev->pClassData)->usbd_str_desc;
  156. USBD_GetString((uint8_t*)str, str_desc, length);
  157. return str_desc;
  158. }
  159. const USBD_DescriptorsTypeDef USBD_Descriptors = {
  160. USBD_DeviceDescriptor,
  161. USBD_StrDescriptor,
  162. };
  163. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/