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
28
29
30
31
32
33
34
40
41
42
43
44
45
46
49
50
53
54
57
58
62
63
66
69
70
71
74
77
78
79
82
85
86
87
90
91
94
95
96
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
124
125
126
129
130
131
137
138
139
140
141
142
143
144
145
146
147
148
149
153
154
155
156
160
161
162
163
164
169
170
171
172
173
174
175
176
181
186
191
192
193
194
195
196
197
198
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
248
249
250
251
252
258
259
265
266
271
272
273
274
275
282
283
284
285
286
287
288
289
290
291
292
293
300
301
302
303
304
309
314
318
319
320
321
322
323
324
325
326
330
337
338
339
340
341
342
343
344
345
346
347
348
349
350
355
359
360
361
362
364
365
366
367
368
369
375
376
377
378
379
380
381
382
383
384
385
386
390
396
397
398
404
405
406
407
408
409
410
414
419
424
429
430
431
437
438
439
440
441
442
446
447
448
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
485
486
487
488
501
502
503
505
506
507
508
509
510
516
517
518
519
520
521
522
523
524
525
526
527
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
558
559
560
561
562
563
564
565
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
698
699
700
701
702
703
704
705
709
713
714
715
716
717
718
719
720
721
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
765
766
767
768
769
770
785
786
787
788
789
795
796
797
798
799
800
801
802
803
804
805
806
807
811
812
813
814
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
841
842
843
844
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
912
913
914
915
916
917
918
923
928
931
932
935
936
939
940
941
944
945
946
949
/* ... */
/* ... */
#include "usbh_hid.h"
#include "usbh_hid_parser.h"
/* ... */
/* ... */
/* ... */
/* ... */
/* ... */
/* ... */
/* ... */
/* ... */
/* ... */
/* ... */
/* ... */
/* ... */
/* ... */
static USBH_StatusTypeDef USBH_HID_InterfaceInit(USBH_HandleTypeDef *phost);
static USBH_StatusTypeDef USBH_HID_InterfaceDeInit(USBH_HandleTypeDef *phost);
static USBH_StatusTypeDef USBH_HID_ClassRequest(USBH_HandleTypeDef *phost);
static USBH_StatusTypeDef USBH_HID_Process(USBH_HandleTypeDef *phost);
static USBH_StatusTypeDef USBH_HID_SOFProcess(USBH_HandleTypeDef *phost);
static void USBH_HID_ParseHIDDesc(HID_DescTypeDef *desc, uint8_t *buf);
extern USBH_StatusTypeDef USBH_HID_MouseInit(USBH_HandleTypeDef *phost);
extern USBH_StatusTypeDef USBH_HID_KeybdInit(USBH_HandleTypeDef *phost);
USBH_ClassTypeDef HID_Class =
{
"HID",
USB_HID_CLASS,
USBH_HID_InterfaceInit,
USBH_HID_InterfaceDeInit,
USBH_HID_ClassRequest,
USBH_HID_Process,
USBH_HID_SOFProcess,
NULL,
...};
/* ... */
/* ... */
/* ... */
static USBH_StatusTypeDef USBH_HID_InterfaceInit(USBH_HandleTypeDef *phost)
{
USBH_StatusTypeDef status;
HID_HandleTypeDef *HID_Handle;
uint16_t ep_mps;
uint8_t max_ep;
uint8_t num = 0U;
uint8_t interface;
interface = USBH_FindInterface(phost, phost->pActiveClass->ClassCode, HID_BOOT_CODE, 0xFFU);
if ((interface == 0xFFU) || (interface >= USBH_MAX_NUM_INTERFACES))
{
USBH_DbgLog("Cannot Find the interface for %s class.", phost->pActiveClass->Name);
return USBH_FAIL;
...}
status = USBH_SelectInterface(phost, interface);
if (status != USBH_OK)
{
return USBH_FAIL;
}if (status != USBH_OK) { ... }
phost->pActiveClass->pData = (HID_HandleTypeDef *)USBH_malloc(sizeof(HID_HandleTypeDef));
HID_Handle = (HID_HandleTypeDef *) phost->pActiveClass->pData;
if (HID_Handle == NULL)
{
USBH_DbgLog("Cannot allocate memory for HID Handle");
return USBH_FAIL;
}if (HID_Handle == NULL) { ... }
(void)USBH_memset(HID_Handle, 0, sizeof(HID_HandleTypeDef));
HID_Handle->state = USBH_HID_ERROR;
if (phost->device.CfgDesc.Itf_Desc[interface].bInterfaceProtocol == HID_KEYBRD_BOOT_CODE)
{
USBH_UsrLog("KeyBoard device found!");
HID_Handle->Init = USBH_HID_KeybdInit;
}if (phost->device.CfgDesc.Itf_Desc[interface].bInterfaceProtocol == HID_KEYBRD_BOOT_CODE) { ... }
else if (phost->device.CfgDesc.Itf_Desc[interface].bInterfaceProtocol == HID_MOUSE_BOOT_CODE)
{
USBH_UsrLog("Mouse device found!");
HID_Handle->Init = USBH_HID_MouseInit;
}else if (phost->device.CfgDesc.Itf_Desc[interface].bInterfaceProtocol == HID_MOUSE_BOOT_CODE) { ... }
else
{
USBH_UsrLog("Protocol not supported.");
return USBH_FAIL;
}else { ... }
HID_Handle->state = USBH_HID_INIT;
HID_Handle->ctl_state = USBH_HID_REQ_INIT;
HID_Handle->ep_addr = phost->device.CfgDesc.Itf_Desc[interface].Ep_Desc[0].bEndpointAddress;
HID_Handle->length = phost->device.CfgDesc.Itf_Desc[interface].Ep_Desc[0].wMaxPacketSize;
HID_Handle->poll = phost->device.CfgDesc.Itf_Desc[interface].Ep_Desc[0].bInterval;
if (HID_Handle->poll < HID_MIN_POLL)
{
HID_Handle->poll = HID_MIN_POLL;
}if (HID_Handle->poll < HID_MIN_POLL) { ... }
max_ep = ((phost->device.CfgDesc.Itf_Desc[interface].bNumEndpoints <= USBH_MAX_NUM_ENDPOINTS) ?
phost->device.CfgDesc.Itf_Desc[interface].bNumEndpoints : USBH_MAX_NUM_ENDPOINTS);
for (num = 0U; num < max_ep; num++)
{
if ((phost->device.CfgDesc.Itf_Desc[interface].Ep_Desc[num].bEndpointAddress & 0x80U) != 0U)
{
HID_Handle->InEp = (phost->device.CfgDesc.Itf_Desc[interface].Ep_Desc[num].bEndpointAddress);
HID_Handle->InPipe = USBH_AllocPipe(phost, HID_Handle->InEp);
ep_mps = phost->device.CfgDesc.Itf_Desc[interface].Ep_Desc[num].wMaxPacketSize;
(void)USBH_OpenPipe(phost, HID_Handle->InPipe, HID_Handle->InEp, phost->device.address,
phost->device.speed, USB_EP_TYPE_INTR, ep_mps);
(void)USBH_LL_SetToggle(phost, HID_Handle->InPipe, 0U);
}if ((phost->device.CfgDesc.Itf_Desc[interface].Ep_Desc[num].bEndpointAddress & 0x80U) != 0U) { ... }
else
{
HID_Handle->OutEp = (phost->device.CfgDesc.Itf_Desc[interface].Ep_Desc[num].bEndpointAddress);
HID_Handle->OutPipe = USBH_AllocPipe(phost, HID_Handle->OutEp);
ep_mps = phost->device.CfgDesc.Itf_Desc[interface].Ep_Desc[num].wMaxPacketSize;
(void)USBH_OpenPipe(phost, HID_Handle->OutPipe, HID_Handle->OutEp, phost->device.address,
phost->device.speed, USB_EP_TYPE_INTR, ep_mps);
(void)USBH_LL_SetToggle(phost, HID_Handle->OutPipe, 0U);
}else { ... }
}for (num = 0U; num < max_ep; num++) { ... }
return USBH_OK;
}{ ... }
/* ... */
static USBH_StatusTypeDef USBH_HID_InterfaceDeInit(USBH_HandleTypeDef *phost)
{
HID_HandleTypeDef *HID_Handle = (HID_HandleTypeDef *) phost->pActiveClass->pData;
if (HID_Handle->InPipe != 0x00U)
{
(void)USBH_ClosePipe(phost, HID_Handle->InPipe);
(void)USBH_FreePipe(phost, HID_Handle->InPipe);
HID_Handle->InPipe = 0U;
}if (HID_Handle->InPipe != 0x00U) { ... }
if (HID_Handle->OutPipe != 0x00U)
{
(void)USBH_ClosePipe(phost, HID_Handle->OutPipe);
(void)USBH_FreePipe(phost, HID_Handle->OutPipe);
HID_Handle->OutPipe = 0U;
}if (HID_Handle->OutPipe != 0x00U) { ... }
if ((phost->pActiveClass->pData) != NULL)
{
USBH_free(phost->pActiveClass->pData);
phost->pActiveClass->pData = 0U;
}if ((phost->pActiveClass->pData) != NULL) { ... }
return USBH_OK;
}{ ... }
/* ... */
static USBH_StatusTypeDef USBH_HID_ClassRequest(USBH_HandleTypeDef *phost)
{
USBH_StatusTypeDef status = USBH_BUSY;
USBH_StatusTypeDef classReqStatus = USBH_BUSY;
HID_HandleTypeDef *HID_Handle = (HID_HandleTypeDef *) phost->pActiveClass->pData;
switch (HID_Handle->ctl_state)
{
case USBH_HID_REQ_INIT:
case USBH_HID_REQ_GET_HID_DESC:
USBH_HID_ParseHIDDesc(&HID_Handle->HID_Desc, phost->device.CfgDesc_Raw);
HID_Handle->ctl_state = USBH_HID_REQ_GET_REPORT_DESC;
break;case USBH_HID_REQ_GET_HID_DESC:
case USBH_HID_REQ_GET_REPORT_DESC:
classReqStatus = USBH_HID_GetHIDReportDescriptor(phost, HID_Handle->HID_Desc.wItemLength);
if (classReqStatus == USBH_OK)
{
HID_Handle->ctl_state = USBH_HID_REQ_SET_IDLE;
}if (classReqStatus == USBH_OK) { ... }
else if (classReqStatus == USBH_NOT_SUPPORTED)
{
USBH_ErrLog("Control error: HID: Device Get Report Descriptor request failed");
status = USBH_FAIL;
}else if (classReqStatus == USBH_NOT_SUPPORTED) { ... }
else
{
}else { ... }
break;
case USBH_HID_REQ_GET_REPORT_DESC:
case USBH_HID_REQ_SET_IDLE:
classReqStatus = USBH_HID_SetIdle(phost, 0U, 0U);
if (classReqStatus == USBH_OK)
{
HID_Handle->ctl_state = USBH_HID_REQ_SET_PROTOCOL;
}if (classReqStatus == USBH_OK) { ... }
else
{
if (classReqStatus == USBH_NOT_SUPPORTED)
{
HID_Handle->ctl_state = USBH_HID_REQ_SET_PROTOCOL;
}if (classReqStatus == USBH_NOT_SUPPORTED) { ... }
}else { ... }
break;
case USBH_HID_REQ_SET_IDLE:
case USBH_HID_REQ_SET_PROTOCOL:
classReqStatus = USBH_HID_SetProtocol(phost, 0U);
if (classReqStatus == USBH_OK)
{
HID_Handle->ctl_state = USBH_HID_REQ_IDLE;
phost->pUser(phost, HOST_USER_CLASS_ACTIVE);
status = USBH_OK;
}if (classReqStatus == USBH_OK) { ... }
else if (classReqStatus == USBH_NOT_SUPPORTED)
{
USBH_ErrLog("Control error: HID: Device Set protocol request failed");
status = USBH_FAIL;
}else if (classReqStatus == USBH_NOT_SUPPORTED) { ... }
else
{
}else { ... }
break;
case USBH_HID_REQ_SET_PROTOCOL:
case USBH_HID_REQ_IDLE:
default:
break;default
}switch (HID_Handle->ctl_state) { ... }
return status;
}{ ... }
/* ... */
static USBH_StatusTypeDef USBH_HID_Process(USBH_HandleTypeDef *phost)
{
USBH_StatusTypeDef status = USBH_OK;
HID_HandleTypeDef *HID_Handle = (HID_HandleTypeDef *) phost->pActiveClass->pData;
uint32_t XferSize;
switch (HID_Handle->state)
{
case USBH_HID_INIT:
status = HID_Handle->Init(phost);
if (status == USBH_OK)
{
HID_Handle->state = USBH_HID_IDLE;
}if (status == USBH_OK) { ... }
else
{
USBH_ErrLog("HID Class Init failed");
HID_Handle->state = USBH_HID_ERROR;
status = USBH_FAIL;
}else { ... }
#if (USBH_USE_OS == 1U)
phost->os_msg = (uint32_t)USBH_URB_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 USBH_HID_INIT:
case USBH_HID_IDLE:
status = USBH_HID_GetReport(phost, 0x01U, 0U, HID_Handle->pData, (uint8_t)HID_Handle->length);
if (status == USBH_OK)
{
HID_Handle->state = USBH_HID_SYNC;
}if (status == USBH_OK) { ... }
else if (status == USBH_BUSY)
{
HID_Handle->state = USBH_HID_IDLE;
status = USBH_OK;
}else if (status == USBH_BUSY) { ... }
else if (status == USBH_NOT_SUPPORTED)
{
HID_Handle->state = USBH_HID_SYNC;
status = USBH_OK;
}else if (status == USBH_NOT_SUPPORTED) { ... }
else
{
HID_Handle->state = USBH_HID_ERROR;
status = USBH_FAIL;
}else { ... }
#if (USBH_USE_OS == 1U)
phost->os_msg = (uint32_t)USBH_URB_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 USBH_HID_IDLE:
case USBH_HID_SYNC:
if ((phost->Timer & 1U) != 0U)
{
HID_Handle->state = USBH_HID_GET_DATA;
}if ((phost->Timer & 1U) != 0U) { ... }
#if (USBH_USE_OS == 1U)
phost->os_msg = (uint32_t)USBH_URB_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 USBH_HID_SYNC:
case USBH_HID_GET_DATA:
(void)USBH_InterruptReceiveData(phost, HID_Handle->pData,
(uint8_t)HID_Handle->length,
HID_Handle->InPipe);
HID_Handle->state = USBH_HID_POLL;
HID_Handle->timer = phost->Timer;
HID_Handle->DataReady = 0U;
break;
case USBH_HID_GET_DATA:
case USBH_HID_POLL:
if (USBH_LL_GetURBState(phost, HID_Handle->InPipe) == USBH_URB_DONE)
{
XferSize = USBH_LL_GetLastXferSize(phost, HID_Handle->InPipe);
if ((HID_Handle->DataReady == 0U) && (XferSize != 0U) && (HID_Handle->fifo.buf != NULL))
{
(void)USBH_HID_FifoWrite(&HID_Handle->fifo, HID_Handle->pData, HID_Handle->length);
HID_Handle->DataReady = 1U;
USBH_HID_EventCallback(phost);
#if (USBH_USE_OS == 1U)
phost->os_msg = (uint32_t)USBH_URB_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 ((HID_Handle->DataReady == 0U) && (XferSize != 0U) && (HID_Handle->fifo.buf != NULL)) { ... }
}if (USBH_LL_GetURBState(phost, HID_Handle->InPipe) == USBH_URB_DONE) { ... }
else
{
if (USBH_LL_GetURBState(phost, HID_Handle->InPipe) == USBH_URB_STALL)
{
if (USBH_ClrFeature(phost, HID_Handle->ep_addr) == USBH_OK)
{
HID_Handle->state = USBH_HID_GET_DATA;
}if (USBH_ClrFeature(phost, HID_Handle->ep_addr) == USBH_OK) { ... }
}if (USBH_LL_GetURBState(phost, HID_Handle->InPipe) == USBH_URB_STALL) { ... }
}else { ... }
break;
case USBH_HID_POLL:
default:
break;default
}switch (HID_Handle->state) { ... }
return status;
}{ ... }
/* ... */
static USBH_StatusTypeDef USBH_HID_SOFProcess(USBH_HandleTypeDef *phost)
{
HID_HandleTypeDef *HID_Handle = (HID_HandleTypeDef *) phost->pActiveClass->pData;
if (HID_Handle->state == USBH_HID_POLL)
{
if ((phost->Timer - HID_Handle->timer) >= HID_Handle->poll)
{
HID_Handle->state = USBH_HID_GET_DATA;
#if (USBH_USE_OS == 1U)
phost->os_msg = (uint32_t)USBH_URB_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 ((phost->Timer - HID_Handle->timer) >= HID_Handle->poll) { ... }
}if (HID_Handle->state == USBH_HID_POLL) { ... }
return USBH_OK;
}{ ... }
/* ... */
USBH_StatusTypeDef USBH_HID_GetHIDReportDescriptor(USBH_HandleTypeDef *phost,
uint16_t length)
{
USBH_StatusTypeDef status;
if (length > sizeof(phost->device.Data))
{
USBH_ErrLog("Control error: Get HID Report Descriptor failed, data buffer size issue");
return USBH_NOT_SUPPORTED;
}if (length > sizeof(phost->device.Data)) { ... }
status = USBH_GetDescriptor(phost,
USB_REQ_RECIPIENT_INTERFACE | USB_REQ_TYPE_STANDARD,
USB_DESC_HID_REPORT,
phost->device.Data,
length);
/* ... */
return status;
}{ ... }
/* ... */
USBH_StatusTypeDef USBH_HID_GetHIDDescriptor(USBH_HandleTypeDef *phost,
uint16_t length)
{
USBH_StatusTypeDef status;
if (length > sizeof(phost->device.Data))
{
USBH_ErrLog("Control error: Get HID Descriptor failed, data buffer size issue");
return USBH_NOT_SUPPORTED;
}if (length > sizeof(phost->device.Data)) { ... }
status = USBH_GetDescriptor(phost,
USB_REQ_RECIPIENT_INTERFACE | USB_REQ_TYPE_STANDARD,
USB_DESC_HID,
phost->device.Data,
length);
return status;
}{ ... }
/* ... */
USBH_StatusTypeDef USBH_HID_SetIdle(USBH_HandleTypeDef *phost,
uint8_t duration,
uint8_t reportId)
{
phost->Control.setup.b.bmRequestType = USB_H2D | USB_REQ_RECIPIENT_INTERFACE | \
USB_REQ_TYPE_CLASS;
phost->Control.setup.b.bRequest = USB_HID_SET_IDLE;
phost->Control.setup.b.wValue.w = (uint16_t)(((uint32_t)duration << 8U) | (uint32_t)reportId);
phost->Control.setup.b.wIndex.w = 0U;
phost->Control.setup.b.wLength.w = 0U;
return USBH_CtlReq(phost, NULL, 0U);
}{ ... }
/* ... */
USBH_StatusTypeDef USBH_HID_SetReport(USBH_HandleTypeDef *phost,
uint8_t reportType,
uint8_t reportId,
uint8_t *reportBuff,
uint8_t reportLen)
{
phost->Control.setup.b.bmRequestType = USB_H2D | USB_REQ_RECIPIENT_INTERFACE | \
USB_REQ_TYPE_CLASS;
phost->Control.setup.b.bRequest = USB_HID_SET_REPORT;
phost->Control.setup.b.wValue.w = (uint16_t)(((uint32_t)reportType << 8U) | (uint32_t)reportId);
phost->Control.setup.b.wIndex.w = 0U;
phost->Control.setup.b.wLength.w = reportLen;
return USBH_CtlReq(phost, reportBuff, (uint16_t)reportLen);
}{ ... }
/* ... */
USBH_StatusTypeDef USBH_HID_GetReport(USBH_HandleTypeDef *phost,
uint8_t reportType,
uint8_t reportId,
uint8_t *reportBuff,
uint8_t reportLen)
{
phost->Control.setup.b.bmRequestType = USB_D2H | USB_REQ_RECIPIENT_INTERFACE | \
USB_REQ_TYPE_CLASS;
phost->Control.setup.b.bRequest = USB_HID_GET_REPORT;
phost->Control.setup.b.wValue.w = (uint16_t)(((uint32_t)reportType << 8U) | (uint32_t)reportId);
phost->Control.setup.b.wIndex.w = 0U;
phost->Control.setup.b.wLength.w = reportLen;
return USBH_CtlReq(phost, reportBuff, (uint16_t)reportLen);
}{ ... }
/* ... */
USBH_StatusTypeDef USBH_HID_SetProtocol(USBH_HandleTypeDef *phost,
uint8_t protocol)
{
phost->Control.setup.b.bmRequestType = USB_H2D | USB_REQ_RECIPIENT_INTERFACE
| USB_REQ_TYPE_CLASS;
phost->Control.setup.b.bRequest = USB_HID_SET_PROTOCOL;
if (protocol != 0U)
{
phost->Control.setup.b.wValue.w = 0U;
}if (protocol != 0U) { ... }
else
{
phost->Control.setup.b.wValue.w = 1U;
}else { ... }
phost->Control.setup.b.wIndex.w = 0U;
phost->Control.setup.b.wLength.w = 0U;
return USBH_CtlReq(phost, NULL, 0U);
}{ ... }
/* ... */
static void USBH_HID_ParseHIDDesc(HID_DescTypeDef *desc, uint8_t *buf)
{
USBH_DescHeader_t *pdesc = (USBH_DescHeader_t *)buf;
uint16_t CfgDescLen;
uint16_t ptr;
CfgDescLen = LE16(buf + 2U);
if (CfgDescLen > USB_CONFIGURATION_DESC_SIZE)
{
ptr = USB_LEN_CFG_DESC;
while (ptr < CfgDescLen)
{
pdesc = USBH_GetNextDesc((uint8_t *)pdesc, &ptr);
if (pdesc->bDescriptorType == USB_DESC_TYPE_HID)
{
desc->bLength = *(uint8_t *)((uint8_t *)pdesc + 0U);
desc->bDescriptorType = *(uint8_t *)((uint8_t *)pdesc + 1U);
desc->bcdHID = LE16((uint8_t *)pdesc + 2U);
desc->bCountryCode = *(uint8_t *)((uint8_t *)pdesc + 4U);
desc->bNumDescriptors = *(uint8_t *)((uint8_t *)pdesc + 5U);
desc->bReportDescriptorType = *(uint8_t *)((uint8_t *)pdesc + 6U);
desc->wItemLength = LE16((uint8_t *)pdesc + 7U);
break;
}if (pdesc->bDescriptorType == USB_DESC_TYPE_HID) { ... }
}while (ptr < CfgDescLen) { ... }
}if (CfgDescLen > USB_CONFIGURATION_DESC_SIZE) { ... }
}{ ... }
/* ... */
HID_TypeTypeDef USBH_HID_GetDeviceType(USBH_HandleTypeDef *phost)
{
HID_TypeTypeDef type = HID_UNKNOWN;
uint8_t InterfaceProtocol;
if (phost->gState == HOST_CLASS)
{
InterfaceProtocol = phost->device.CfgDesc.Itf_Desc[phost->device.current_interface].bInterfaceProtocol;
if (InterfaceProtocol == HID_KEYBRD_BOOT_CODE)
{
type = HID_KEYBOARD;
}if (InterfaceProtocol == HID_KEYBRD_BOOT_CODE) { ... }
else
{
if (InterfaceProtocol == HID_MOUSE_BOOT_CODE)
{
type = HID_MOUSE;
}if (InterfaceProtocol == HID_MOUSE_BOOT_CODE) { ... }
}else { ... }
}if (phost->gState == HOST_CLASS) { ... }
return type;
}{ ... }
/* ... */
uint8_t USBH_HID_GetPollInterval(USBH_HandleTypeDef *phost)
{
HID_HandleTypeDef *HID_Handle = (HID_HandleTypeDef *) phost->pActiveClass->pData;
if ((phost->gState == HOST_CLASS_REQUEST) ||
(phost->gState == HOST_INPUT) ||
(phost->gState == HOST_SET_CONFIGURATION) ||
(phost->gState == HOST_CHECK_CLASS) ||
((phost->gState == HOST_CLASS)))
{
return (uint8_t)(HID_Handle->poll);
}if ((phost->gState == HOST_CLASS_REQUEST) || (phost->gState == HOST_INPUT) || (phost->gState == HOST_SET_CONFIGURATION) || (phost->gState == HOST_CHECK_CLASS) || ((phost->gState == HOST_CLASS))) { ... }
else
{
return 0U;
}else { ... }
}{ ... }
/* ... */
void USBH_HID_FifoInit(FIFO_TypeDef *f, uint8_t *buf, uint16_t size)
{
f->head = 0U;
f->tail = 0U;
f->lock = 0U;
f->size = size;
f->buf = buf;
}{ ... }
/* ... */
uint16_t USBH_HID_FifoRead(FIFO_TypeDef *f, void *buf, uint16_t nbytes)
{
uint16_t i;
uint8_t *p;
p = (uint8_t *) buf;
if (f->lock == 0U)
{
f->lock = 1U;
for (i = 0U; i < nbytes; i++)
{
if (f->tail != f->head)
{
*p++ = f->buf[f->tail];
f->tail++;
if (f->tail == f->size)
{
f->tail = 0U;
}if (f->tail == f->size) { ... }
}if (f->tail != f->head) { ... }
else
{
f->lock = 0U;
return i;
}else { ... }
}for (i = 0U; i < nbytes; i++) { ... }
}if (f->lock == 0U) { ... }
f->lock = 0U;
return nbytes;
}{ ... }
/* ... */
uint16_t USBH_HID_FifoWrite(FIFO_TypeDef *f, void *buf, uint16_t nbytes)
{
uint16_t i;
uint8_t *p;
p = (uint8_t *) buf;
if (f->lock == 0U)
{
f->lock = 1U;
for (i = 0U; i < nbytes; i++)
{
if (((f->head + 1U) == f->tail) ||
(((f->head + 1U) == f->size) && (f->tail == 0U)))
{
f->lock = 0U;
return i;
}if (((f->head + 1U) == f->tail) || (((f->head + 1U) == f->size) && (f->tail == 0U))) { ... }
else
{
f->buf[f->head] = *p++;
f->head++;
if (f->head == f->size)
{
f->head = 0U;
}if (f->head == f->size) { ... }
}else { ... }
}for (i = 0U; i < nbytes; i++) { ... }
}if (f->lock == 0U) { ... }
f->lock = 0U;
return nbytes;
}{ ... }
/* ... */
__weak void USBH_HID_EventCallback(USBH_HandleTypeDef *phost)
{
UNUSED(phost);
}{ ... }
/* ... */
/* ... */
/* ... */
/* ... */
/* ... */
Includes