Browse code

Add source code of the firmware for the IO Board

Pawel Jablonski authored on 05/06/2023 17:01:27
Showing 1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,452 @@
1
+/**
2
+  ******************************************************************************
3
+  * @file    usbd_desc_template.c
4
+  * @author  MCD Application Team
5
+  * @brief   This file provides the USBD descriptors and string formatting method.
6
+  *          This template should be copied to the user folder,
7
+  *          renamed and customized following user needs.
8
+  ******************************************************************************
9
+  * @attention
10
+  *
11
+  * Copyright (c) 2015 STMicroelectronics.
12
+  * All rights reserved.
13
+  *
14
+  * This software is licensed under terms that can be found in the LICENSE file
15
+  * in the root directory of this software component.
16
+  * If no LICENSE file comes with this software, it is provided AS-IS.
17
+  *
18
+  ******************************************************************************
19
+  */
20
+
21
+/* Includes ------------------------------------------------------------------*/
22
+#include "usbd_core.h"
23
+#include "usbd_desc.h"
24
+#include "usbd_conf.h"
25
+
26
+/* Private typedef -----------------------------------------------------------*/
27
+/* Private define ------------------------------------------------------------*/
28
+#define USBD_VID                      1155
29
+#define USBD_PID                      22314  /* Replace '0xaaaa' with your device product ID */
30
+#define USBD_LANGID_STRING            1033   /* Replace '0xbbb' with your device language ID */
31
+#define USBD_MANUFACTURER_STRING      "STMicroelectronics" /* Add your manufacturer string */
32
+#define USBD_PRODUCT_HS_STRING        "STM32 Mass Storage" /* Add your product High Speed string */
33
+#define USBD_PRODUCT_FS_STRING        "STM32 Mass Storage" /* Add your product Full Speed string */
34
+#define USBD_CONFIGURATION_HS_STRING  "MSC Config" /* Add your configuration High Speed string */
35
+#define USBD_INTERFACE_HS_STRING      "MSC Interface" /* Add your Interface High Speed string */
36
+#define USBD_CONFIGURATION_FS_STRING  "MSC Config" /* Add your configuration Full Speed string */
37
+#define USBD_INTERFACE_FS_STRING      "MSC Interface" /* Add your Interface Full Speed string */
38
+
39
+/* Private macro -------------------------------------------------------------*/
40
+/* Private function prototypes -----------------------------------------------*/
41
+uint8_t *USBD_Class_DeviceDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
42
+uint8_t *USBD_Class_LangIDStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
43
+uint8_t *USBD_Class_ManufacturerStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
44
+uint8_t *USBD_Class_ProductStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
45
+uint8_t *USBD_Class_SerialStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
46
+uint8_t *USBD_Class_ConfigStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
47
+uint8_t *USBD_Class_InterfaceStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
48
+
49
+#if (USBD_CLASS_USER_STRING_DESC == 1)
50
+uint8_t *USBD_Class_UserStrDescriptor(USBD_SpeedTypeDef speed, uint8_t idx, uint16_t *length);
51
+#endif /* USB_CLASS_USER_STRING_DESC */
52
+
53
+#if ((USBD_LPM_ENABLED == 1) || (USBD_CLASS_BOS_ENABLED == 1))
54
+uint8_t *USBD_USR_BOSDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
55
+#endif /* (USBD_LPM_ENABLED == 1) || (USBD_CLASS_BOS_ENABLED == 1) */
56
+
57
+/* Private variables ---------------------------------------------------------*/
58
+USBD_DescriptorsTypeDef USBD_MSC_Desc =
59
+{
60
+  USBD_Class_DeviceDescriptor,
61
+  USBD_Class_LangIDStrDescriptor,
62
+  USBD_Class_ManufacturerStrDescriptor,
63
+  USBD_Class_ProductStrDescriptor,
64
+  USBD_Class_SerialStrDescriptor,
65
+  USBD_Class_ConfigStrDescriptor,
66
+  USBD_Class_InterfaceStrDescriptor,
67
+#if (USBD_CLASS_USER_STRING_DESC == 1)
68
+  USBD_CLASS_UserStrDescriptor,
69
+#endif /* USB_CLASS_USER_STRING_DESC */
70
+
71
+#if ((USBD_LPM_ENABLED == 1) || (USBD_CLASS_BOS_ENABLED == 1))
72
+  USBD_USR_BOSDescriptor,
73
+#endif /* (USBD_LPM_ENABLED == 1) || (USBD_CLASS_BOS_ENABLED == 1) */
74
+};
75
+
76
+/* USB Standard Device Descriptor */
77
+#if defined ( __ICCARM__ ) /*!< IAR Compiler */
78
+#pragma data_alignment=4
79
+#endif /* __ICCARM__ */
80
+__ALIGN_BEGIN uint8_t USBD_DeviceDesc[USB_LEN_DEV_DESC] __ALIGN_END =
81
+{
82
+  0x12,                       /* bLength */
83
+  USB_DESC_TYPE_DEVICE,       /* bDescriptorType */
84
+#if ((USBD_LPM_ENABLED == 1) || (USBD_CLASS_BOS_ENABLED == 1))
85
+  0x01,                       /*bcdUSB */     /* changed to USB version 2.01
86
+                                              in order to support BOS Desc */
87
+#else
88
+  0x00,                       /* bcdUSB */
89
+#endif /* (USBD_LPM_ENABLED == 1) || (USBD_CLASS_BOS_ENABLED == 1) */
90
+  0x02,
91
+  0x00,                       /* bDeviceClass */
92
+  0x00,                       /* bDeviceSubClass */
93
+  0x00,                       /* bDeviceProtocol */
94
+  USB_MAX_EP0_SIZE,           /* bMaxPacketSize */
95
+  LOBYTE(USBD_VID),           /* idVendor */
96
+  HIBYTE(USBD_VID),           /* idVendor */
97
+  LOBYTE(USBD_PID),           /* idVendor */
98
+  HIBYTE(USBD_PID),           /* idVendor */
99
+  0x00,                       /* bcdDevice rel. 2.00 */
100
+  0x02,
101
+  USBD_IDX_MFC_STR,           /* Index of manufacturer string */
102
+  USBD_IDX_PRODUCT_STR,       /* Index of product string */
103
+  USBD_IDX_SERIAL_STR,        /* Index of serial number string */
104
+  USBD_MAX_NUM_CONFIGURATION  /* bNumConfigurations */
105
+}; /* USB_DeviceDescriptor */
106
+
107
+
108
+/* USB Device LPM BOS descriptor */
109
+#if (USBD_LPM_ENABLED == 1)
110
+#if defined ( __ICCARM__ ) /*!< IAR Compiler */
111
+#pragma data_alignment=4
112
+#endif /* __ICCARM__ */
113
+__ALIGN_BEGIN  uint8_t USBD_BOSDesc[USB_SIZ_BOS_DESC] __ALIGN_END =
114
+{
115
+  0x5,
116
+  USB_DESC_TYPE_BOS,
117
+  0xC,
118
+  0x0,
119
+  0x1,  /* 1 device capability */
120
+  /* device capability */
121
+  0x7,
122
+  USB_DEVICE_CAPABITY_TYPE,
123
+  0x2,
124
+  0x6, /*LPM capability bit set */
125
+  0x0,
126
+  0x0,
127
+  0x0
128
+};
129
+#endif /* USBD_LPM_ENABLED */
130
+
131
+/* USB Device Billboard BOS descriptor Template */
132
+#if (USBD_CLASS_BOS_ENABLED == 1)
133
+#if defined ( __ICCARM__ ) /*!< IAR Compiler */
134
+#pragma data_alignment=4
135
+#endif /* __ICCARM__ */
136
+__ALIGN_BEGIN  uint8_t USBD_BOSDesc[USB_SIZ_BOS_DESC] __ALIGN_END =
137
+{
138
+  0x05,                                /* bLength */
139
+  USB_DESC_TYPE_BOS,                   /* Device Descriptor Type */
140
+  USB_SIZ_BOS_DESC,                    /* Total length of BOS descriptor and all of its sub descs */
141
+  0x00,
142
+  0x04,                                /* The number of separate device capability descriptors in the BOS */
143
+
144
+  /* ----------- Device Capability Descriptor: CONTAINER_ID ---------- */
145
+  0x14,                                /* bLength */
146
+  0x10,                                /* bDescriptorType: DEVICE CAPABILITY Type */
147
+  0x04,                                /* bDevCapabilityType: CONTAINER_ID */
148
+  0x00,                                /* bReserved */
149
+  0xa7, 0xd6, 0x1b, 0xfa,              /* ContainerID: This is a Unique 128-bit number GUID */
150
+  0x91, 0xa6, 0xa8, 0x4e,
151
+  0xa8, 0x21, 0x9f, 0x2b,
152
+  0xaf, 0xf7, 0x94, 0xd4,
153
+
154
+  /* ----------- Device Capability Descriptor: BillBoard ---------- */
155
+  0x34,                                /* bLength */
156
+  0x10,                                /* bDescriptorType: DEVICE CAPABILITY Type */
157
+  0x0D,                                /* bDevCapabilityType: BILLBOARD_CAPABILITY */
158
+  USBD_BB_URL_STRING_INDEX,            /* iAddtionalInfoURL: Index of string descriptor providing a URL where the user
159
+                                          can go to get more detailed information about the product and the various
160
+                                          Alternate Modes it supports */
161
+
162
+  0x02,                                /* bNumberOfAlternateModes: Number of Alternate modes supported. The
163
+                                        maximum value that this field can be set to is MAX_NUM_ALT_MODE. */
164
+
165
+  0x00,                                /* bPreferredAlternateMode: Index of the preferred Alternate Mode. System
166
+                                          software may use this information to provide the user with a better
167
+                                          user experience. */
168
+
169
+  0x00, 0x00,                          /* VCONN Power needed by the adapter for full functionality 000b = 1W */
170
+
171
+  0x01, 0x00, 0x00, 0x00,              /* bmConfigured. 01b: Alternate Mode configuration not attempted or exited */
172
+  0x00, 0x00, 0x00, 0x00,
173
+  0x00, 0x00, 0x00, 0x00,
174
+  0x00, 0x00, 0x00, 0x00,
175
+  0x00, 0x00, 0x00, 0x00,
176
+  0x00, 0x00, 0x00, 0x00,
177
+  0x00, 0x00, 0x00, 0x00,
178
+  0x00, 0x00, 0x00, 0x00,
179
+  0x21, 0x01,                          /* bcdVersion = 0x0121 */
180
+  0x00,                                /* bAdditionalFailureInfo */
181
+  0x00,                                /* bReserved */
182
+  LOBYTE(USBD_VID),
183
+  HIBYTE(USBD_VID),                    /* wSVID[0]: Standard or Vendor ID. This shall match one of the SVIDs
184
+                                        returned in response to a USB PD Discover SVIDs command */
185
+
186
+  0x00,                                /* bAlternateMode[0] Index of the Alternate Mode within the SVID as
187
+                                        returned in response to a Discover Modes command. Example:
188
+                                        0  first Mode entry
189
+                                        1  second mode entry */
190
+
191
+  USBD_BB_ALTMODE0_STRING_INDEX,           /* iAlternateModeString[0]: Index of string descriptor describing protocol.
192
+                                        It is optional to support this string. */
193
+  LOBYTE(USBD_VID),
194
+  HIBYTE(USBD_VID),                    /* wSVID[1]: Standard or Vendor ID. This shall match one of the SVIDs
195
+                                        returned in response to a USB PD Discover SVIDs command */
196
+
197
+  0x01,                                /* bAlternateMode[1] Index of the Alternate Mode within the SVID as
198
+                                        returned in response to a Discover Modes command. Example:
199
+                                        0  first Mode entry
200
+                                        1  second Mode entry */
201
+
202
+  USBD_BB_ALTMODE1_STRING_INDEX,       /* iAlternateModeString[1]: Index of string descriptor describing protocol.
203
+                                        It is optional to support this string. */
204
+  /* Alternate Mode Desc */
205
+  /* ----------- Device Capability Descriptor: BillBoard Alternate Mode Desc ---------- */
206
+  0x08,                                /* bLength */
207
+  0x10,                                /* bDescriptorType: Device Descriptor Type */
208
+  0x0F,                                /* bDevCapabilityType: BILLBOARD ALTERNATE MODE CAPABILITY */
209
+  0x00,                                /* bIndex: Index of Alternate Mode described in the Billboard Capability Desc */
210
+  0x10, 0x00, 0x00, 0x00,              /* dwAlternateModeVdo: contents of the Mode VDO for the alternate mode
211
+                                          identified by bIndex */
212
+
213
+  0x08,                                /* bLength */
214
+  0x10,                                /* bDescriptorType: Device Descriptor Type */
215
+  0x0F,                                /* bDevCapabilityType: BILLBOARD ALTERNATE MODE CAPABILITY */
216
+  0x01,                                /* bIndex: Index of Alternate Mode described in the Billboard Capability Desc */
217
+  0x20, 0x00, 0x00, 0x00,              /* dwAlternateModeVdo: contents of the Mode VDO for the alternate mode
218
+                                          identified by bIndex */
219
+};
220
+#endif /* USBD_CLASS_BOS_ENABLED */
221
+
222
+
223
+/* USB Standard Device Descriptor */
224
+#if defined ( __ICCARM__ ) /*!< IAR Compiler */
225
+#pragma data_alignment=4
226
+#endif /* __ICCARM__ */
227
+__ALIGN_BEGIN uint8_t USBD_LangIDDesc[USB_LEN_LANGID_STR_DESC] __ALIGN_END =
228
+{
229
+  USB_LEN_LANGID_STR_DESC,
230
+  USB_DESC_TYPE_STRING,
231
+  LOBYTE(USBD_LANGID_STRING),
232
+  HIBYTE(USBD_LANGID_STRING),
233
+};
234
+
235
+#if defined ( __ICCARM__ ) /*!< IAR Compiler */
236
+#pragma data_alignment=4
237
+#endif /* __ICCARM__ */
238
+__ALIGN_BEGIN uint8_t USBD_StringSerial[USB_SIZ_STRING_SERIAL] =
239
+{
240
+  USB_SIZ_STRING_SERIAL,
241
+  USB_DESC_TYPE_STRING,
242
+};
243
+
244
+#if defined ( __ICCARM__ ) /*!< IAR Compiler */
245
+#pragma data_alignment=4
246
+#endif /* __ICCARM__ */
247
+__ALIGN_BEGIN uint8_t USBD_StrDesc[USBD_MAX_STR_DESC_SIZ] __ALIGN_END;
248
+
249
+/* Private functions ---------------------------------------------------------*/
250
+static void IntToUnicode(uint32_t value, uint8_t *pbuf, uint8_t len);
251
+static void Get_SerialNum(void);
252
+
253
+/**
254
+  * @brief  Returns the device descriptor.
255
+  * @param  speed: Current device speed
256
+  * @param  length: Pointer to data length variable
257
+  * @retval Pointer to descriptor buffer
258
+  */
259
+uint8_t *USBD_Class_DeviceDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
260
+{
261
+  UNUSED(speed);
262
+
263
+  *length = sizeof(USBD_DeviceDesc);
264
+  return (uint8_t *)USBD_DeviceDesc;
265
+}
266
+
267
+/**
268
+  * @brief  Returns the LangID string descriptor.
269
+  * @param  speed: Current device speed
270
+  * @param  length: Pointer to data length variable
271
+  * @retval Pointer to descriptor buffer
272
+  */
273
+uint8_t *USBD_Class_LangIDStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
274
+{
275
+  UNUSED(speed);
276
+
277
+  *length = sizeof(USBD_LangIDDesc);
278
+  return (uint8_t *)USBD_LangIDDesc;
279
+}
280
+
281
+/**
282
+  * @brief  Returns the product string descriptor.
283
+  * @param  speed: Current device speed
284
+  * @param  length: Pointer to data length variable
285
+  * @retval Pointer to descriptor buffer
286
+  */
287
+uint8_t *USBD_Class_ProductStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
288
+{
289
+  if (speed == USBD_SPEED_HIGH)
290
+  {
291
+    USBD_GetString((uint8_t *)USBD_PRODUCT_HS_STRING, USBD_StrDesc, length);
292
+  }
293
+  else
294
+  {
295
+    USBD_GetString((uint8_t *)USBD_PRODUCT_FS_STRING, USBD_StrDesc, length);
296
+  }
297
+  return USBD_StrDesc;
298
+}
299
+
300
+/**
301
+  * @brief  Returns the manufacturer string descriptor.
302
+  * @param  speed: Current device speed
303
+  * @param  length: Pointer to data length variable
304
+  * @retval Pointer to descriptor buffer
305
+  */
306
+uint8_t *USBD_Class_ManufacturerStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
307
+{
308
+  UNUSED(speed);
309
+
310
+  USBD_GetString((uint8_t *)USBD_MANUFACTURER_STRING, USBD_StrDesc, length);
311
+  return USBD_StrDesc;
312
+}
313
+
314
+/**
315
+  * @brief  Returns the serial number string descriptor.
316
+  * @param  speed: Current device speed
317
+  * @param  length: Pointer to data length variable
318
+  * @retval Pointer to descriptor buffer
319
+  */
320
+uint8_t *USBD_Class_SerialStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
321
+{
322
+  UNUSED(speed);
323
+
324
+  *length = USB_SIZ_STRING_SERIAL;
325
+
326
+  /* Update the serial number string descriptor with the data from the unique ID*/
327
+  Get_SerialNum();
328
+
329
+  return (uint8_t *)USBD_StringSerial;
330
+}
331
+
332
+/**
333
+  * @brief  Returns the configuration string descriptor.
334
+  * @param  speed: Current device speed
335
+  * @param  length: Pointer to data length variable
336
+  * @retval Pointer to descriptor buffer
337
+  */
338
+uint8_t *USBD_Class_ConfigStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
339
+{
340
+  if (speed == USBD_SPEED_HIGH)
341
+  {
342
+    USBD_GetString((uint8_t *)USBD_CONFIGURATION_HS_STRING, USBD_StrDesc, length);
343
+  }
344
+  else
345
+  {
346
+    USBD_GetString((uint8_t *)USBD_CONFIGURATION_FS_STRING, USBD_StrDesc, length);
347
+  }
348
+  return USBD_StrDesc;
349
+}
350
+
351
+/**
352
+  * @brief  Returns the interface string descriptor.
353
+  * @param  speed: Current device speed
354
+  * @param  length: Pointer to data length variable
355
+  * @retval Pointer to descriptor buffer
356
+  */
357
+uint8_t *USBD_Class_InterfaceStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
358
+{
359
+  if (speed == USBD_SPEED_HIGH)
360
+  {
361
+    USBD_GetString((uint8_t *)USBD_INTERFACE_HS_STRING, USBD_StrDesc, length);
362
+  }
363
+  else
364
+  {
365
+    USBD_GetString((uint8_t *)USBD_INTERFACE_FS_STRING, USBD_StrDesc, length);
366
+  }
367
+  return USBD_StrDesc;
368
+}
369
+
370
+/**
371
+  * @brief  Create the serial number string descriptor
372
+  * @param  None
373
+  * @retval None
374
+  */
375
+static void Get_SerialNum(void)
376
+{
377
+  uint32_t deviceserial0, deviceserial1, deviceserial2;
378
+
379
+  deviceserial0 = *(uint32_t *)DEVICE_ID1;
380
+  deviceserial1 = *(uint32_t *)DEVICE_ID2;
381
+  deviceserial2 = *(uint32_t *)DEVICE_ID3;
382
+
383
+  deviceserial0 += deviceserial2;
384
+
385
+  if (deviceserial0 != 0U)
386
+  {
387
+    IntToUnicode(deviceserial0, &USBD_StringSerial[2], 8U);
388
+    IntToUnicode(deviceserial1, &USBD_StringSerial[18], 4U);
389
+  }
390
+}
391
+
392
+
393
+#if ((USBD_LPM_ENABLED == 1) || (USBD_CLASS_BOS_ENABLED == 1))
394
+/**
395
+  * @brief  USBD_USR_BOSDescriptor
396
+  *         return the BOS descriptor
397
+  * @param  speed : current device speed
398
+  * @param  length : pointer to data length variable
399
+  * @retval pointer to descriptor buffer
400
+  */
401
+uint8_t *USBD_USR_BOSDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
402
+{
403
+  *length = sizeof(USBD_BOSDesc);
404
+  return (uint8_t *)USBD_BOSDesc;
405
+}
406
+#endif /* (USBD_LPM_ENABLED == 1) || (USBD_CLASS_BOS_ENABLED == 1) */
407
+
408
+
409
+#if (USBD_CLASS_USER_STRING_DESC == 1)
410
+/**
411
+  * @brief  Returns the Class User string descriptor.
412
+  * @param  speed: Current device speed
413
+  * @param  idx: index of string descriptor
414
+  * @param  length: Pointer to data length variable
415
+  * @retval Pointer to descriptor buffer
416
+  */
417
+uint8_t *USBD_Class_UserStrDescriptor(USBD_SpeedTypeDef speed, uint8_t idx, uint16_t *length)
418
+{
419
+  static uint8_t USBD_StrDesc[255];
420
+
421
+  return USBD_StrDesc;
422
+}
423
+#endif /* USBD_CLASS_USER_STRING_DESC */
424
+
425
+
426
+/**
427
+  * @brief  Convert Hex 32Bits value into char
428
+  * @param  value: value to convert
429
+  * @param  pbuf: pointer to the buffer
430
+  * @param  len: buffer length
431
+  * @retval None
432
+  */
433
+static void IntToUnicode(uint32_t value, uint8_t *pbuf, uint8_t len)
434
+{
435
+  uint8_t idx = 0U;
436
+
437
+  for (idx = 0U ; idx < len ; idx ++)
438
+  {
439
+    if (((value >> 28)) < 0xAU)
440
+    {
441
+      pbuf[ 2U * idx] = (value >> 28) + '0';
442
+    }
443
+    else
444
+    {
445
+      pbuf[2U * idx] = (value >> 28) + 'A' - 10U;
446
+    }
447
+
448
+    value = value << 4;
449
+
450
+    pbuf[2U * idx + 1] = 0U;
451
+  }
452
+}