usbd_desc.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  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. * <h2><center>&copy; Copyright (c) 2021 STMicroelectronics.
  11. * All rights reserved.</center></h2>
  12. *
  13. * This software component is licensed by ST under Ultimate Liberty license
  14. * SLA0044, the "License"; You may not use this file except in compliance with
  15. * the License. You may obtain a copy of the License at:
  16. * www.st.com/SLA0044
  17. *
  18. ******************************************************************************
  19. */
  20. /* USER CODE END Header */
  21. /* Includes ------------------------------------------------------------------*/
  22. #include "usbd_core.h"
  23. #include "usbd_desc.h"
  24. #include "usbd_conf.h"
  25. /* USER CODE BEGIN INCLUDE */
  26. /* USER CODE END INCLUDE */
  27. /* Private typedef -----------------------------------------------------------*/
  28. /* Private define ------------------------------------------------------------*/
  29. /* Private macro -------------------------------------------------------------*/
  30. /* USER CODE BEGIN PV */
  31. /* Private variables ---------------------------------------------------------*/
  32. /* USER CODE END PV */
  33. /** @addtogroup STM32_USB_OTG_DEVICE_LIBRARY
  34. * @{
  35. */
  36. /** @addtogroup USBD_DESC
  37. * @{
  38. */
  39. /** @defgroup USBD_DESC_Private_TypesDefinitions USBD_DESC_Private_TypesDefinitions
  40. * @brief Private types.
  41. * @{
  42. */
  43. /* USER CODE BEGIN PRIVATE_TYPES */
  44. /* USER CODE END PRIVATE_TYPES */
  45. /**
  46. * @}
  47. */
  48. /** @defgroup USBD_DESC_Private_Defines USBD_DESC_Private_Defines
  49. * @brief Private defines.
  50. * @{
  51. */
  52. #define USBD_VID 1155
  53. #define USBD_LANGID_STRING 1033
  54. #define USBD_MANUFACTURER_STRING "STMicroelectronics"
  55. #define USBD_PID_FS 22336
  56. #define USBD_PRODUCT_STRING_FS "STM32 Virtual ComPort"
  57. #define USBD_CONFIGURATION_STRING_FS "CDC Config"
  58. #define USBD_INTERFACE_STRING_FS "CDC Interface"
  59. #define USB_SIZ_BOS_DESC 0x0C
  60. /* USER CODE BEGIN PRIVATE_DEFINES */
  61. /* USER CODE END PRIVATE_DEFINES */
  62. /**
  63. * @}
  64. */
  65. /* USER CODE BEGIN 0 */
  66. /* USER CODE END 0 */
  67. /** @defgroup USBD_DESC_Private_Macros USBD_DESC_Private_Macros
  68. * @brief Private macros.
  69. * @{
  70. */
  71. /* USER CODE BEGIN PRIVATE_MACRO */
  72. /* USER CODE END PRIVATE_MACRO */
  73. /**
  74. * @}
  75. */
  76. /** @defgroup USBD_DESC_Private_FunctionPrototypes USBD_DESC_Private_FunctionPrototypes
  77. * @brief Private functions declaration.
  78. * @{
  79. */
  80. static void Get_SerialNum(void);
  81. static void IntToUnicode(uint32_t value, uint8_t * pbuf, uint8_t len);
  82. /**
  83. * @}
  84. */
  85. /** @defgroup USBD_DESC_Private_FunctionPrototypes USBD_DESC_Private_FunctionPrototypes
  86. * @brief Private functions declaration for FS.
  87. * @{
  88. */
  89. uint8_t * USBD_FS_DeviceDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
  90. uint8_t * USBD_FS_LangIDStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
  91. uint8_t * USBD_FS_ManufacturerStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
  92. uint8_t * USBD_FS_ProductStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
  93. uint8_t * USBD_FS_SerialStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
  94. uint8_t * USBD_FS_ConfigStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
  95. uint8_t * USBD_FS_InterfaceStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
  96. #if (USBD_LPM_ENABLED == 1)
  97. uint8_t * USBD_FS_USR_BOSDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
  98. #endif /* (USBD_LPM_ENABLED == 1) */
  99. /**
  100. * @}
  101. */
  102. /** @defgroup USBD_DESC_Private_Variables USBD_DESC_Private_Variables
  103. * @brief Private variables.
  104. * @{
  105. */
  106. USBD_DescriptorsTypeDef FS_Desc =
  107. {
  108. USBD_FS_DeviceDescriptor
  109. , USBD_FS_LangIDStrDescriptor
  110. , USBD_FS_ManufacturerStrDescriptor
  111. , USBD_FS_ProductStrDescriptor
  112. , USBD_FS_SerialStrDescriptor
  113. , USBD_FS_ConfigStrDescriptor
  114. , USBD_FS_InterfaceStrDescriptor
  115. #if (USBD_LPM_ENABLED == 1)
  116. , USBD_FS_USR_BOSDescriptor
  117. #endif /* (USBD_LPM_ENABLED == 1) */
  118. };
  119. #if defined ( __ICCARM__ ) /* IAR Compiler */
  120. #pragma data_alignment=4
  121. #endif /* defined ( __ICCARM__ ) */
  122. /** USB standard device descriptor. */
  123. __ALIGN_BEGIN uint8_t USBD_FS_DeviceDesc[USB_LEN_DEV_DESC] __ALIGN_END =
  124. {
  125. 0x12, /*bLength */
  126. USB_DESC_TYPE_DEVICE, /*bDescriptorType*/
  127. #if (USBD_LPM_ENABLED == 1)
  128. 0x01, /*bcdUSB */ /* changed to USB version 2.01
  129. in order to support LPM L1 suspend
  130. resume test of USBCV3.0*/
  131. #else
  132. 0x00, /*bcdUSB */
  133. #endif /* (USBD_LPM_ENABLED == 1) */
  134. 0x02,
  135. 0x02, /*bDeviceClass*/
  136. 0x02, /*bDeviceSubClass*/
  137. 0x00, /*bDeviceProtocol*/
  138. USB_MAX_EP0_SIZE, /*bMaxPacketSize*/
  139. LOBYTE(USBD_VID), /*idVendor*/
  140. HIBYTE(USBD_VID), /*idVendor*/
  141. LOBYTE(USBD_PID_FS), /*idProduct*/
  142. HIBYTE(USBD_PID_FS), /*idProduct*/
  143. 0x00, /*bcdDevice rel. 2.00*/
  144. 0x02,
  145. USBD_IDX_MFC_STR, /*Index of manufacturer string*/
  146. USBD_IDX_PRODUCT_STR, /*Index of product string*/
  147. USBD_IDX_SERIAL_STR, /*Index of serial number string*/
  148. USBD_MAX_NUM_CONFIGURATION /*bNumConfigurations*/
  149. };
  150. /* USB_DeviceDescriptor */
  151. /** BOS descriptor. */
  152. #if (USBD_LPM_ENABLED == 1)
  153. #if defined ( __ICCARM__ ) /* IAR Compiler */
  154. #pragma data_alignment=4
  155. #endif /* defined ( __ICCARM__ ) */
  156. __ALIGN_BEGIN uint8_t USBD_FS_BOSDesc[USB_SIZ_BOS_DESC] __ALIGN_END =
  157. {
  158. 0x5,
  159. USB_DESC_TYPE_BOS,
  160. 0xC,
  161. 0x0,
  162. 0x1, /* 1 device capability*/
  163. /* device capability*/
  164. 0x7,
  165. USB_DEVICE_CAPABITY_TYPE,
  166. 0x2,
  167. 0x2, /* LPM capability bit set*/
  168. 0x0,
  169. 0x0,
  170. 0x0
  171. };
  172. #endif /* (USBD_LPM_ENABLED == 1) */
  173. /**
  174. * @}
  175. */
  176. /** @defgroup USBD_DESC_Private_Variables USBD_DESC_Private_Variables
  177. * @brief Private variables.
  178. * @{
  179. */
  180. #if defined ( __ICCARM__ ) /* IAR Compiler */
  181. #pragma data_alignment=4
  182. #endif /* defined ( __ICCARM__ ) */
  183. /** USB lang identifier descriptor. */
  184. __ALIGN_BEGIN uint8_t USBD_LangIDDesc[USB_LEN_LANGID_STR_DESC] __ALIGN_END =
  185. {
  186. USB_LEN_LANGID_STR_DESC,
  187. USB_DESC_TYPE_STRING,
  188. LOBYTE(USBD_LANGID_STRING),
  189. HIBYTE(USBD_LANGID_STRING)
  190. };
  191. #if defined ( __ICCARM__ ) /* IAR Compiler */
  192. #pragma data_alignment=4
  193. #endif /* defined ( __ICCARM__ ) */
  194. /* Internal string descriptor. */
  195. __ALIGN_BEGIN uint8_t USBD_StrDesc[USBD_MAX_STR_DESC_SIZ] __ALIGN_END;
  196. #if defined ( __ICCARM__ ) /*!< IAR Compiler */
  197. #pragma data_alignment=4
  198. #endif
  199. __ALIGN_BEGIN uint8_t USBD_StringSerial[USB_SIZ_STRING_SERIAL] __ALIGN_END = {
  200. USB_SIZ_STRING_SERIAL,
  201. USB_DESC_TYPE_STRING,
  202. };
  203. /**
  204. * @}
  205. */
  206. /** @defgroup USBD_DESC_Private_Functions USBD_DESC_Private_Functions
  207. * @brief Private functions.
  208. * @{
  209. */
  210. /**
  211. * @brief Return the device descriptor
  212. * @param speed : Current device speed
  213. * @param length : Pointer to data length variable
  214. * @retval Pointer to descriptor buffer
  215. */
  216. uint8_t * USBD_FS_DeviceDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
  217. {
  218. UNUSED(speed);
  219. *length = sizeof(USBD_FS_DeviceDesc);
  220. return USBD_FS_DeviceDesc;
  221. }
  222. /**
  223. * @brief Return the LangID string descriptor
  224. * @param speed : Current device speed
  225. * @param length : Pointer to data length variable
  226. * @retval Pointer to descriptor buffer
  227. */
  228. uint8_t * USBD_FS_LangIDStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
  229. {
  230. UNUSED(speed);
  231. *length = sizeof(USBD_LangIDDesc);
  232. return USBD_LangIDDesc;
  233. }
  234. /**
  235. * @brief Return the product string descriptor
  236. * @param speed : Current device speed
  237. * @param length : Pointer to data length variable
  238. * @retval Pointer to descriptor buffer
  239. */
  240. uint8_t * USBD_FS_ProductStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
  241. {
  242. if(speed == 0)
  243. {
  244. USBD_GetString((uint8_t *)USBD_PRODUCT_STRING_FS, USBD_StrDesc, length);
  245. }
  246. else
  247. {
  248. USBD_GetString((uint8_t *)USBD_PRODUCT_STRING_FS, USBD_StrDesc, length);
  249. }
  250. return USBD_StrDesc;
  251. }
  252. /**
  253. * @brief Return the manufacturer string descriptor
  254. * @param speed : Current device speed
  255. * @param length : Pointer to data length variable
  256. * @retval Pointer to descriptor buffer
  257. */
  258. uint8_t * USBD_FS_ManufacturerStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
  259. {
  260. UNUSED(speed);
  261. USBD_GetString((uint8_t *)USBD_MANUFACTURER_STRING, USBD_StrDesc, length);
  262. return USBD_StrDesc;
  263. }
  264. /**
  265. * @brief Return the serial number string descriptor
  266. * @param speed : Current device speed
  267. * @param length : Pointer to data length variable
  268. * @retval Pointer to descriptor buffer
  269. */
  270. uint8_t * USBD_FS_SerialStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
  271. {
  272. UNUSED(speed);
  273. *length = USB_SIZ_STRING_SERIAL;
  274. /* Update the serial number string descriptor with the data from the unique
  275. * ID */
  276. Get_SerialNum();
  277. /* USER CODE BEGIN USBD_FS_SerialStrDescriptor */
  278. /* USER CODE END USBD_FS_SerialStrDescriptor */
  279. return (uint8_t *) USBD_StringSerial;
  280. }
  281. /**
  282. * @brief Return the configuration string 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_FS_ConfigStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
  288. {
  289. if(speed == USBD_SPEED_HIGH)
  290. {
  291. USBD_GetString((uint8_t *)USBD_CONFIGURATION_STRING_FS, USBD_StrDesc, length);
  292. }
  293. else
  294. {
  295. USBD_GetString((uint8_t *)USBD_CONFIGURATION_STRING_FS, USBD_StrDesc, length);
  296. }
  297. return USBD_StrDesc;
  298. }
  299. /**
  300. * @brief Return the interface string descriptor
  301. * @param speed : Current device speed
  302. * @param length : Pointer to data length variable
  303. * @retval Pointer to descriptor buffer
  304. */
  305. uint8_t * USBD_FS_InterfaceStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
  306. {
  307. if(speed == 0)
  308. {
  309. USBD_GetString((uint8_t *)USBD_INTERFACE_STRING_FS, USBD_StrDesc, length);
  310. }
  311. else
  312. {
  313. USBD_GetString((uint8_t *)USBD_INTERFACE_STRING_FS, USBD_StrDesc, length);
  314. }
  315. return USBD_StrDesc;
  316. }
  317. #if (USBD_LPM_ENABLED == 1)
  318. /**
  319. * @brief Return the BOS descriptor
  320. * @param speed : Current device speed
  321. * @param length : Pointer to data length variable
  322. * @retval Pointer to descriptor buffer
  323. */
  324. uint8_t * USBD_FS_USR_BOSDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
  325. {
  326. UNUSED(speed);
  327. *length = sizeof(USBD_FS_BOSDesc);
  328. return (uint8_t*)USBD_FS_BOSDesc;
  329. }
  330. #endif /* (USBD_LPM_ENABLED == 1) */
  331. /**
  332. * @brief Create the serial number string descriptor
  333. * @param None
  334. * @retval None
  335. */
  336. static void Get_SerialNum(void)
  337. {
  338. uint32_t deviceserial0, deviceserial1, deviceserial2;
  339. deviceserial0 = *(uint32_t *) DEVICE_ID1;
  340. deviceserial1 = *(uint32_t *) DEVICE_ID2;
  341. deviceserial2 = *(uint32_t *) DEVICE_ID3;
  342. deviceserial0 += deviceserial2;
  343. if (deviceserial0 != 0)
  344. {
  345. IntToUnicode(deviceserial0, &USBD_StringSerial[2], 8);
  346. IntToUnicode(deviceserial1, &USBD_StringSerial[18], 4);
  347. }
  348. }
  349. /**
  350. * @brief Convert Hex 32Bits value into char
  351. * @param value: value to convert
  352. * @param pbuf: pointer to the buffer
  353. * @param len: buffer length
  354. * @retval None
  355. */
  356. static void IntToUnicode(uint32_t value, uint8_t * pbuf, uint8_t len)
  357. {
  358. uint8_t idx = 0;
  359. for (idx = 0; idx < len; idx++)
  360. {
  361. if (((value >> 28)) < 0xA)
  362. {
  363. pbuf[2 * idx] = (value >> 28) + '0';
  364. }
  365. else
  366. {
  367. pbuf[2 * idx] = (value >> 28) + 'A' - 10;
  368. }
  369. value = value << 4;
  370. pbuf[2 * idx + 1] = 0;
  371. }
  372. }
  373. /**
  374. * @}
  375. */
  376. /**
  377. * @}
  378. */
  379. /**
  380. * @}
  381. */
  382. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/