Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 
 

446 рядки
11 KiB

  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. #if (USBD_LPM_ENABLED == 1)
  96. uint8_t * USBD_FS_USR_BOSDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
  97. #endif /* (USBD_LPM_ENABLED == 1) */
  98. /**
  99. * @}
  100. */
  101. /** @defgroup USBD_DESC_Private_Variables USBD_DESC_Private_Variables
  102. * @brief Private variables.
  103. * @{
  104. */
  105. USBD_DescriptorsTypeDef FS_Desc =
  106. {
  107. USBD_FS_DeviceDescriptor
  108. , USBD_FS_LangIDStrDescriptor
  109. , USBD_FS_ManufacturerStrDescriptor
  110. , USBD_FS_ProductStrDescriptor
  111. , USBD_FS_SerialStrDescriptor
  112. , USBD_FS_ConfigStrDescriptor
  113. , USBD_FS_InterfaceStrDescriptor
  114. #if (USBD_LPM_ENABLED == 1)
  115. , USBD_FS_USR_BOSDescriptor
  116. #endif /* (USBD_LPM_ENABLED == 1) */
  117. };
  118. #if defined ( __ICCARM__ ) /* IAR Compiler */
  119. #pragma data_alignment=4
  120. #endif /* defined ( __ICCARM__ ) */
  121. /** USB standard device descriptor. */
  122. __ALIGN_BEGIN uint8_t USBD_FS_DeviceDesc[USB_LEN_DEV_DESC] __ALIGN_END =
  123. {
  124. 0x12, /*bLength */
  125. USB_DESC_TYPE_DEVICE, /*bDescriptorType*/
  126. #if (USBD_LPM_ENABLED == 1)
  127. 0x01, /*bcdUSB */ /* changed to USB version 2.01
  128. in order to support LPM L1 suspend
  129. resume test of USBCV3.0*/
  130. #else
  131. 0x00, /*bcdUSB */
  132. #endif /* (USBD_LPM_ENABLED == 1) */
  133. 0x02,
  134. 0x02, /*bDeviceClass*/
  135. 0x02, /*bDeviceSubClass*/
  136. 0x00, /*bDeviceProtocol*/
  137. USB_MAX_EP0_SIZE, /*bMaxPacketSize*/
  138. LOBYTE(USBD_VID), /*idVendor*/
  139. HIBYTE(USBD_VID), /*idVendor*/
  140. LOBYTE(USBD_PID_FS), /*idProduct*/
  141. HIBYTE(USBD_PID_FS), /*idProduct*/
  142. 0x00, /*bcdDevice rel. 2.00*/
  143. 0x02,
  144. USBD_IDX_MFC_STR, /*Index of manufacturer string*/
  145. USBD_IDX_PRODUCT_STR, /*Index of product string*/
  146. USBD_IDX_SERIAL_STR, /*Index of serial number string*/
  147. USBD_MAX_NUM_CONFIGURATION /*bNumConfigurations*/
  148. };
  149. /* USB_DeviceDescriptor */
  150. /** BOS descriptor. */
  151. #if (USBD_LPM_ENABLED == 1)
  152. #if defined ( __ICCARM__ ) /* IAR Compiler */
  153. #pragma data_alignment=4
  154. #endif /* defined ( __ICCARM__ ) */
  155. __ALIGN_BEGIN uint8_t USBD_FS_BOSDesc[USB_SIZ_BOS_DESC] __ALIGN_END =
  156. {
  157. 0x5,
  158. USB_DESC_TYPE_BOS,
  159. 0xC,
  160. 0x0,
  161. 0x1, /* 1 device capability*/
  162. /* device capability*/
  163. 0x7,
  164. USB_DEVICE_CAPABITY_TYPE,
  165. 0x2,
  166. 0x2, /* LPM capability bit set*/
  167. 0x0,
  168. 0x0,
  169. 0x0
  170. };
  171. #endif /* (USBD_LPM_ENABLED == 1) */
  172. /**
  173. * @}
  174. */
  175. /** @defgroup USBD_DESC_Private_Variables USBD_DESC_Private_Variables
  176. * @brief Private variables.
  177. * @{
  178. */
  179. #if defined ( __ICCARM__ ) /* IAR Compiler */
  180. #pragma data_alignment=4
  181. #endif /* defined ( __ICCARM__ ) */
  182. /** USB lang identifier descriptor. */
  183. __ALIGN_BEGIN uint8_t USBD_LangIDDesc[USB_LEN_LANGID_STR_DESC] __ALIGN_END =
  184. {
  185. USB_LEN_LANGID_STR_DESC,
  186. USB_DESC_TYPE_STRING,
  187. LOBYTE(USBD_LANGID_STRING),
  188. HIBYTE(USBD_LANGID_STRING)
  189. };
  190. #if defined ( __ICCARM__ ) /* IAR Compiler */
  191. #pragma data_alignment=4
  192. #endif /* defined ( __ICCARM__ ) */
  193. /* Internal string descriptor. */
  194. __ALIGN_BEGIN uint8_t USBD_StrDesc[USBD_MAX_STR_DESC_SIZ] __ALIGN_END;
  195. #if defined ( __ICCARM__ ) /*!< IAR Compiler */
  196. #pragma data_alignment=4
  197. #endif
  198. __ALIGN_BEGIN uint8_t USBD_StringSerial[USB_SIZ_STRING_SERIAL] __ALIGN_END = {
  199. USB_SIZ_STRING_SERIAL,
  200. USB_DESC_TYPE_STRING,
  201. };
  202. /**
  203. * @}
  204. */
  205. /** @defgroup USBD_DESC_Private_Functions USBD_DESC_Private_Functions
  206. * @brief Private functions.
  207. * @{
  208. */
  209. /**
  210. * @brief Return the device 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_DeviceDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
  216. {
  217. UNUSED(speed);
  218. *length = sizeof(USBD_FS_DeviceDesc);
  219. return USBD_FS_DeviceDesc;
  220. }
  221. /**
  222. * @brief Return the LangID 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_LangIDStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
  228. {
  229. UNUSED(speed);
  230. *length = sizeof(USBD_LangIDDesc);
  231. return USBD_LangIDDesc;
  232. }
  233. /**
  234. * @brief Return the product string descriptor
  235. * @param speed : Current device speed
  236. * @param length : Pointer to data length variable
  237. * @retval Pointer to descriptor buffer
  238. */
  239. uint8_t * USBD_FS_ProductStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
  240. {
  241. if(speed == 0)
  242. {
  243. USBD_GetString((uint8_t *)USBD_PRODUCT_STRING_FS, USBD_StrDesc, length);
  244. }
  245. else
  246. {
  247. USBD_GetString((uint8_t *)USBD_PRODUCT_STRING_FS, USBD_StrDesc, length);
  248. }
  249. return USBD_StrDesc;
  250. }
  251. /**
  252. * @brief Return the manufacturer 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_ManufacturerStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
  258. {
  259. UNUSED(speed);
  260. USBD_GetString((uint8_t *)USBD_MANUFACTURER_STRING, USBD_StrDesc, length);
  261. return USBD_StrDesc;
  262. }
  263. /**
  264. * @brief Return the serial number string descriptor
  265. * @param speed : Current device speed
  266. * @param length : Pointer to data length variable
  267. * @retval Pointer to descriptor buffer
  268. */
  269. uint8_t * USBD_FS_SerialStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
  270. {
  271. UNUSED(speed);
  272. *length = USB_SIZ_STRING_SERIAL;
  273. /* Update the serial number string descriptor with the data from the unique
  274. * ID */
  275. Get_SerialNum();
  276. /* USER CODE BEGIN USBD_FS_SerialStrDescriptor */
  277. /* USER CODE END USBD_FS_SerialStrDescriptor */
  278. return (uint8_t *) USBD_StringSerial;
  279. }
  280. /**
  281. * @brief Return the configuration string descriptor
  282. * @param speed : Current device speed
  283. * @param length : Pointer to data length variable
  284. * @retval Pointer to descriptor buffer
  285. */
  286. uint8_t * USBD_FS_ConfigStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
  287. {
  288. if(speed == USBD_SPEED_HIGH)
  289. {
  290. USBD_GetString((uint8_t *)USBD_CONFIGURATION_STRING_FS, USBD_StrDesc, length);
  291. }
  292. else
  293. {
  294. USBD_GetString((uint8_t *)USBD_CONFIGURATION_STRING_FS, USBD_StrDesc, length);
  295. }
  296. return USBD_StrDesc;
  297. }
  298. /**
  299. * @brief Return the interface string descriptor
  300. * @param speed : Current device speed
  301. * @param length : Pointer to data length variable
  302. * @retval Pointer to descriptor buffer
  303. */
  304. uint8_t * USBD_FS_InterfaceStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
  305. {
  306. if(speed == 0)
  307. {
  308. USBD_GetString((uint8_t *)USBD_INTERFACE_STRING_FS, USBD_StrDesc, length);
  309. }
  310. else
  311. {
  312. USBD_GetString((uint8_t *)USBD_INTERFACE_STRING_FS, USBD_StrDesc, length);
  313. }
  314. return USBD_StrDesc;
  315. }
  316. #if (USBD_LPM_ENABLED == 1)
  317. /**
  318. * @brief Return the BOS descriptor
  319. * @param speed : Current device speed
  320. * @param length : Pointer to data length variable
  321. * @retval Pointer to descriptor buffer
  322. */
  323. uint8_t * USBD_FS_USR_BOSDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
  324. {
  325. UNUSED(speed);
  326. *length = sizeof(USBD_FS_BOSDesc);
  327. return (uint8_t*)USBD_FS_BOSDesc;
  328. }
  329. #endif /* (USBD_LPM_ENABLED == 1) */
  330. /**
  331. * @brief Create the serial number string descriptor
  332. * @param None
  333. * @retval None
  334. */
  335. static void Get_SerialNum(void)
  336. {
  337. uint32_t deviceserial0;
  338. uint32_t deviceserial1;
  339. uint32_t deviceserial2;
  340. deviceserial0 = *(uint32_t *) DEVICE_ID1;
  341. deviceserial1 = *(uint32_t *) DEVICE_ID2;
  342. deviceserial2 = *(uint32_t *) DEVICE_ID3;
  343. deviceserial0 += deviceserial2;
  344. if (deviceserial0 != 0)
  345. {
  346. IntToUnicode(deviceserial0, &USBD_StringSerial[2], 8);
  347. IntToUnicode(deviceserial1, &USBD_StringSerial[18], 4);
  348. }
  349. }
  350. /**
  351. * @brief Convert Hex 32Bits value into char
  352. * @param value: value to convert
  353. * @param pbuf: pointer to the buffer
  354. * @param len: buffer length
  355. * @retval None
  356. */
  357. static void IntToUnicode(uint32_t value, uint8_t * pbuf, uint8_t len)
  358. {
  359. uint8_t idx = 0;
  360. for (idx = 0; idx < len; idx++)
  361. {
  362. if (((value >> 28)) < 0xA)
  363. {
  364. pbuf[2 * idx] = (value >> 28) + '0';
  365. }
  366. else
  367. {
  368. pbuf[2 * idx] = (value >> 28) + 'A' - 10;
  369. }
  370. value = value << 4;
  371. pbuf[2 * idx + 1] = 0;
  372. }
  373. }
  374. /**
  375. * @}
  376. */
  377. /**
  378. * @}
  379. */
  380. /**
  381. * @}
  382. */