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