1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
25
26
29
30
34
35
36
39
42
43
44
47
50
51
52
53
56
59
60
61
64
67
68
71
72
73
74
75
76
77
78
81
82
83
86
87
88
96
97
98
99
100
105
106
107
108
109
110
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
139
140
141
142
143
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
172
173
174
175
176
177
178
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
212
216
217
218
219
220
221
222
223
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
385
389
390
391
399
403
404
405
406
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
462
463
464
465
466
467
468
469
470
471
472
473
474
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
501
503
507
508
509
510
511
512
513
514
515
516
517
521
522
523
524
525
526
527
531
532
533
534
535
536
537
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
584
585
586
587
594
601
602
610
614
615
616
617
625
626
635
636
643
647
648
670
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
695
696
698
699
700
701
702
703
704
705
706
707
708
709
715
716
717
718
719
720
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
772
773
774
775
776
777
783
788
792
793
799
800
801
802
804
805
806
807
808
809
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
853
854
868
869
870
876
877
878
879
880
881
882
883
884
885
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
921
922
923
924
925
926
927
928
929
930
931
937
938
939
940
941
942
943
944
945
946
947
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
983
984
985
986
987
988
989
990
991
992
993
994
1000
1001
1002
1003
1004
1005
1006
1007
1008
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1062
1063
1064
1065
1066
1067
1068
1069
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1117
1118
1119
1120
1121
1122
1123
1124
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1182
1183
1184
1185
1186
1187
1190
1191
1194
1195
1198
1199
1202
1203
1204
1205
/* ... */
#include "usbh_ctlreq.h"
/* ... */
/* ... */
/* ... */
/* ... */
/* ... */
/* ... */
/* ... */
/* ... */
/* ... */
/* ... */
/* ... */
/* ... */
static USBH_StatusTypeDef USBH_HandleControl(USBH_HandleTypeDef *phost);
static USBH_StatusTypeDef USBH_ParseDevDesc(USBH_HandleTypeDef *phost, uint8_t *buf, uint16_t length);
static USBH_StatusTypeDef USBH_ParseCfgDesc(USBH_HandleTypeDef *phost, uint8_t *buf, uint16_t length);
static USBH_StatusTypeDef USBH_ParseEPDesc(USBH_HandleTypeDef *phost, USBH_EpDescTypeDef *ep_descriptor, uint8_t *buf);
static void USBH_ParseStringDesc(uint8_t *psrc, uint8_t *pdest, uint16_t length);
static void USBH_ParseInterfaceDesc(USBH_InterfaceDescTypeDef *if_descriptor, uint8_t *buf);
/* ... */
/* ... */
/* ... */
USBH_StatusTypeDef USBH_Get_DevDesc(USBH_HandleTypeDef *phost, uint16_t length)
{
USBH_StatusTypeDef status;
if (length > sizeof(phost->device.Data))
{
USBH_ErrLog("Control error: Get Device Descriptor failed, data buffer size issue");
return USBH_NOT_SUPPORTED;
}if (length > sizeof(phost->device.Data)) { ... }
status = USBH_GetDescriptor(phost,
USB_REQ_RECIPIENT_DEVICE | USB_REQ_TYPE_STANDARD,
USB_DESC_DEVICE, phost->device.Data, length);
if (status == USBH_OK)
{
status = USBH_ParseDevDesc(phost, phost->device.Data, length);
}if (status == USBH_OK) { ... }
return status;
}{ ... }
/* ... */
USBH_StatusTypeDef USBH_Get_CfgDesc(USBH_HandleTypeDef *phost, uint16_t length)
{
USBH_StatusTypeDef status;
uint8_t *pData = phost->device.CfgDesc_Raw;
if (length > sizeof(phost->device.CfgDesc_Raw))
{
USBH_ErrLog("Control error: Get configuration Descriptor failed, data buffer size issue");
return USBH_NOT_SUPPORTED;
}if (length > sizeof(phost->device.CfgDesc_Raw)) { ... }
status = USBH_GetDescriptor(phost, (USB_REQ_RECIPIENT_DEVICE | USB_REQ_TYPE_STANDARD),
USB_DESC_CONFIGURATION, pData, length);
if (status == USBH_OK)
{
status = USBH_ParseCfgDesc(phost, pData, length);
}if (status == USBH_OK) { ... }
return status;
}{ ... }
/* ... */
USBH_StatusTypeDef USBH_Get_StringDesc(USBH_HandleTypeDef *phost, uint8_t string_index, uint8_t *buff, uint16_t length)
{
USBH_StatusTypeDef status;
if ((length > sizeof(phost->device.Data)) || (buff == NULL))
{
USBH_ErrLog("Control error: Get String Descriptor failed, data buffer size issue");
return USBH_NOT_SUPPORTED;
}if ((length > sizeof(phost->device.Data)) || (buff == NULL)) { ... }
status = USBH_GetDescriptor(phost,
USB_REQ_RECIPIENT_DEVICE | USB_REQ_TYPE_STANDARD,
USB_DESC_STRING | string_index,
phost->device.Data, length);
if (status == USBH_OK)
{
USBH_ParseStringDesc(phost->device.Data, buff, length);
}if (status == USBH_OK) { ... }
return status;
}{ ... }
/* ... */
USBH_StatusTypeDef USBH_GetDescriptor(USBH_HandleTypeDef *phost, uint8_t req_type, uint16_t value_idx,
uint8_t *buff, uint16_t length)
{
if (phost->RequestState == CMD_SEND)
{
phost->Control.setup.b.bmRequestType = USB_D2H | req_type;
phost->Control.setup.b.bRequest = USB_REQ_GET_DESCRIPTOR;
phost->Control.setup.b.wValue.w = value_idx;
if ((value_idx & 0xff00U) == USB_DESC_STRING)
{
phost->Control.setup.b.wIndex.w = 0x0409U;
}if ((value_idx & 0xff00U) == USB_DESC_STRING) { ... }
else
{
phost->Control.setup.b.wIndex.w = 0U;
}else { ... }
phost->Control.setup.b.wLength.w = length;
}if (phost->RequestState == CMD_SEND) { ... }
return USBH_CtlReq(phost, buff, length);
}{ ... }
/* ... */
USBH_StatusTypeDef USBH_SetAddress(USBH_HandleTypeDef *phost,
uint8_t DeviceAddress)
{
if (phost->RequestState == CMD_SEND)
{
phost->Control.setup.b.bmRequestType = USB_H2D | USB_REQ_RECIPIENT_DEVICE | \
USB_REQ_TYPE_STANDARD;
phost->Control.setup.b.bRequest = USB_REQ_SET_ADDRESS;
phost->Control.setup.b.wValue.w = (uint16_t)DeviceAddress;
phost->Control.setup.b.wIndex.w = 0U;
phost->Control.setup.b.wLength.w = 0U;
}if (phost->RequestState == CMD_SEND) { ... }
return USBH_CtlReq(phost, NULL, 0U);
}{ ... }
/* ... */
USBH_StatusTypeDef USBH_SetCfg(USBH_HandleTypeDef *phost, uint16_t cfg_idx)
{
if (phost->RequestState == CMD_SEND)
{
phost->Control.setup.b.bmRequestType = USB_H2D | USB_REQ_RECIPIENT_DEVICE
| USB_REQ_TYPE_STANDARD;
phost->Control.setup.b.bRequest = USB_REQ_SET_CONFIGURATION;
phost->Control.setup.b.wValue.w = cfg_idx;
phost->Control.setup.b.wIndex.w = 0U;
phost->Control.setup.b.wLength.w = 0U;
}if (phost->RequestState == CMD_SEND) { ... }
return USBH_CtlReq(phost, NULL, 0U);
}{ ... }
/* ... */
USBH_StatusTypeDef USBH_SetInterface(USBH_HandleTypeDef *phost, uint8_t ep_num, uint8_t altSetting)
{
if (phost->RequestState == CMD_SEND)
{
phost->Control.setup.b.bmRequestType = USB_H2D | USB_REQ_RECIPIENT_INTERFACE
| USB_REQ_TYPE_STANDARD;
phost->Control.setup.b.bRequest = USB_REQ_SET_INTERFACE;
phost->Control.setup.b.wValue.w = altSetting;
phost->Control.setup.b.wIndex.w = ep_num;
phost->Control.setup.b.wLength.w = 0U;
}if (phost->RequestState == CMD_SEND) { ... }
return USBH_CtlReq(phost, NULL, 0U);
}{ ... }
/* ... */
USBH_StatusTypeDef USBH_SetFeature(USBH_HandleTypeDef *phost, uint8_t wValue)
{
if (phost->RequestState == CMD_SEND)
{
phost->Control.setup.b.bmRequestType = USB_H2D | USB_REQ_RECIPIENT_DEVICE
| USB_REQ_TYPE_STANDARD;
phost->Control.setup.b.bRequest = USB_REQ_SET_FEATURE;
phost->Control.setup.b.wValue.w = wValue;
phost->Control.setup.b.wIndex.w = 0U;
phost->Control.setup.b.wLength.w = 0U;
}if (phost->RequestState == CMD_SEND) { ... }
return USBH_CtlReq(phost, NULL, 0U);
}{ ... }
/* ... */
USBH_StatusTypeDef USBH_ClrFeature(USBH_HandleTypeDef *phost, uint8_t ep_num)
{
if (phost->RequestState == CMD_SEND)
{
phost->Control.setup.b.bmRequestType = USB_H2D | USB_REQ_RECIPIENT_ENDPOINT
| USB_REQ_TYPE_STANDARD;
phost->Control.setup.b.bRequest = USB_REQ_CLEAR_FEATURE;
phost->Control.setup.b.wValue.w = FEATURE_SELECTOR_ENDPOINT;
phost->Control.setup.b.wIndex.w = ep_num;
phost->Control.setup.b.wLength.w = 0U;
}if (phost->RequestState == CMD_SEND) { ... }
return USBH_CtlReq(phost, NULL, 0U);
}{ ... }
/* ... */
static USBH_StatusTypeDef USBH_ParseDevDesc(USBH_HandleTypeDef *phost, uint8_t *buf, uint16_t length)
{
USBH_DevDescTypeDef *dev_desc = &phost->device.DevDesc;
USBH_StatusTypeDef status = USBH_OK;
if (buf == NULL)
{
return USBH_FAIL;
}if (buf == NULL) { ... }
dev_desc->bLength = *(uint8_t *)(buf + 0U);
dev_desc->bDescriptorType = *(uint8_t *)(buf + 1U);
dev_desc->bcdUSB = LE16(buf + 2U);
dev_desc->bDeviceClass = *(uint8_t *)(buf + 4U);
dev_desc->bDeviceSubClass = *(uint8_t *)(buf + 5U);
dev_desc->bDeviceProtocol = *(uint8_t *)(buf + 6U);
dev_desc->bMaxPacketSize = *(uint8_t *)(buf + 7U);
if ((phost->device.speed == (uint8_t)USBH_SPEED_HIGH) ||
(phost->device.speed == (uint8_t)USBH_SPEED_FULL))
{
switch (dev_desc->bMaxPacketSize)
{
case 8:
case 16:
case 32:
case 64:
break;
case 64:
default:
dev_desc->bMaxPacketSize = 8U;
break;default
}switch (dev_desc->bMaxPacketSize) { ... }
}if ((phost->device.speed == (uint8_t)USBH_SPEED_HIGH) || (phost->device.speed == (uint8_t)USBH_SPEED_FULL)) { ... }
else if (phost->device.speed == (uint8_t)USBH_SPEED_LOW)
{
if (dev_desc->bMaxPacketSize != 8U)
{
dev_desc->bMaxPacketSize = 8U;
}if (dev_desc->bMaxPacketSize != 8U) { ... }
}else if (phost->device.speed == (uint8_t)USBH_SPEED_LOW) { ... }
else
{
status = USBH_NOT_SUPPORTED;
}else { ... }
if (length > 8U)
{
/* ... */
dev_desc->idVendor = LE16(buf + 8U);
dev_desc->idProduct = LE16(buf + 10U);
dev_desc->bcdDevice = LE16(buf + 12U);
dev_desc->iManufacturer = *(uint8_t *)(buf + 14U);
dev_desc->iProduct = *(uint8_t *)(buf + 15U);
dev_desc->iSerialNumber = *(uint8_t *)(buf + 16U);
dev_desc->bNumConfigurations = *(uint8_t *)(buf + 17U);
}if (length > 8U) { ... }
return status;
}{ ... }
/* ... */
static USBH_StatusTypeDef USBH_ParseCfgDesc(USBH_HandleTypeDef *phost, uint8_t *buf, uint16_t length)
{
USBH_CfgDescTypeDef *cfg_desc = &phost->device.CfgDesc;
USBH_StatusTypeDef status = USBH_OK;
USBH_InterfaceDescTypeDef *pif;
USBH_EpDescTypeDef *pep;
USBH_DescHeader_t *pdesc;
uint16_t ptr;
uint8_t if_ix = 0U;
uint8_t ep_ix = 0U;
if (buf == NULL)
{
return USBH_FAIL;
}if (buf == NULL) { ... }
pdesc = (USBH_DescHeader_t *)(void *)buf;
cfg_desc->bLength = *(uint8_t *)(buf + 0U);
cfg_desc->bDescriptorType = *(uint8_t *)(buf + 1U);
cfg_desc->wTotalLength = MIN(((uint16_t) LE16(buf + 2U)), ((uint16_t)USBH_MAX_SIZE_CONFIGURATION));
cfg_desc->bNumInterfaces = *(uint8_t *)(buf + 4U);
cfg_desc->bConfigurationValue = *(uint8_t *)(buf + 5U);
cfg_desc->iConfiguration = *(uint8_t *)(buf + 6U);
cfg_desc->bmAttributes = *(uint8_t *)(buf + 7U);
cfg_desc->bMaxPower = *(uint8_t *)(buf + 8U);
if (cfg_desc->bLength != USB_CONFIGURATION_DESC_SIZE)
{
cfg_desc->bLength = USB_CONFIGURATION_DESC_SIZE;
}if (cfg_desc->bLength != USB_CONFIGURATION_DESC_SIZE) { ... }
if (length > USB_CONFIGURATION_DESC_SIZE)
{
ptr = USB_LEN_CFG_DESC;
pif = (USBH_InterfaceDescTypeDef *)NULL;
while ((if_ix < USBH_MAX_NUM_INTERFACES) && (ptr < cfg_desc->wTotalLength))
{
pdesc = USBH_GetNextDesc((uint8_t *)(void *)pdesc, &ptr);
if (pdesc->bDescriptorType == USB_DESC_TYPE_INTERFACE)
{
if (pdesc->bLength != USB_INTERFACE_DESC_SIZE)
{
pdesc->bLength = USB_INTERFACE_DESC_SIZE;
}if (pdesc->bLength != USB_INTERFACE_DESC_SIZE) { ... }
pif = &cfg_desc->Itf_Desc[if_ix];
USBH_ParseInterfaceDesc(pif, (uint8_t *)(void *)pdesc);
ep_ix = 0U;
pep = (USBH_EpDescTypeDef *)NULL;
while ((ep_ix < pif->bNumEndpoints) && (ptr < cfg_desc->wTotalLength))
{
pdesc = USBH_GetNextDesc((uint8_t *)(void *)pdesc, &ptr);
if (pdesc->bDescriptorType == USB_DESC_TYPE_ENDPOINT)
{
if ((pif->bInterfaceClass == 0x01U) &&
((pif->bInterfaceSubClass == 0x02U) || (pif->bInterfaceSubClass == 0x03U)))
{
if ((pif->bInterfaceProtocol == 0x00U) && (pdesc->bLength != 0x09U))
{
pdesc->bLength = 0x09U;
}if ((pif->bInterfaceProtocol == 0x00U) && (pdesc->bLength != 0x09U)) { ... }
}if ((pif->bInterfaceClass == 0x01U) && ((pif->bInterfaceSubClass == 0x02U) || (pif->bInterfaceSubClass == 0x03U))) { ... }
/* ... */
else
{
pdesc->bLength = USB_ENDPOINT_DESC_SIZE;
}else { ... }
pep = &cfg_desc->Itf_Desc[if_ix].Ep_Desc[ep_ix];
status = USBH_ParseEPDesc(phost, pep, (uint8_t *)(void *)pdesc);
ep_ix++;
}if (pdesc->bDescriptorType == USB_DESC_TYPE_ENDPOINT) { ... }
}while ((ep_ix < pif->bNumEndpoints) && (ptr < cfg_desc->wTotalLength)) { ... }
if (ep_ix < pif->bNumEndpoints)
{
return USBH_NOT_SUPPORTED;
}if (ep_ix < pif->bNumEndpoints) { ... }
if_ix++;
}if (pdesc->bDescriptorType == USB_DESC_TYPE_INTERFACE) { ... }
}while ((if_ix < USBH_MAX_NUM_INTERFACES) && (ptr < cfg_desc->wTotalLength)) { ... }
if (if_ix < MIN(cfg_desc->bNumInterfaces, (uint8_t)USBH_MAX_NUM_INTERFACES))
{
return USBH_NOT_SUPPORTED;
}if (if_ix < MIN(cfg_desc->bNumInterfaces, (uint8_t)USBH_MAX_NUM_INTERFACES)) { ... }
}if (length > USB_CONFIGURATION_DESC_SIZE) { ... }
return status;
}{ ... }
/* ... */
static void USBH_ParseInterfaceDesc(USBH_InterfaceDescTypeDef *if_descriptor, uint8_t *buf)
{
if_descriptor->bLength = *(uint8_t *)(buf + 0U);
if_descriptor->bDescriptorType = *(uint8_t *)(buf + 1U);
if_descriptor->bInterfaceNumber = *(uint8_t *)(buf + 2U);
if_descriptor->bAlternateSetting = *(uint8_t *)(buf + 3U);
if_descriptor->bNumEndpoints = MIN(*(uint8_t *)(buf + 4U), USBH_MAX_NUM_ENDPOINTS);
if_descriptor->bInterfaceClass = *(uint8_t *)(buf + 5U);
if_descriptor->bInterfaceSubClass = *(uint8_t *)(buf + 6U);
if_descriptor->bInterfaceProtocol = *(uint8_t *)(buf + 7U);
if_descriptor->iInterface = *(uint8_t *)(buf + 8U);
}{ ... }
/* ... */
static USBH_StatusTypeDef USBH_ParseEPDesc(USBH_HandleTypeDef *phost, USBH_EpDescTypeDef *ep_descriptor, uint8_t *buf)
{
USBH_StatusTypeDef status = USBH_OK;
ep_descriptor->bLength = *(uint8_t *)(buf + 0U);
ep_descriptor->bDescriptorType = *(uint8_t *)(buf + 1U);
ep_descriptor->bEndpointAddress = *(uint8_t *)(buf + 2U);
ep_descriptor->bmAttributes = *(uint8_t *)(buf + 3U);
ep_descriptor->wMaxPacketSize = LE16(buf + 4U);
ep_descriptor->bInterval = *(uint8_t *)(buf + 6U);
if ((ep_descriptor->wMaxPacketSize == 0x00U) ||
(ep_descriptor->wMaxPacketSize > USBH_MAX_EP_PACKET_SIZE) ||
(ep_descriptor->wMaxPacketSize > USBH_MAX_DATA_BUFFER))
{
status = USBH_NOT_SUPPORTED;
}if ((ep_descriptor->wMaxPacketSize == 0x00U) || (ep_descriptor->wMaxPacketSize > USBH_MAX_EP_PACKET_SIZE) || (ep_descriptor->wMaxPacketSize > USBH_MAX_DATA_BUFFER)) { ... }
if (phost->device.speed == (uint8_t)USBH_SPEED_HIGH)
{
if ((ep_descriptor->bmAttributes & EP_TYPE_MSK) == EP_TYPE_BULK)
{
if (ep_descriptor->wMaxPacketSize > 512U)
{
status = USBH_NOT_SUPPORTED;
}if (ep_descriptor->wMaxPacketSize > 512U) { ... }
}if ((ep_descriptor->bmAttributes & EP_TYPE_MSK) == EP_TYPE_BULK) { ... }
else if ((ep_descriptor->bmAttributes & EP_TYPE_MSK) == EP_TYPE_CTRL)
{
if (ep_descriptor->wMaxPacketSize > 64U)
{
status = USBH_NOT_SUPPORTED;
}if (ep_descriptor->wMaxPacketSize > 64U) { ... }
}else if ((ep_descriptor->bmAttributes & EP_TYPE_MSK) == EP_TYPE_CTRL) { ... }
else if (((ep_descriptor->bmAttributes & EP_TYPE_MSK) == EP_TYPE_ISOC) ||
((ep_descriptor->bmAttributes & EP_TYPE_MSK) == EP_TYPE_INTR))
{
if ((ep_descriptor->bInterval == 0U) || (ep_descriptor->bInterval > 0x10U))
{
status = USBH_NOT_SUPPORTED;
}if ((ep_descriptor->bInterval == 0U) || (ep_descriptor->bInterval > 0x10U)) { ... }
}else if (((ep_descriptor->bmAttributes & EP_TYPE_MSK) == EP_TYPE_ISOC) || ((ep_descriptor->bmAttributes & EP_TYPE_MSK) == EP_TYPE_INTR)) { ... }
else
{
status = USBH_NOT_SUPPORTED;
}else { ... }
}if (phost->device.speed == (uint8_t)USBH_SPEED_HIGH) { ... }
else if (phost->device.speed == (uint8_t)USBH_SPEED_FULL)
{
if (((ep_descriptor->bmAttributes & EP_TYPE_MSK) == EP_TYPE_BULK) ||
((ep_descriptor->bmAttributes & EP_TYPE_MSK) == EP_TYPE_CTRL))
{
if (ep_descriptor->wMaxPacketSize > 64U)
{
status = USBH_NOT_SUPPORTED;
}if (ep_descriptor->wMaxPacketSize > 64U) { ... }
}if (((ep_descriptor->bmAttributes & EP_TYPE_MSK) == EP_TYPE_BULK) || ((ep_descriptor->bmAttributes & EP_TYPE_MSK) == EP_TYPE_CTRL)) { ... }
else if ((ep_descriptor->bmAttributes & EP_TYPE_MSK) == EP_TYPE_ISOC)
{
if ((ep_descriptor->bInterval == 0U) ||
(ep_descriptor->bInterval > 0x10U) ||
(ep_descriptor->wMaxPacketSize > 64U))
{
status = USBH_NOT_SUPPORTED;
}if ((ep_descriptor->bInterval == 0U) || (ep_descriptor->bInterval > 0x10U) || (ep_descriptor->wMaxPacketSize > 64U)) { ... }
}else if ((ep_descriptor->bmAttributes & EP_TYPE_MSK) == EP_TYPE_ISOC) { ... }
else if ((ep_descriptor->bmAttributes & EP_TYPE_MSK) == EP_TYPE_INTR)
{
if ((ep_descriptor->bInterval == 0U) || (ep_descriptor->wMaxPacketSize > 1023U))
{
status = USBH_NOT_SUPPORTED;
}if ((ep_descriptor->bInterval == 0U) || (ep_descriptor->wMaxPacketSize > 1023U)) { ... }
}else if ((ep_descriptor->bmAttributes & EP_TYPE_MSK) == EP_TYPE_INTR) { ... }
else
{
status = USBH_NOT_SUPPORTED;
}else { ... }
}else if (phost->device.speed == (uint8_t)USBH_SPEED_FULL) { ... }
else if (phost->device.speed == (uint8_t)USBH_SPEED_LOW)
{
if ((ep_descriptor->bmAttributes & EP_TYPE_MSK) == EP_TYPE_CTRL)
{
if (ep_descriptor->wMaxPacketSize != 8U)
{
status = USBH_NOT_SUPPORTED;
}if (ep_descriptor->wMaxPacketSize != 8U) { ... }
}if ((ep_descriptor->bmAttributes & EP_TYPE_MSK) == EP_TYPE_CTRL) { ... }
else if ((ep_descriptor->bmAttributes & EP_TYPE_MSK) == EP_TYPE_INTR)
{
if ((ep_descriptor->bInterval == 0U) || (ep_descriptor->wMaxPacketSize > 8U))
{
status = USBH_NOT_SUPPORTED;
}if ((ep_descriptor->bInterval == 0U) || (ep_descriptor->wMaxPacketSize > 8U)) { ... }
}else if ((ep_descriptor->bmAttributes & EP_TYPE_MSK) == EP_TYPE_INTR) { ... }
else
{
status = USBH_NOT_SUPPORTED;
}else { ... }
}else if (phost->device.speed == (uint8_t)USBH_SPEED_LOW) { ... }
else
{
status = USBH_NOT_SUPPORTED;
}else { ... }
return status;
}{ ... }
/* ... */
static void USBH_ParseStringDesc(uint8_t *psrc, uint8_t *pdest, uint16_t length)
{
uint16_t strlength;
uint16_t idx;
/* ... */
/* ... */
if (psrc[1] == USB_DESC_TYPE_STRING)
{
strlength = ((((uint16_t)psrc[0] - 2U) <= length) ? ((uint16_t)psrc[0] - 2U) : length);
psrc += 2U;
for (idx = 0U; idx < strlength; idx += 2U)
{
*pdest = psrc[idx];
pdest++;
}for (idx = 0U; idx < strlength; idx += 2U) { ... }
*pdest = 0U;
}if (psrc[1] == USB_DESC_TYPE_STRING) { ... }
}{ ... }
/* ... */
USBH_DescHeader_t *USBH_GetNextDesc(uint8_t *pbuf, uint16_t *ptr)
{
USBH_DescHeader_t *pnext;
*ptr += ((USBH_DescHeader_t *)(void *)pbuf)->bLength;
pnext = (USBH_DescHeader_t *)(void *)((uint8_t *)(void *)pbuf + \
((USBH_DescHeader_t *)(void *)pbuf)->bLength);
return (pnext);
}{ ... }
/* ... */
USBH_StatusTypeDef USBH_CtlReq(USBH_HandleTypeDef *phost, uint8_t *buff,
uint16_t length)
{
USBH_StatusTypeDef status;
status = USBH_BUSY;
switch (phost->RequestState)
{
case CMD_SEND:
phost->Control.buff = buff;
phost->Control.length = length;
phost->Control.state = CTRL_SETUP;
phost->RequestState = CMD_WAIT;
status = USBH_BUSY;
#if (USBH_USE_OS == 1U)
phost->os_msg = (uint32_t)USBH_CONTROL_EVENT;
#if (osCMSIS < 0x20000U)
(void)osMessagePut(phost->os_event, phost->os_msg, 0U);
#else
(void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, 0U);
#endif/* ... */
#endif
break;
case CMD_SEND:
case CMD_WAIT:
status = USBH_HandleControl(phost);
if ((status == USBH_OK) || (status == USBH_NOT_SUPPORTED))
{
phost->RequestState = CMD_SEND;
phost->Control.state = CTRL_IDLE;
}if ((status == USBH_OK) || (status == USBH_NOT_SUPPORTED)) { ... }
else if (status == USBH_FAIL)
{
phost->RequestState = CMD_SEND;
}else if (status == USBH_FAIL) { ... }
else
{
}else { ... }
#if (USBH_USE_OS == 1U)
phost->os_msg = (uint32_t)USBH_CONTROL_EVENT;
#if (osCMSIS < 0x20000U)
(void)osMessagePut(phost->os_event, phost->os_msg, 0U);
#else
(void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, 0U);
#endif/* ... */
#endif
break;
case CMD_WAIT:
default:
break;default
}switch (phost->RequestState) { ... }
return status;
}{ ... }
/* ... */
static USBH_StatusTypeDef USBH_HandleControl(USBH_HandleTypeDef *phost)
{
uint8_t direction;
USBH_StatusTypeDef status = USBH_BUSY;
USBH_URBStateTypeDef URB_Status = USBH_URB_IDLE;
switch (phost->Control.state)
{
case CTRL_SETUP:
(void)USBH_CtlSendSetup(phost, (uint8_t *)(void *)phost->Control.setup.d8,
phost->Control.pipe_out);
phost->Control.state = CTRL_SETUP_WAIT;
break;
case CTRL_SETUP:
case CTRL_SETUP_WAIT:
URB_Status = USBH_LL_GetURBState(phost, phost->Control.pipe_out);
if (URB_Status == USBH_URB_DONE)
{
direction = (phost->Control.setup.b.bmRequestType & USB_REQ_DIR_MASK);
if (phost->Control.setup.b.wLength.w != 0U)
{
if (direction == USB_D2H)
{
phost->Control.state = CTRL_DATA_IN;
}if (direction == USB_D2H) { ... }
else
{
phost->Control.state = CTRL_DATA_OUT;
}else { ... }
}if (phost->Control.setup.b.wLength.w != 0U) { ... }
else
{
if (direction == USB_D2H)
{
phost->Control.state = CTRL_STATUS_OUT;
}if (direction == USB_D2H) { ... }
else
{
phost->Control.state = CTRL_STATUS_IN;
}else { ... }
}else { ... }
#if (USBH_USE_OS == 1U)
phost->os_msg = (uint32_t)USBH_CONTROL_EVENT;
#if (osCMSIS < 0x20000U)
(void)osMessagePut(phost->os_event, phost->os_msg, 0U);
#else
(void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, 0U);
#endif/* ... */
#endif
}if (URB_Status == USBH_URB_DONE) { ... }
else
{
if ((URB_Status == USBH_URB_ERROR) || (URB_Status == USBH_URB_NOTREADY))
{
phost->Control.state = CTRL_ERROR;
#if (USBH_USE_OS == 1U)
phost->os_msg = (uint32_t)USBH_CONTROL_EVENT;
#if (osCMSIS < 0x20000U)
(void)osMessagePut(phost->os_event, phost->os_msg, 0U);
#else
(void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, 0U);
#endif/* ... */
#endif
}if ((URB_Status == USBH_URB_ERROR) || (URB_Status == USBH_URB_NOTREADY)) { ... }
}else { ... }
break;
case CTRL_SETUP_WAIT:
case CTRL_DATA_IN:
phost->Control.timer = (uint16_t)phost->Timer;
(void)USBH_CtlReceiveData(phost, phost->Control.buff,
phost->Control.length, phost->Control.pipe_in);
phost->Control.state = CTRL_DATA_IN_WAIT;
break;
case CTRL_DATA_IN:
case CTRL_DATA_IN_WAIT:
URB_Status = USBH_LL_GetURBState(phost, phost->Control.pipe_in);
if (URB_Status == USBH_URB_DONE)
{
phost->Control.state = CTRL_STATUS_OUT;
#if (USBH_USE_OS == 1U)
phost->os_msg = (uint32_t)USBH_CONTROL_EVENT;
#if (osCMSIS < 0x20000U)
(void)osMessagePut(phost->os_event, phost->os_msg, 0U);
#else
(void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, 0U);
#endif/* ... */
#endif
}if (URB_Status == USBH_URB_DONE) { ... }
if (URB_Status == USBH_URB_STALL)
{
status = USBH_NOT_SUPPORTED;
#if (USBH_USE_OS == 1U)
phost->os_msg = (uint32_t)USBH_CONTROL_EVENT;
#if (osCMSIS < 0x20000U)
(void)osMessagePut(phost->os_event, phost->os_msg, 0U);
#else
(void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, 0U);
#endif/* ... */
#endif
}if (URB_Status == USBH_URB_STALL) { ... }
else
{
if (URB_Status == USBH_URB_ERROR)
{
phost->Control.state = CTRL_ERROR;
#if (USBH_USE_OS == 1U)
phost->os_msg = (uint32_t)USBH_CONTROL_EVENT;
#if (osCMSIS < 0x20000U)
(void)osMessagePut(phost->os_event, phost->os_msg, 0U);
#else
(void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, 0U);
#endif/* ... */
#endif
}if (URB_Status == USBH_URB_ERROR) { ... }
}else { ... }
break;
case CTRL_DATA_IN_WAIT:
case CTRL_DATA_OUT:
(void)USBH_CtlSendData(phost, phost->Control.buff, phost->Control.length,
phost->Control.pipe_out, 1U);
phost->Control.timer = (uint16_t)phost->Timer;
phost->Control.state = CTRL_DATA_OUT_WAIT;
break;
case CTRL_DATA_OUT:
case CTRL_DATA_OUT_WAIT:
URB_Status = USBH_LL_GetURBState(phost, phost->Control.pipe_out);
if (URB_Status == USBH_URB_DONE)
{
phost->Control.state = CTRL_STATUS_IN;
#if (USBH_USE_OS == 1U)
phost->os_msg = (uint32_t)USBH_CONTROL_EVENT;
#if (osCMSIS < 0x20000U)
(void)osMessagePut(phost->os_event, phost->os_msg, 0U);
#else
(void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, 0U);
#endif/* ... */
#endif
}if (URB_Status == USBH_URB_DONE) { ... }
else if (URB_Status == USBH_URB_STALL)
{
phost->Control.state = CTRL_STALLED;
status = USBH_NOT_SUPPORTED;
#if (USBH_USE_OS == 1U)
phost->os_msg = (uint32_t)USBH_CONTROL_EVENT;
#if (osCMSIS < 0x20000U)
(void)osMessagePut(phost->os_event, phost->os_msg, 0U);
#else
(void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, 0U);
#endif/* ... */
#endif
}else if (URB_Status == USBH_URB_STALL) { ... }
else if (URB_Status == USBH_URB_NOTREADY)
{
phost->Control.state = CTRL_DATA_OUT;
#if (USBH_USE_OS == 1U)
phost->os_msg = (uint32_t)USBH_CONTROL_EVENT;
#if (osCMSIS < 0x20000U)
(void)osMessagePut(phost->os_event, phost->os_msg, 0U);
#else
(void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, 0U);
#endif/* ... */
#endif
}else if (URB_Status == USBH_URB_NOTREADY) { ... }
else
{
if (URB_Status == USBH_URB_ERROR)
{
phost->Control.state = CTRL_ERROR;
status = USBH_FAIL;
#if (USBH_USE_OS == 1U)
phost->os_msg = (uint32_t)USBH_CONTROL_EVENT;
#if (osCMSIS < 0x20000U)
(void)osMessagePut(phost->os_event, phost->os_msg, 0U);
#else
(void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, 0U);
#endif/* ... */
#endif
}if (URB_Status == USBH_URB_ERROR) { ... }
}else { ... }
break;
case CTRL_DATA_OUT_WAIT:
case CTRL_STATUS_IN:
(void)USBH_CtlReceiveData(phost, NULL, 0U, phost->Control.pipe_in);
phost->Control.timer = (uint16_t)phost->Timer;
phost->Control.state = CTRL_STATUS_IN_WAIT;
break;
case CTRL_STATUS_IN:
case CTRL_STATUS_IN_WAIT:
URB_Status = USBH_LL_GetURBState(phost, phost->Control.pipe_in);
if (URB_Status == USBH_URB_DONE)
{
phost->Control.state = CTRL_COMPLETE;
status = USBH_OK;
#if (USBH_USE_OS == 1U)
phost->os_msg = (uint32_t)USBH_CONTROL_EVENT;
#if (osCMSIS < 0x20000U)
(void)osMessagePut(phost->os_event, phost->os_msg, 0U);
#else
(void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, 0U);
#endif/* ... */
#endif
}if (URB_Status == USBH_URB_DONE) { ... }
else if (URB_Status == USBH_URB_ERROR)
{
phost->Control.state = CTRL_ERROR;
#if (USBH_USE_OS == 1U)
phost->os_msg = (uint32_t)USBH_CONTROL_EVENT;
#if (osCMSIS < 0x20000U)
(void)osMessagePut(phost->os_event, phost->os_msg, 0U);
#else
(void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, 0U);
#endif/* ... */
#endif
}else if (URB_Status == USBH_URB_ERROR) { ... }
else
{
if (URB_Status == USBH_URB_STALL)
{
status = USBH_NOT_SUPPORTED;
#if (USBH_USE_OS == 1U)
phost->os_msg = (uint32_t)USBH_CONTROL_EVENT;
#if (osCMSIS < 0x20000U)
(void)osMessagePut(phost->os_event, phost->os_msg, 0U);
#else
(void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, 0U);
#endif/* ... */
#endif
}if (URB_Status == USBH_URB_STALL) { ... }
}else { ... }
break;
case CTRL_STATUS_IN_WAIT:
case CTRL_STATUS_OUT:
(void)USBH_CtlSendData(phost, NULL, 0U, phost->Control.pipe_out, 1U);
phost->Control.timer = (uint16_t)phost->Timer;
phost->Control.state = CTRL_STATUS_OUT_WAIT;
break;
case CTRL_STATUS_OUT:
case CTRL_STATUS_OUT_WAIT:
URB_Status = USBH_LL_GetURBState(phost, phost->Control.pipe_out);
if (URB_Status == USBH_URB_DONE)
{
status = USBH_OK;
phost->Control.state = CTRL_COMPLETE;
#if (USBH_USE_OS == 1U)
phost->os_msg = (uint32_t)USBH_CONTROL_EVENT;
#if (osCMSIS < 0x20000U)
(void)osMessagePut(phost->os_event, phost->os_msg, 0U);
#else
(void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, 0U);
#endif/* ... */
#endif
}if (URB_Status == USBH_URB_DONE) { ... }
else if (URB_Status == USBH_URB_NOTREADY)
{
phost->Control.state = CTRL_STATUS_OUT;
#if (USBH_USE_OS == 1U)
phost->os_msg = (uint32_t)USBH_CONTROL_EVENT;
#if (osCMSIS < 0x20000U)
(void)osMessagePut(phost->os_event, phost->os_msg, 0U);
#else
(void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, 0U);
#endif/* ... */
#endif
}else if (URB_Status == USBH_URB_NOTREADY) { ... }
else
{
if (URB_Status == USBH_URB_ERROR)
{
phost->Control.state = CTRL_ERROR;
#if (USBH_USE_OS == 1U)
phost->os_msg = (uint32_t)USBH_CONTROL_EVENT;
#if (osCMSIS < 0x20000U)
(void)osMessagePut(phost->os_event, phost->os_msg, 0U);
#else
(void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, 0U);
#endif/* ... */
#endif
}if (URB_Status == USBH_URB_ERROR) { ... }
}else { ... }
break;
case CTRL_STATUS_OUT_WAIT:
case CTRL_ERROR:
/* ... */
if (++phost->Control.errorcount <= USBH_MAX_ERROR_COUNT)
{
phost->Control.state = CTRL_SETUP;
phost->RequestState = CMD_SEND;
}if (++phost->Control.errorcount <= USBH_MAX_ERROR_COUNT) { ... }
else
{
phost->pUser(phost, HOST_USER_UNRECOVERED_ERROR);
phost->Control.errorcount = 0U;
USBH_ErrLog("Control error: Device not responding");
(void)USBH_FreePipe(phost, phost->Control.pipe_out);
(void)USBH_FreePipe(phost, phost->Control.pipe_in);
phost->gState = HOST_IDLE;
status = USBH_FAIL;
}else { ... }
break;
case CTRL_ERROR:
default:
break;default
}switch (phost->Control.state) { ... }
return status;
}{ ... }
/* ... */
/* ... */
/* ... */
/* ... */
Includes