STAR
Cal STAR — firmware, DAQ server, and engine design
Loading...
Searching...
No Matches
usbd_desc.c
Go to the documentation of this file.
1/* USER CODE BEGIN Header */
2/**
3 ******************************************************************************
4 * @file : App/usbd_desc.c
5 * @version : v1.0_Cube
6 * @brief : This file implements the USB device descriptors.
7 ******************************************************************************
8 * @attention
9 *
10 * Copyright (c) 2026 STMicroelectronics.
11 * All rights reserved.
12 *
13 * This software is licensed under terms that can be found in the LICENSE file
14 * in the root directory of this software component.
15 * If no LICENSE file comes with this software, it is provided AS-IS.
16 *
17 ******************************************************************************
18 */
19/* USER CODE END Header */
20
21/* Includes ------------------------------------------------------------------*/
22#include "usbd_core.h"
23#include "usbd_desc.h"
24#include "usbd_conf.h"
25
26/* USER CODE BEGIN INCLUDE */
27
28/* USER CODE END INCLUDE */
29
30/* Private typedef -----------------------------------------------------------*/
31/* Private define ------------------------------------------------------------*/
32/* Private macro -------------------------------------------------------------*/
33
34/* USER CODE BEGIN PV */
35/* Private variables ---------------------------------------------------------*/
36
37/* USER CODE END PV */
38
39/** @addtogroup STM32_USB_OTG_DEVICE_LIBRARY
40 * @{
41 */
42
43/** @addtogroup USBD_DESC
44 * @{
45 */
46
47/** @defgroup USBD_DESC_Private_TypesDefinitions USBD_DESC_Private_TypesDefinitions
48 * @brief Private types.
49 * @{
50 */
51
52/* USER CODE BEGIN PRIVATE_TYPES */
53
54/* USER CODE END PRIVATE_TYPES */
55
56/**
57 * @}
58 */
59
60/** @defgroup USBD_DESC_Private_Defines USBD_DESC_Private_Defines
61 * @brief Private defines.
62 * @{
63 */
64
65#define USBD_VID 1155
66#define USBD_LANGID_STRING 1033
67#define USBD_MANUFACTURER_STRING "STMicroelectronics"
68#define USBD_PID_HS 22336
69#define USBD_PRODUCT_STRING_HS "STM32 Virtual ComPort"
70#define USBD_CONFIGURATION_STRING_HS "CDC Config"
71#define USBD_INTERFACE_STRING_HS "CDC Interface"
72
73#define USB_SIZ_BOS_DESC 0x0C
74
75/* USER CODE BEGIN PRIVATE_DEFINES */
76
77/* USER CODE END PRIVATE_DEFINES */
78
79/**
80 * @}
81 */
82
83/* USER CODE BEGIN 0 */
84
85/* USER CODE END 0 */
86
87/** @defgroup USBD_DESC_Private_Macros USBD_DESC_Private_Macros
88 * @brief Private macros.
89 * @{
90 */
91
92/* USER CODE BEGIN PRIVATE_MACRO */
93
94/* USER CODE END PRIVATE_MACRO */
95
96/**
97 * @}
98 */
99
100/** @defgroup USBD_DESC_Private_FunctionPrototypes USBD_DESC_Private_FunctionPrototypes
101 * @brief Private functions declaration.
102 * @{
103 */
104
105static void Get_SerialNum(void);
106static void IntToUnicode(uint32_t value, uint8_t * pbuf, uint8_t len);
107
108/**
109 * @}
110 */
111
112/** @defgroup USBD_DESC_Private_FunctionPrototypes USBD_DESC_Private_FunctionPrototypes
113 * @brief Private functions declaration for HS.
114 * @{
115 */
116
117uint8_t * USBD_HS_DeviceDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
118uint8_t * USBD_HS_LangIDStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
119uint8_t * USBD_HS_ManufacturerStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
120uint8_t * USBD_HS_ProductStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
121uint8_t * USBD_HS_SerialStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
122uint8_t * USBD_HS_ConfigStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
123uint8_t * USBD_HS_InterfaceStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
124
125/**
126 * @}
127 */
128
129/** @defgroup USBD_DESC_Private_Variables USBD_DESC_Private_Variables
130 * @brief Private variables.
131 * @{
132 */
133
144
145#if defined ( __ICCARM__ ) /* IAR Compiler */
146 #pragma data_alignment=4
147#endif /* defined ( __ICCARM__ ) */
148/** USB standard device descriptor. */
149__ALIGN_BEGIN uint8_t USBD_HS_DeviceDesc[USB_LEN_DEV_DESC] __ALIGN_END =
150{
151 0x12, /*bLength */
152 USB_DESC_TYPE_DEVICE, /*bDescriptorType*/
153 0x00, /*bcdUSB */
154
155 0x02,
156 0x02, /*bDeviceClass*/
157 0x02, /*bDeviceSubClass*/
158 0x00, /*bDeviceProtocol*/
159 USB_MAX_EP0_SIZE, /*bMaxPacketSize*/
160 LOBYTE(USBD_VID), /*idVendor*/
161 HIBYTE(USBD_VID), /*idVendor*/
162 LOBYTE(USBD_PID_HS), /*idProduct*/
163 HIBYTE(USBD_PID_HS), /*idProduct*/
164 0x00, /*bcdDevice rel. 2.00*/
165 0x02,
166 USBD_IDX_MFC_STR, /*Index of manufacturer string*/
167 USBD_IDX_PRODUCT_STR, /*Index of product string*/
168 USBD_IDX_SERIAL_STR, /*Index of serial number string*/
169 USBD_MAX_NUM_CONFIGURATION /*bNumConfigurations*/
170};
171
172/** BOS descriptor. */
173#if (USBD_LPM_ENABLED == 1)
174#if defined ( __ICCARM__ ) /* IAR Compiler */
175 #pragma data_alignment=4
176#endif /* defined ( __ICCARM__ ) */
177__ALIGN_BEGIN uint8_t USBD_HS_BOSDesc[USB_SIZ_BOS_DESC] __ALIGN_END =
178{
179 0x5,
181 0xC,
182 0x0,
183 0x1, /* 1 device capability */
184 /* device capability */
185 0x7,
187 0x2,
188 0x2, /*LPM capability bit set */
189 0x0,
190 0x0,
191 0x0
192};
193#endif /* (USBD_LPM_ENABLED == 1) */
194
195/**
196 * @}
197 */
198
199/** @defgroup USBD_DESC_Private_Variables USBD_DESC_Private_Variables
200 * @brief Private variables.
201 * @{
202 */
203
204#if defined ( __ICCARM__ ) /* IAR Compiler */
205 #pragma data_alignment=4
206#endif /* defined ( __ICCARM__ ) */
207
208/** USB lang identifier descriptor. */
209__ALIGN_BEGIN uint8_t USBD_LangIDDesc[USB_LEN_LANGID_STR_DESC] __ALIGN_END =
210{
215};
216
217#if defined ( __ICCARM__ ) /* IAR Compiler */
218 #pragma data_alignment=4
219#endif /* defined ( __ICCARM__ ) */
220/* Internal string descriptor. */
221__ALIGN_BEGIN uint8_t USBD_StrDesc[USBD_MAX_STR_DESC_SIZ] __ALIGN_END;
222
223#if defined ( __ICCARM__ ) /*!< IAR Compiler */
224 #pragma data_alignment=4
225#endif
226__ALIGN_BEGIN uint8_t USBD_StringSerial[USB_SIZ_STRING_SERIAL] __ALIGN_END = {
229};
230
231/**
232 * @}
233 */
234
235/** @defgroup USBD_DESC_Private_Functions USBD_DESC_Private_Functions
236 * @brief Private functions.
237 * @{
238 */
239
240/**
241 * @brief Return the device descriptor
242 * @param speed : Current device speed
243 * @param length : Pointer to data length variable
244 * @retval Pointer to descriptor buffer
245 */
246uint8_t * USBD_HS_DeviceDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
247{
248 UNUSED(speed);
249 *length = sizeof(USBD_HS_DeviceDesc);
250 return USBD_HS_DeviceDesc;
251}
252
253/**
254 * @brief Return the LangID string descriptor
255 * @param speed : Current device speed
256 * @param length : Pointer to data length variable
257 * @retval Pointer to descriptor buffer
258 */
259uint8_t * USBD_HS_LangIDStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
260{
261 UNUSED(speed);
262 *length = sizeof(USBD_LangIDDesc);
263 return USBD_LangIDDesc;
264}
265
266/**
267 * @brief Return the product string descriptor
268 * @param speed : current device speed
269 * @param length : pointer to data length variable
270 * @retval pointer to descriptor buffer
271 */
272uint8_t * USBD_HS_ProductStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
273{
274 if(speed == 0)
275 {
276 USBD_GetString((uint8_t *)USBD_PRODUCT_STRING_HS, USBD_StrDesc, length);
277 }
278 else
279 {
280 USBD_GetString((uint8_t *)USBD_PRODUCT_STRING_HS, USBD_StrDesc, length);
281 }
282 return USBD_StrDesc;
283}
284
285/**
286 * @brief Return the manufacturer string descriptor
287 * @param speed : Current device speed
288 * @param length : Pointer to data length variable
289 * @retval Pointer to descriptor buffer
290 */
291uint8_t * USBD_HS_ManufacturerStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
292{
293 UNUSED(speed);
294 USBD_GetString((uint8_t *)USBD_MANUFACTURER_STRING, USBD_StrDesc, length);
295 return USBD_StrDesc;
296}
297
298/**
299 * @brief Return the serial number string descriptor
300 * @param speed : Current device speed
301 * @param length : Pointer to data length variable
302 * @retval Pointer to descriptor buffer
303 */
304uint8_t * USBD_HS_SerialStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
305{
306 UNUSED(speed);
307 *length = USB_SIZ_STRING_SERIAL;
308
309 /* Update the serial number string descriptor with the data from the unique
310 * ID */
312 /* USER CODE BEGIN USBD_HS_SerialStrDescriptor */
313
314 /* USER CODE END USBD_HS_SerialStrDescriptor */
315
316 return (uint8_t *) USBD_StringSerial;
317}
318
319/**
320 * @brief Return the configuration string descriptor
321 * @param speed : Current device speed
322 * @param length : Pointer to data length variable
323 * @retval Pointer to descriptor buffer
324 */
325uint8_t * USBD_HS_ConfigStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
326{
327 if(speed == USBD_SPEED_HIGH)
328 {
329 USBD_GetString((uint8_t *)USBD_CONFIGURATION_STRING_HS, USBD_StrDesc, length);
330 }
331 else
332 {
333 USBD_GetString((uint8_t *)USBD_CONFIGURATION_STRING_HS, USBD_StrDesc, length);
334 }
335 return USBD_StrDesc;
336}
337
338/**
339 * @brief Return the interface string descriptor
340 * @param speed : Current device speed
341 * @param length : Pointer to data length variable
342 * @retval Pointer to descriptor buffer
343 */
344uint8_t * USBD_HS_InterfaceStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
345{
346 if(speed == 0)
347 {
348 USBD_GetString((uint8_t *)USBD_INTERFACE_STRING_HS, USBD_StrDesc, length);
349 }
350 else
351 {
352 USBD_GetString((uint8_t *)USBD_INTERFACE_STRING_HS, USBD_StrDesc, length);
353 }
354 return USBD_StrDesc;
355}
356
357#if (USBD_LPM_ENABLED == 1)
358/**
359 * @brief Return the BOS descriptor
360 * @param speed : Current device speed
361 * @param length : Pointer to data length variable
362 * @retval Pointer to descriptor buffer
363 */
364uint8_t * USBD_HS_USR_BOSDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
365{
366 UNUSED(speed);
367 *length = sizeof(USBD_HS_BOSDesc);
368 return (uint8_t*)USBD_HS_BOSDesc;
369}
370#endif /* (USBD_LPM_ENABLED == 1) */
371
372/**
373 * @brief Create the serial number string descriptor
374 * @param None
375 * @retval None
376 */
377static void Get_SerialNum(void)
378{
379 uint32_t deviceserial0;
380 uint32_t deviceserial1;
381 uint32_t deviceserial2;
382
383 deviceserial0 = *(uint32_t *) DEVICE_ID1;
384 deviceserial1 = *(uint32_t *) DEVICE_ID2;
385 deviceserial2 = *(uint32_t *) DEVICE_ID3;
386
387 deviceserial0 += deviceserial2;
388
389 if (deviceserial0 != 0)
390 {
391 IntToUnicode(deviceserial0, &USBD_StringSerial[2], 8);
392 IntToUnicode(deviceserial1, &USBD_StringSerial[18], 4);
393 }
394}
395
396/**
397 * @brief Convert Hex 32Bits value into char
398 * @param value: value to convert
399 * @param pbuf: pointer to the buffer
400 * @param len: buffer length
401 * @retval None
402 */
403static void IntToUnicode(uint32_t value, uint8_t * pbuf, uint8_t len)
404{
405 uint8_t idx = 0;
406
407 for (idx = 0; idx < len; idx++)
408 {
409 if (((value >> 28)) < 0xA)
410 {
411 pbuf[2 * idx] = (value >> 28) + '0';
412 }
413 else
414 {
415 pbuf[2 * idx] = (value >> 28) + 'A' - 10;
416 }
417
418 value = value << 4;
419
420 pbuf[2 * idx + 1] = 0;
421 }
422}
423/**
424 * @}
425 */
426
427/**
428 * @}
429 */
430
431/**
432 * @}
433 */
434
#define LOBYTE(x)
Definition usbd_def.h:442
#define HIBYTE(x)
Definition usbd_def.h:446
USBD_SpeedTypeDef
Definition usbd_def.h:272
@ USBD_SPEED_HIGH
Definition usbd_def.h:273
#define USBD_INTERFACE_STRING_HS
Definition usbd_desc.c:71
#define USBD_LANGID_STRING
Definition usbd_desc.c:66
#define USBD_CONFIGURATION_STRING_HS
Definition usbd_desc.c:70
#define USB_SIZ_BOS_DESC
Definition usbd_desc.c:73
#define USBD_PID_HS
Definition usbd_desc.c:68
#define USBD_VID
Definition usbd_desc.c:65
#define USBD_PRODUCT_STRING_HS
Definition usbd_desc.c:69
#define USBD_MANUFACTURER_STRING
Definition usbd_desc.c:67
uint8_t * USBD_HS_LangIDStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
Return the LangID string descriptor.
Definition usbd_desc.c:259
uint8_t * USBD_HS_ProductStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
Return the product string descriptor.
Definition usbd_desc.c:272
uint8_t * USBD_HS_InterfaceStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
Return the interface string descriptor.
Definition usbd_desc.c:344
uint8_t * USBD_HS_SerialStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
Return the serial number string descriptor.
Definition usbd_desc.c:304
uint8_t * USBD_HS_ManufacturerStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
Return the manufacturer string descriptor.
Definition usbd_desc.c:291
static void IntToUnicode(uint32_t value, uint8_t *pbuf, uint8_t len)
Convert Hex 32Bits value into char.
Definition usbd_desc.c:403
static void Get_SerialNum(void)
Create the serial number string descriptor.
Definition usbd_desc.c:377
uint8_t * USBD_HS_DeviceDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
Return the device descriptor.
Definition usbd_desc.c:246
uint8_t * USBD_HS_ConfigStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
Return the configuration string descriptor.
Definition usbd_desc.c:325
USBD_DescriptorsTypeDef HS_Desc
Descriptor for the Usb device.
Definition usbd_desc.c:134
#define DEVICE_ID3
Definition usbd_desc.h:50
#define USB_SIZ_STRING_SERIAL
Definition usbd_desc.h:52
#define DEVICE_ID2
Definition usbd_desc.h:49
#define DEVICE_ID1
Definition usbd_desc.h:48
void USBD_GetString(uint8_t *desc, uint8_t *unicode, uint16_t *len)
USBD_GetString Convert Ascii string into unicode one.
#define USBD_MAX_STR_DESC_SIZ
Definition usbd_ctlreq.c:50
#define USB_LEN_LANGID_STR_DESC
Definition usbd_def.h:99
#define USB_DESC_TYPE_DEVICE
Definition usbd_def.h:131
#define USB_LEN_DEV_DESC
Definition usbd_def.h:94
#define USB_MAX_EP0_SIZE
Definition usbd_def.h:157
#define USBD_IDX_PRODUCT_STR
Definition usbd_def.h:104
#define USBD_IDX_SERIAL_STR
Definition usbd_def.h:105
#define USB_DESC_TYPE_BOS
Definition usbd_def.h:139
#define USBD_IDX_MFC_STR
Definition usbd_def.h:103
#define USB_DESC_TYPE_STRING
Definition usbd_def.h:133
#define USB_DEVICE_CAPABITY_TYPE
Definition usbd_def.h:148
#define USBD_MAX_NUM_CONFIGURATION
Definition usbd_def.h:52
#define UNUSED(x)
#define __ALIGN_END
: Header for usbd_conf.c file.
Header file for usbd_core.c file.