usbd_desc.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. /* USER CODE BEGIN Header */
  2. /**
  3. ******************************************************************************
  4. * @file : App/usbd_desc.c
  5. * @version : v2.0_Cube
  6. * @brief : This file implements the USB device descriptors.
  7. ******************************************************************************
  8. * @attention
  9. *
  10. * <h2><center>&copy; Copyright (c) 2022 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 "DETECTOR"
  55. #define USBD_PID_FS 22336
  56. #define USBD_PRODUCT_STRING_FS "Scintillation Dosimeter"
  57. #define USBD_CONFIGURATION_STRING_FS "CDC Config"
  58. #define USBD_INTERFACE_STRING_FS "CDC Interface"
  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. /**
  139. * @}
  140. */
  141. /** @defgroup USBD_DESC_Private_Variables USBD_DESC_Private_Variables
  142. * @brief Private variables.
  143. * @{
  144. */
  145. #if defined ( __ICCARM__ ) /* IAR Compiler */
  146. #pragma data_alignment=4
  147. #endif /* defined ( __ICCARM__ ) */
  148. /** USB lang identifier descriptor. */
  149. __ALIGN_BEGIN uint8_t USBD_LangIDDesc[USB_LEN_LANGID_STR_DESC] __ALIGN_END =
  150. {
  151. USB_LEN_LANGID_STR_DESC,
  152. USB_DESC_TYPE_STRING,
  153. LOBYTE(USBD_LANGID_STRING),
  154. HIBYTE(USBD_LANGID_STRING)
  155. };
  156. #if defined ( __ICCARM__ ) /* IAR Compiler */
  157. #pragma data_alignment=4
  158. #endif /* defined ( __ICCARM__ ) */
  159. /* Internal string descriptor. */
  160. __ALIGN_BEGIN uint8_t USBD_StrDesc[USBD_MAX_STR_DESC_SIZ] __ALIGN_END;
  161. #if defined ( __ICCARM__ ) /*!< IAR Compiler */
  162. #pragma data_alignment=4
  163. #endif
  164. __ALIGN_BEGIN uint8_t USBD_StringSerial[USB_SIZ_STRING_SERIAL] __ALIGN_END = {
  165. USB_SIZ_STRING_SERIAL,
  166. USB_DESC_TYPE_STRING,
  167. };
  168. /**
  169. * @}
  170. */
  171. /** @defgroup USBD_DESC_Private_Functions USBD_DESC_Private_Functions
  172. * @brief Private functions.
  173. * @{
  174. */
  175. /**
  176. * @brief Return the device descriptor
  177. * @param speed : Current device speed
  178. * @param length : Pointer to data length variable
  179. * @retval Pointer to descriptor buffer
  180. */
  181. uint8_t * USBD_FS_DeviceDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
  182. {
  183. UNUSED(speed);
  184. *length = sizeof(USBD_FS_DeviceDesc);
  185. return USBD_FS_DeviceDesc;
  186. }
  187. /**
  188. * @brief Return the LangID string descriptor
  189. * @param speed : Current device speed
  190. * @param length : Pointer to data length variable
  191. * @retval Pointer to descriptor buffer
  192. */
  193. uint8_t * USBD_FS_LangIDStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
  194. {
  195. UNUSED(speed);
  196. *length = sizeof(USBD_LangIDDesc);
  197. return USBD_LangIDDesc;
  198. }
  199. /**
  200. * @brief Return the product string descriptor
  201. * @param speed : Current device speed
  202. * @param length : Pointer to data length variable
  203. * @retval Pointer to descriptor buffer
  204. */
  205. uint8_t * USBD_FS_ProductStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
  206. {
  207. if(speed == 0)
  208. {
  209. USBD_GetString((uint8_t *)USBD_PRODUCT_STRING_FS, USBD_StrDesc, length);
  210. }
  211. else
  212. {
  213. USBD_GetString((uint8_t *)USBD_PRODUCT_STRING_FS, USBD_StrDesc, length);
  214. }
  215. return USBD_StrDesc;
  216. }
  217. /**
  218. * @brief Return the manufacturer string descriptor
  219. * @param speed : Current device speed
  220. * @param length : Pointer to data length variable
  221. * @retval Pointer to descriptor buffer
  222. */
  223. uint8_t * USBD_FS_ManufacturerStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
  224. {
  225. UNUSED(speed);
  226. USBD_GetString((uint8_t *)USBD_MANUFACTURER_STRING, USBD_StrDesc, length);
  227. return USBD_StrDesc;
  228. }
  229. /**
  230. * @brief Return the serial number string descriptor
  231. * @param speed : Current device speed
  232. * @param length : Pointer to data length variable
  233. * @retval Pointer to descriptor buffer
  234. */
  235. uint8_t * USBD_FS_SerialStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
  236. {
  237. UNUSED(speed);
  238. *length = USB_SIZ_STRING_SERIAL;
  239. /* Update the serial number string descriptor with the data from the unique
  240. * ID */
  241. Get_SerialNum();
  242. /* USER CODE BEGIN USBD_FS_SerialStrDescriptor */
  243. /* USER CODE END USBD_FS_SerialStrDescriptor */
  244. return (uint8_t *) USBD_StringSerial;
  245. }
  246. /**
  247. * @brief Return the configuration string descriptor
  248. * @param speed : Current device speed
  249. * @param length : Pointer to data length variable
  250. * @retval Pointer to descriptor buffer
  251. */
  252. uint8_t * USBD_FS_ConfigStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
  253. {
  254. if(speed == USBD_SPEED_HIGH)
  255. {
  256. USBD_GetString((uint8_t *)USBD_CONFIGURATION_STRING_FS, USBD_StrDesc, length);
  257. }
  258. else
  259. {
  260. USBD_GetString((uint8_t *)USBD_CONFIGURATION_STRING_FS, USBD_StrDesc, length);
  261. }
  262. return USBD_StrDesc;
  263. }
  264. /**
  265. * @brief Return the interface 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_InterfaceStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
  271. {
  272. if(speed == 0)
  273. {
  274. USBD_GetString((uint8_t *)USBD_INTERFACE_STRING_FS, USBD_StrDesc, length);
  275. }
  276. else
  277. {
  278. USBD_GetString((uint8_t *)USBD_INTERFACE_STRING_FS, USBD_StrDesc, length);
  279. }
  280. return USBD_StrDesc;
  281. }
  282. /**
  283. * @brief Create the serial number string descriptor
  284. * @param None
  285. * @retval None
  286. */
  287. static void Get_SerialNum(void)
  288. {
  289. uint32_t deviceserial0, deviceserial1, deviceserial2;
  290. deviceserial0 = *(uint32_t *) DEVICE_ID1;
  291. deviceserial1 = *(uint32_t *) DEVICE_ID2;
  292. deviceserial2 = *(uint32_t *) DEVICE_ID3;
  293. deviceserial0 += deviceserial2;
  294. if (deviceserial0 != 0)
  295. {
  296. IntToUnicode(deviceserial0, &USBD_StringSerial[2], 8);
  297. IntToUnicode(deviceserial1, &USBD_StringSerial[18], 4);
  298. }
  299. }
  300. /**
  301. * @brief Convert Hex 32Bits value into char
  302. * @param value: value to convert
  303. * @param pbuf: pointer to the buffer
  304. * @param len: buffer length
  305. * @retval None
  306. */
  307. static void IntToUnicode(uint32_t value, uint8_t * pbuf, uint8_t len)
  308. {
  309. uint8_t idx = 0;
  310. for (idx = 0; idx < len; idx++)
  311. {
  312. if (((value >> 28)) < 0xA)
  313. {
  314. pbuf[2 * idx] = (value >> 28) + '0';
  315. }
  316. else
  317. {
  318. pbuf[2 * idx] = (value >> 28) + 'A' - 10;
  319. }
  320. value = value << 4;
  321. pbuf[2 * idx + 1] = 0;
  322. }
  323. }
  324. /**
  325. * @}
  326. */
  327. /**
  328. * @}
  329. */
  330. /**
  331. * @}
  332. */