-
-
Notifications
You must be signed in to change notification settings - Fork 754
/
Copy pathusbd_cdc_hid.c
864 lines (741 loc) · 28.4 KB
/
usbd_cdc_hid.c
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
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
441
442
443
444
445
446
447
448
449
450
451
452
453
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
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
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
579
580
581
582
583
584
585
586
587
588
589
590
591
592
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
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
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
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
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
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
#include "usbd_cdc_hid.h"
#include "usbd_desc.h"
#include "usbd_ctlreq.h"
#include "jshardware.h"
#include "jsinteractive.h"
#ifdef STM32F1
// don't know why :( probably setup in usdb_conf
#define CDC_IN_EP 0x81 /* EP1 for data IN */
#define CDC_OUT_EP 0x01 /* EP1 for data OUT */
#define CDC_CMD_EP 0x82 /* EP2 for CDC commands */
#else
#define CDC_IN_EP 0x83 /* EP1 for data IN */
#define CDC_OUT_EP 0x03 /* EP1 for data OUT */
#define CDC_CMD_EP 0x82 /* EP2 for CDC commands */
#define HID_IN_EP 0x81
#define HID_INTERFACE_NUMBER 2
#endif
extern USBD_HandleTypeDef hUsbDeviceFS;
//-------------------------------------------------
static int8_t CDC_Control_FS (uint8_t cmd, uint8_t* pbuf, uint16_t length);
//-------------------------------------------------
static uint8_t USBD_CDC_HID_Init (USBD_HandleTypeDef *pdev, uint8_t cfgidx);
static uint8_t USBD_CDC_HID_DeInit (USBD_HandleTypeDef *pdev, uint8_t cfgidx);
static uint8_t USBD_CDC_HID_Setup (USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *req);
static uint8_t USBD_CDC_HID_DataIn (USBD_HandleTypeDef *pdev, uint8_t epnum);
static uint8_t USBD_CDC_HID_DataOut (USBD_HandleTypeDef *pdev, uint8_t epnum);
static uint8_t USBD_CDC_HID_SOF (struct _USBD_HandleTypeDef *pdev);
static uint8_t USBD_CDC_HID_EP0_RxReady (USBD_HandleTypeDef *pdev);
static uint8_t *USBD_CDC_HID_GetCfgDesc (uint16_t *length);
uint8_t *USBD_CDC_HID_GetDeviceQualifierDescriptor (uint16_t *length);
/* CDC interface class callbacks structure */
const USBD_ClassTypeDef USBD_CDC_HID =
{
USBD_CDC_HID_Init,
USBD_CDC_HID_DeInit,
USBD_CDC_HID_Setup,
NULL, /* EP0_TxSent, */
USBD_CDC_HID_EP0_RxReady,
USBD_CDC_HID_DataIn,
USBD_CDC_HID_DataOut,
USBD_CDC_HID_SOF,
NULL,
NULL,
USBD_CDC_HID_GetCfgDesc,
USBD_CDC_HID_GetCfgDesc,
USBD_CDC_HID_GetCfgDesc,
USBD_CDC_HID_GetDeviceQualifierDescriptor,
};
/* USB Standard Device Descriptor */
__ALIGN_BEGIN static const uint8_t USBD_CDC_HID_DeviceQualifierDesc[USB_LEN_DEV_QUALIFIER_DESC] __ALIGN_END =
{
USB_LEN_DEV_QUALIFIER_DESC, // bLength
USB_DESC_TYPE_DEVICE_QUALIFIER,// bDescriptorType
0x00, // bcdUSB lo
0x02, // bcdUSB hi
0x00, // bDeviceClass
0x00, // bDeviceSubClass
0x00, // bDeviceProtocol
0x40, // bMaxPacketSize0
0x01, // bNumConfigurations
0x00, // bReserved
};
/* USB CDC device Configuration Descriptor
* ============================================================================
*
* No HID
* CDC on Interfaces 0 and 1
*/
#define USBD_CDC_CFGDESC_SIZE (67+8)
const __ALIGN_BEGIN uint8_t USBD_CDC_CfgDesc[USBD_CDC_CFGDESC_SIZE] __ALIGN_END =
{
/*Configuration Descriptor*/
0x09, /* bLength: Configuration Descriptor size */
USB_DESC_TYPE_CONFIGURATION, /* bDescriptorType: Configuration */
USBD_CDC_CFGDESC_SIZE, 0x00, /* wTotalLength:no of returned bytes */
0x02, /* bNumInterfaces: 2 interface */
0x01, /* bConfigurationValue: Configuration value */
0x00, /* iConfiguration: Index of string descriptor describing the configuration */
0xC0, /* bmAttributes: self powered */
0x32, /* MaxPower 0 mA */
/* Interface Association Descriptor */
0x08, /* bLength: IAD descriptor size */
0x0B, /* bDescriptorType: IAD */
0x00, /* bFirstInterface: Number if first interface for grouping */
0x02, /* bInterfaceCount: Interfaces count for grouping */
0x02, /* bFunctionClass: The same as bInterfaceClass below */
0x02, /* bFunctionSubClass: The same as bInterfaceSubClass below */
0x01, /* bFunctionProtocol: The same as bInterfaceProtocol below */
0x00, /* iFunction */
// -----------------------------------------------------------------------
/*CDC Interface Descriptor */
0x09, /* bLength: Interface Descriptor size */
USB_DESC_TYPE_INTERFACE, /* bDescriptorType: Interface */
/* Interface descriptor type */
0, /* bInterfaceNumber: Number of Interface */
0x00, /* bAlternateSetting: Alternate setting */
0x01, /* bNumEndpoints: One endpoints used */
0x02, /* bInterfaceClass: Communication Interface Class */
0x02, /* bInterfaceSubClass: Abstract Control Model */
0x01, /* bInterfaceProtocol: Common AT commands */
0x00, /* iInterface: */
/*Header Functional Descriptor*/
0x05, /* bLength: Endpoint Descriptor size */
0x24, /* bDescriptorType: CS_INTERFACE */
0x00, /* bDescriptorSubtype: Header Func Desc */
0x10, /* bcdCDC: spec release number */
0x01,
/*Call Management Functional Descriptor*/
0x05, /* bFunctionLength */
0x24, /* bDescriptorType: CS_INTERFACE */
0x01, /* bDescriptorSubtype: Call Management Func Desc */
0x00, /* bmCapabilities: D0+D1 */
1, /* bDataInterface */
/*ACM Functional Descriptor*/
0x04, /* bFunctionLength */
0x24, /* bDescriptorType: CS_INTERFACE */
0x02, /* bDescriptorSubtype: Abstract Control Management desc */
0x02, /* bmCapabilities */
/*Union Functional Descriptor*/
0x05, /* bFunctionLength */
0x24, /* bDescriptorType: CS_INTERFACE */
0x06, /* bDescriptorSubtype: Union func desc */
0, /* bMasterInterface: Communication class interface */
1, /* bSlaveInterface0: Data Class Interface */
/*Endpoint 2 Descriptor*/
0x07, /* bLength: Endpoint Descriptor size */
USB_DESC_TYPE_ENDPOINT, /* bDescriptorType: Endpoint */
CDC_CMD_EP, /* bEndpointAddress */
0x03, /* bmAttributes: Interrupt */
LOBYTE(CDC_CMD_PACKET_SIZE), /* wMaxPacketSize: */
HIBYTE(CDC_CMD_PACKET_SIZE),
0x10, /* bInterval: */
/*---------------------------------------------------------------------------*/
/*Data class interface descriptor*/
0x09, /* bLength: Endpoint Descriptor size */
USB_DESC_TYPE_INTERFACE, /* bDescriptorType: */
1, /* bInterfaceNumber: Number of Interface */
0x00, /* bAlternateSetting: Alternate setting */
0x02, /* bNumEndpoints: Two endpoints used */
0x0A, /* bInterfaceClass: CDC */
0x00, /* bInterfaceSubClass: */
0x00, /* bInterfaceProtocol: */
0x00, /* iInterface: */
/*Endpoint OUT Descriptor*/
0x07, /* bLength: Endpoint Descriptor size */
USB_DESC_TYPE_ENDPOINT, /* bDescriptorType: Endpoint */
CDC_OUT_EP, /* bEndpointAddress */
0x02, /* bmAttributes: Bulk */
LOBYTE(CDC_DATA_FS_OUT_PACKET_SIZE), /* wMaxPacketSize: */
HIBYTE(CDC_DATA_FS_OUT_PACKET_SIZE),
0x00, /* bInterval: ignore for Bulk transfer */
/*Endpoint IN Descriptor*/
0x07, /* bLength: Endpoint Descriptor size */
USB_DESC_TYPE_ENDPOINT, /* bDescriptorType: Endpoint */
CDC_IN_EP, /* bEndpointAddress */
0x02, /* bmAttributes: Bulk */
LOBYTE(CDC_DATA_FS_IN_PACKET_SIZE), /* wMaxPacketSize: */
HIBYTE(CDC_DATA_FS_IN_PACKET_SIZE),
0x00, /* bInterval: ignore for Bulk transfer */
} ;
#ifdef USE_USB_HID
/* USB HID + CDC device Configuration Descriptor
* ============================================================================
*
* HID on Interface 0
* CDC on Interfaces 1 and 2
*/
#define USBD_CDC_HID_CFGDESC_SIZE (67+25+8)
#define USBD_CDC_HID_CFGDESC_REPORT_SIZE_IDX 91
// NOT CONST - descriptor size needs updating as this is sent out
__ALIGN_BEGIN uint8_t USBD_CDC_HID_CfgDesc[USBD_CDC_HID_CFGDESC_SIZE] __ALIGN_END =
{
/*Configuration Descriptor*/
0x09, /* bLength: Configuration Descriptor size */
USB_DESC_TYPE_CONFIGURATION, /* bDescriptorType: Configuration */
USBD_CDC_HID_CFGDESC_SIZE, 0x00, /* wTotalLength:no of returned bytes */
0x03, /* bNumInterfaces: 3 interface */
0x01, /* bConfigurationValue: Configuration value */
0x00, /* iConfiguration: Index of string descriptor describing the configuration */
0xC0, /* bmAttributes: self powered */
0x32, /* MaxPower 0 mA */
/* Interface Association Descriptor */
0x08, /* bLength: IAD descriptor size */
0x0B, /* bDescriptorType: IAD */
0x00, /* bFirstInterface: Number if first interface for grouping */
0x02, /* bInterfaceCount: Interfaces count for grouping */
0x02, /* bFunctionClass: The same as bInterfaceClass below */
0x02, /* bFunctionSubClass: The same as bInterfaceSubClass below */
0x01, /* bFunctionProtocol: The same as bInterfaceProtocol below */
0x00, /* iFunction */
// -----------------------------------------------------------------------
/*CDC Interface Descriptor */
0x09, /* bLength: Interface Descriptor size */
USB_DESC_TYPE_INTERFACE, /* bDescriptorType: Interface */
/* Interface descriptor type */
0, /* bInterfaceNumber: Number of Interface */
0x00, /* bAlternateSetting: Alternate setting */
0x01, /* bNumEndpoints: One endpoints used */
0x02, /* bInterfaceClass: Communication Interface Class */
0x02, /* bInterfaceSubClass: Abstract Control Model */
0x01, /* bInterfaceProtocol: Common AT commands */
0x00, /* iInterface: */
/*Header Functional Descriptor*/
0x05, /* bLength: Endpoint Descriptor size */
0x24, /* bDescriptorType: CS_INTERFACE */
0x00, /* bDescriptorSubtype: Header Func Desc */
0x10, /* bcdCDC: spec release number */
0x01,
/*Call Management Functional Descriptor*/
0x05, /* bFunctionLength */
0x24, /* bDescriptorType: CS_INTERFACE */
0x01, /* bDescriptorSubtype: Call Management Func Desc */
0x00, /* bmCapabilities: D0+D1 */
1, /* bDataInterface */
/*ACM Functional Descriptor*/
0x04, /* bFunctionLength */
0x24, /* bDescriptorType: CS_INTERFACE */
0x02, /* bDescriptorSubtype: Abstract Control Management desc */
0x02, /* bmCapabilities */
/*Union Functional Descriptor*/
0x05, /* bFunctionLength */
0x24, /* bDescriptorType: CS_INTERFACE */
0x06, /* bDescriptorSubtype: Union func desc */
0, /* bMasterInterface: Communication class interface */
1, /* bSlaveInterface0: Data Class Interface */
/*Endpoint 2 Descriptor*/
0x07, /* bLength: Endpoint Descriptor size */
USB_DESC_TYPE_ENDPOINT, /* bDescriptorType: Endpoint */
CDC_CMD_EP, /* bEndpointAddress */
0x03, /* bmAttributes: Interrupt */
LOBYTE(CDC_CMD_PACKET_SIZE), /* wMaxPacketSize: */
HIBYTE(CDC_CMD_PACKET_SIZE),
0x10, /* bInterval: */
/*---------------------------------------------------------------------------*/
/*Data class interface descriptor*/
0x09, /* bLength: Endpoint Descriptor size */
USB_DESC_TYPE_INTERFACE, /* bDescriptorType: */
1, /* bInterfaceNumber: Number of Interface */
0x00, /* bAlternateSetting: Alternate setting */
0x02, /* bNumEndpoints: Two endpoints used */
0x0A, /* bInterfaceClass: CDC */
0x00, /* bInterfaceSubClass: */
0x00, /* bInterfaceProtocol: */
0x00, /* iInterface: */
/*Endpoint OUT Descriptor*/
0x07, /* bLength: Endpoint Descriptor size */
USB_DESC_TYPE_ENDPOINT, /* bDescriptorType: Endpoint */
CDC_OUT_EP, /* bEndpointAddress */
0x02, /* bmAttributes: Bulk */
LOBYTE(CDC_DATA_FS_OUT_PACKET_SIZE), /* wMaxPacketSize: */
HIBYTE(CDC_DATA_FS_OUT_PACKET_SIZE),
0x00, /* bInterval: ignore for Bulk transfer */
/*Endpoint IN Descriptor*/
0x07, /* bLength: Endpoint Descriptor size */
USB_DESC_TYPE_ENDPOINT, /* bDescriptorType: Endpoint */
CDC_IN_EP, /* bEndpointAddress */
0x02, /* bmAttributes: Bulk */
LOBYTE(CDC_DATA_FS_IN_PACKET_SIZE), /* wMaxPacketSize: */
HIBYTE(CDC_DATA_FS_IN_PACKET_SIZE),
0x00, /* bInterval: ignore for Bulk transfer */
/************** Descriptor of Joystick Mouse interface ****************/
/* 9 */
0x09, /*bLength: Interface Descriptor size*/
USB_DESC_TYPE_INTERFACE,/*bDescriptorType: Interface descriptor type*/
HID_INTERFACE_NUMBER, /*bInterfaceNumber: Number of Interface*/
0x00, /*bAlternateSetting: Alternate setting*/
0x01, /*bNumEndpoints*/
0x03, /*bInterfaceClass: HID*/
0x01, /*bInterfaceSubClass : 1=BOOT, 0=no boot*/
0, /*nInterfaceProtocol : 0=none, 1=keyboard, 2=mouse*/
0, /*iInterface: Index of string descriptor*/
/******************** Descriptor of Joystick Mouse HID ********************/
/* 18 */
0x09, /*bLength: HID Descriptor size*/
HID_DESCRIPTOR_TYPE, /*bDescriptorType: HID*/
0x11, /*bcdHID: HID Class Spec release number*/
0x01,
0x00, /*bCountryCode: Hardware target country*/
0x01, /*bNumDescriptors: Number of HID class descriptors to follow*/
0x22, /*bDescriptorType*/
0/*HID_REPORT_DESC_SIZE*/, 0, /*wItemLength: Total length of Report descriptor*/
/******************** Descriptor of Mouse endpoint ********************/
/* 27 */
0x07, /*bLength: Endpoint Descriptor size*/
USB_DESC_TYPE_ENDPOINT, /*bDescriptorType:*/
HID_IN_EP, /*bEndpointAddress: Endpoint Address (IN)*/
0x03, /*bmAttributes: Interrupt endpoint*/
HID_DATA_IN_PACKET_SIZE,0x00, /*wMaxPacketSize: 4 Byte max */
HID_FS_BINTERVAL, /*bInterval: Polling Interval (10 ms)*/
} ;
#define USB_HID_DESC_SIZ 9
#define USBD_HID_DESC_REPORT_SIZE_IDX 7
/* USB HID device Configuration Descriptor */
__ALIGN_BEGIN static uint8_t USBD_HID_Desc[USB_HID_DESC_SIZ] __ALIGN_END =
{
0x09, /*bLength: HID Descriptor size*/
HID_DESCRIPTOR_TYPE, /*bDescriptorType: HID*/
0x11, /*bcdHID: HID Class Spec release number*/
0x01,
0x00, /*bCountryCode: Hardware target country*/
0x01, /*bNumDescriptors: Number of HID class descriptors to follow*/
0x22, /*bDescriptorType*/
0/*HID_REPORT_DESC_SIZE*/, 0x00, /*wItemLength: Total length of Report descriptor*/
};
#endif
// -------------------------------------------------------------------------------------------------
// -------------------------------------------------------------------------------------------------
// -------------------------------------------------------------------------------------------------
// -------------------------------------------------------------------------------------------------
USBD_CDC_HID_HandleTypeDef cdc_hid;
/**
* @brief USBD_CDC_HID_Init
* Initialize the CDC interface
* @param pdev: device instance
* @param cfgidx: Configuration index
* @retval status
*/
static uint8_t USBD_CDC_HID_Init (USBD_HandleTypeDef *pdev,
uint8_t cfgidx)
{
pdev->pClassData = &cdc_hid;
USBD_CDC_HID_HandleTypeDef *handle = (USBD_CDC_HID_HandleTypeDef*) pdev->pClassData;
NOT_USED(cfgidx);
uint8_t ret = 0;
/* Open EP IN */
USBD_LL_OpenEP(pdev,
CDC_IN_EP,
USBD_EP_TYPE_BULK,
CDC_DATA_FS_IN_PACKET_SIZE);
/* Open EP OUT */
USBD_LL_OpenEP(pdev,
CDC_OUT_EP,
USBD_EP_TYPE_BULK,
CDC_DATA_FS_OUT_PACKET_SIZE);
/* Open Command IN EP */
USBD_LL_OpenEP(pdev,
CDC_CMD_EP,
USBD_EP_TYPE_INTR,
CDC_CMD_PACKET_SIZE);
/* Init Xfer states */
handle->cdcState = CDC_IDLE;
/* Prepare Out endpoint to receive next packet */
USBD_LL_PrepareReceive(pdev,
CDC_OUT_EP,
handle->cdcRX,
CDC_DATA_FS_OUT_PACKET_SIZE);
#ifdef USE_USB_HID
unsigned int reportSize = 0;
handle->hidReportDesc = USB_GetHIDReportDesc(&reportSize);
handle->hidReportDescSize = (uint16_t)reportSize;
/* Open HID EP IN - even if we're not using it */
USBD_LL_OpenEP(pdev,
HID_IN_EP,
USBD_EP_TYPE_INTR,
HID_DATA_IN_PACKET_SIZE);
handle->hidState = HID_IDLE;
#endif
return ret;
}
/**
* @brief USBD_CDC_HID_Init
* DeInitialize the CDC layer
* @param pdev: device instance
* @param cfgidx: Configuration index
* @retval status
*/
static uint8_t USBD_CDC_HID_DeInit (USBD_HandleTypeDef *pdev,
uint8_t cfgidx)
{
USBD_CDC_HID_HandleTypeDef *handle = (USBD_CDC_HID_HandleTypeDef*) pdev->pClassData;
NOT_USED(cfgidx);
uint8_t ret = 0;
/* Open EP IN */
USBD_LL_CloseEP(pdev,
CDC_IN_EP);
/* Open EP OUT */
USBD_LL_CloseEP(pdev,
CDC_OUT_EP);
/* Open Command IN EP */
USBD_LL_CloseEP(pdev,
CDC_CMD_EP);
#ifdef USE_USB_HID
USBD_LL_CloseEP(pdev,
HID_IN_EP);
#endif
/* DeInit physical Interface components */
if(pdev->pClassData != NULL)
{
//USBD_free(pdev->pClassData);
pdev->pClassData = NULL;
}
return ret;
}
static uint8_t USBD_CDC_Setup (USBD_HandleTypeDef *pdev,
USBD_SetupReqTypedef *req)
{
USBD_CDC_HID_HandleTypeDef *handle = (USBD_CDC_HID_HandleTypeDef*) pdev->pClassData;
static uint8_t ifalt = 0;
switch (req->bmRequest & USB_REQ_TYPE_MASK)
{
case USB_REQ_TYPE_CLASS :
if (req->wLength)
{
if (req->bmRequest & 0x80)
{
CDC_Control_FS(req->bRequest, (uint8_t *)handle->data, req->wLength);
USBD_CtlSendData (pdev,
(uint8_t *)handle->data,
req->wLength);
}
else
{
handle->CmdOpCode = req->bRequest;
handle->CmdLength = (uint8_t)req->wLength;
USBD_CtlPrepareRx (pdev,
(uint8_t *)handle->data,
req->wLength);
}
}
else
{
CDC_Control_FS(req->bRequest, (uint8_t*)req, 0);
}
break;
case USB_REQ_TYPE_STANDARD:
switch (req->bRequest)
{
case USB_REQ_GET_INTERFACE :
USBD_CtlSendData (pdev,
&ifalt,
1);
break;
case USB_REQ_SET_INTERFACE :
break;
}
default:
break;
}
return USBD_OK;
}
#ifdef USE_USB_HID
static uint8_t USBD_HID_Setup (USBD_HandleTypeDef *pdev,
USBD_SetupReqTypedef *req)
{
int len = 0;
uint8_t *pbuf = NULL;
USBD_CDC_HID_HandleTypeDef *handle = (USBD_CDC_HID_HandleTypeDef*)pdev->pClassData;
switch (req->bmRequest & USB_REQ_TYPE_MASK)
{
case USB_REQ_TYPE_CLASS :
switch (req->bRequest)
{
case HID_REQ_SET_PROTOCOL:
handle->hidProtocol = (uint8_t)(req->wValue);
break;
case HID_REQ_GET_PROTOCOL:
USBD_CtlSendData (pdev,
(uint8_t *)&handle->hidProtocol,
1);
break;
case HID_REQ_SET_IDLE:
handle->hidIdleState = (uint8_t)(req->wValue >> 8);
break;
case HID_REQ_GET_IDLE:
USBD_CtlSendData (pdev,
(uint8_t *)&handle->hidIdleState,
1);
break;
default:
USBD_CtlError (pdev, req);
return USBD_FAIL;
}
break;
case USB_REQ_TYPE_STANDARD:
switch (req->bRequest)
{
case USB_REQ_GET_DESCRIPTOR:
if( req->wValue >> 8 == HID_REPORT_DESC)
{
pbuf = handle->hidReportDesc;
len = MIN(handle->hidReportDescSize, req->wLength);
}
else if( req->wValue >> 8 == HID_DESCRIPTOR_TYPE)
{
USBD_HID_Desc[USBD_HID_DESC_REPORT_SIZE_IDX] = (uint8_t)handle->hidReportDescSize;
USBD_HID_Desc[USBD_HID_DESC_REPORT_SIZE_IDX+1] = (uint8_t)(handle->hidReportDescSize>>8);
pbuf = USBD_HID_Desc;
len = MIN(USB_HID_DESC_SIZ , req->wLength);
}
USBD_CtlSendData (pdev,
pbuf,
(uint16_t)len);
break;
case USB_REQ_GET_INTERFACE :
USBD_CtlSendData (pdev,
(uint8_t *)&handle->hidAltSetting,
1);
break;
case USB_REQ_SET_INTERFACE :
handle->hidAltSetting = (uint8_t)(req->wValue);
break;
}
}
return USBD_OK;
}
#endif
static uint8_t USBD_CDC_HID_Setup (USBD_HandleTypeDef *pdev,
USBD_SetupReqTypedef *req) {
USBD_CDC_HID_HandleTypeDef *handle = (USBD_CDC_HID_HandleTypeDef*)pdev->pClassData;
#ifdef USE_USB_HID
if (handle->hidReportDescSize && req->wIndex == HID_INTERFACE_NUMBER)
return USBD_HID_Setup(pdev, req);
else
#endif
return USBD_CDC_Setup(pdev, req);
}
/**
* @brief usbd_audio_DataIn
* Data sent on non-control IN endpoint
* @param pdev: device instance
* @param epnum: endpoint number
* @retval status
*/
static uint8_t USBD_CDC_HID_DataIn (USBD_HandleTypeDef *pdev, uint8_t epnum)
{
USBD_CDC_HID_HandleTypeDef *handle = (USBD_CDC_HID_HandleTypeDef*)pdev->pClassData;
#ifdef USE_USB_HID
if (epnum == (HID_IN_EP&0x7F)) {
/* Ensure that the FIFO is empty before a new transfer, this condition could
be caused by a new transfer before the end of the previous transfer */
handle->hidState = HID_IDLE;
} else
#endif
{
// USB CDC
handle->cdcState &= ~CDC_WRITE_TX_WAIT;
}
return USBD_OK;
}
/**
* @brief USBD_CDC_HID_DataOut
* Data received on non-control Out endpoint
* @param pdev: device instance
* @param epnum: endpoint number
* @retval status
*/
static uint8_t USBD_CDC_HID_DataOut (USBD_HandleTypeDef *pdev, uint8_t epnum)
{
USBD_CDC_HID_HandleTypeDef *handle = (USBD_CDC_HID_HandleTypeDef*)pdev->pClassData;
/* Get the received data length */
unsigned int rxLength = USBD_LL_GetRxDataSize (pdev, epnum);
if (handle) {
// Process data
jshPushIOCharEvents(EV_USBSERIAL, (char*)handle->cdcRX, rxLength);
jshHadEvent();
// Set CDC_READ_WAIT_EMPTY flag - we'll re-enable USB RX using
// USBD_LL_PrepareReceive ONLY when we have enough space
handle->cdcState |= CDC_READ_WAIT_EMPTY;
return USBD_OK;
} else {
return USBD_FAIL;
}
}
static uint8_t USBD_CDC_HID_SOF (struct _USBD_HandleTypeDef *pdev) {
USBD_CDC_HID_HandleTypeDef *handle = (USBD_CDC_HID_HandleTypeDef*)pdev->pClassData;
if (!handle ||
pdev->dev_state != USBD_STATE_CONFIGURED)
return USBD_OK;
if ((handle->cdcState & CDC_READ_WAIT_EMPTY) &&
jshGetEventsUsed() < IOBUFFER_XOFF) {
handle->cdcState &= ~CDC_READ_WAIT_EMPTY;
USBD_LL_PrepareReceive(pdev,
CDC_OUT_EP,
handle->cdcRX,
CDC_DATA_FS_OUT_PACKET_SIZE);
}
if (!(handle->cdcState & CDC_WRITE_TX_WAIT)) {
// try and fill the buffer
unsigned int len = 0;
int c;
while (len<CDC_DATA_FS_IN_PACKET_SIZE-1 && // TODO: send max packet size -1 to ensure data is pushed through
((c = jshGetCharToTransmit(EV_USBSERIAL)) >= 0) ) { // get byte to transmit
handle->cdcTX[len++] = (uint8_t)c;
}
jshClearUSBIdleTimeout(); // flag that USB is ready to receive data, so we can keep it in our buffer (otherwise chuck it)
// send data if we have any...
if (len) {
/* Transmit next packet */
handle->cdcState |= CDC_WRITE_TX_WAIT;
USBD_LL_Transmit(pdev,
CDC_IN_EP,
handle->cdcTX,
(uint16_t)len);
}
}
return USBD_OK;
}
/**
* @brief USBD_CDC_HID_DataOut
* Data received on non-control Out endpoint
* @param pdev: device instance
* @param epnum: endpoint number
* @retval status
*/
static uint8_t USBD_CDC_HID_EP0_RxReady (USBD_HandleTypeDef *pdev)
{
USBD_CDC_HID_HandleTypeDef *handle = (USBD_CDC_HID_HandleTypeDef*) pdev->pClassData;
if(handle->CmdOpCode != 0xFF)
{
CDC_Control_FS(handle->CmdOpCode, (uint8_t *)handle->data, handle->CmdLength);
handle->CmdOpCode = 0xFF;
}
return USBD_OK;
}
/**
* @brief USBD_CDC_HID_GetFSCfgDesc
* Return configuration descriptor
* @param speed : current device speed
* @param length : pointer data length
* @retval pointer to descriptor buffer
*/
static uint8_t *USBD_CDC_HID_GetCfgDesc (uint16_t *length)
{
#ifdef USE_USB_HID
// Check if we have a HID report here - we do it now so we can actually allow
// Espruino to be plugged and unplugged WITHOUT A RESET
unsigned int reportSize = 0;
USB_GetHIDReportDesc(&reportSize);
if (reportSize) {
USBD_CDC_HID_CfgDesc[USBD_CDC_HID_CFGDESC_REPORT_SIZE_IDX] = (uint8_t)reportSize;
USBD_CDC_HID_CfgDesc[USBD_CDC_HID_CFGDESC_REPORT_SIZE_IDX+1] = (uint8_t)(reportSize>>8);
*length = USBD_CDC_HID_CFGDESC_SIZE;
return USBD_CDC_HID_CfgDesc;
} else
#endif
{
*length = USBD_CDC_CFGDESC_SIZE;
return USBD_CDC_CfgDesc;
}
}
/**
* @brief DeviceQualifierDescriptor
* return Device Qualifier descriptor
* @param length : pointer data length
* @retval pointer to descriptor buffer
*/
uint8_t *USBD_CDC_HID_GetDeviceQualifierDescriptor (uint16_t *length)
{
*length = sizeof (USBD_CDC_HID_DeviceQualifierDesc);
return USBD_CDC_HID_DeviceQualifierDesc;
}
//----------------------------------------------------------------------------
static int8_t CDC_Control_FS (uint8_t cmd, uint8_t* pbuf, uint16_t length)
{
NOT_USED(pbuf);
NOT_USED(length);
switch (cmd)
{
case CDC_SEND_ENCAPSULATED_COMMAND:
break;
case CDC_GET_ENCAPSULATED_RESPONSE:
break;
case CDC_SET_COMM_FEATURE:
break;
case CDC_GET_COMM_FEATURE:
break;
case CDC_CLEAR_COMM_FEATURE:
break;
/*******************************************************************************/
/* Line Coding Structure */
/*-----------------------------------------------------------------------------*/
/* Offset | Field | Size | Value | Description */
/* 0 | dwDTERate | 4 | Number |Data terminal rate, in bits per second*/
/* 4 | bCharFormat | 1 | Number | Stop bits */
/* 0 - 1 Stop bit */
/* 1 - 1.5 Stop bits */
/* 2 - 2 Stop bits */
/* 5 | bParityType | 1 | Number | Parity */
/* 0 - None */
/* 1 - Odd */
/* 2 - Even */
/* 3 - Mark */
/* 4 - Space */
/* 6 | bDataBits | 1 | Number Data bits (5, 6, 7, 8 or 16). */
/*******************************************************************************/
case CDC_SET_LINE_CODING:
// called when plugged in and app connects
break;
case CDC_GET_LINE_CODING:
break;
case CDC_SET_CONTROL_LINE_STATE:
// called on connect/disconnect by app
break;
case CDC_SEND_BREAK:
break;
default:
break;
}
return (USBD_OK);
}
// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------
#ifdef USE_USB_HID
unsigned char *USB_GetHIDReportDesc(unsigned int *len) {
if (execInfo.hiddenRoot) {
JsVar *v = jsvObjectGetChildIfExists(execInfo.hiddenRoot, JS_USB_HID_VAR_NAME);
if (jsvIsFlatString(v)) {
if (len) *len = jsvGetStringLength(v);
unsigned char *p = jsvGetFlatStringPointer(v);
jsvUnLock(v);
return p;
}
}
if (len) *len = 0;
return 0;
}
#endif
// ----------------------------------------------------------------------------
// --------------------------------------------------------- PUBLIC Functions
// ----------------------------------------------------------------------------
#ifdef USE_USB_HID
uint8_t USBD_HID_SendReport (uint8_t *report, unsigned int len)
{
USBD_CDC_HID_HandleTypeDef *handle = (USBD_CDC_HID_HandleTypeDef*)hUsbDeviceFS.pClassData;
if (USB_IsConnected() &&
handle->hidReportDescSize && // HID enabled?
handle->hidState == HID_IDLE) { // busy?
handle->hidState = HID_BUSY;
memcpy(handle->hidData, report, len);
USBD_LL_Transmit (&hUsbDeviceFS,
HID_IN_EP,
(uint8_t*)handle->hidData,
(uint16_t)len);
return USBD_OK;
}
return USBD_FAIL;
}
#endif
int USB_IsConnected() {
return hUsbDeviceFS.dev_state == USBD_STATE_CONFIGURED;
}