1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
30
31
32
36
37
40
41
44
45
46
49
50
53
54
55
58
59
62
63
64
67
68
71
72
73
76
77
78
79
80
81
82
83
84
85
88
89
90
93
94
95
102
103
104
105
106
107
108
109
113
114
115
116
120
124
128
132
136
140
144
147
148
149
150
153
154
155
156
157
158
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
199
204
205
209
210
214
215
216
219
220
221
222
225
226
227
228
229
230
237
238
239
240
241
242
243
244
245
246
247
248
249
263
264
265
266
299
300
301
302
303
315
316
317
318
322
323
324
325
326
335
336
337
338
341
342
343
344
345
346
347
348
353
354
355
356
357
358
359
360
361
362
370
378
379
380
381
382
386
390
394
395
396
397
398
401
402
403
404
407
408
409
410
413
414
415
416
417
418
419
426
427
428
429
430
431
432
433
434
435
447
451
482
483
484
485
497
509
521
533
545
557
558
559
560
561
562
563
564
565
566
567
568
569
572
576
577
578
579
580
581
591
592
593
595
596
597
598
599
600
621
643
647
648
649
653
654
666
670
671
672
673
680
681
682
683
684
685
686
687
688
692
693
694
695
696
697
698
702
706
707
708
712
713
714
721
722
723
724
725
726
727
728
733
734
735
736
737
738
739
740
741
742
743
748
749
750
751
752
753
754
759
760
761
765
766
767
768
775
776
777
778
779
780
781
782
783
784
785
791
795
796
800
801
802
807
808
809
810
811
812
819
845
846
853
854
855
856
857
858
859
860
865
866
867
868
869
870
871
872
876
877
878
879
880
883
884
885
886
887
894
911
912
913
920
939
940
941
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
977
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1030
1031
1032
1033
1034
1035
1040
1041
1042
1043
1046
1047
1048
1051
1052
1053
1056
/* ... */
#include "usbd_ctlreq.h"
#include "usbd_ioreq.h"
#ifdef USE_USBD_COMPOSITE
#include "usbd_composite_builder.h"
#endif
/* ... */
/* ... */
/* ... */
/* ... */
/* ... */
/* ... */
/* ... */
/* ... */
/* ... */
/* ... */
/* ... */
static void USBD_GetDescriptor(USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *req);
static void USBD_SetAddress(USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *req);
static USBD_StatusTypeDef USBD_SetConfig(USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *req);
static void USBD_GetConfig(USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *req);
static void USBD_GetStatus(USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *req);
static void USBD_SetFeature(USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *req);
static void USBD_ClrFeature(USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *req);
static uint8_t USBD_GetLen(uint8_t *buf);
/* ... */
/* ... */
/* ... */
USBD_StatusTypeDef USBD_StdDevReq(USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *req)
{
USBD_StatusTypeDef ret = USBD_OK;
switch (req->bmRequest & USB_REQ_TYPE_MASK)
{
case USB_REQ_TYPE_CLASS:
case USB_REQ_TYPE_VENDOR:
ret = (USBD_StatusTypeDef)pdev->pClass[pdev->classId]->Setup(pdev, req);
break;
case USB_REQ_TYPE_VENDOR:
case USB_REQ_TYPE_STANDARD:
switch (req->bRequest)
{
case USB_REQ_GET_DESCRIPTOR:
USBD_GetDescriptor(pdev, req);
break;
case USB_REQ_GET_DESCRIPTOR:
case USB_REQ_SET_ADDRESS:
USBD_SetAddress(pdev, req);
break;
case USB_REQ_SET_ADDRESS:
case USB_REQ_SET_CONFIGURATION:
ret = USBD_SetConfig(pdev, req);
break;
case USB_REQ_SET_CONFIGURATION:
case USB_REQ_GET_CONFIGURATION:
USBD_GetConfig(pdev, req);
break;
case USB_REQ_GET_CONFIGURATION:
case USB_REQ_GET_STATUS:
USBD_GetStatus(pdev, req);
break;
case USB_REQ_GET_STATUS:
case USB_REQ_SET_FEATURE:
USBD_SetFeature(pdev, req);
break;
case USB_REQ_SET_FEATURE:
case USB_REQ_CLEAR_FEATURE:
USBD_ClrFeature(pdev, req);
break;
case USB_REQ_CLEAR_FEATURE:
default:
USBD_CtlError(pdev, req);
break;default
}switch (req->bRequest) { ... }
break;
case USB_REQ_TYPE_STANDARD:
default:
USBD_CtlError(pdev, req);
break;default
}switch (req->bmRequest & USB_REQ_TYPE_MASK) { ... }
return ret;
}{ ... }
/* ... */
USBD_StatusTypeDef USBD_StdItfReq(USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *req)
{
USBD_StatusTypeDef ret = USBD_OK;
uint8_t idx;
switch (req->bmRequest & USB_REQ_TYPE_MASK)
{
case USB_REQ_TYPE_CLASS:
case USB_REQ_TYPE_VENDOR:
case USB_REQ_TYPE_STANDARD:
switch (pdev->dev_state)
{
case USBD_STATE_DEFAULT:
case USBD_STATE_ADDRESSED:
case USBD_STATE_CONFIGURED:
if (LOBYTE(req->wIndex) <= USBD_MAX_NUM_INTERFACES)
{
idx = USBD_CoreFindIF(pdev, LOBYTE(req->wIndex));
if (((uint8_t)idx != 0xFFU) && (idx < USBD_MAX_SUPPORTED_CLASS))
{
if (pdev->pClass[idx]->Setup != NULL)
{
pdev->classId = idx;
ret = (USBD_StatusTypeDef)(pdev->pClass[idx]->Setup(pdev, req));
}if (pdev->pClass[idx]->Setup != NULL) { ... }
else
{
ret = USBD_FAIL;
}else { ... }
}if (((uint8_t)idx != 0xFFU) && (idx < USBD_MAX_SUPPORTED_CLASS)) { ... }
else
{
ret = USBD_FAIL;
}else { ... }
if ((req->wLength == 0U) && (ret == USBD_OK))
{
(void)USBD_CtlSendStatus(pdev);
}if ((req->wLength == 0U) && (ret == USBD_OK)) { ... }
}if (LOBYTE(req->wIndex) <= USBD_MAX_NUM_INTERFACES) { ... }
else
{
USBD_CtlError(pdev, req);
}else { ... }
break;
case USBD_STATE_CONFIGURED:
default:
USBD_CtlError(pdev, req);
break;default
}switch (pdev->dev_state) { ... }
break;
case USB_REQ_TYPE_STANDARD:
default:
USBD_CtlError(pdev, req);
break;default
}switch (req->bmRequest & USB_REQ_TYPE_MASK) { ... }
return ret;
}{ ... }
/* ... */
USBD_StatusTypeDef USBD_StdEPReq(USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *req)
{
USBD_EndpointTypeDef *pep;
uint8_t ep_addr;
uint8_t idx;
USBD_StatusTypeDef ret = USBD_OK;
ep_addr = LOBYTE(req->wIndex);
switch (req->bmRequest & USB_REQ_TYPE_MASK)
{
case USB_REQ_TYPE_CLASS:
case USB_REQ_TYPE_VENDOR:
idx = USBD_CoreFindEP(pdev, ep_addr);
if (((uint8_t)idx != 0xFFU) && (idx < USBD_MAX_SUPPORTED_CLASS))
{
pdev->classId = idx;
if (pdev->pClass[idx]->Setup != NULL)
{
ret = (USBD_StatusTypeDef)pdev->pClass[idx]->Setup(pdev, req);
}if (pdev->pClass[idx]->Setup != NULL) { ... }
}if (((uint8_t)idx != 0xFFU) && (idx < USBD_MAX_SUPPORTED_CLASS)) { ... }
break;
case USB_REQ_TYPE_VENDOR:
case USB_REQ_TYPE_STANDARD:
switch (req->bRequest)
{
case USB_REQ_SET_FEATURE:
switch (pdev->dev_state)
{
case USBD_STATE_ADDRESSED:
if ((ep_addr != 0x00U) && (ep_addr != 0x80U))
{
(void)USBD_LL_StallEP(pdev, ep_addr);
(void)USBD_LL_StallEP(pdev, 0x80U);
}if ((ep_addr != 0x00U) && (ep_addr != 0x80U)) { ... }
else
{
USBD_CtlError(pdev, req);
}else { ... }
break;
case USBD_STATE_ADDRESSED:
case USBD_STATE_CONFIGURED:
if (req->wValue == USB_FEATURE_EP_HALT)
{
if ((ep_addr != 0x00U) && (ep_addr != 0x80U) && (req->wLength == 0x00U))
{
(void)USBD_LL_StallEP(pdev, ep_addr);
}if ((ep_addr != 0x00U) && (ep_addr != 0x80U) && (req->wLength == 0x00U)) { ... }
}if (req->wValue == USB_FEATURE_EP_HALT) { ... }
(void)USBD_CtlSendStatus(pdev);
break;
case USBD_STATE_CONFIGURED:
default:
USBD_CtlError(pdev, req);
break;default
}switch (pdev->dev_state) { ... }
break;
case USB_REQ_SET_FEATURE:
case USB_REQ_CLEAR_FEATURE:
switch (pdev->dev_state)
{
case USBD_STATE_ADDRESSED:
if ((ep_addr != 0x00U) && (ep_addr != 0x80U))
{
(void)USBD_LL_StallEP(pdev, ep_addr);
(void)USBD_LL_StallEP(pdev, 0x80U);
}if ((ep_addr != 0x00U) && (ep_addr != 0x80U)) { ... }
else
{
USBD_CtlError(pdev, req);
}else { ... }
break;
case USBD_STATE_ADDRESSED:
case USBD_STATE_CONFIGURED:
if (req->wValue == USB_FEATURE_EP_HALT)
{
if ((ep_addr & 0x7FU) != 0x00U)
{
(void)USBD_LL_ClearStallEP(pdev, ep_addr);
}if ((ep_addr & 0x7FU) != 0x00U) { ... }
(void)USBD_CtlSendStatus(pdev);
idx = USBD_CoreFindEP(pdev, ep_addr);
if (((uint8_t)idx != 0xFFU) && (idx < USBD_MAX_SUPPORTED_CLASS))
{
pdev->classId = idx;
if (pdev->pClass[idx]->Setup != NULL)
{
ret = (USBD_StatusTypeDef)(pdev->pClass[idx]->Setup(pdev, req));
}if (pdev->pClass[idx]->Setup != NULL) { ... }
}if (((uint8_t)idx != 0xFFU) && (idx < USBD_MAX_SUPPORTED_CLASS)) { ... }
}if (req->wValue == USB_FEATURE_EP_HALT) { ... }
break;
case USBD_STATE_CONFIGURED:
default:
USBD_CtlError(pdev, req);
break;default
}switch (pdev->dev_state) { ... }
break;
case USB_REQ_CLEAR_FEATURE:
case USB_REQ_GET_STATUS:
switch (pdev->dev_state)
{
case USBD_STATE_ADDRESSED:
if ((ep_addr != 0x00U) && (ep_addr != 0x80U))
{
USBD_CtlError(pdev, req);
break;
}if ((ep_addr != 0x00U) && (ep_addr != 0x80U)) { ... }
pep = ((ep_addr & 0x80U) == 0x80U) ? &pdev->ep_in[ep_addr & 0x7FU] : \
&pdev->ep_out[ep_addr & 0x7FU];
pep->status = 0x0000U;
(void)USBD_CtlSendData(pdev, (uint8_t *)&pep->status, 2U);
break;
case USBD_STATE_ADDRESSED:
case USBD_STATE_CONFIGURED:
if ((ep_addr & 0x80U) == 0x80U)
{
if (pdev->ep_in[ep_addr & 0xFU].is_used == 0U)
{
USBD_CtlError(pdev, req);
break;
}if (pdev->ep_in[ep_addr & 0xFU].is_used == 0U) { ... }
}if ((ep_addr & 0x80U) == 0x80U) { ... }
else
{
if (pdev->ep_out[ep_addr & 0xFU].is_used == 0U)
{
USBD_CtlError(pdev, req);
break;
}if (pdev->ep_out[ep_addr & 0xFU].is_used == 0U) { ... }
}else { ... }
pep = ((ep_addr & 0x80U) == 0x80U) ? &pdev->ep_in[ep_addr & 0x7FU] : \
&pdev->ep_out[ep_addr & 0x7FU];
if ((ep_addr == 0x00U) || (ep_addr == 0x80U))
{
pep->status = 0x0000U;
}if ((ep_addr == 0x00U) || (ep_addr == 0x80U)) { ... }
else if (USBD_LL_IsStallEP(pdev, ep_addr) != 0U)
{
pep->status = 0x0001U;
}else if (USBD_LL_IsStallEP(pdev, ep_addr) != 0U) { ... }
else
{
pep->status = 0x0000U;
}else { ... }
(void)USBD_CtlSendData(pdev, (uint8_t *)&pep->status, 2U);
break;
case USBD_STATE_CONFIGURED:
default:
USBD_CtlError(pdev, req);
break;default
}switch (pdev->dev_state) { ... }
break;
case USB_REQ_GET_STATUS:
default:
USBD_CtlError(pdev, req);
break;default
}switch (req->bRequest) { ... }
break;
case USB_REQ_TYPE_STANDARD:
default:
USBD_CtlError(pdev, req);
break;default
}switch (req->bmRequest & USB_REQ_TYPE_MASK) { ... }
return ret;
}{ ... }
/* ... */
static void USBD_GetDescriptor(USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *req)
{
uint16_t len = 0U;
uint8_t *pbuf = NULL;
uint8_t err = 0U;
switch (req->wValue >> 8)
{
#if ((USBD_LPM_ENABLED == 1U) || (USBD_CLASS_BOS_ENABLED == 1U))
case USB_DESC_TYPE_BOS:
if (pdev->pDesc->GetBOSDescriptor != NULL)
{
pbuf = pdev->pDesc->GetBOSDescriptor(pdev->dev_speed, &len);
}if (pdev->pDesc->GetBOSDescriptor != NULL) { ... }
else
{
USBD_CtlError(pdev, req);
err++;
}else { ... }
break;/* ... */
#endif case USB_DESC_TYPE_BOS:
case USB_DESC_TYPE_DEVICE:
pbuf = pdev->pDesc->GetDeviceDescriptor(pdev->dev_speed, &len);
break;
case USB_DESC_TYPE_DEVICE:
case USB_DESC_TYPE_CONFIGURATION:
if (pdev->dev_speed == USBD_SPEED_HIGH)
{
#ifdef USE_USBD_COMPOSITE
if ((uint8_t)(pdev->NumClasses) > 0U)
{
pbuf = (uint8_t *)USBD_CMPSIT.GetHSConfigDescriptor(&len);
}if ((uint8_t)(pdev->NumClasses) > 0U) { ... }
else
#endif
{
pbuf = (uint8_t *)pdev->pClass[0]->GetHSConfigDescriptor(&len);
}else { ... }
pbuf[1] = USB_DESC_TYPE_CONFIGURATION;
}if (pdev->dev_speed == USBD_SPEED_HIGH) { ... }
else
{
#ifdef USE_USBD_COMPOSITE
if ((uint8_t)(pdev->NumClasses) > 0U)
{
pbuf = (uint8_t *)USBD_CMPSIT.GetFSConfigDescriptor(&len);
}if ((uint8_t)(pdev->NumClasses) > 0U) { ... }
else
#endif
{
pbuf = (uint8_t *)pdev->pClass[0]->GetFSConfigDescriptor(&len);
}else { ... }
pbuf[1] = USB_DESC_TYPE_CONFIGURATION;
}else { ... }
break;
case USB_DESC_TYPE_CONFIGURATION:
case USB_DESC_TYPE_STRING:
switch ((uint8_t)(req->wValue))
{
case USBD_IDX_LANGID_STR:
if (pdev->pDesc->GetLangIDStrDescriptor != NULL)
{
pbuf = pdev->pDesc->GetLangIDStrDescriptor(pdev->dev_speed, &len);
}if (pdev->pDesc->GetLangIDStrDescriptor != NULL) { ... }
else
{
USBD_CtlError(pdev, req);
err++;
}else { ... }
break;
case USBD_IDX_LANGID_STR:
case USBD_IDX_MFC_STR:
if (pdev->pDesc->GetManufacturerStrDescriptor != NULL)
{
pbuf = pdev->pDesc->GetManufacturerStrDescriptor(pdev->dev_speed, &len);
}if (pdev->pDesc->GetManufacturerStrDescriptor != NULL) { ... }
else
{
USBD_CtlError(pdev, req);
err++;
}else { ... }
break;
case USBD_IDX_MFC_STR:
case USBD_IDX_PRODUCT_STR:
if (pdev->pDesc->GetProductStrDescriptor != NULL)
{
pbuf = pdev->pDesc->GetProductStrDescriptor(pdev->dev_speed, &len);
}if (pdev->pDesc->GetProductStrDescriptor != NULL) { ... }
else
{
USBD_CtlError(pdev, req);
err++;
}else { ... }
break;
case USBD_IDX_PRODUCT_STR:
case USBD_IDX_SERIAL_STR:
if (pdev->pDesc->GetSerialStrDescriptor != NULL)
{
pbuf = pdev->pDesc->GetSerialStrDescriptor(pdev->dev_speed, &len);
}if (pdev->pDesc->GetSerialStrDescriptor != NULL) { ... }
else
{
USBD_CtlError(pdev, req);
err++;
}else { ... }
break;
case USBD_IDX_SERIAL_STR:
case USBD_IDX_CONFIG_STR:
if (pdev->pDesc->GetConfigurationStrDescriptor != NULL)
{
pbuf = pdev->pDesc->GetConfigurationStrDescriptor(pdev->dev_speed, &len);
}if (pdev->pDesc->GetConfigurationStrDescriptor != NULL) { ... }
else
{
USBD_CtlError(pdev, req);
err++;
}else { ... }
break;
case USBD_IDX_CONFIG_STR:
case USBD_IDX_INTERFACE_STR:
if (pdev->pDesc->GetInterfaceStrDescriptor != NULL)
{
pbuf = pdev->pDesc->GetInterfaceStrDescriptor(pdev->dev_speed, &len);
}if (pdev->pDesc->GetInterfaceStrDescriptor != NULL) { ... }
else
{
USBD_CtlError(pdev, req);
err++;
}else { ... }
break;
case USBD_IDX_INTERFACE_STR:
default:
#if (USBD_SUPPORT_USER_STRING_DESC == 1U)
pbuf = NULL;
for (uint32_t idx = 0U; (idx < pdev->NumClasses); idx++)
{
if (pdev->pClass[idx]->GetUsrStrDescriptor != NULL)
{
pdev->classId = idx;
pbuf = pdev->pClass[idx]->GetUsrStrDescriptor(pdev, LOBYTE(req->wValue), &len);
if (pbuf == NULL)
{
continue;
...}
else
{
break;
}else { ... }
}if (pdev->pClass[idx]->GetUsrStrDescriptor != NULL) { ... }
}for (uint32_t idx = 0U; (idx < pdev->NumClasses); idx++) { ... }
/* ... */#endif
#if (USBD_CLASS_USER_STRING_DESC == 1U)
if (pdev->pDesc->GetUserStrDescriptor != NULL)
{
pbuf = pdev->pDesc->GetUserStrDescriptor(pdev->dev_speed, LOBYTE(req->wValue), &len);
}if (pdev->pDesc->GetUserStrDescriptor != NULL) { ... }
else
{
USBD_CtlError(pdev, req);
err++;
}else { ... }
/* ... */#endif
#if ((USBD_CLASS_USER_STRING_DESC == 0U) && (USBD_SUPPORT_USER_STRING_DESC == 0U))
USBD_CtlError(pdev, req);
err++;/* ... */
#endif
break;default
}switch ((uint8_t)(req->wValue)) { ... }
break;
case USB_DESC_TYPE_STRING:
case USB_DESC_TYPE_DEVICE_QUALIFIER:
if (pdev->dev_speed == USBD_SPEED_HIGH)
{
#ifdef USE_USBD_COMPOSITE
if ((uint8_t)(pdev->NumClasses) > 0U)
{
pbuf = (uint8_t *)USBD_CMPSIT.GetDeviceQualifierDescriptor(&len);
}if ((uint8_t)(pdev->NumClasses) > 0U) { ... }
else
#endif
{
pbuf = (uint8_t *)pdev->pClass[0]->GetDeviceQualifierDescriptor(&len);
}else { ... }
}if (pdev->dev_speed == USBD_SPEED_HIGH) { ... }
else
{
USBD_CtlError(pdev, req);
err++;
}else { ... }
break;
case USB_DESC_TYPE_DEVICE_QUALIFIER:
case USB_DESC_TYPE_OTHER_SPEED_CONFIGURATION:
if (pdev->dev_speed == USBD_SPEED_HIGH)
{
#ifdef USE_USBD_COMPOSITE
if ((uint8_t)(pdev->NumClasses) > 0U)
{
pbuf = (uint8_t *)USBD_CMPSIT.GetOtherSpeedConfigDescriptor(&len);
}if ((uint8_t)(pdev->NumClasses) > 0U) { ... }
else
#endif
{
pbuf = (uint8_t *)pdev->pClass[0]->GetOtherSpeedConfigDescriptor(&len);
}else { ... }
pbuf[1] = USB_DESC_TYPE_OTHER_SPEED_CONFIGURATION;
}if (pdev->dev_speed == USBD_SPEED_HIGH) { ... }
else
{
USBD_CtlError(pdev, req);
err++;
}else { ... }
break;
case USB_DESC_TYPE_OTHER_SPEED_CONFIGURATION:
default:
USBD_CtlError(pdev, req);
err++;
break;default
}switch (req->wValue >> 8) { ... }
if (err != 0U)
{
return;
}if (err != 0U) { ... }
if (req->wLength != 0U)
{
if (len != 0U)
{
len = MIN(len, req->wLength);
(void)USBD_CtlSendData(pdev, pbuf, len);
}if (len != 0U) { ... }
else
{
USBD_CtlError(pdev, req);
}else { ... }
}if (req->wLength != 0U) { ... }
else
{
(void)USBD_CtlSendStatus(pdev);
}else { ... }
}{ ... }
/* ... */
static void USBD_SetAddress(USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *req)
{
uint8_t dev_addr;
if ((req->wIndex == 0U) && (req->wLength == 0U) && (req->wValue < 128U))
{
dev_addr = (uint8_t)(req->wValue) & 0x7FU;
if (pdev->dev_state == USBD_STATE_CONFIGURED)
{
USBD_CtlError(pdev, req);
}if (pdev->dev_state == USBD_STATE_CONFIGURED) { ... }
else
{
pdev->dev_address = dev_addr;
(void)USBD_LL_SetUSBAddress(pdev, dev_addr);
(void)USBD_CtlSendStatus(pdev);
if (dev_addr != 0U)
{
pdev->dev_state = USBD_STATE_ADDRESSED;
}if (dev_addr != 0U) { ... }
else
{
pdev->dev_state = USBD_STATE_DEFAULT;
}else { ... }
}else { ... }
}if ((req->wIndex == 0U) && (req->wLength == 0U) && (req->wValue < 128U)) { ... }
else
{
USBD_CtlError(pdev, req);
}else { ... }
}{ ... }
/* ... */
static USBD_StatusTypeDef USBD_SetConfig(USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *req)
{
USBD_StatusTypeDef ret = USBD_OK;
static uint8_t cfgidx;
cfgidx = (uint8_t)(req->wValue);
if (cfgidx > USBD_MAX_NUM_CONFIGURATION)
{
USBD_CtlError(pdev, req);
return USBD_FAIL;
}if (cfgidx > USBD_MAX_NUM_CONFIGURATION) { ... }
switch (pdev->dev_state)
{
case USBD_STATE_ADDRESSED:
if (cfgidx != 0U)
{
pdev->dev_config = cfgidx;
ret = USBD_SetClassConfig(pdev, cfgidx);
if (ret != USBD_OK)
{
USBD_CtlError(pdev, req);
pdev->dev_state = USBD_STATE_ADDRESSED;
}if (ret != USBD_OK) { ... }
else
{
(void)USBD_CtlSendStatus(pdev);
pdev->dev_state = USBD_STATE_CONFIGURED;
#if (USBD_USER_REGISTER_CALLBACK == 1U)
if (pdev->DevStateCallback != NULL)
{
pdev->DevStateCallback(USBD_STATE_CONFIGURED, cfgidx);
}if (pdev->DevStateCallback != NULL) { ... }
/* ... */#endif
}else { ... }
}if (cfgidx != 0U) { ... }
else
{
(void)USBD_CtlSendStatus(pdev);
}else { ... }
break;
case USBD_STATE_ADDRESSED:
case USBD_STATE_CONFIGURED:
if (cfgidx == 0U)
{
pdev->dev_state = USBD_STATE_ADDRESSED;
pdev->dev_config = cfgidx;
(void)USBD_ClrClassConfig(pdev, cfgidx);
(void)USBD_CtlSendStatus(pdev);
}if (cfgidx == 0U) { ... }
else if (cfgidx != pdev->dev_config)
{
(void)USBD_ClrClassConfig(pdev, (uint8_t)pdev->dev_config);
pdev->dev_config = cfgidx;
ret = USBD_SetClassConfig(pdev, cfgidx);
if (ret != USBD_OK)
{
USBD_CtlError(pdev, req);
(void)USBD_ClrClassConfig(pdev, (uint8_t)pdev->dev_config);
pdev->dev_state = USBD_STATE_ADDRESSED;
}if (ret != USBD_OK) { ... }
else
{
(void)USBD_CtlSendStatus(pdev);
}else { ... }
}else if (cfgidx != pdev->dev_config) { ... }
else
{
(void)USBD_CtlSendStatus(pdev);
}else { ... }
break;
case USBD_STATE_CONFIGURED:
default:
USBD_CtlError(pdev, req);
(void)USBD_ClrClassConfig(pdev, cfgidx);
ret = USBD_FAIL;
break;default
}switch (pdev->dev_state) { ... }
return ret;
}{ ... }
/* ... */
static void USBD_GetConfig(USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *req)
{
if (req->wLength != 1U)
{
USBD_CtlError(pdev, req);
}if (req->wLength != 1U) { ... }
else
{
switch (pdev->dev_state)
{
case USBD_STATE_DEFAULT:
case USBD_STATE_ADDRESSED:
pdev->dev_default_config = 0U;
(void)USBD_CtlSendData(pdev, (uint8_t *)&pdev->dev_default_config, 1U);
break;
case USBD_STATE_ADDRESSED:
case USBD_STATE_CONFIGURED:
(void)USBD_CtlSendData(pdev, (uint8_t *)&pdev->dev_config, 1U);
break;
case USBD_STATE_CONFIGURED:
default:
USBD_CtlError(pdev, req);
break;default
}switch (pdev->dev_state) { ... }
}else { ... }
}{ ... }
/* ... */
static void USBD_GetStatus(USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *req)
{
switch (pdev->dev_state)
{
case USBD_STATE_DEFAULT:
case USBD_STATE_ADDRESSED:
case USBD_STATE_CONFIGURED:
if (req->wLength != 0x2U)
{
USBD_CtlError(pdev, req);
break;
}if (req->wLength != 0x2U) { ... }
#if (USBD_SELF_POWERED == 1U)
pdev->dev_config_status = USB_CONFIG_SELF_POWERED;
#else
pdev->dev_config_status = 0U;
#endif
if (pdev->dev_remote_wakeup != 0U)
{
pdev->dev_config_status |= USB_CONFIG_REMOTE_WAKEUP;
}if (pdev->dev_remote_wakeup != 0U) { ... }
(void)USBD_CtlSendData(pdev, (uint8_t *)&pdev->dev_config_status, 2U);
break;
case USBD_STATE_CONFIGURED:
default:
USBD_CtlError(pdev, req);
break;default
}switch (pdev->dev_state) { ... }
}{ ... }
/* ... */
static void USBD_SetFeature(USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *req)
{
if (req->wValue == USB_FEATURE_REMOTE_WAKEUP)
{
pdev->dev_remote_wakeup = 1U;
(void)USBD_CtlSendStatus(pdev);
}if (req->wValue == USB_FEATURE_REMOTE_WAKEUP) { ... }
else if (req->wValue == USB_FEATURE_TEST_MODE)
{
pdev->dev_test_mode = (uint8_t)(req->wIndex >> 8);
(void)USBD_CtlSendStatus(pdev);
}else if (req->wValue == USB_FEATURE_TEST_MODE) { ... }
else
{
USBD_CtlError(pdev, req);
}else { ... }
}{ ... }
/* ... */
static void USBD_ClrFeature(USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *req)
{
switch (pdev->dev_state)
{
case USBD_STATE_DEFAULT:
case USBD_STATE_ADDRESSED:
case USBD_STATE_CONFIGURED:
if (req->wValue == USB_FEATURE_REMOTE_WAKEUP)
{
pdev->dev_remote_wakeup = 0U;
(void)USBD_CtlSendStatus(pdev);
}if (req->wValue == USB_FEATURE_REMOTE_WAKEUP) { ... }
break;
case USBD_STATE_CONFIGURED:
default:
USBD_CtlError(pdev, req);
break;default
}switch (pdev->dev_state) { ... }
}{ ... }
/* ... */
void USBD_ParseSetupRequest(USBD_SetupReqTypedef *req, uint8_t *pdata)
{
uint8_t *pbuff = pdata;
req->bmRequest = *(uint8_t *)(pbuff);
pbuff++;
req->bRequest = *(uint8_t *)(pbuff);
pbuff++;
req->wValue = SWAPBYTE(pbuff);
pbuff++;
pbuff++;
req->wIndex = SWAPBYTE(pbuff);
pbuff++;
pbuff++;
req->wLength = SWAPBYTE(pbuff);
}{ ... }
/* ... */
void USBD_CtlError(USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *req)
{
UNUSED(req);
(void)USBD_LL_StallEP(pdev, 0x80U);
(void)USBD_LL_StallEP(pdev, 0U);
}{ ... }
/* ... */
void USBD_GetString(uint8_t *desc, uint8_t *unicode, uint16_t *len)
{
uint8_t idx = 0U;
uint8_t *pdesc;
if (desc == NULL)
{
return;
}if (desc == NULL) { ... }
pdesc = desc;
*len = ((uint16_t)USBD_GetLen(pdesc) * 2U) + 2U;
unicode[idx] = *(uint8_t *)len;
idx++;
unicode[idx] = USB_DESC_TYPE_STRING;
idx++;
while (*pdesc != (uint8_t)'\0')
{
unicode[idx] = *pdesc;
pdesc++;
idx++;
unicode[idx] = 0U;
idx++;
}while (*pdesc != (uint8_t)'\0') { ... }
}{ ... }
/* ... */
static uint8_t USBD_GetLen(uint8_t *buf)
{
uint8_t len = 0U;
uint8_t *pbuff = buf;
while (*pbuff != (uint8_t)'\0')
{
len++;
pbuff++;
}while (*pbuff != (uint8_t)'\0') { ... }
return len;
}{ ... }
/* ... */
/* ... */
/* ... */
Includes