usbd_desc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. /* USER CODE BEGIN Header */
  2. /**
  3. ******************************************************************************
  4. * @file : usbd_desc.c
  5. * @version : v3.0_Cube
  6. * @brief : This file implements the USB device descriptors.
  7. ******************************************************************************
  8. * @attention
  9. *
  10. * <h2><center>&copy; Copyright (c) 2020 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. #include "api-hal-version.h"
  26. /* USER CODE BEGIN INCLUDE */
  27. /* USER CODE END INCLUDE */
  28. /* Private typedef -----------------------------------------------------------*/
  29. /* Private define ------------------------------------------------------------*/
  30. /* Private macro -------------------------------------------------------------*/
  31. /* USER CODE BEGIN PV */
  32. /* Private variables ---------------------------------------------------------*/
  33. /* USER CODE END PV */
  34. /** @addtogroup STM32_USB_OTG_DEVICE_LIBRARY
  35. * @{
  36. */
  37. /** @addtogroup USBD_DESC
  38. * @{
  39. */
  40. /** @defgroup USBD_DESC_Private_TypesDefinitions USBD_DESC_Private_TypesDefinitions
  41. * @brief Private types.
  42. * @{
  43. */
  44. /* USER CODE BEGIN PRIVATE_TYPES */
  45. /* USER CODE END PRIVATE_TYPES */
  46. /**
  47. * @}
  48. */
  49. /** @defgroup USBD_DESC_Private_Defines USBD_DESC_Private_Defines
  50. * @brief Private defines.
  51. * @{
  52. */
  53. #define USBD_VID 1155
  54. #define USBD_LANGID_STRING 1033
  55. #define USBD_MANUFACTURER_STRING "Flipper Devices Inc."
  56. #define USBD_PID 22336
  57. #define USBD_PRODUCT_STRING "Flipper Control Virtual ComPort"
  58. #define USBD_CONFIGURATION_STRING "CDC Config"
  59. #define USBD_INTERFACE_STRING "CDC Interface"
  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.
  87. * @{
  88. */
  89. uint8_t * USBD_CDC_DeviceDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
  90. uint8_t * USBD_CDC_LangIDStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
  91. uint8_t * USBD_CDC_ManufacturerStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
  92. uint8_t * USBD_CDC_ProductStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
  93. uint8_t * USBD_CDC_SerialStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
  94. uint8_t * USBD_CDC_ConfigStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
  95. uint8_t * USBD_CDC_InterfaceStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
  96. /**
  97. * @}
  98. */
  99. /** @defgroup USBD_DESC_Private_Variables USBD_DESC_Private_Variables
  100. * @brief Private variables.
  101. * @{
  102. */
  103. USBD_DescriptorsTypeDef CDC_Desc =
  104. {
  105. USBD_CDC_DeviceDescriptor,
  106. USBD_CDC_LangIDStrDescriptor,
  107. USBD_CDC_ManufacturerStrDescriptor,
  108. USBD_CDC_ProductStrDescriptor,
  109. USBD_CDC_SerialStrDescriptor,
  110. USBD_CDC_ConfigStrDescriptor,
  111. USBD_CDC_InterfaceStrDescriptor
  112. };
  113. #if defined ( __ICCARM__ ) /* IAR Compiler */
  114. #pragma data_alignment=4
  115. #endif /* defined ( __ICCARM__ ) */
  116. /** USB standard device descriptor. */
  117. __ALIGN_BEGIN uint8_t USBD_CDC_DeviceDesc[USB_LEN_DEV_DESC] __ALIGN_END =
  118. {
  119. 0x12, /*bLength */
  120. USB_DESC_TYPE_DEVICE, /*bDescriptorType*/
  121. 0x00, /*bcdUSB */
  122. 0x02,
  123. 0x02, /*bDeviceClass*/
  124. 0x02, /*bDeviceSubClass*/
  125. 0x00, /*bDeviceProtocol*/
  126. USB_MAX_EP0_SIZE, /*bMaxPacketSize*/
  127. LOBYTE(USBD_VID), /*idVendor*/
  128. HIBYTE(USBD_VID), /*idVendor*/
  129. LOBYTE(USBD_PID), /*idProduct*/
  130. HIBYTE(USBD_PID), /*idProduct*/
  131. 0x00, /*bcdDevice rel. 2.00*/
  132. 0x02,
  133. USBD_IDX_MFC_STR, /*Index of manufacturer string*/
  134. USBD_IDX_PRODUCT_STR, /*Index of product string*/
  135. USBD_IDX_SERIAL_STR, /*Index of serial number string*/
  136. USBD_MAX_NUM_CONFIGURATION /*bNumConfigurations*/
  137. };
  138. /* USB_DeviceDescriptor */
  139. /**
  140. * @}
  141. */
  142. /** @defgroup USBD_DESC_Private_Variables USBD_DESC_Private_Variables
  143. * @brief Private variables.
  144. * @{
  145. */
  146. #if defined ( __ICCARM__ ) /* IAR Compiler */
  147. #pragma data_alignment=4
  148. #endif /* defined ( __ICCARM__ ) */
  149. /** USB lang indentifier descriptor. */
  150. __ALIGN_BEGIN uint8_t USBD_LangIDDesc[USB_LEN_LANGID_STR_DESC] __ALIGN_END =
  151. {
  152. USB_LEN_LANGID_STR_DESC,
  153. USB_DESC_TYPE_STRING,
  154. LOBYTE(USBD_LANGID_STRING),
  155. HIBYTE(USBD_LANGID_STRING)
  156. };
  157. #if defined ( __ICCARM__ ) /* IAR Compiler */
  158. #pragma data_alignment=4
  159. #endif /* defined ( __ICCARM__ ) */
  160. /* Internal string descriptor. */
  161. __ALIGN_BEGIN uint8_t USBD_StrDesc[USBD_MAX_STR_DESC_SIZ] __ALIGN_END;
  162. #if defined ( __ICCARM__ ) /*!< IAR Compiler */
  163. #pragma data_alignment=4
  164. #endif
  165. __ALIGN_BEGIN uint8_t USBD_StringSerial[USB_SIZ_STRING_SERIAL] __ALIGN_END = {
  166. USB_SIZ_STRING_SERIAL,
  167. USB_DESC_TYPE_STRING,
  168. };
  169. /**
  170. * @}
  171. */
  172. /** @defgroup USBD_DESC_Private_Functions USBD_DESC_Private_Functions
  173. * @brief Private functions.
  174. * @{
  175. */
  176. /**
  177. * @brief Return the device descriptor
  178. * @param speed : Current device speed
  179. * @param length : Pointer to data length variable
  180. * @retval Pointer to descriptor buffer
  181. */
  182. uint8_t * USBD_CDC_DeviceDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
  183. {
  184. UNUSED(speed);
  185. *length = sizeof(USBD_CDC_DeviceDesc);
  186. return USBD_CDC_DeviceDesc;
  187. }
  188. /**
  189. * @brief Return the LangID string descriptor
  190. * @param speed : Current device speed
  191. * @param length : Pointer to data length variable
  192. * @retval Pointer to descriptor buffer
  193. */
  194. uint8_t * USBD_CDC_LangIDStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
  195. {
  196. UNUSED(speed);
  197. *length = sizeof(USBD_LangIDDesc);
  198. return USBD_LangIDDesc;
  199. }
  200. /**
  201. * @brief Return the product string descriptor
  202. * @param speed : Current device speed
  203. * @param length : Pointer to data length variable
  204. * @retval Pointer to descriptor buffer
  205. */
  206. uint8_t * USBD_CDC_ProductStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
  207. {
  208. if(speed == 0)
  209. {
  210. USBD_GetString((uint8_t *)USBD_PRODUCT_STRING, USBD_StrDesc, length);
  211. }
  212. else
  213. {
  214. USBD_GetString((uint8_t *)USBD_PRODUCT_STRING, USBD_StrDesc, length);
  215. }
  216. return USBD_StrDesc;
  217. }
  218. /**
  219. * @brief Return the manufacturer string descriptor
  220. * @param speed : Current device speed
  221. * @param length : Pointer to data length variable
  222. * @retval Pointer to descriptor buffer
  223. */
  224. uint8_t * USBD_CDC_ManufacturerStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
  225. {
  226. UNUSED(speed);
  227. USBD_GetString((uint8_t *)USBD_MANUFACTURER_STRING, USBD_StrDesc, length);
  228. return USBD_StrDesc;
  229. }
  230. /**
  231. * @brief Return the serial number string descriptor
  232. * @param speed : Current device speed
  233. * @param length : Pointer to data length variable
  234. * @retval Pointer to descriptor buffer
  235. */
  236. uint8_t * USBD_CDC_SerialStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
  237. {
  238. UNUSED(speed);
  239. *length = USB_SIZ_STRING_SERIAL;
  240. /* Update the serial number string descriptor with the data from the unique
  241. * ID */
  242. if(api_hal_version_get_name_ptr()){
  243. char buffer[14] = "flip_";
  244. strncat(buffer, api_hal_version_get_name_ptr(), 8);
  245. USBD_GetString((uint8_t*) buffer, USBD_StringSerial, length);
  246. } else {
  247. Get_SerialNum();
  248. }
  249. /* USER CODE BEGIN USBD_CDC_SerialStrDescriptor */
  250. /* USER CODE END USBD_CDC_SerialStrDescriptor */
  251. return (uint8_t *) USBD_StringSerial;
  252. }
  253. /**
  254. * @brief Return the configuration string descriptor
  255. * @param speed : Current device speed
  256. * @param length : Pointer to data length variable
  257. * @retval Pointer to descriptor buffer
  258. */
  259. uint8_t * USBD_CDC_ConfigStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
  260. {
  261. if(speed == USBD_SPEED_HIGH)
  262. {
  263. USBD_GetString((uint8_t *)USBD_CONFIGURATION_STRING, USBD_StrDesc, length);
  264. }
  265. else
  266. {
  267. USBD_GetString((uint8_t *)USBD_CONFIGURATION_STRING, USBD_StrDesc, length);
  268. }
  269. return USBD_StrDesc;
  270. }
  271. /**
  272. * @brief Return the interface string descriptor
  273. * @param speed : Current device speed
  274. * @param length : Pointer to data length variable
  275. * @retval Pointer to descriptor buffer
  276. */
  277. uint8_t * USBD_CDC_InterfaceStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
  278. {
  279. if(speed == 0)
  280. {
  281. USBD_GetString((uint8_t *)USBD_INTERFACE_STRING, USBD_StrDesc, length);
  282. }
  283. else
  284. {
  285. USBD_GetString((uint8_t *)USBD_INTERFACE_STRING, USBD_StrDesc, length);
  286. }
  287. return USBD_StrDesc;
  288. }
  289. /**
  290. * @brief Create the serial number string descriptor
  291. * @param None
  292. * @retval None
  293. */
  294. static void Get_SerialNum(void)
  295. {
  296. uint32_t deviceserial0, deviceserial1, deviceserial2;
  297. deviceserial0 = *(uint32_t *) DEVICE_ID1;
  298. deviceserial1 = *(uint32_t *) DEVICE_ID2;
  299. deviceserial2 = *(uint32_t *) DEVICE_ID3;
  300. deviceserial0 += deviceserial2;
  301. if (deviceserial0 != 0)
  302. {
  303. IntToUnicode(deviceserial0, &USBD_StringSerial[2], 8);
  304. IntToUnicode(deviceserial1, &USBD_StringSerial[18], 4);
  305. }
  306. }
  307. /**
  308. * @brief Convert Hex 32Bits value into char
  309. * @param value: value to convert
  310. * @param pbuf: pointer to the buffer
  311. * @param len: buffer length
  312. * @retval None
  313. */
  314. static void IntToUnicode(uint32_t value, uint8_t * pbuf, uint8_t len)
  315. {
  316. uint8_t idx = 0;
  317. for (idx = 0; idx < len; idx++)
  318. {
  319. if (((value >> 28)) < 0xA)
  320. {
  321. pbuf[2 * idx] = (value >> 28) + '0';
  322. }
  323. else
  324. {
  325. pbuf[2 * idx] = (value >> 28) + 'A' - 10;
  326. }
  327. value = value << 4;
  328. pbuf[2 * idx + 1] = 0;
  329. }
  330. }
  331. /**
  332. * @}
  333. */
  334. /**
  335. * @}
  336. */
  337. /**
  338. * @}
  339. */
  340. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/