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
65 #define USBD_PRODUCT_STRING (uint8_t*)"SCSI2SD V6"
68 #define USBD_PRODUCT_STRING (uint8_t*)"SCSI2SD V6 2020"
72 #define USBD_PRODUCT_STRING (uint8_t*)"SCSI2SD V6 2021"
74 #define USBD_CONFIGURATION_STRING_FS (uint8_t*)"SCSI2SD Config"
75 #define USBD_INTERFACE_STRING_FS (uint8_t*)"SCSI2SD Interface"
76 #define USB_SIZ_BOS_DESC 0x0C
81 /** @defgroup USBD_DESC_Private_Macros
85 static void Get_SerialNum(void);
86 static void IntToUnicode(uint32_t value, uint8_t * pbuf, uint8_t len);
92 /** @defgroup USBD_DESC_Private_Variables
95 uint8_t * USBD_FS_DeviceDescriptor( USBD_SpeedTypeDef speed , uint16_t *length);
96 uint8_t * USBD_FS_LangIDStrDescriptor( USBD_SpeedTypeDef speed , uint16_t *length);
97 uint8_t * USBD_FS_ManufacturerStrDescriptor ( USBD_SpeedTypeDef speed , uint16_t *length);
98 uint8_t * USBD_FS_ProductStrDescriptor ( USBD_SpeedTypeDef speed , uint16_t *length);
99 uint8_t * USBD_FS_SerialStrDescriptor( USBD_SpeedTypeDef speed , uint16_t *length);
100 uint8_t * USBD_FS_ConfigStrDescriptor( USBD_SpeedTypeDef speed , uint16_t *length);
101 uint8_t * USBD_FS_InterfaceStrDescriptor( USBD_SpeedTypeDef speed , uint16_t *length);
102 #if (USBD_LPM_ENABLED == 1)
103 uint8_t * USBD_FS_USR_BOSDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
104 #endif /* (USBD_LPM_ENABLED == 1) */
107 USBD_DescriptorsTypeDef FS_Desc =
109 USBD_FS_DeviceDescriptor,
110 USBD_FS_LangIDStrDescriptor,
111 USBD_FS_ManufacturerStrDescriptor,
112 USBD_FS_ProductStrDescriptor,
113 USBD_FS_SerialStrDescriptor,
114 USBD_FS_ConfigStrDescriptor,
115 USBD_FS_InterfaceStrDescriptor,
116 #if (USBD_LPM_ENABLED == 1)
117 USBD_FS_USR_BOSDescriptor
118 #endif /* (USBD_LPM_ENABLED == 1) */
121 USBD_DescriptorsTypeDef HS_Desc =
123 USBD_FS_DeviceDescriptor,
124 USBD_FS_LangIDStrDescriptor,
125 USBD_FS_ManufacturerStrDescriptor,
126 USBD_FS_ProductStrDescriptor,
127 USBD_FS_SerialStrDescriptor,
128 USBD_FS_ConfigStrDescriptor,
129 USBD_FS_InterfaceStrDescriptor,
130 #if (USBD_LPM_ENABLED == 1)
131 USBD_FS_USR_BOSDescriptor
132 #endif /* (USBD_LPM_ENABLED == 1) */
135 #if defined ( __ICCARM__ ) /*!< IAR Compiler */
136 #pragma data_alignment=4
138 /* USB Standard Device Descriptor */
139 __ALIGN_BEGIN uint8_t USBD_FS_DeviceDesc[USB_LEN_DEV_DESC] __ALIGN_END =
142 USB_DESC_TYPE_DEVICE, /*bDescriptorType*/
143 #if (USBD_LPM_ENABLED == 1)
144 0x01, /*bcdUSB */ /* changed to USB version 2.01
145 in order to support LPM L1 suspend
146 resume test of USBCV3.0*/
149 #endif /* (USBD_LPM_ENABLED == 1) */
151 0x00, /*bDeviceClass*/
152 0x00, /*bDeviceSubClass*/
153 0x00, /*bDeviceProtocol*/
154 USB_MAX_EP0_SIZE, /*bMaxPacketSize*/
155 LOBYTE(USBD_VID), /*idVendor*/
156 HIBYTE(USBD_VID), /*idVendor*/
157 LOBYTE(USBD_PID_FS), /*idVendor*/
158 HIBYTE(USBD_PID_FS), /*idVendor*/
159 0x00, /*bcdDevice rel. 2.00*/
161 USBD_IDX_MFC_STR, /*Index of manufacturer string*/
162 USBD_IDX_PRODUCT_STR, /*Index of product string*/
163 USBD_IDX_SERIAL_STR, /*Index of serial number string*/
164 USBD_MAX_NUM_CONFIGURATION /*bNumConfigurations*/
166 /* USB_DeviceDescriptor */
168 /** BOS descriptor. */
169 #if (USBD_LPM_ENABLED == 1)
170 __ALIGN_BEGIN uint8_t USBD_FS_BOSDesc[USB_SIZ_BOS_DESC] __ALIGN_END =
176 0x1, /* 1 device capability*/
177 /* device capability*/
179 USB_DEVICE_CAPABITY_TYPE,
181 0x2, /* LPM capability bit set*/
186 #endif /* (USBD_LPM_ENABLED == 1) */
188 /* USB Standard Device Descriptor */
189 __ALIGN_BEGIN uint8_t USBD_LangIDDesc[USB_LEN_LANGID_STR_DESC] __ALIGN_END =
191 USB_LEN_LANGID_STR_DESC,
192 USB_DESC_TYPE_STRING,
193 LOBYTE(USBD_LANGID_STRING),
194 HIBYTE(USBD_LANGID_STRING),
197 #if defined ( __ICCARM__ ) /*!< IAR Compiler */
198 #pragma data_alignment=4
200 __ALIGN_BEGIN uint8_t USBD_StrDesc[USBD_MAX_STR_DESC_SIZ] __ALIGN_END;
205 /** @defgroup USBD_DESC_Private_FunctionPrototypes
209 #define USB_SIZ_STRING_SERIAL 0x1A
210 __ALIGN_BEGIN uint8_t USBD_StringSerial[USB_SIZ_STRING_SERIAL] __ALIGN_END = {
211 USB_SIZ_STRING_SERIAL,
212 USB_DESC_TYPE_STRING,
219 /** @defgroup USBD_DESC_Private_Functions
224 * @brief USBD_FS_DeviceDescriptor
225 * return the device descriptor
226 * @param speed : current device speed
227 * @param length : pointer to data length variable
228 * @retval pointer to descriptor buffer
230 uint8_t * USBD_FS_DeviceDescriptor( USBD_SpeedTypeDef speed , uint16_t *length)
232 *length = sizeof(USBD_FS_DeviceDesc);
233 return USBD_FS_DeviceDesc;
237 * @brief USBD_FS_LangIDStrDescriptor
238 * return the LangID string descriptor
239 * @param speed : current device speed
240 * @param length : pointer to data length variable
241 * @retval pointer to descriptor buffer
243 uint8_t * USBD_FS_LangIDStrDescriptor( USBD_SpeedTypeDef speed , uint16_t *length)
245 *length = sizeof(USBD_LangIDDesc);
246 return USBD_LangIDDesc;
250 * @brief USBD_FS_ProductStrDescriptor
251 * return the product string descriptor
252 * @param speed : current device speed
253 * @param length : pointer to data length variable
254 * @retval pointer to descriptor buffer
256 uint8_t * USBD_FS_ProductStrDescriptor( USBD_SpeedTypeDef speed , uint16_t *length)
258 USBD_GetString (USBD_PRODUCT_STRING, USBD_StrDesc, length);
263 * @brief USBD_FS_ManufacturerStrDescriptor
264 * return the manufacturer string descriptor
265 * @param speed : current device speed
266 * @param length : pointer to data length variable
267 * @retval pointer to descriptor buffer
269 uint8_t * USBD_FS_ManufacturerStrDescriptor( USBD_SpeedTypeDef speed , uint16_t *length)
271 USBD_GetString (USBD_MANUFACTURER_STRING, USBD_StrDesc, length);
276 * @brief USBD_FS_SerialStrDescriptor
277 * return the serial number string descriptor
278 * @param speed : current device speed
279 * @param length : pointer to data length variable
280 * @retval pointer to descriptor buffer
282 uint8_t * USBD_FS_SerialStrDescriptor( USBD_SpeedTypeDef speed , uint16_t *length)
284 *length = USB_SIZ_STRING_SERIAL;
286 // Update the serial number string descriptor with the data from the unique
290 return (uint8_t *) USBD_StringSerial;
294 * @brief Create the serial number string descriptor
298 static void Get_SerialNum(void)
300 uint32_t deviceserial0, deviceserial1, deviceserial2;
302 // UID_BASE good for STM32F2 and F4
304 #define UID_BASE 0x1FFF7A10
307 #define DEVICE_ID1 (UID_BASE)
308 #define DEVICE_ID2 (UID_BASE + 0x4)
309 #define DEVICE_ID3 (UID_BASE + 0x8)
311 deviceserial0 = *(uint32_t *) DEVICE_ID1;
312 deviceserial1 = *(uint32_t *) DEVICE_ID2;
313 deviceserial2 = *(uint32_t *) DEVICE_ID3;
315 deviceserial0 += deviceserial2;
317 if (deviceserial0 != 0)
319 IntToUnicode(deviceserial0, &USBD_StringSerial[2], 8);
320 IntToUnicode(deviceserial1, &USBD_StringSerial[18], 4);
325 * @brief Convert Hex 32Bits value into char
326 * @param value: value to convert
327 * @param pbuf: pointer to the buffer
328 * @param len: buffer length
331 static void IntToUnicode(uint32_t value, uint8_t * pbuf, uint8_t len)
335 for (idx = 0; idx < len; idx++)
337 if (((value >> 28)) < 0xA)
339 pbuf[2 * idx] = (value >> 28) + '0';
343 pbuf[2 * idx] = (value >> 28) + 'A' - 10;
348 pbuf[2 * idx + 1] = 0;
353 * @brief USBD_FS_ConfigStrDescriptor
354 * return the configuration string descriptor
355 * @param speed : current device speed
356 * @param length : pointer to data length variable
357 * @retval pointer to descriptor buffer
359 uint8_t * USBD_FS_ConfigStrDescriptor( USBD_SpeedTypeDef speed , uint16_t *length)
361 if(speed == USBD_SPEED_HIGH)
363 USBD_GetString (USBD_CONFIGURATION_STRING_FS, USBD_StrDesc, length);
367 USBD_GetString (USBD_CONFIGURATION_STRING_FS, USBD_StrDesc, length);
373 * @brief USBD_FS_InterfaceStrDescriptor
374 * return the interface string descriptor
375 * @param speed : current device speed
376 * @param length : pointer to data length variable
377 * @retval pointer to descriptor buffer
379 uint8_t * USBD_FS_InterfaceStrDescriptor( USBD_SpeedTypeDef speed , uint16_t *length)
383 USBD_GetString (USBD_INTERFACE_STRING_FS, USBD_StrDesc, length);
387 USBD_GetString (USBD_INTERFACE_STRING_FS, USBD_StrDesc, length);
392 #if (USBD_LPM_ENABLED == 1)
394 * @brief Return the BOS descriptor
395 * @param speed : Current device speed
396 * @param length : Pointer to data length variable
397 * @retval Pointer to descriptor buffer
399 uint8_t * USBD_FS_USR_BOSDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
402 *length = sizeof(USBD_FS_BOSDesc);
403 return (uint8_t*)USBD_FS_BOSDesc;
405 #endif /* (USBD_LPM_ENABLED == 1) */
419 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/