usbd_desc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. /* USER CODE BEGIN Header */
  2. /**
  3. ******************************************************************************
  4. * @file : App/usbd_desc.c
  5. * @version : v1.0_Cube
  6. * @brief : This file implements the USB device descriptors.
  7. ******************************************************************************
  8. * @attention
  9. *
  10. * Copyright (c) 2022 STMicroelectronics.
  11. * All rights reserved.
  12. *
  13. * This software is licensed under terms that can be found in the LICENSE file
  14. * in the root directory of this software component.
  15. * If no LICENSE file comes with this software, it is provided AS-IS.
  16. *
  17. ******************************************************************************
  18. */
  19. /* USER CODE END Header */
  20. /* Includes ------------------------------------------------------------------*/
  21. #include "usbd_core.h"
  22. #include "usbd_desc.h"
  23. #include "usbd_conf.h"
  24. /* USER CODE BEGIN INCLUDE */
  25. /* USER CODE END INCLUDE */
  26. /* Private typedef -----------------------------------------------------------*/
  27. /* Private define ------------------------------------------------------------*/
  28. /* Private macro -------------------------------------------------------------*/
  29. /* USER CODE BEGIN PV */
  30. /* Private variables ---------------------------------------------------------*/
  31. /* USER CODE END PV */
  32. /** @addtogroup STM32_USB_OTG_DEVICE_LIBRARY
  33. * @{
  34. */
  35. /** @addtogroup USBD_DESC
  36. * @{
  37. */
  38. /** @defgroup USBD_DESC_Private_TypesDefinitions USBD_DESC_Private_TypesDefinitions
  39. * @brief Private types.
  40. * @{
  41. */
  42. /* USER CODE BEGIN PRIVATE_TYPES */
  43. /* USER CODE END PRIVATE_TYPES */
  44. /**
  45. * @}
  46. */
  47. /** @defgroup USBD_DESC_Private_Defines USBD_DESC_Private_Defines
  48. * @brief Private defines.
  49. * @{
  50. */
  51. #define USBD_VID 1155
  52. #define USBD_LANGID_STRING 1033
  53. #define USBD_MANUFACTURER_STRING "STMicroelectronics"
  54. #define USBD_PID_HS 22336
  55. #define USBD_PRODUCT_STRING_HS "STM32 Virtual ComPort"
  56. #define USBD_CONFIGURATION_STRING_HS "CDC Config"
  57. #define USBD_INTERFACE_STRING_HS "CDC Interface"
  58. /* USER CODE BEGIN PRIVATE_DEFINES */
  59. /* USER CODE END PRIVATE_DEFINES */
  60. /**
  61. * @}
  62. */
  63. /* USER CODE BEGIN 0 */
  64. /* USER CODE END 0 */
  65. /** @defgroup USBD_DESC_Private_Macros USBD_DESC_Private_Macros
  66. * @brief Private macros.
  67. * @{
  68. */
  69. /* USER CODE BEGIN PRIVATE_MACRO */
  70. /* USER CODE END PRIVATE_MACRO */
  71. /**
  72. * @}
  73. */
  74. /** @defgroup USBD_DESC_Private_FunctionPrototypes USBD_DESC_Private_FunctionPrototypes
  75. * @brief Private functions declaration.
  76. * @{
  77. */
  78. static void Get_SerialNum(void);
  79. static void IntToUnicode(uint32_t value, uint8_t * pbuf, uint8_t len);
  80. /**
  81. * @}
  82. */
  83. /** @defgroup USBD_DESC_Private_FunctionPrototypes USBD_DESC_Private_FunctionPrototypes
  84. * @brief Private functions declaration for HS.
  85. * @{
  86. */
  87. uint8_t * USBD_HS_DeviceDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
  88. uint8_t * USBD_HS_LangIDStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
  89. uint8_t * USBD_HS_ManufacturerStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
  90. uint8_t * USBD_HS_ProductStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
  91. uint8_t * USBD_HS_SerialStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
  92. uint8_t * USBD_HS_ConfigStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
  93. uint8_t * USBD_HS_InterfaceStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
  94. /**
  95. * @}
  96. */
  97. /** @defgroup USBD_DESC_Private_Variables USBD_DESC_Private_Variables
  98. * @brief Private variables.
  99. * @{
  100. */
  101. USBD_DescriptorsTypeDef HS_Desc =
  102. {
  103. USBD_HS_DeviceDescriptor
  104. , USBD_HS_LangIDStrDescriptor
  105. , USBD_HS_ManufacturerStrDescriptor
  106. , USBD_HS_ProductStrDescriptor
  107. , USBD_HS_SerialStrDescriptor
  108. , USBD_HS_ConfigStrDescriptor
  109. , USBD_HS_InterfaceStrDescriptor
  110. };
  111. #if defined ( __ICCARM__ ) /* IAR Compiler */
  112. #pragma data_alignment=4
  113. #endif /* defined ( __ICCARM__ ) */
  114. /** USB standard device descriptor. */
  115. __ALIGN_BEGIN uint8_t USBD_HS_DeviceDesc[USB_LEN_DEV_DESC] __ALIGN_END =
  116. {
  117. 0x12, /*bLength */
  118. USB_DESC_TYPE_DEVICE, /*bDescriptorType*/
  119. 0x00, /*bcdUSB */
  120. 0x02,
  121. 0x02, /*bDeviceClass*/
  122. 0x02, /*bDeviceSubClass*/
  123. 0x00, /*bDeviceProtocol*/
  124. USB_MAX_EP0_SIZE, /*bMaxPacketSize*/
  125. LOBYTE(USBD_VID), /*idVendor*/
  126. HIBYTE(USBD_VID), /*idVendor*/
  127. LOBYTE(USBD_PID_HS), /*idProduct*/
  128. HIBYTE(USBD_PID_HS), /*idProduct*/
  129. 0x00, /*bcdDevice rel. 2.00*/
  130. 0x02,
  131. USBD_IDX_MFC_STR, /*Index of manufacturer string*/
  132. USBD_IDX_PRODUCT_STR, /*Index of product string*/
  133. USBD_IDX_SERIAL_STR, /*Index of serial number string*/
  134. USBD_MAX_NUM_CONFIGURATION /*bNumConfigurations*/
  135. };
  136. /**
  137. * @}
  138. */
  139. /** @defgroup USBD_DESC_Private_Variables USBD_DESC_Private_Variables
  140. * @brief Private variables.
  141. * @{
  142. */
  143. #if defined ( __ICCARM__ ) /* IAR Compiler */
  144. #pragma data_alignment=4
  145. #endif /* defined ( __ICCARM__ ) */
  146. /** USB lang identifier descriptor. */
  147. __ALIGN_BEGIN uint8_t USBD_LangIDDesc[USB_LEN_LANGID_STR_DESC] __ALIGN_END =
  148. {
  149. USB_LEN_LANGID_STR_DESC,
  150. USB_DESC_TYPE_STRING,
  151. LOBYTE(USBD_LANGID_STRING),
  152. HIBYTE(USBD_LANGID_STRING)
  153. };
  154. #if defined ( __ICCARM__ ) /* IAR Compiler */
  155. #pragma data_alignment=4
  156. #endif /* defined ( __ICCARM__ ) */
  157. /* Internal string descriptor. */
  158. __ALIGN_BEGIN uint8_t USBD_StrDesc[USBD_MAX_STR_DESC_SIZ] __ALIGN_END;
  159. #if defined ( __ICCARM__ ) /*!< IAR Compiler */
  160. #pragma data_alignment=4
  161. #endif
  162. __ALIGN_BEGIN uint8_t USBD_StringSerial[USB_SIZ_STRING_SERIAL] __ALIGN_END = {
  163. USB_SIZ_STRING_SERIAL,
  164. USB_DESC_TYPE_STRING,
  165. };
  166. /**
  167. * @}
  168. */
  169. /** @defgroup USBD_DESC_Private_Functions USBD_DESC_Private_Functions
  170. * @brief Private functions.
  171. * @{
  172. */
  173. /**
  174. * @brief Return the device descriptor
  175. * @param speed : Current device speed
  176. * @param length : Pointer to data length variable
  177. * @retval Pointer to descriptor buffer
  178. */
  179. uint8_t * USBD_HS_DeviceDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
  180. {
  181. UNUSED(speed);
  182. *length = sizeof(USBD_HS_DeviceDesc);
  183. return USBD_HS_DeviceDesc;
  184. }
  185. /**
  186. * @brief Return the LangID string descriptor
  187. * @param speed : Current device speed
  188. * @param length : Pointer to data length variable
  189. * @retval Pointer to descriptor buffer
  190. */
  191. uint8_t * USBD_HS_LangIDStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
  192. {
  193. UNUSED(speed);
  194. *length = sizeof(USBD_LangIDDesc);
  195. return USBD_LangIDDesc;
  196. }
  197. /**
  198. * @brief Return the product string descriptor
  199. * @param speed : current device speed
  200. * @param length : pointer to data length variable
  201. * @retval pointer to descriptor buffer
  202. */
  203. uint8_t * USBD_HS_ProductStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
  204. {
  205. if(speed == 0)
  206. {
  207. USBD_GetString((uint8_t *)USBD_PRODUCT_STRING_HS, USBD_StrDesc, length);
  208. }
  209. else
  210. {
  211. USBD_GetString((uint8_t *)USBD_PRODUCT_STRING_HS, USBD_StrDesc, length);
  212. }
  213. return USBD_StrDesc;
  214. }
  215. /**
  216. * @brief Return the manufacturer string descriptor
  217. * @param speed : Current device speed
  218. * @param length : Pointer to data length variable
  219. * @retval Pointer to descriptor buffer
  220. */
  221. uint8_t * USBD_HS_ManufacturerStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
  222. {
  223. UNUSED(speed);
  224. USBD_GetString((uint8_t *)USBD_MANUFACTURER_STRING, USBD_StrDesc, length);
  225. return USBD_StrDesc;
  226. }
  227. /**
  228. * @brief Return the serial number string descriptor
  229. * @param speed : Current device speed
  230. * @param length : Pointer to data length variable
  231. * @retval Pointer to descriptor buffer
  232. */
  233. uint8_t * USBD_HS_SerialStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
  234. {
  235. UNUSED(speed);
  236. *length = USB_SIZ_STRING_SERIAL;
  237. /* Update the serial number string descriptor with the data from the unique
  238. * ID */
  239. Get_SerialNum();
  240. /* USER CODE BEGIN USBD_HS_SerialStrDescriptor */
  241. /* USER CODE END USBD_HS_SerialStrDescriptor */
  242. return (uint8_t *) USBD_StringSerial;
  243. }
  244. /**
  245. * @brief Return the configuration string descriptor
  246. * @param speed : Current device speed
  247. * @param length : Pointer to data length variable
  248. * @retval Pointer to descriptor buffer
  249. */
  250. uint8_t * USBD_HS_ConfigStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
  251. {
  252. if(speed == USBD_SPEED_HIGH)
  253. {
  254. USBD_GetString((uint8_t *)USBD_CONFIGURATION_STRING_HS, USBD_StrDesc, length);
  255. }
  256. else
  257. {
  258. USBD_GetString((uint8_t *)USBD_CONFIGURATION_STRING_HS, USBD_StrDesc, length);
  259. }
  260. return USBD_StrDesc;
  261. }
  262. /**
  263. * @brief Return the interface string descriptor
  264. * @param speed : Current device speed
  265. * @param length : Pointer to data length variable
  266. * @retval Pointer to descriptor buffer
  267. */
  268. uint8_t * USBD_HS_InterfaceStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
  269. {
  270. if(speed == 0)
  271. {
  272. USBD_GetString((uint8_t *)USBD_INTERFACE_STRING_HS, USBD_StrDesc, length);
  273. }
  274. else
  275. {
  276. USBD_GetString((uint8_t *)USBD_INTERFACE_STRING_HS, USBD_StrDesc, length);
  277. }
  278. return USBD_StrDesc;
  279. }
  280. #if (USBD_LPM_ENABLED == 1)
  281. /**
  282. * @brief Return the BOS descriptor
  283. * @param speed : Current device speed
  284. * @param length : Pointer to data length variable
  285. * @retval Pointer to descriptor buffer
  286. */
  287. uint8_t * USBD_HS_USR_BOSDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
  288. {
  289. UNUSED(speed);
  290. *length = sizeof(USBD_HS_BOSDesc);
  291. return (uint8_t*)USBD_HS_BOSDesc;
  292. }
  293. #endif /* (USBD_LPM_ENABLED == 1) */
  294. /**
  295. * @brief Create the serial number string descriptor
  296. * @param None
  297. * @retval None
  298. */
  299. static void Get_SerialNum(void)
  300. {
  301. uint32_t deviceserial0, deviceserial1, deviceserial2;
  302. deviceserial0 = *(uint32_t *) DEVICE_ID1;
  303. deviceserial1 = *(uint32_t *) DEVICE_ID2;
  304. deviceserial2 = *(uint32_t *) DEVICE_ID3;
  305. deviceserial0 += deviceserial2;
  306. if (deviceserial0 != 0)
  307. {
  308. IntToUnicode(deviceserial0, &USBD_StringSerial[2], 8);
  309. IntToUnicode(deviceserial1, &USBD_StringSerial[18], 4);
  310. }
  311. }
  312. /**
  313. * @brief Convert Hex 32Bits value into char
  314. * @param value: value to convert
  315. * @param pbuf: pointer to the buffer
  316. * @param len: buffer length
  317. * @retval None
  318. */
  319. static void IntToUnicode(uint32_t value, uint8_t * pbuf, uint8_t len)
  320. {
  321. uint8_t idx = 0;
  322. for (idx = 0; idx < len; idx++)
  323. {
  324. if (((value >> 28)) < 0xA)
  325. {
  326. pbuf[2 * idx] = (value >> 28) + '0';
  327. }
  328. else
  329. {
  330. pbuf[2 * idx] = (value >> 28) + 'A' - 10;
  331. }
  332. value = value << 4;
  333. pbuf[2 * idx + 1] = 0;
  334. }
  335. }
  336. /**
  337. * @}
  338. */
  339. /**
  340. * @}
  341. */
  342. /**
  343. * @}
  344. */