2 ******************************************************************************
5 * @brief : This file implements the USB Device descriptors
6 ******************************************************************************
8 * COPYRIGHT(c) 2016 STMicroelectronics
10 * Redistribution and use in source and binary forms, with or without modification,
11 * are permitted provided that the following conditions are met:
12 * 1. Redistributions of source code must retain the above copyright notice,
13 * this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright notice,
15 * this list of conditions and the following disclaimer in the documentation
16 * and/or other materials provided with the distribution.
17 * 3. Neither the name of STMicroelectronics nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 ******************************************************************************
35 /* Includes ------------------------------------------------------------------*/
36 #include "usbd_core.h"
37 #include "usbd_desc.h"
38 #include "usbd_conf.h"
40 /** @addtogroup STM32_USB_OTG_DEVICE_LIBRARY
44 /** @defgroup USBD_DESC
45 * @brief USBD descriptors module
49 /** @defgroup USBD_DESC_Private_TypesDefinitions
56 /** @defgroup USBD_DESC_Private_Defines
59 #define USBD_VID 0x16D0
60 #define USBD_LANGID_STRING 1033
61 #define USBD_MANUFACTURER_STRING (uint8_t*)"codesrc.com"
62 #define USBD_PID_FS 0x0BD4
63 #define USBD_PRODUCT_STRING_FS (uint8_t*)"SCSI2SD 2020"
64 #define USBD_CONFIGURATION_STRING_FS (uint8_t*)"SCSI2SD Config"
65 #define USBD_INTERFACE_STRING_FS (uint8_t*)"SCSI2SD Interface"
66 #define USB_SIZ_BOS_DESC 0x0C
71 /** @defgroup USBD_DESC_Private_Macros
75 static void Get_SerialNum(void);
76 static void IntToUnicode(uint32_t value, uint8_t * pbuf, uint8_t len);
82 /** @defgroup USBD_DESC_Private_Variables
85 uint8_t * USBD_FS_DeviceDescriptor( USBD_SpeedTypeDef speed , uint16_t *length);
86 uint8_t * USBD_FS_LangIDStrDescriptor( USBD_SpeedTypeDef speed , uint16_t *length);
87 uint8_t * USBD_FS_ManufacturerStrDescriptor ( USBD_SpeedTypeDef speed , uint16_t *length);
88 uint8_t * USBD_FS_ProductStrDescriptor ( USBD_SpeedTypeDef speed , uint16_t *length);
89 uint8_t * USBD_FS_SerialStrDescriptor( USBD_SpeedTypeDef speed , uint16_t *length);
90 uint8_t * USBD_FS_ConfigStrDescriptor( USBD_SpeedTypeDef speed , uint16_t *length);
91 uint8_t * USBD_FS_InterfaceStrDescriptor( USBD_SpeedTypeDef speed , uint16_t *length);
92 #if (USBD_LPM_ENABLED == 1)
93 uint8_t * USBD_FS_USR_BOSDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
94 #endif /* (USBD_LPM_ENABLED == 1) */
97 USBD_DescriptorsTypeDef FS_Desc =
99 USBD_FS_DeviceDescriptor,
100 USBD_FS_LangIDStrDescriptor,
101 USBD_FS_ManufacturerStrDescriptor,
102 USBD_FS_ProductStrDescriptor,
103 USBD_FS_SerialStrDescriptor,
104 USBD_FS_ConfigStrDescriptor,
105 USBD_FS_InterfaceStrDescriptor,
106 #if (USBD_LPM_ENABLED == 1)
107 USBD_FS_USR_BOSDescriptor
108 #endif /* (USBD_LPM_ENABLED == 1) */
111 USBD_DescriptorsTypeDef HS_Desc =
113 USBD_FS_DeviceDescriptor,
114 USBD_FS_LangIDStrDescriptor,
115 USBD_FS_ManufacturerStrDescriptor,
116 USBD_FS_ProductStrDescriptor,
117 USBD_FS_SerialStrDescriptor,
118 USBD_FS_ConfigStrDescriptor,
119 USBD_FS_InterfaceStrDescriptor,
120 #if (USBD_LPM_ENABLED == 1)
121 USBD_FS_USR_BOSDescriptor
122 #endif /* (USBD_LPM_ENABLED == 1) */
125 #if defined ( __ICCARM__ ) /*!< IAR Compiler */
126 #pragma data_alignment=4
128 /* USB Standard Device Descriptor */
129 __ALIGN_BEGIN uint8_t USBD_FS_DeviceDesc[USB_LEN_DEV_DESC] __ALIGN_END =
132 USB_DESC_TYPE_DEVICE, /*bDescriptorType*/
133 #if (USBD_LPM_ENABLED == 1)
134 0x01, /*bcdUSB */ /* changed to USB version 2.01
135 in order to support LPM L1 suspend
136 resume test of USBCV3.0*/
139 #endif /* (USBD_LPM_ENABLED == 1) */
141 0x00, /*bDeviceClass*/
142 0x00, /*bDeviceSubClass*/
143 0x00, /*bDeviceProtocol*/
144 USB_MAX_EP0_SIZE, /*bMaxPacketSize*/
145 LOBYTE(USBD_VID), /*idVendor*/
146 HIBYTE(USBD_VID), /*idVendor*/
147 LOBYTE(USBD_PID_FS), /*idVendor*/
148 HIBYTE(USBD_PID_FS), /*idVendor*/
149 0x00, /*bcdDevice rel. 2.00*/
151 USBD_IDX_MFC_STR, /*Index of manufacturer string*/
152 USBD_IDX_PRODUCT_STR, /*Index of product string*/
153 USBD_IDX_SERIAL_STR, /*Index of serial number string*/
154 USBD_MAX_NUM_CONFIGURATION /*bNumConfigurations*/
156 /* USB_DeviceDescriptor */
158 /** BOS descriptor. */
159 #if (USBD_LPM_ENABLED == 1)
160 __ALIGN_BEGIN uint8_t USBD_FS_BOSDesc[USB_SIZ_BOS_DESC] __ALIGN_END =
166 0x1, /* 1 device capability*/
167 /* device capability*/
169 USB_DEVICE_CAPABITY_TYPE,
171 0x2, /* LPM capability bit set*/
176 #endif /* (USBD_LPM_ENABLED == 1) */
178 /* USB Standard Device Descriptor */
179 __ALIGN_BEGIN uint8_t USBD_LangIDDesc[USB_LEN_LANGID_STR_DESC] __ALIGN_END =
181 USB_LEN_LANGID_STR_DESC,
182 USB_DESC_TYPE_STRING,
183 LOBYTE(USBD_LANGID_STRING),
184 HIBYTE(USBD_LANGID_STRING),
187 #if defined ( __ICCARM__ ) /*!< IAR Compiler */
188 #pragma data_alignment=4
190 __ALIGN_BEGIN uint8_t USBD_StrDesc[USBD_MAX_STR_DESC_SIZ] __ALIGN_END;
195 /** @defgroup USBD_DESC_Private_FunctionPrototypes
199 #define USB_SIZ_STRING_SERIAL 0x1A
200 __ALIGN_BEGIN uint8_t USBD_StringSerial[USB_SIZ_STRING_SERIAL] __ALIGN_END = {
201 USB_SIZ_STRING_SERIAL,
202 USB_DESC_TYPE_STRING,
209 /** @defgroup USBD_DESC_Private_Functions
214 * @brief USBD_FS_DeviceDescriptor
215 * return the device descriptor
216 * @param speed : current device speed
217 * @param length : pointer to data length variable
218 * @retval pointer to descriptor buffer
220 uint8_t * USBD_FS_DeviceDescriptor( USBD_SpeedTypeDef speed , uint16_t *length)
222 *length = sizeof(USBD_FS_DeviceDesc);
223 return USBD_FS_DeviceDesc;
227 * @brief USBD_FS_LangIDStrDescriptor
228 * return the LangID string descriptor
229 * @param speed : current device speed
230 * @param length : pointer to data length variable
231 * @retval pointer to descriptor buffer
233 uint8_t * USBD_FS_LangIDStrDescriptor( USBD_SpeedTypeDef speed , uint16_t *length)
235 *length = sizeof(USBD_LangIDDesc);
236 return USBD_LangIDDesc;
240 * @brief USBD_FS_ProductStrDescriptor
241 * return the product string descriptor
242 * @param speed : current device speed
243 * @param length : pointer to data length variable
244 * @retval pointer to descriptor buffer
246 uint8_t * USBD_FS_ProductStrDescriptor( USBD_SpeedTypeDef speed , uint16_t *length)
250 USBD_GetString (USBD_PRODUCT_STRING_FS, USBD_StrDesc, length);
254 USBD_GetString (USBD_PRODUCT_STRING_FS, USBD_StrDesc, length);
260 * @brief USBD_FS_ManufacturerStrDescriptor
261 * return the manufacturer string descriptor
262 * @param speed : current device speed
263 * @param length : pointer to data length variable
264 * @retval pointer to descriptor buffer
266 uint8_t * USBD_FS_ManufacturerStrDescriptor( USBD_SpeedTypeDef speed , uint16_t *length)
268 USBD_GetString (USBD_MANUFACTURER_STRING, USBD_StrDesc, length);
273 * @brief USBD_FS_SerialStrDescriptor
274 * return the serial number string descriptor
275 * @param speed : current device speed
276 * @param length : pointer to data length variable
277 * @retval pointer to descriptor buffer
279 uint8_t * USBD_FS_SerialStrDescriptor( USBD_SpeedTypeDef speed , uint16_t *length)
281 *length = USB_SIZ_STRING_SERIAL;
283 // Update the serial number string descriptor with the data from the unique
287 return (uint8_t *) USBD_StringSerial;
291 * @brief Create the serial number string descriptor
295 static void Get_SerialNum(void)
297 uint32_t deviceserial0, deviceserial1, deviceserial2;
299 // UID_BASE good for STM32F2 and F4
301 #define UID_BASE 0x1FFF7A10
304 #define DEVICE_ID1 (UID_BASE)
305 #define DEVICE_ID2 (UID_BASE + 0x4)
306 #define DEVICE_ID3 (UID_BASE + 0x8)
308 deviceserial0 = *(uint32_t *) DEVICE_ID1;
309 deviceserial1 = *(uint32_t *) DEVICE_ID2;
310 deviceserial2 = *(uint32_t *) DEVICE_ID3;
312 deviceserial0 += deviceserial2;
314 if (deviceserial0 != 0)
316 IntToUnicode(deviceserial0, &USBD_StringSerial[2], 8);
317 IntToUnicode(deviceserial1, &USBD_StringSerial[18], 4);
322 * @brief Convert Hex 32Bits value into char
323 * @param value: value to convert
324 * @param pbuf: pointer to the buffer
325 * @param len: buffer length
328 static void IntToUnicode(uint32_t value, uint8_t * pbuf, uint8_t len)
332 for (idx = 0; idx < len; idx++)
334 if (((value >> 28)) < 0xA)
336 pbuf[2 * idx] = (value >> 28) + '0';
340 pbuf[2 * idx] = (value >> 28) + 'A' - 10;
345 pbuf[2 * idx + 1] = 0;
350 * @brief USBD_FS_ConfigStrDescriptor
351 * return the configuration string descriptor
352 * @param speed : current device speed
353 * @param length : pointer to data length variable
354 * @retval pointer to descriptor buffer
356 uint8_t * USBD_FS_ConfigStrDescriptor( USBD_SpeedTypeDef speed , uint16_t *length)
358 if(speed == USBD_SPEED_HIGH)
360 USBD_GetString (USBD_CONFIGURATION_STRING_FS, USBD_StrDesc, length);
364 USBD_GetString (USBD_CONFIGURATION_STRING_FS, USBD_StrDesc, length);
370 * @brief USBD_FS_InterfaceStrDescriptor
371 * return the interface string descriptor
372 * @param speed : current device speed
373 * @param length : pointer to data length variable
374 * @retval pointer to descriptor buffer
376 uint8_t * USBD_FS_InterfaceStrDescriptor( USBD_SpeedTypeDef speed , uint16_t *length)
380 USBD_GetString (USBD_INTERFACE_STRING_FS, USBD_StrDesc, length);
384 USBD_GetString (USBD_INTERFACE_STRING_FS, USBD_StrDesc, length);
389 #if (USBD_LPM_ENABLED == 1)
391 * @brief Return the BOS descriptor
392 * @param speed : Current device speed
393 * @param length : Pointer to data length variable
394 * @retval Pointer to descriptor buffer
396 uint8_t * USBD_FS_USR_BOSDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
399 *length = sizeof(USBD_FS_BOSDesc);
400 return (uint8_t*)USBD_FS_BOSDesc;
402 #endif /* (USBD_LPM_ENABLED == 1) */
416 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/