Merge pull request #11500 from klutvott123/crsf-baud-negotiation-improvements-2
[betaflight.git] / src / main / vcp_hal / usbd_desc.c
blobe30f1b7447eb0ea56f386cfd0e7871e7f6f7d88d
1 /**
2 ******************************************************************************
3 * @file USB_Device/CDC_Standalone/Src/usbd_desc.c
4 * @author MCD Application Team
5 * @version V1.0.0
6 * @date 22-April-2016
7 * @brief This file provides the USBD descriptors and string formating method.
8 ******************************************************************************
9 * @attention
11 * <h2><center>&copy; Copyright (c) 2016 STMicroelectronics International N.V.
12 * All rights reserved.</center></h2>
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted, provided that the following conditions are met:
17 * 1. Redistribution of source code must retain the above copyright notice,
18 * this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright notice,
20 * this list of conditions and the following disclaimer in the documentation
21 * and/or other materials provided with the distribution.
22 * 3. Neither the name of STMicroelectronics nor the names of other
23 * contributors to this software may be used to endorse or promote products
24 * derived from this software without specific written permission.
25 * 4. This software, including modifications and/or derivative works of this
26 * software, must execute solely and exclusively on microcontroller or
27 * microprocessor devices manufactured by or for STMicroelectronics.
28 * 5. Redistribution and use of this software other than as permitted under
29 * this license is void and will automatically terminate your rights under
30 * this license.
32 * THIS SOFTWARE IS PROVIDED BY STMICROELECTRONICS AND CONTRIBUTORS "AS IS"
33 * AND ANY EXPRESS, IMPLIED OR STATUTORY WARRANTIES, INCLUDING, BUT NOT
34 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
35 * PARTICULAR PURPOSE AND NON-INFRINGEMENT OF THIRD PARTY INTELLECTUAL PROPERTY
36 * RIGHTS ARE DISCLAIMED TO THE FULLEST EXTENT PERMITTED BY LAW. IN NO EVENT
37 * SHALL STMICROELECTRONICS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
38 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
39 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
40 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
41 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
42 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
43 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
45 ******************************************************************************
48 /* Includes ------------------------------------------------------------------*/
49 #include "usbd_core.h"
50 #include "usbd_desc.h"
51 #include "usbd_conf.h"
52 #include "platform.h"
53 #include "build/version.h"
55 #include "pg/pg.h"
56 #include "pg/usb.h"
57 #ifdef USE_USB_MSC
58 #include "drivers/usb_msc.h"
59 #endif
61 /* Private typedef -----------------------------------------------------------*/
62 /* Private define ------------------------------------------------------------*/
63 #define USBD_VID 0x0483
64 #define USBD_PID 0x5740
65 #define USBD_LANGID_STRING 0x409
66 #define USBD_MANUFACTURER_STRING FC_FIRMWARE_NAME
68 #ifdef USBD_PRODUCT_STRING
69 #define USBD_PRODUCT_HS_STRING USBD_PRODUCT_STRING
70 #define USBD_PRODUCT_FS_STRING USBD_PRODUCT_STRING
71 #else
72 #define USBD_PRODUCT_HS_STRING "STM32 Virtual ComPort in HS Mode"
73 #define USBD_PRODUCT_FS_STRING "STM32 Virtual ComPort in FS Mode"
74 #endif
76 #define USBD_CONFIGURATION_HS_STRING "VCP Config"
77 #define USBD_INTERFACE_HS_STRING "VCP Interface"
78 #define USBD_CONFIGURATION_FS_STRING "VCP Config"
79 #define USBD_INTERFACE_FS_STRING "VCP Interface"
81 /* Private macro -------------------------------------------------------------*/
82 /* Private function prototypes -----------------------------------------------*/
83 uint8_t *USBD_VCP_DeviceDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
84 uint8_t *USBD_VCP_LangIDStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
85 uint8_t *USBD_VCP_ManufacturerStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
86 uint8_t *USBD_VCP_ProductStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
87 uint8_t *USBD_VCP_SerialStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
88 uint8_t *USBD_VCP_ConfigStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
89 uint8_t *USBD_VCP_InterfaceStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
90 #ifdef USB_SUPPORT_USER_STRING_DESC
91 uint8_t *USBD_VCP_USRStringDesc (USBD_SpeedTypeDef speed, uint8_t idx, uint16_t *length);
92 #endif /* USB_SUPPORT_USER_STRING_DESC */
94 /* Private variables ---------------------------------------------------------*/
95 USBD_DescriptorsTypeDef VCP_Desc = {
96 USBD_VCP_DeviceDescriptor,
97 USBD_VCP_LangIDStrDescriptor,
98 USBD_VCP_ManufacturerStrDescriptor,
99 USBD_VCP_ProductStrDescriptor,
100 USBD_VCP_SerialStrDescriptor,
101 USBD_VCP_ConfigStrDescriptor,
102 USBD_VCP_InterfaceStrDescriptor,
105 /* USB Standard Device Descriptor */
106 #if defined ( __ICCARM__ ) /*!< IAR Compiler */
107 #pragma data_alignment=4
108 #endif
109 __ALIGN_BEGIN uint8_t USBD_DeviceDesc[USB_LEN_DEV_DESC] __ALIGN_END = {
110 0x12, /* bLength */
111 USB_DESC_TYPE_DEVICE, /* bDescriptorType */
112 0x00, /* bcdUSB */
113 0x02,
114 0x02, /* bDeviceClass */
115 0x00, /* bDeviceSubClass */
116 0x00, /* bDeviceProtocol */
117 USB_MAX_EP0_SIZE, /* bMaxPacketSize */
118 LOBYTE(USBD_VID), /* idVendor */
119 HIBYTE(USBD_VID), /* idVendor */
120 LOBYTE(USBD_PID), /* idVendor */
121 HIBYTE(USBD_PID), /* idVendor */
122 0x00, /* bcdDevice rel. 2.00 */
123 0x02,
124 USBD_IDX_MFC_STR, /* Index of manufacturer string */
125 USBD_IDX_PRODUCT_STR, /* Index of product string */
126 USBD_IDX_SERIAL_STR, /* Index of serial number string */
127 USBD_MAX_NUM_CONFIGURATION /* bNumConfigurations */
128 }; /* USB_DeviceDescriptor */
130 #ifdef USE_USB_CDC_HID
131 extern uint8_t USBD_HID_CDC_DeviceDescriptor[USB_LEN_DEV_DESC];
132 #endif
134 #ifdef USE_USB_MSC
136 #define USBD_PID_MSC 22314
138 __ALIGN_BEGIN uint8_t USBD_MSC_DeviceDesc[USB_LEN_DEV_DESC] __ALIGN_END =
140 0x12, /*bLength */
141 USB_DESC_TYPE_DEVICE, /*bDescriptorType*/
142 0x00, /*bcdUSB */
143 0x02,
144 0x00, /*bDeviceClass*/
145 0x00, /*bDeviceSubClass*/
146 0x00, /*bDeviceProtocol*/
147 USB_MAX_EP0_SIZE, /*bMaxPacketSize*/
148 LOBYTE(USBD_VID), /*idVendor*/
149 HIBYTE(USBD_VID), /*idVendor*/
150 LOBYTE(USBD_PID_MSC), /*idProduct*/
151 HIBYTE(USBD_PID_MSC), /*idProduct*/
152 0x00, /*bcdDevice rel. 2.00*/
153 0x02,
154 USBD_IDX_MFC_STR, /*Index of manufacturer string*/
155 USBD_IDX_PRODUCT_STR, /*Index of product string*/
156 USBD_IDX_SERIAL_STR, /*Index of serial number string*/
157 USBD_MAX_NUM_CONFIGURATION /*bNumConfigurations*/
159 #endif
161 /* USB Standard Device Descriptor */
162 #if defined ( __ICCARM__ ) /*!< IAR Compiler */
163 #pragma data_alignment=4
164 #endif
165 __ALIGN_BEGIN uint8_t USBD_LangIDDesc[USB_LEN_LANGID_STR_DESC] __ALIGN_END = {
166 USB_LEN_LANGID_STR_DESC,
167 USB_DESC_TYPE_STRING,
168 LOBYTE(USBD_LANGID_STRING),
169 HIBYTE(USBD_LANGID_STRING),
172 uint8_t USBD_StringSerial[USB_SIZ_STRING_SERIAL] =
174 USB_SIZ_STRING_SERIAL,
175 USB_DESC_TYPE_STRING,
178 #if defined ( __ICCARM__ ) /*!< IAR Compiler */
179 #pragma data_alignment=4
180 #endif
181 __ALIGN_BEGIN uint8_t USBD_StrDesc[USBD_MAX_STR_DESC_SIZ] __ALIGN_END;
183 /* Private functions ---------------------------------------------------------*/
184 static void IntToUnicode (uint32_t value , uint8_t *pbuf , uint8_t len);
185 static void Get_SerialNum(void);
188 * @brief Returns the device descriptor.
189 * @param speed: Current device speed
190 * @param length: Pointer to data length variable
191 * @retval Pointer to descriptor buffer
193 uint8_t *USBD_VCP_DeviceDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
195 (void)speed;
196 #ifdef USE_USB_MSC
197 if (mscCheckBootAndReset()) {
198 *length = sizeof(USBD_MSC_DeviceDesc);
199 return USBD_MSC_DeviceDesc;
201 #endif
202 #ifdef USE_USB_CDC_HID
203 if (usbDevConfig()->type == COMPOSITE) {
204 *length = sizeof(USBD_HID_CDC_DeviceDescriptor);
205 return USBD_HID_CDC_DeviceDescriptor;
207 #endif
208 *length = sizeof(USBD_DeviceDesc);
209 return (uint8_t*)USBD_DeviceDesc;
213 * @brief Returns the LangID string descriptor.
214 * @param speed: Current device speed
215 * @param length: Pointer to data length variable
216 * @retval Pointer to descriptor buffer
218 uint8_t *USBD_VCP_LangIDStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
220 (void)speed;
221 *length = sizeof(USBD_LangIDDesc);
222 return (uint8_t*)USBD_LangIDDesc;
226 * @brief Returns the product string descriptor.
227 * @param speed: Current device speed
228 * @param length: Pointer to data length variable
229 * @retval Pointer to descriptor buffer
231 uint8_t *USBD_VCP_ProductStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
233 if (speed == USBD_SPEED_HIGH)
235 USBD_GetString((uint8_t *)USBD_PRODUCT_HS_STRING, USBD_StrDesc, length);
237 else
239 USBD_GetString((uint8_t *)USBD_PRODUCT_FS_STRING, USBD_StrDesc, length);
241 return USBD_StrDesc;
245 * @brief Returns the manufacturer string descriptor.
246 * @param speed: Current device speed
247 * @param length: Pointer to data length variable
248 * @retval Pointer to descriptor buffer
250 uint8_t *USBD_VCP_ManufacturerStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
252 (void)speed;
253 USBD_GetString((uint8_t *)USBD_MANUFACTURER_STRING, USBD_StrDesc, length);
254 return USBD_StrDesc;
258 * @brief Returns the serial number string descriptor.
259 * @param speed: Current device speed
260 * @param length: Pointer to data length variable
261 * @retval Pointer to descriptor buffer
263 uint8_t *USBD_VCP_SerialStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
265 (void)speed;
266 *length = USB_SIZ_STRING_SERIAL;
268 /* Update the serial number string descriptor with the data from the unique ID*/
269 Get_SerialNum();
271 return (uint8_t*)USBD_StringSerial;
275 * @brief Returns the configuration string descriptor.
276 * @param speed: Current device speed
277 * @param length: Pointer to data length variable
278 * @retval Pointer to descriptor buffer
280 uint8_t *USBD_VCP_ConfigStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
282 if (speed == USBD_SPEED_HIGH)
284 USBD_GetString((uint8_t *)USBD_CONFIGURATION_HS_STRING, USBD_StrDesc, length);
286 else
288 USBD_GetString((uint8_t *)USBD_CONFIGURATION_FS_STRING, USBD_StrDesc, length);
290 return USBD_StrDesc;
294 * @brief Returns the interface string descriptor.
295 * @param speed: Current device speed
296 * @param length: Pointer to data length variable
297 * @retval Pointer to descriptor buffer
299 uint8_t *USBD_VCP_InterfaceStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
301 if (speed == USBD_SPEED_HIGH)
303 USBD_GetString((uint8_t *)USBD_INTERFACE_HS_STRING, USBD_StrDesc, length);
305 else
307 USBD_GetString((uint8_t *)USBD_INTERFACE_FS_STRING, USBD_StrDesc, length);
309 return USBD_StrDesc;
313 * @brief Create the serial number string descriptor
314 * @param None
315 * @retval None
317 static void Get_SerialNum(void)
319 uint32_t deviceserial0, deviceserial1, deviceserial2;
321 deviceserial0 = U_ID_0;
322 deviceserial1 = U_ID_1;
323 deviceserial2 = U_ID_2;
325 deviceserial0 += deviceserial2;
327 if (deviceserial0 != 0)
329 IntToUnicode (deviceserial0, &USBD_StringSerial[2] ,8);
330 IntToUnicode (deviceserial1, &USBD_StringSerial[18] ,4);
335 * @brief Convert Hex 32Bits value into char
336 * @param value: value to convert
337 * @param pbuf: pointer to the buffer
338 * @param len: buffer length
339 * @retval None
341 static void IntToUnicode (uint32_t value , uint8_t *pbuf , uint8_t len)
343 uint8_t idx = 0;
345 for ( idx = 0; idx < len; idx ++)
347 if ( ((value >> 28)) < 0xA )
349 pbuf[ 2* idx] = (value >> 28) + '0';
351 else
353 pbuf[2* idx] = (value >> 28) + 'A' - 10;
356 value = value << 4;
358 pbuf[ 2* idx + 1] = 0;
361 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/