usbd_desc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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) 2026 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_FS 22336
  55. #define USBD_PRODUCT_STRING_FS "STM32 Virtual ComPort"
  56. #define USBD_CONFIGURATION_STRING_FS "CDC Config"
  57. #define USBD_INTERFACE_STRING_FS "CDC Interface"
  58. #define USB_SIZ_BOS_DESC 0x0C
  59. /* USER CODE BEGIN PRIVATE_DEFINES */
  60. /* USER CODE END PRIVATE_DEFINES */
  61. /**
  62. * @}
  63. */
  64. /* USER CODE BEGIN 0 */
  65. /* USER CODE END 0 */
  66. /** @defgroup USBD_DESC_Private_Macros USBD_DESC_Private_Macros
  67. * @brief Private macros.
  68. * @{
  69. */
  70. /* USER CODE BEGIN PRIVATE_MACRO */
  71. /* USER CODE END PRIVATE_MACRO */
  72. /**
  73. * @}
  74. */
  75. /** @defgroup USBD_DESC_Private_FunctionPrototypes USBD_DESC_Private_FunctionPrototypes
  76. * @brief Private functions declaration.
  77. * @{
  78. */
  79. static void Get_SerialNum(void);
  80. static void IntToUnicode(uint32_t value, uint8_t * pbuf, uint8_t len);
  81. /**
  82. * @}
  83. */
  84. /** @defgroup USBD_DESC_Private_FunctionPrototypes USBD_DESC_Private_FunctionPrototypes
  85. * @brief Private functions declaration for FS.
  86. * @{
  87. */
  88. uint8_t * USBD_FS_DeviceDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
  89. uint8_t * USBD_FS_LangIDStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
  90. uint8_t * USBD_FS_ManufacturerStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
  91. uint8_t * USBD_FS_ProductStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
  92. uint8_t * USBD_FS_SerialStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
  93. uint8_t * USBD_FS_ConfigStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
  94. uint8_t * USBD_FS_InterfaceStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
  95. /**
  96. * @}
  97. */
  98. /** @defgroup USBD_DESC_Private_Variables USBD_DESC_Private_Variables
  99. * @brief Private variables.
  100. * @{
  101. */
  102. USBD_DescriptorsTypeDef FS_Desc =
  103. {
  104. USBD_FS_DeviceDescriptor
  105. , USBD_FS_LangIDStrDescriptor
  106. , USBD_FS_ManufacturerStrDescriptor
  107. , USBD_FS_ProductStrDescriptor
  108. , USBD_FS_SerialStrDescriptor
  109. , USBD_FS_ConfigStrDescriptor
  110. , USBD_FS_InterfaceStrDescriptor
  111. };
  112. #if defined ( __ICCARM__ ) /* IAR Compiler */
  113. #pragma data_alignment=4
  114. #endif /* defined ( __ICCARM__ ) */
  115. /** USB standard device descriptor. */
  116. __ALIGN_BEGIN uint8_t USBD_FS_DeviceDesc[USB_LEN_DEV_DESC] __ALIGN_END =
  117. {
  118. 0x12, /*bLength */
  119. USB_DESC_TYPE_DEVICE, /*bDescriptorType*/
  120. 0x00, /*bcdUSB */
  121. 0x02,
  122. 0x02, /*bDeviceClass*/
  123. 0x02, /*bDeviceSubClass*/
  124. 0x00, /*bDeviceProtocol*/
  125. USB_MAX_EP0_SIZE, /*bMaxPacketSize*/
  126. LOBYTE(USBD_VID), /*idVendor*/
  127. HIBYTE(USBD_VID), /*idVendor*/
  128. LOBYTE(USBD_PID_FS), /*idProduct*/
  129. HIBYTE(USBD_PID_FS), /*idProduct*/
  130. 0x00, /*bcdDevice rel. 2.00*/
  131. 0x02,
  132. USBD_IDX_MFC_STR, /*Index of manufacturer string*/
  133. USBD_IDX_PRODUCT_STR, /*Index of product string*/
  134. USBD_IDX_SERIAL_STR, /*Index of serial number string*/
  135. USBD_MAX_NUM_CONFIGURATION /*bNumConfigurations*/
  136. };
  137. /* USB_DeviceDescriptor */
  138. /** BOS descriptor. */
  139. #if (USBD_LPM_ENABLED == 1)
  140. #if defined ( __ICCARM__ ) /* IAR Compiler */
  141. #pragma data_alignment=4
  142. #endif /* defined ( __ICCARM__ ) */
  143. __ALIGN_BEGIN uint8_t USBD_FS_BOSDesc[USB_SIZ_BOS_DESC] __ALIGN_END =
  144. {
  145. 0x5,
  146. USB_DESC_TYPE_BOS,
  147. 0xC,
  148. 0x0,
  149. 0x1, /* 1 device capability*/
  150. /* device capability*/
  151. 0x7,
  152. USB_DEVICE_CAPABITY_TYPE,
  153. 0x2,
  154. 0x2, /* LPM capability bit set*/
  155. 0x0,
  156. 0x0,
  157. 0x0
  158. };
  159. #endif /* (USBD_LPM_ENABLED == 1) */
  160. /**
  161. * @}
  162. */
  163. /** @defgroup USBD_DESC_Private_Variables USBD_DESC_Private_Variables
  164. * @brief Private variables.
  165. * @{
  166. */
  167. #if defined ( __ICCARM__ ) /* IAR Compiler */
  168. #pragma data_alignment=4
  169. #endif /* defined ( __ICCARM__ ) */
  170. /** USB lang identifier descriptor. */
  171. __ALIGN_BEGIN uint8_t USBD_LangIDDesc[USB_LEN_LANGID_STR_DESC] __ALIGN_END =
  172. {
  173. USB_LEN_LANGID_STR_DESC,
  174. USB_DESC_TYPE_STRING,
  175. LOBYTE(USBD_LANGID_STRING),
  176. HIBYTE(USBD_LANGID_STRING)
  177. };
  178. #if defined ( __ICCARM__ ) /* IAR Compiler */
  179. #pragma data_alignment=4
  180. #endif /* defined ( __ICCARM__ ) */
  181. /* Internal string descriptor. */
  182. __ALIGN_BEGIN uint8_t USBD_StrDesc[USBD_MAX_STR_DESC_SIZ] __ALIGN_END;
  183. #if defined ( __ICCARM__ ) /*!< IAR Compiler */
  184. #pragma data_alignment=4
  185. #endif
  186. __ALIGN_BEGIN uint8_t USBD_StringSerial[USB_SIZ_STRING_SERIAL] __ALIGN_END = {
  187. USB_SIZ_STRING_SERIAL,
  188. USB_DESC_TYPE_STRING,
  189. };
  190. /**
  191. * @}
  192. */
  193. /** @defgroup USBD_DESC_Private_Functions USBD_DESC_Private_Functions
  194. * @brief Private functions.
  195. * @{
  196. */
  197. /**
  198. * @brief Return the device 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_FS_DeviceDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
  204. {
  205. UNUSED(speed);
  206. *length = sizeof(USBD_FS_DeviceDesc);
  207. return USBD_FS_DeviceDesc;
  208. }
  209. /**
  210. * @brief Return the LangID string descriptor
  211. * @param speed : Current device speed
  212. * @param length : Pointer to data length variable
  213. * @retval Pointer to descriptor buffer
  214. */
  215. uint8_t * USBD_FS_LangIDStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
  216. {
  217. UNUSED(speed);
  218. *length = sizeof(USBD_LangIDDesc);
  219. return USBD_LangIDDesc;
  220. }
  221. /**
  222. * @brief Return the product string descriptor
  223. * @param speed : Current device speed
  224. * @param length : Pointer to data length variable
  225. * @retval Pointer to descriptor buffer
  226. */
  227. uint8_t * USBD_FS_ProductStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
  228. {
  229. if(speed == 0)
  230. {
  231. USBD_GetString((uint8_t *)USBD_PRODUCT_STRING_FS, USBD_StrDesc, length);
  232. }
  233. else
  234. {
  235. USBD_GetString((uint8_t *)USBD_PRODUCT_STRING_FS, USBD_StrDesc, length);
  236. }
  237. return USBD_StrDesc;
  238. }
  239. /**
  240. * @brief Return the manufacturer string descriptor
  241. * @param speed : Current device speed
  242. * @param length : Pointer to data length variable
  243. * @retval Pointer to descriptor buffer
  244. */
  245. uint8_t * USBD_FS_ManufacturerStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
  246. {
  247. UNUSED(speed);
  248. USBD_GetString((uint8_t *)USBD_MANUFACTURER_STRING, USBD_StrDesc, length);
  249. return USBD_StrDesc;
  250. }
  251. /**
  252. * @brief Return the serial number string descriptor
  253. * @param speed : Current device speed
  254. * @param length : Pointer to data length variable
  255. * @retval Pointer to descriptor buffer
  256. */
  257. uint8_t * USBD_FS_SerialStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
  258. {
  259. UNUSED(speed);
  260. *length = USB_SIZ_STRING_SERIAL;
  261. /* Update the serial number string descriptor with the data from the unique
  262. * ID */
  263. Get_SerialNum();
  264. /* USER CODE BEGIN USBD_FS_SerialStrDescriptor */
  265. /* USER CODE END USBD_FS_SerialStrDescriptor */
  266. return (uint8_t *) USBD_StringSerial;
  267. }
  268. /**
  269. * @brief Return the configuration string descriptor
  270. * @param speed : Current device speed
  271. * @param length : Pointer to data length variable
  272. * @retval Pointer to descriptor buffer
  273. */
  274. uint8_t * USBD_FS_ConfigStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
  275. {
  276. if(speed == USBD_SPEED_HIGH)
  277. {
  278. USBD_GetString((uint8_t *)USBD_CONFIGURATION_STRING_FS, USBD_StrDesc, length);
  279. }
  280. else
  281. {
  282. USBD_GetString((uint8_t *)USBD_CONFIGURATION_STRING_FS, USBD_StrDesc, length);
  283. }
  284. return USBD_StrDesc;
  285. }
  286. /**
  287. * @brief Return the interface string descriptor
  288. * @param speed : Current device speed
  289. * @param length : Pointer to data length variable
  290. * @retval Pointer to descriptor buffer
  291. */
  292. uint8_t * USBD_FS_InterfaceStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
  293. {
  294. if(speed == 0)
  295. {
  296. USBD_GetString((uint8_t *)USBD_INTERFACE_STRING_FS, USBD_StrDesc, length);
  297. }
  298. else
  299. {
  300. USBD_GetString((uint8_t *)USBD_INTERFACE_STRING_FS, USBD_StrDesc, length);
  301. }
  302. return USBD_StrDesc;
  303. }
  304. /**
  305. * @brief Create the serial number string descriptor
  306. * @param None
  307. * @retval None
  308. */
  309. static void Get_SerialNum(void)
  310. {
  311. uint32_t deviceserial0;
  312. uint32_t deviceserial1;
  313. uint32_t deviceserial2;
  314. deviceserial0 = *(uint32_t *) DEVICE_ID1;
  315. deviceserial1 = *(uint32_t *) DEVICE_ID2;
  316. deviceserial2 = *(uint32_t *) DEVICE_ID3;
  317. deviceserial0 += deviceserial2;
  318. if (deviceserial0 != 0)
  319. {
  320. IntToUnicode(deviceserial0, &USBD_StringSerial[2], 8);
  321. IntToUnicode(deviceserial1, &USBD_StringSerial[18], 4);
  322. }
  323. }
  324. /**
  325. * @brief Convert Hex 32Bits value into char
  326. * @param value: value to convert
  327. * @param pbuf: pointer to the buffer
  328. * @param len: buffer length
  329. * @retval None
  330. */
  331. static void IntToUnicode(uint32_t value, uint8_t * pbuf, uint8_t len)
  332. {
  333. uint8_t idx = 0;
  334. for (idx = 0; idx < len; idx++)
  335. {
  336. if (((value >> 28)) < 0xA)
  337. {
  338. pbuf[2 * idx] = (value >> 28) + '0';
  339. }
  340. else
  341. {
  342. pbuf[2 * idx] = (value >> 28) + 'A' - 10;
  343. }
  344. value = value << 4;
  345. pbuf[2 * idx + 1] = 0;
  346. }
  347. }
  348. /**
  349. * @}
  350. */
  351. /**
  352. * @}
  353. */
  354. /**
  355. * @}
  356. */