1 /* USER CODE BEGIN Header */
2 /**
3 ******************************************************************************
4 * @file : usbd_desc.c
5 * @version : v2.0_Cube
6 * @brief : This file implements the USB device descriptors.
7 ******************************************************************************
8 * @attention
9 *
10 * <h2><center>© Copyright (c) 2019 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
22 /* Includes ------------------------------------------------------------------*/
23 #include "usbd_core.h"
24 #include "usbd_desc.h"
25 #include "usbd_conf.h"
26
27 /* USER CODE BEGIN INCLUDE */
28
29 /* USER CODE END INCLUDE */
30
31 /* Private typedef -----------------------------------------------------------*/
32 /* Private define ------------------------------------------------------------*/
33 /* Private macro -------------------------------------------------------------*/
34
35 /* USER CODE BEGIN PV */
36 /* Private variables ---------------------------------------------------------*/
37
38 /* USER CODE END PV */
39
40 /** @addtogroup STM32_USB_OTG_DEVICE_LIBRARY
41 * @{
42 */
43
44 /** @addtogroup USBD_DESC
45 * @{
46 */
47
48 /** @defgroup USBD_DESC_Private_TypesDefinitions USBD_DESC_Private_TypesDefinitions
49 * @brief Private types.
50 * @{
51 */
52
53 /* USER CODE BEGIN PRIVATE_TYPES */
54
55 /* USER CODE END PRIVATE_TYPES */
56
57 /**
58 * @}
59 */
60
61 /** @defgroup USBD_DESC_Private_Defines USBD_DESC_Private_Defines
62 * @brief Private defines.
63 * @{
64 */
65
66 #define USBD_VID 1155
67 #define USBD_LANGID_STRING 1033
68 #define USBD_MANUFACTURER_STRING "STMicroelectronics"
69 #define USBD_PID_FS 22336
70 #define USBD_PRODUCT_STRING_FS "STM32 Virtual ComPort"
71 #define USBD_CONFIGURATION_STRING_FS "CDC Config"
72 #define USBD_INTERFACE_STRING_FS "CDC Interface"
73
74 /* USER CODE BEGIN PRIVATE_DEFINES */
75
76 /* USER CODE END PRIVATE_DEFINES */
77
78 /**
79 * @}
80 */
81
82 /* USER CODE BEGIN 0 */
83
84 /* USER CODE END 0 */
85
86 /** @defgroup USBD_DESC_Private_Macros USBD_DESC_Private_Macros
87 * @brief Private macros.
88 * @{
89 */
90
91 /* USER CODE BEGIN PRIVATE_MACRO */
92
93 /* USER CODE END PRIVATE_MACRO */
94
95 /**
96 * @}
97 */
98
99 /** @defgroup USBD_DESC_Private_FunctionPrototypes USBD_DESC_Private_FunctionPrototypes
100 * @brief Private functions declaration.
101 * @{
102 */
103
104 static void Get_SerialNum(void);
105 static void IntToUnicode(uint32_t value, uint8_t * pbuf, uint8_t len);
106
107 /**
108 * @}
109 */
110
111
112 /** @defgroup USBD_DESC_Private_FunctionPrototypes USBD_DESC_Private_FunctionPrototypes
113 * @brief Private functions declaration for FS.
114 * @{
115 */
116
117 uint8_t * USBD_FS_DeviceDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
118 uint8_t * USBD_FS_LangIDStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
119 uint8_t * USBD_FS_ManufacturerStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
120 uint8_t * USBD_FS_ProductStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
121 uint8_t * USBD_FS_SerialStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
122 uint8_t * USBD_FS_ConfigStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
123 uint8_t * USBD_FS_InterfaceStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
124
125 #ifdef USBD_SUPPORT_USER_STRING_DESC
126 uint8_t * USBD_FS_USRStringDesc(USBD_SpeedTypeDef speed, uint8_t idx, uint16_t *length);
127 #endif /* USBD_SUPPORT_USER_STRING_DESC */
128
129 /**
130 * @}
131 */
132
133 /** @defgroup USBD_DESC_Private_Variables USBD_DESC_Private_Variables
134 * @brief Private variables.
135 * @{
136 */
137
138 USBD_DescriptorsTypeDef FS_Desc =
139 {
140 USBD_FS_DeviceDescriptor
141 , USBD_FS_LangIDStrDescriptor
142 , USBD_FS_ManufacturerStrDescriptor
143 , USBD_FS_ProductStrDescriptor
144 , USBD_FS_SerialStrDescriptor
145 , USBD_FS_ConfigStrDescriptor
146 , USBD_FS_InterfaceStrDescriptor
147 };
148
149 #if defined ( __ICCARM__ ) /* IAR Compiler */
150 #pragma data_alignment=4
151 #endif /* defined ( __ICCARM__ ) */
152 /** USB standard device descriptor. */
153 __ALIGN_BEGIN uint8_t USBD_FS_DeviceDesc[USB_LEN_DEV_DESC] __ALIGN_END =
154 {
155 0x12, /*bLength */
156 USB_DESC_TYPE_DEVICE, /*bDescriptorType*/
157 0x00, /*bcdUSB */
158 0x02,
159 0x02, /*bDeviceClass*/
160 0x02, /*bDeviceSubClass*/
161 0x00, /*bDeviceProtocol*/
162 USB_MAX_EP0_SIZE, /*bMaxPacketSize*/
163 LOBYTE(USBD_VID), /*idVendor*/
164 HIBYTE(USBD_VID), /*idVendor*/
165 LOBYTE(USBD_PID_FS), /*idProduct*/
166 HIBYTE(USBD_PID_FS), /*idProduct*/
167 0x00, /*bcdDevice rel. 2.00*/
168 0x02,
169 USBD_IDX_MFC_STR, /*Index of manufacturer string*/
170 USBD_IDX_PRODUCT_STR, /*Index of product string*/
171 USBD_IDX_SERIAL_STR, /*Index of serial number string*/
172 USBD_MAX_NUM_CONFIGURATION /*bNumConfigurations*/
173 };
174
175 /* USB_DeviceDescriptor */
176
177 /**
178 * @}
179 */
180
181 /** @defgroup USBD_DESC_Private_Variables USBD_DESC_Private_Variables
182 * @brief Private variables.
183 * @{
184 */
185
186 #if defined ( __ICCARM__ ) /* IAR Compiler */
187 #pragma data_alignment=4
188 #endif /* defined ( __ICCARM__ ) */
189
190 /** USB lang indentifier descriptor. */
191 __ALIGN_BEGIN uint8_t USBD_LangIDDesc[USB_LEN_LANGID_STR_DESC] __ALIGN_END =
192 {
193 USB_LEN_LANGID_STR_DESC,
194 USB_DESC_TYPE_STRING,
195 LOBYTE(USBD_LANGID_STRING),
196 HIBYTE(USBD_LANGID_STRING)
197 };
198
199 #if defined ( __ICCARM__ ) /* IAR Compiler */
200 #pragma data_alignment=4
201 #endif /* defined ( __ICCARM__ ) */
202 /* Internal string descriptor. */
203 __ALIGN_BEGIN uint8_t USBD_StrDesc[USBD_MAX_STR_DESC_SIZ] __ALIGN_END;
204
205 #if defined ( __ICCARM__ ) /*!< IAR Compiler */
206 #pragma data_alignment=4
207 #endif
208 __ALIGN_BEGIN uint8_t USBD_StringSerial[USB_SIZ_STRING_SERIAL] __ALIGN_END = {
209 USB_SIZ_STRING_SERIAL,
210 USB_DESC_TYPE_STRING,
211 };
212
213 /**
214 * @}
215 */
216
217 /** @defgroup USBD_DESC_Private_Functions USBD_DESC_Private_Functions
218 * @brief Private functions.
219 * @{
220 */
221
222 /**
223 * @brief Return the device descriptor
224 * @param speed : Current device speed
225 * @param length : Pointer to data length variable
226 * @retval Pointer to descriptor buffer
227 */
USBD_FS_DeviceDescriptor(USBD_SpeedTypeDef speed,uint16_t * length)228 uint8_t * USBD_FS_DeviceDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
229 {
230 UNUSED(speed);
231 *length = sizeof(USBD_FS_DeviceDesc);
232 return USBD_FS_DeviceDesc;
233 }
234
235 /**
236 * @brief Return the LangID string descriptor
237 * @param speed : Current device speed
238 * @param length : Pointer to data length variable
239 * @retval Pointer to descriptor buffer
240 */
USBD_FS_LangIDStrDescriptor(USBD_SpeedTypeDef speed,uint16_t * length)241 uint8_t * USBD_FS_LangIDStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
242 {
243 UNUSED(speed);
244 *length = sizeof(USBD_LangIDDesc);
245 return USBD_LangIDDesc;
246 }
247
248 /**
249 * @brief Return the product string descriptor
250 * @param speed : Current device speed
251 * @param length : Pointer to data length variable
252 * @retval Pointer to descriptor buffer
253 */
USBD_FS_ProductStrDescriptor(USBD_SpeedTypeDef speed,uint16_t * length)254 uint8_t * USBD_FS_ProductStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
255 {
256 if(speed == 0)
257 {
258 USBD_GetString((uint8_t *)USBD_PRODUCT_STRING_FS, USBD_StrDesc, length);
259 }
260 else
261 {
262 USBD_GetString((uint8_t *)USBD_PRODUCT_STRING_FS, USBD_StrDesc, length);
263 }
264 return USBD_StrDesc;
265 }
266
267 /**
268 * @brief Return the manufacturer string descriptor
269 * @param speed : Current device speed
270 * @param length : Pointer to data length variable
271 * @retval Pointer to descriptor buffer
272 */
USBD_FS_ManufacturerStrDescriptor(USBD_SpeedTypeDef speed,uint16_t * length)273 uint8_t * USBD_FS_ManufacturerStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
274 {
275 UNUSED(speed);
276 USBD_GetString((uint8_t *)USBD_MANUFACTURER_STRING, USBD_StrDesc, length);
277 return USBD_StrDesc;
278 }
279
280 /**
281 * @brief Return the serial number string descriptor
282 * @param speed : Current device speed
283 * @param length : Pointer to data length variable
284 * @retval Pointer to descriptor buffer
285 */
USBD_FS_SerialStrDescriptor(USBD_SpeedTypeDef speed,uint16_t * length)286 uint8_t * USBD_FS_SerialStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
287 {
288 UNUSED(speed);
289 *length = USB_SIZ_STRING_SERIAL;
290
291 /* Update the serial number string descriptor with the data from the unique
292 * ID */
293 Get_SerialNum();
294 /* USER CODE BEGIN USBD_FS_SerialStrDescriptor */
295
296 /* USER CODE END USBD_FS_SerialStrDescriptor */
297 return (uint8_t *) USBD_StringSerial;
298 }
299
300 /**
301 * @brief Return the configuration string descriptor
302 * @param speed : Current device speed
303 * @param length : Pointer to data length variable
304 * @retval Pointer to descriptor buffer
305 */
USBD_FS_ConfigStrDescriptor(USBD_SpeedTypeDef speed,uint16_t * length)306 uint8_t * USBD_FS_ConfigStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
307 {
308 if(speed == USBD_SPEED_HIGH)
309 {
310 USBD_GetString((uint8_t *)USBD_CONFIGURATION_STRING_FS, USBD_StrDesc, length);
311 }
312 else
313 {
314 USBD_GetString((uint8_t *)USBD_CONFIGURATION_STRING_FS, USBD_StrDesc, length);
315 }
316 return USBD_StrDesc;
317 }
318
319 /**
320 * @brief Return the interface string descriptor
321 * @param speed : Current device speed
322 * @param length : Pointer to data length variable
323 * @retval Pointer to descriptor buffer
324 */
USBD_FS_InterfaceStrDescriptor(USBD_SpeedTypeDef speed,uint16_t * length)325 uint8_t * USBD_FS_InterfaceStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
326 {
327 if(speed == 0)
328 {
329 USBD_GetString((uint8_t *)USBD_INTERFACE_STRING_FS, USBD_StrDesc, length);
330 }
331 else
332 {
333 USBD_GetString((uint8_t *)USBD_INTERFACE_STRING_FS, USBD_StrDesc, length);
334 }
335 return USBD_StrDesc;
336 }
337
338 /**
339 * @brief Create the serial number string descriptor
340 * @param None
341 * @retval None
342 */
Get_SerialNum(void)343 static void Get_SerialNum(void)
344 {
345 uint32_t deviceserial0, deviceserial1, deviceserial2;
346
347 deviceserial0 = *(uint32_t *) DEVICE_ID1;
348 deviceserial1 = *(uint32_t *) DEVICE_ID2;
349 deviceserial2 = *(uint32_t *) DEVICE_ID3;
350
351 deviceserial0 += deviceserial2;
352
353 if (deviceserial0 != 0)
354 {
355 IntToUnicode(deviceserial0, &USBD_StringSerial[2], 8);
356 IntToUnicode(deviceserial1, &USBD_StringSerial[18], 4);
357 }
358 }
359
360 /**
361 * @brief Convert Hex 32Bits value into char
362 * @param value: value to convert
363 * @param pbuf: pointer to the buffer
364 * @param len: buffer length
365 * @retval None
366 */
IntToUnicode(uint32_t value,uint8_t * pbuf,uint8_t len)367 static void IntToUnicode(uint32_t value, uint8_t * pbuf, uint8_t len)
368 {
369 uint8_t idx = 0;
370
371 for (idx = 0; idx < len; idx++)
372 {
373 if (((value >> 28)) < 0xA)
374 {
375 pbuf[2 * idx] = (value >> 28) + '0';
376 }
377 else
378 {
379 pbuf[2 * idx] = (value >> 28) + 'A' - 10;
380 }
381
382 value = value << 4;
383
384 pbuf[2 * idx + 1] = 0;
385 }
386 }
387 /**
388 * @}
389 */
390
391 /**
392 * @}
393 */
394
395 /**
396 * @}
397 */
398
399 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
400