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