usbd_desc.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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) 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 "Flipper"
  55. #define USBD_PID 22336
  56. #define USBD_PRODUCT_STRING "Flipper Control Virtual ComPort"
  57. #define USBD_CONFIGURATION_STRING "CDC Config"
  58. #define USBD_INTERFACE_STRING "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.
  86. * @{
  87. */
  88. uint8_t* USBD_CDC_DeviceDescriptor(USBD_SpeedTypeDef speed, uint16_t* length);
  89. uint8_t* USBD_CDC_LangIDStrDescriptor(USBD_SpeedTypeDef speed, uint16_t* length);
  90. uint8_t* USBD_CDC_ManufacturerStrDescriptor(USBD_SpeedTypeDef speed, uint16_t* length);
  91. uint8_t* USBD_CDC_ProductStrDescriptor(USBD_SpeedTypeDef speed, uint16_t* length);
  92. uint8_t* USBD_CDC_SerialStrDescriptor(USBD_SpeedTypeDef speed, uint16_t* length);
  93. uint8_t* USBD_CDC_ConfigStrDescriptor(USBD_SpeedTypeDef speed, uint16_t* length);
  94. uint8_t* USBD_CDC_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 CDC_Desc = {
  103. USBD_CDC_DeviceDescriptor,
  104. USBD_CDC_LangIDStrDescriptor,
  105. USBD_CDC_ManufacturerStrDescriptor,
  106. USBD_CDC_ProductStrDescriptor,
  107. USBD_CDC_SerialStrDescriptor,
  108. USBD_CDC_ConfigStrDescriptor,
  109. USBD_CDC_InterfaceStrDescriptor};
  110. #if defined(__ICCARM__) /* IAR Compiler */
  111. #pragma data_alignment = 4
  112. #endif /* defined ( __ICCARM__ ) */
  113. /** USB standard device descriptor. */
  114. __ALIGN_BEGIN uint8_t USBD_CDC_DeviceDesc[USB_LEN_DEV_DESC] __ALIGN_END = {
  115. 0x12, /*bLength */
  116. USB_DESC_TYPE_DEVICE, /*bDescriptorType*/
  117. 0x00, /*bcdUSB */
  118. 0x02,
  119. 0x02, /*bDeviceClass*/
  120. 0x02, /*bDeviceSubClass*/
  121. 0x00, /*bDeviceProtocol*/
  122. USB_MAX_EP0_SIZE, /*bMaxPacketSize*/
  123. LOBYTE(USBD_VID), /*idVendor*/
  124. HIBYTE(USBD_VID), /*idVendor*/
  125. LOBYTE(USBD_PID), /*idProduct*/
  126. HIBYTE(USBD_PID), /*idProduct*/
  127. 0x00, /*bcdDevice rel. 2.00*/
  128. 0x02,
  129. USBD_IDX_MFC_STR, /*Index of manufacturer string*/
  130. USBD_IDX_PRODUCT_STR, /*Index of product string*/
  131. USBD_IDX_SERIAL_STR, /*Index of serial number string*/
  132. USBD_MAX_NUM_CONFIGURATION /*bNumConfigurations*/
  133. };
  134. /* USB_DeviceDescriptor */
  135. /**
  136. * @}
  137. */
  138. /** @defgroup USBD_DESC_Private_Variables USBD_DESC_Private_Variables
  139. * @brief Private variables.
  140. * @{
  141. */
  142. #if defined(__ICCARM__) /* IAR Compiler */
  143. #pragma data_alignment = 4
  144. #endif /* defined ( __ICCARM__ ) */
  145. /** USB lang identifier descriptor. */
  146. __ALIGN_BEGIN uint8_t USBD_LangIDDesc[USB_LEN_LANGID_STR_DESC] __ALIGN_END = {
  147. USB_LEN_LANGID_STR_DESC,
  148. USB_DESC_TYPE_STRING,
  149. LOBYTE(USBD_LANGID_STRING),
  150. HIBYTE(USBD_LANGID_STRING)};
  151. #if defined(__ICCARM__) /* IAR Compiler */
  152. #pragma data_alignment = 4
  153. #endif /* defined ( __ICCARM__ ) */
  154. /* Internal string descriptor. */
  155. __ALIGN_BEGIN uint8_t USBD_StrDesc[USBD_MAX_STR_DESC_SIZ] __ALIGN_END;
  156. #if defined(__ICCARM__) /*!< IAR Compiler */
  157. #pragma data_alignment = 4
  158. #endif
  159. __ALIGN_BEGIN uint8_t USBD_StringSerial[USB_SIZ_STRING_SERIAL] __ALIGN_END = {
  160. USB_SIZ_STRING_SERIAL,
  161. USB_DESC_TYPE_STRING,
  162. };
  163. /**
  164. * @}
  165. */
  166. /** @defgroup USBD_DESC_Private_Functions USBD_DESC_Private_Functions
  167. * @brief Private functions.
  168. * @{
  169. */
  170. /**
  171. * @brief Return the device descriptor
  172. * @param speed : Current device speed
  173. * @param length : Pointer to data length variable
  174. * @retval Pointer to descriptor buffer
  175. */
  176. uint8_t* USBD_CDC_DeviceDescriptor(USBD_SpeedTypeDef speed, uint16_t* length) {
  177. UNUSED(speed);
  178. *length = sizeof(USBD_CDC_DeviceDesc);
  179. return USBD_CDC_DeviceDesc;
  180. }
  181. /**
  182. * @brief Return the LangID string descriptor
  183. * @param speed : Current device speed
  184. * @param length : Pointer to data length variable
  185. * @retval Pointer to descriptor buffer
  186. */
  187. uint8_t* USBD_CDC_LangIDStrDescriptor(USBD_SpeedTypeDef speed, uint16_t* length) {
  188. UNUSED(speed);
  189. *length = sizeof(USBD_LangIDDesc);
  190. return USBD_LangIDDesc;
  191. }
  192. /**
  193. * @brief Return the product string descriptor
  194. * @param speed : Current device speed
  195. * @param length : Pointer to data length variable
  196. * @retval Pointer to descriptor buffer
  197. */
  198. uint8_t* USBD_CDC_ProductStrDescriptor(USBD_SpeedTypeDef speed, uint16_t* length) {
  199. if(speed == 0) {
  200. USBD_GetString((uint8_t*)USBD_PRODUCT_STRING, USBD_StrDesc, length);
  201. } else {
  202. USBD_GetString((uint8_t*)USBD_PRODUCT_STRING, USBD_StrDesc, length);
  203. }
  204. return USBD_StrDesc;
  205. }
  206. /**
  207. * @brief Return the manufacturer string descriptor
  208. * @param speed : Current device speed
  209. * @param length : Pointer to data length variable
  210. * @retval Pointer to descriptor buffer
  211. */
  212. uint8_t* USBD_CDC_ManufacturerStrDescriptor(USBD_SpeedTypeDef speed, uint16_t* length) {
  213. UNUSED(speed);
  214. USBD_GetString((uint8_t*)USBD_MANUFACTURER_STRING, USBD_StrDesc, length);
  215. return USBD_StrDesc;
  216. }
  217. /**
  218. * @brief Return the serial number 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_CDC_SerialStrDescriptor(USBD_SpeedTypeDef speed, uint16_t* length) {
  224. UNUSED(speed);
  225. *length = USB_SIZ_STRING_SERIAL;
  226. /* Update the serial number string descriptor with the data from the unique
  227. * ID */
  228. Get_SerialNum();
  229. /* USER CODE BEGIN USBD_CDC_SerialStrDescriptor */
  230. /* USER CODE END USBD_CDC_SerialStrDescriptor */
  231. return (uint8_t*)USBD_StringSerial;
  232. }
  233. /**
  234. * @brief Return the configuration 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_CDC_ConfigStrDescriptor(USBD_SpeedTypeDef speed, uint16_t* length) {
  240. if(speed == USBD_SPEED_HIGH) {
  241. USBD_GetString((uint8_t*)USBD_CONFIGURATION_STRING, USBD_StrDesc, length);
  242. } else {
  243. USBD_GetString((uint8_t*)USBD_CONFIGURATION_STRING, USBD_StrDesc, length);
  244. }
  245. return USBD_StrDesc;
  246. }
  247. /**
  248. * @brief Return the interface string descriptor
  249. * @param speed : Current device speed
  250. * @param length : Pointer to data length variable
  251. * @retval Pointer to descriptor buffer
  252. */
  253. uint8_t* USBD_CDC_InterfaceStrDescriptor(USBD_SpeedTypeDef speed, uint16_t* length) {
  254. if(speed == 0) {
  255. USBD_GetString((uint8_t*)USBD_INTERFACE_STRING, USBD_StrDesc, length);
  256. } else {
  257. USBD_GetString((uint8_t*)USBD_INTERFACE_STRING, USBD_StrDesc, length);
  258. }
  259. return USBD_StrDesc;
  260. }
  261. /**
  262. * @brief Create the serial number string descriptor
  263. * @param None
  264. * @retval None
  265. */
  266. static void Get_SerialNum(void) {
  267. uint32_t deviceserial0, deviceserial1, deviceserial2;
  268. deviceserial0 = *(uint32_t*)DEVICE_ID1;
  269. deviceserial1 = *(uint32_t*)DEVICE_ID2;
  270. deviceserial2 = *(uint32_t*)DEVICE_ID3;
  271. deviceserial0 += deviceserial2;
  272. if(deviceserial0 != 0) {
  273. IntToUnicode(deviceserial0, &USBD_StringSerial[2], 8);
  274. IntToUnicode(deviceserial1, &USBD_StringSerial[18], 4);
  275. }
  276. }
  277. /**
  278. * @brief Convert Hex 32Bits value into char
  279. * @param value: value to convert
  280. * @param pbuf: pointer to the buffer
  281. * @param len: buffer length
  282. * @retval None
  283. */
  284. static void IntToUnicode(uint32_t value, uint8_t* pbuf, uint8_t len) {
  285. uint8_t idx = 0;
  286. for(idx = 0; idx < len; idx++) {
  287. if(((value >> 28)) < 0xA) {
  288. pbuf[2 * idx] = (value >> 28) + '0';
  289. } else {
  290. pbuf[2 * idx] = (value >> 28) + 'A' - 10;
  291. }
  292. value = value << 4;
  293. pbuf[2 * idx + 1] = 0;
  294. }
  295. }
  296. /**
  297. * @}
  298. */
  299. /**
  300. * @}
  301. */
  302. /**
  303. * @}
  304. */
  305. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/