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
35
36
37
38
39
40
41
45
46
47
48
49
50
51
54
55
56
60
61
64
67
68
69
72
73
76
77
78
81
84
85
86
89
90
91
92
93
94
95
99
100
103
104
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
125
126
130
131
132
133
134
135
136
137
138
139
140
141
142
143
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
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
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
267
268
271
272
279
280
281
282
283
284
285
286
287
292
293
294
295
296
297
299
300
301
305
309
310
311
312
313
314
315
316
317
318
319
326
327
328
329
330
331
333
334
335
336
337
338
339
340
341
346
347
348
349
350
357
358
359
360
361
362
363
364
365
369
370
371
372
373
374
375
379
383
387
391
395
396
397
398
399
400
412
432
444
456
459
463
464
465
466
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
488
489
490
491
492
493
494
498
499
500
502
503
504
512
513
514
515
516
522
523
524
525
526
527
534
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
560
561
562
563
564
565
572
573
574
575
576
580
581
582
583
584
585
592
593
594
595
596
600
601
602
603
604
605
606
613
614
615
616
618
619
620
621
622
623
624
625
626
627
628
629
630
636
637
640
641
642
645
646
647
650
/* ... */
/* ... */
#include "usbd_hid.h"
#include "usbd_ctlreq.h"
/* ... */
/* ... */
/* ... */
/* ... */
/* ... */
/* ... */
/* ... */
/* ... */
/* ... */
static uint8_t USBD_HID_Init(USBD_HandleTypeDef *pdev, uint8_t cfgidx);
static uint8_t USBD_HID_DeInit(USBD_HandleTypeDef *pdev, uint8_t cfgidx);
static uint8_t USBD_HID_Setup(USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *req);
static uint8_t USBD_HID_DataIn(USBD_HandleTypeDef *pdev, uint8_t epnum);
#ifndef USE_USBD_COMPOSITE
static uint8_t *USBD_HID_GetFSCfgDesc(uint16_t *length);
static uint8_t *USBD_HID_GetHSCfgDesc(uint16_t *length);
static uint8_t *USBD_HID_GetOtherSpeedCfgDesc(uint16_t *length);
static uint8_t *USBD_HID_GetDeviceQualifierDesc(uint16_t *length);/* ... */
#endif
/* ... */
/* ... */
USBD_ClassTypeDef USBD_HID =
{
USBD_HID_Init,
USBD_HID_DeInit,
USBD_HID_Setup,
NULL,
NULL,
USBD_HID_DataIn,
NULL,
NULL,
NULL,
NULL,
#ifdef USE_USBD_COMPOSITE
NULL,
NULL,
NULL,
NULL,/* ... */
#else
USBD_HID_GetHSCfgDesc,
USBD_HID_GetFSCfgDesc,
USBD_HID_GetOtherSpeedCfgDesc,
USBD_HID_GetDeviceQualifierDesc,/* ... */
#endif
...};
#ifndef USE_USBD_COMPOSITE
__ALIGN_BEGIN static uint8_t USBD_HID_CfgDesc[USB_HID_CONFIG_DESC_SIZ] __ALIGN_END =
{
0x09,
USB_DESC_TYPE_CONFIGURATION,
USB_HID_CONFIG_DESC_SIZ,
0x00,
0x01,
0x01,
0x00,
/* ... */
#if (USBD_SELF_POWERED == 1U)
0xE0,
#else
0xA0,
#endif
USBD_MAX_POWER,
0x09,
USB_DESC_TYPE_INTERFACE,
0x00,
0x00,
0x01,
0x03,
0x01,
0x02,
0, Descriptor of Joystick Mouse interface
0x09,
HID_DESCRIPTOR_TYPE,
0x11,
0x01,
0x00,
0x01,
0x22,
HID_MOUSE_REPORT_DESC_SIZE,
0x00, Descriptor of Joystick Mouse HID
0x07,
USB_DESC_TYPE_ENDPOINT,
HID_EPIN_ADDR,
0x03,
HID_EPIN_SIZE,
0x00,
HID_FS_BINTERVAL,
...};/* ... */
#endif
__ALIGN_BEGIN static uint8_t USBD_HID_Desc[USB_HID_DESC_SIZ] __ALIGN_END =
{
0x09,
HID_DESCRIPTOR_TYPE,
0x11,
0x01,
0x00,
0x01,
0x22,
HID_MOUSE_REPORT_DESC_SIZE,
0x00,
...};
#ifndef USE_USBD_COMPOSITE
__ALIGN_BEGIN static uint8_t USBD_HID_DeviceQualifierDesc[USB_LEN_DEV_QUALIFIER_DESC] __ALIGN_END =
{
USB_LEN_DEV_QUALIFIER_DESC,
USB_DESC_TYPE_DEVICE_QUALIFIER,
0x00,
0x02,
0x00,
0x00,
0x00,
0x40,
0x01,
0x00,
...};/* ... */
#endif
__ALIGN_BEGIN static uint8_t HID_MOUSE_ReportDesc[HID_MOUSE_REPORT_DESC_SIZE] __ALIGN_END =
{
0x05, 0x01,
0x09, 0x02,
0xA1, 0x01,
0x09, 0x01,
0xA1, 0x00,
0x05, 0x09,
0x19, 0x01,
0x29, 0x03,
0x15, 0x00,
0x25, 0x01,
0x95, 0x03,
0x75, 0x01,
0x81, 0x02,
0x95, 0x01,
0x75, 0x05,
0x81, 0x01,
0x05, 0x01,
0x09, 0x30,
0x09, 0x31,
0x09, 0x38,
0x15, 0x81,
0x25, 0x7F,
0x75, 0x08,
0x95, 0x03,
0x81, 0x06,
0xC0,
0x09, 0x3C,
0x05, 0xFF,
0x09, 0x01,
0x15, 0x00,
0x25, 0x01,
0x75, 0x01,
0x95, 0x02,
0xB1, 0x22,
0x75, 0x06,
0x95, 0x01,
0xB1, 0x01,
0xC0
...};
static uint8_t HIDInEpAdd = HID_EPIN_ADDR;
/* ... */
/* ... */
/* ... */
static uint8_t USBD_HID_Init(USBD_HandleTypeDef *pdev, uint8_t cfgidx)
{
UNUSED(cfgidx);
USBD_HID_HandleTypeDef *hhid;
hhid = (USBD_HID_HandleTypeDef *)USBD_malloc(sizeof(USBD_HID_HandleTypeDef));
if (hhid == NULL)
{
pdev->pClassDataCmsit[pdev->classId] = NULL;
return (uint8_t)USBD_EMEM;
}if (hhid == NULL) { ... }
pdev->pClassDataCmsit[pdev->classId] = (void *)hhid;
pdev->pClassData = pdev->pClassDataCmsit[pdev->classId];
#ifdef USE_USBD_COMPOSITE
HIDInEpAdd = USBD_CoreGetEPAdd(pdev, USBD_EP_IN, USBD_EP_TYPE_INTR, (uint8_t)pdev->classId);/* ... */
#endif
if (pdev->dev_speed == USBD_SPEED_HIGH)
{
pdev->ep_in[HIDInEpAdd & 0xFU].bInterval = HID_HS_BINTERVAL;
}if (pdev->dev_speed == USBD_SPEED_HIGH) { ... }
else
{
pdev->ep_in[HIDInEpAdd & 0xFU].bInterval = HID_FS_BINTERVAL;
}else { ... }
(void)USBD_LL_OpenEP(pdev, HIDInEpAdd, USBD_EP_TYPE_INTR, HID_EPIN_SIZE);
pdev->ep_in[HIDInEpAdd & 0xFU].is_used = 1U;
hhid->state = USBD_HID_IDLE;
return (uint8_t)USBD_OK;
}{ ... }
/* ... */
static uint8_t USBD_HID_DeInit(USBD_HandleTypeDef *pdev, uint8_t cfgidx)
{
UNUSED(cfgidx);
#ifdef USE_USBD_COMPOSITE
HIDInEpAdd = USBD_CoreGetEPAdd(pdev, USBD_EP_IN, USBD_EP_TYPE_INTR, (uint8_t)pdev->classId);/* ... */
#endif
(void)USBD_LL_CloseEP(pdev, HIDInEpAdd);
pdev->ep_in[HIDInEpAdd & 0xFU].is_used = 0U;
pdev->ep_in[HIDInEpAdd & 0xFU].bInterval = 0U;
if (pdev->pClassDataCmsit[pdev->classId] != NULL)
{
(void)USBD_free(pdev->pClassDataCmsit[pdev->classId]);
pdev->pClassDataCmsit[pdev->classId] = NULL;
}if (pdev->pClassDataCmsit[pdev->classId] != NULL) { ... }
return (uint8_t)USBD_OK;
}{ ... }
/* ... */
static uint8_t USBD_HID_Setup(USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *req)
{
USBD_HID_HandleTypeDef *hhid = (USBD_HID_HandleTypeDef *)pdev->pClassDataCmsit[pdev->classId];
USBD_StatusTypeDef ret = USBD_OK;
uint16_t len;
uint8_t *pbuf;
uint16_t status_info = 0U;
if (hhid == NULL)
{
return (uint8_t)USBD_FAIL;
}if (hhid == NULL) { ... }
switch (req->bmRequest & USB_REQ_TYPE_MASK)
{
case USB_REQ_TYPE_CLASS :
switch (req->bRequest)
{
case USBD_HID_REQ_SET_PROTOCOL:
hhid->Protocol = (uint8_t)(req->wValue);
break;
case USBD_HID_REQ_SET_PROTOCOL:
case USBD_HID_REQ_GET_PROTOCOL:
(void)USBD_CtlSendData(pdev, (uint8_t *)&hhid->Protocol, 1U);
break;
case USBD_HID_REQ_GET_PROTOCOL:
case USBD_HID_REQ_SET_IDLE:
hhid->IdleState = (uint8_t)(req->wValue >> 8);
break;
case USBD_HID_REQ_SET_IDLE:
case USBD_HID_REQ_GET_IDLE:
(void)USBD_CtlSendData(pdev, (uint8_t *)&hhid->IdleState, 1U);
break;
case USBD_HID_REQ_GET_IDLE:
default:
USBD_CtlError(pdev, req);
ret = USBD_FAIL;
break;default
}switch (req->bRequest) { ... }
break;case USB_REQ_TYPE_CLASS :
case USB_REQ_TYPE_STANDARD:
switch (req->bRequest)
{
case USB_REQ_GET_STATUS:
if (pdev->dev_state == USBD_STATE_CONFIGURED)
{
(void)USBD_CtlSendData(pdev, (uint8_t *)&status_info, 2U);
}if (pdev->dev_state == USBD_STATE_CONFIGURED) { ... }
else
{
USBD_CtlError(pdev, req);
ret = USBD_FAIL;
}else { ... }
break;
case USB_REQ_GET_STATUS:
case USB_REQ_GET_DESCRIPTOR:
if ((req->wValue >> 8) == HID_REPORT_DESC)
{
len = MIN(HID_MOUSE_REPORT_DESC_SIZE, req->wLength);
pbuf = HID_MOUSE_ReportDesc;
}if ((req->wValue >> 8) == HID_REPORT_DESC) { ... }
else if ((req->wValue >> 8) == HID_DESCRIPTOR_TYPE)
{
pbuf = USBD_HID_Desc;
len = MIN(USB_HID_DESC_SIZ, req->wLength);
}else if ((req->wValue >> 8) == HID_DESCRIPTOR_TYPE) { ... }
else
{
USBD_CtlError(pdev, req);
ret = USBD_FAIL;
break;
}else { ... }
(void)USBD_CtlSendData(pdev, pbuf, len);
break;
case USB_REQ_GET_DESCRIPTOR:
case USB_REQ_GET_INTERFACE :
if (pdev->dev_state == USBD_STATE_CONFIGURED)
{
(void)USBD_CtlSendData(pdev, (uint8_t *)&hhid->AltSetting, 1U);
}if (pdev->dev_state == USBD_STATE_CONFIGURED) { ... }
else
{
USBD_CtlError(pdev, req);
ret = USBD_FAIL;
}else { ... }
break;
case USB_REQ_GET_INTERFACE :
case USB_REQ_SET_INTERFACE:
if (pdev->dev_state == USBD_STATE_CONFIGURED)
{
hhid->AltSetting = (uint8_t)(req->wValue);
}if (pdev->dev_state == USBD_STATE_CONFIGURED) { ... }
else
{
USBD_CtlError(pdev, req);
ret = USBD_FAIL;
}else { ... }
break;
case USB_REQ_SET_INTERFACE:
case USB_REQ_CLEAR_FEATURE:
break;
case USB_REQ_CLEAR_FEATURE:
default:
USBD_CtlError(pdev, req);
ret = USBD_FAIL;
break;default
}switch (req->bRequest) { ... }
break;
case USB_REQ_TYPE_STANDARD:
default:
USBD_CtlError(pdev, req);
ret = USBD_FAIL;
break;default
}switch (req->bmRequest & USB_REQ_TYPE_MASK) { ... }
return (uint8_t)ret;
}{ ... }
/* ... */
#ifdef USE_USBD_COMPOSITE
uint8_t USBD_HID_SendReport(USBD_HandleTypeDef *pdev, uint8_t *report, uint16_t len, uint8_t ClassId)
{
USBD_HID_HandleTypeDef *hhid = (USBD_HID_HandleTypeDef *)pdev->pClassDataCmsit[ClassId];/* ... */
#else
uint8_t USBD_HID_SendReport(USBD_HandleTypeDef *pdev, uint8_t *report, uint16_t len)
{
USBD_HID_HandleTypeDef *hhid = (USBD_HID_HandleTypeDef *)pdev->pClassDataCmsit[pdev->classId];
#endif
if (hhid == NULL)
{
return (uint8_t)USBD_FAIL;
}if (hhid == NULL) { ... }
#ifdef USE_USBD_COMPOSITE
HIDInEpAdd = USBD_CoreGetEPAdd(pdev, USBD_EP_IN, USBD_EP_TYPE_INTR, ClassId);/* ... */
#endif
if (pdev->dev_state == USBD_STATE_CONFIGURED)
{
if (hhid->state == USBD_HID_IDLE)
{
hhid->state = USBD_HID_BUSY;
(void)USBD_LL_Transmit(pdev, HIDInEpAdd, report, len);
}if (hhid->state == USBD_HID_IDLE) { ... }
}if (pdev->dev_state == USBD_STATE_CONFIGURED) { ... }
return (uint8_t)USBD_OK;
}{ ... }
/* ... */
uint32_t USBD_HID_GetPollingInterval(USBD_HandleTypeDef *pdev)
{
uint32_t polling_interval;
if (pdev->dev_speed == USBD_SPEED_HIGH)
{
/* ... */
polling_interval = (((1U << (HID_HS_BINTERVAL - 1U))) / 8U);
}if (pdev->dev_speed == USBD_SPEED_HIGH) { ... }
else
{
/* ... */
polling_interval = HID_FS_BINTERVAL;
}else { ... }
return ((uint32_t)(polling_interval));
}{ ... }
#ifndef USE_USBD_COMPOSITE
/* ... */
static uint8_t *USBD_HID_GetFSCfgDesc(uint16_t *length)
{
USBD_EpDescTypeDef *pEpDesc = USBD_GetEpDesc(USBD_HID_CfgDesc, HID_EPIN_ADDR);
if (pEpDesc != NULL)
{
pEpDesc->bInterval = HID_FS_BINTERVAL;
}if (pEpDesc != NULL) { ... }
*length = (uint16_t)sizeof(USBD_HID_CfgDesc);
return USBD_HID_CfgDesc;
}{ ... }
/* ... */
static uint8_t *USBD_HID_GetHSCfgDesc(uint16_t *length)
{
USBD_EpDescTypeDef *pEpDesc = USBD_GetEpDesc(USBD_HID_CfgDesc, HID_EPIN_ADDR);
if (pEpDesc != NULL)
{
pEpDesc->bInterval = HID_HS_BINTERVAL;
}if (pEpDesc != NULL) { ... }
*length = (uint16_t)sizeof(USBD_HID_CfgDesc);
return USBD_HID_CfgDesc;
}{ ... }
/* ... */
static uint8_t *USBD_HID_GetOtherSpeedCfgDesc(uint16_t *length)
{
USBD_EpDescTypeDef *pEpDesc = USBD_GetEpDesc(USBD_HID_CfgDesc, HID_EPIN_ADDR);
if (pEpDesc != NULL)
{
pEpDesc->bInterval = HID_FS_BINTERVAL;
}if (pEpDesc != NULL) { ... }
*length = (uint16_t)sizeof(USBD_HID_CfgDesc);
return USBD_HID_CfgDesc;
}{ ... }
#endif/* ... */
/* ... */
static uint8_t USBD_HID_DataIn(USBD_HandleTypeDef *pdev, uint8_t epnum)
{
UNUSED(epnum);
/* ... */
((USBD_HID_HandleTypeDef *)pdev->pClassDataCmsit[pdev->classId])->state = USBD_HID_IDLE;
return (uint8_t)USBD_OK;
}{ ... }
#ifndef USE_USBD_COMPOSITE
/* ... */
static uint8_t *USBD_HID_GetDeviceQualifierDesc(uint16_t *length)
{
*length = (uint16_t)sizeof(USBD_HID_DeviceQualifierDesc);
return USBD_HID_DeviceQualifierDesc;
}{ ... }
#endif/* ... */
/* ... */
/* ... */
/* ... */
Includes