-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.c
774 lines (733 loc) · 26.3 KB
/
main.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
typedef unsigned char* PUINT8;
typedef unsigned char volatile UINT8V;
#include "ch554.h"
#include "debug.h"
#include <stdio.h>
#include <string.h>
#include <ch554_usb.h>
#if CONTROLLER_TYPE_SNES
#include "fournsnes.h"
__code uint8_t ProductName[] = {
20, 0x03, // Length = 20 bytes, String Descriptor (0x03)
'0'+NUM_GAMEPADS, 0,
'N', 0,
'E', 0,
'S', 0,
'0'+NUM_GAMEPADS, 0,
'S', 0,
'N', 0,
'E', 0,
'S', 0
};
// String Descriptors:
__code uint8_t DevName1[] = {
34, 0x03, // Length = 34 bytes, String Descriptor (0x03)
'N', 0,
'E', 0,
'S', 0,
'/', 0,
'S', 0,
'N', 0,
'E', 0,
'S', 0,
' ', 0,
'G', 0,
'a', 0,
'm', 0,
'e', 0,
'p', 0,
'a', 0,
'd', 0
};
__code uint8_t ManuName[] = {
16, 0x03, // Length = 30 bytes, String Descriptor (0x03)
'R', 0,
'a', 0,
'p', 0,
'h', 0,
'n', 0,
'e', 0,
't', 0
};
#endif
#if CONTROLLER_TYPE_PSX
#include "fourplay.h"
__code unsigned char ProductName[] = {
20, 0x03, // Length = 20 bytes, String Descriptor (0x03)
'0'+NUM_GAMEPADS, 0,
'P', 0,
'l', 0,
'a', 0,
'y', 0,
' ', 0,
'P', 0,
'a', 0,
'd', 0
};
// String Descriptors:
__code unsigned char DevName1[] = {
24, 0x03, // Length = 24 bytes, String Descriptor (0x03)
'P', 0,
'S', 0,
'X', 0,
' ', 0,
'G', 0,
'a', 0,
'm', 0,
'e', 0,
'p', 0,
'a', 0,
'd', 0
};
__code unsigned char ManuName[] = {
20, 0x03, // Length = 20 bytes, String Descriptor (0x03)
'H', 0,
'a', 0,
'r', 0,
'p', 0,
's', 0,
't', 0,
'o', 0,
'n', 0,
'e', 0
};
#endif
uint32_t millis, last;
#define PORT1REG 0x90
#define PORT2REG 0xA0
#define PORT3REG 0xB0
#define LED_PIN1 0
SBIT(LED1, PORT3REG, LED_PIN1);
#define MULTITAP_PIN 1
#define LATCH_PIN 2 // P3.2
#define CLOCK_PIN 3 // P3.3
#define DATA0_PIN 4 // P1.4
#define DATA1_PIN 5 // P1.5
#define DATA2_PIN 6 // P1.6
#define DATA3_PIN 7 // P1.7
__xdata __at (0x0000) uint8_t Ep0Buffer[64];
__xdata __at (0x0040) uint8_t Ep4Buffer[64];
__xdata __at (0x0080) uint8_t Ep1Buffer[64];
__xdata __at (0x00C0) uint8_t Ep2Buffer[64];
__xdata __at (0x0100) uint8_t Ep3Buffer[64];
uint8_t SetupReq,SetupLen,Ready,SentFlag,UsbConfig;
PUINT8 pDescr;
USB_SETUP_REQ SetupReqBuf;
void mTimer0Interrupt( void ) __interrupt (INT_NO_TMR0)
{
TH0 = (65536 - 2000)/256; // Reload
TL0 = (65536 - 2000)%256; // Reload
millis++;
}
#define BIT0 (0X01)
#define BIT1 (0X02)
#define BIT2 (0X04)
#define BIT3 (0X08)
#define BIT4 (0X10)
#define BIT5 (0X20)
#define BIT6 (0X40)
#define BIT7 (0X80)
#define UsbSetupBuf ((PUSB_SETUP_REQ)Ep0Buffer)
__code uint8_t DevDesc[18] = {
18, // length (18)
0x01, // Descriptor type
0x10,0x01, // USB version (1.1)
0x00, // class
0x00, // subclass
0x00, // protocol
0x08, // Max packet size
0x50,0x1D, // VID 0x1D50 (OpenMoko)
0x2D,0x60, // PID 0x602D (5nes5snes (4x12))
0x00,0x00, // Device version
0x03, // manufacturer string index
0x02, // product string index
0x00, // S/N string index
0x01 // # of configurations
};
// Decoder ring for below descriptors
/*
9, // sizeof(usbDescrInterface): length of descriptor in bytes
0x04, // descriptor type
0, // index of this interface
0, // alternate setting for this interface
1, // endpoints excl 0: number of endpoint descriptors to follow
3, // class
0, // subclass
0, // protocol
1, // string index for interface
9, // sizeof(usbDescrHID): length of descriptor in bytes
0x21, // descriptor type: HID
0x11, 0x01, // BCD representation of HID version
0x00, // target country code
0x01, // number of HID Report (or other HID class) Descriptor infos to follow
0x22, // descriptor type: report
REP_DESC_LEN, 0, // total length of report descriptor
7, // sizeof(usbDescrEndpoint)
5, // descriptor type = endpoint
0x81, // IN endpoint number 1
0x03, // attrib: Interrupt endpoint
16, 0, // maximum packet size
10, // in ms
*/
// poll interval of host in milliseconds
#define POLL_INTERVAL 2
#define CFG_DESC_LEN (9+((9+9+7)*NUM_GAMEPADS))
#define CFG_INTERFACE_DESCR(a) 0x09,0x04,a,0x00,0x01,0x03,0x00,0x00,0x01
#define CFG_HID_DESCR 0x09,0x21,0x11,0x01,0x00,0x01,0x22,REP_DESC_LEN,0x00
#define CFG_EP_DESCR(a) 0x07,0x05,a,0x03,0x10,0x00,POLL_INTERVAL
__code uint8_t CfgDesc[CFG_DESC_LEN] =
{
9, /* sizeof(usbDescriptorConfiguration): length of descriptor in bytes */
0x02, /* descriptor type */
CFG_DESC_LEN,
0, /* total length of data returned (including inlined descriptors) */
NUM_GAMEPADS, /* number of interfaces in this configuration */
1, /* index of this configuration */
0, /* configuration name string index */
0xA0, // USBATTR_BUSPOWER + USBATTR_REMOTEWAKE
100/2, /* max USB current in 2mA units */
/* interface descriptor follows inline: */
CFG_INTERFACE_DESCR(0),
CFG_HID_DESCR,
CFG_EP_DESCR(0x81),
#if NUM_GAMEPADS > 1
CFG_INTERFACE_DESCR(1),
CFG_HID_DESCR,
CFG_EP_DESCR(0x82),
#endif
#if NUM_GAMEPADS > 2
CFG_INTERFACE_DESCR(2),
CFG_HID_DESCR,
CFG_EP_DESCR(0x83),
#endif
#if NUM_GAMEPADS > 3
CFG_INTERFACE_DESCR(3),
CFG_HID_DESCR,
CFG_EP_DESCR(0x84)
#endif
};
// These descriptors are absolutely identical except for the report ID field, hence the #define above.
__code uint8_t ControllerRepDesc[NUM_GAMEPADS][REP_DESC_LEN] = {
{GAMEPAD_REPORT_DESCRIPTOR(1)},
#if NUM_GAMEPADS > 1
{GAMEPAD_REPORT_DESCRIPTOR(2)},
#endif
#if NUM_GAMEPADS > 2
{GAMEPAD_REPORT_DESCRIPTOR(3)},
#endif
#if NUM_GAMEPADS > 3
{GAMEPAD_REPORT_DESCRIPTOR(4)}
#endif
};
// Language Descriptor
__code uint8_t LangDesc[] = {
4, 0x03, // Length = 4 bytes, String Descriptor (0x03)
0x09, 0x04 // 0x0409 English - United States
};
#define USB_STRINGDESC_COUNT 4
__code uint8_t* StringDescs[USB_STRINGDESC_COUNT] = {
LangDesc, // 0 (If you want to support string descriptors, you must have this!)
DevName1, // 1
ProductName, // 2
ManuName // 3
};
// Buffers that the gamepad data gets written into before it's sent to the USB endpoints
uint8_t HIDCtrl[NUM_GAMEPADS][GAMEPAD_XMIT_DATA_LEN];
void CH554SoftReset( )
{
SAFE_MOD = 0x55;
SAFE_MOD = 0xAA;
GLOBAL_CFG |= bSW_RESET;
}
void CH554USBDevWakeup( )
{
UDEV_CTRL |= bUD_LOW_SPEED;
mDelaymS(2);
UDEV_CTRL &= ~bUD_LOW_SPEED;
}
void USBDeviceInit()
{
IE_USB = 0;
USB_CTRL = 0x00; // USB device mode
UEP2_DMA = (uint16_t)Ep2Buffer; // Set endpoint 2's DMA buffer to Ep2Buffer
UEP3_DMA = (uint16_t)Ep3Buffer; // Set endpoint 3's DMA buffer to Ep3Buffer
UEP2_3_MOD = bUEP3_TX_EN | bUEP2_TX_EN; // Set both endpoints 2 and 3 as IN endpoints
UEP2_CTRL = UEP_T_RES_NAK; // Tell host we don't have anything (yet)
UEP3_CTRL = UEP_T_RES_NAK; // Tell host we don't have anything (yet)
UEP0_DMA = (uint16_t)Ep0Buffer; // Set endpoint 0's DMA buffer to Ep0Buffer
// (WARNING: Ep4Buffer is always set to Ep0Buffer+0x40)
UEP1_DMA = (uint16_t)Ep1Buffer; // Set endpoint 1's DMA buffer to Ep3Buffer
UEP4_1_MOD = bUEP1_TX_EN | bUEP4_TX_EN; // Set both endpoints 1 and 4 as IN endpoints
UEP1_CTRL = UEP_T_RES_NAK; // Tell host we don't have anything (yet)
UEP4_CTRL = UEP_T_RES_NAK; // Tell host we don't have anything (yet)
UEP0_CTRL = UEP_R_RES_ACK | UEP_T_RES_NAK; // Tell host we don't have anything (yet) to transmit, but ready to receive
USB_DEV_AD = 0x00;
UDEV_CTRL = bUD_PD_DIS; // Disable DP / DM pull-down resistor
USB_CTRL = bUC_DEV_PU_EN | bUC_INT_BUSY | bUC_DMA_EN; // Start USB device and DMA,
// and automatically respond with NAK before interrupt flag is not cleared during interrupt
UDEV_CTRL |= bUD_PORT_EN; // Enable USB port
USB_INT_FG = 0xFF; // Clear all interrupt flags־
USB_INT_EN = bUIE_SUSPEND | bUIE_TRANSFER | bUIE_BUS_RST; // Enable suspend, transfer and bus reset interrupts
IE_USB = 1;
}
void Enp1IntIn( )
{
memcpy( Ep1Buffer, HIDCtrl[0], GAMEPAD_XMIT_DATA_LEN); // Copy the last generated data to the endpoint
if (SentFlag & 1)//if ((SentFlag & 0x11) == 0x11)
{
UEP1_T_LEN = GAMEPAD_XMIT_DATA_LEN; // Let the Host know we have this many bytes to send
UEP1_CTRL = UEP1_CTRL & ~ MASK_UEP_T_RES | UEP_T_RES_ACK; // Enable acknowledgements
SentFlag ^= 1;
}
}
#if NUM_GAMEPADS > 1
void Enp2IntIn( )
{
memcpy( Ep2Buffer, HIDCtrl[1], GAMEPAD_XMIT_DATA_LEN);
if (SentFlag & 1)//if ((SentFlag & 0x21) == 0x21)
{
UEP2_T_LEN = GAMEPAD_XMIT_DATA_LEN;
UEP2_CTRL = UEP2_CTRL & ~ MASK_UEP_T_RES | UEP_T_RES_ACK;
SentFlag ^= 1;
}
}
#endif
#if NUM_GAMEPADS > 2
void Enp3IntIn( )
{
memcpy( Ep3Buffer, HIDCtrl[2], GAMEPAD_XMIT_DATA_LEN);
if (SentFlag & 1)//if ((SentFlag & 0x41) == 0x41)
{
UEP3_T_LEN = GAMEPAD_XMIT_DATA_LEN;
UEP3_CTRL = UEP3_CTRL & ~ MASK_UEP_T_RES | UEP_T_RES_ACK;
SentFlag ^= 1;
}
}
#endif
#if NUM_GAMEPADS > 3
void Enp4IntIn( )
{
memcpy( Ep4Buffer, HIDCtrl[3], GAMEPAD_XMIT_DATA_LEN);
if (SentFlag & 1)//if ((SentFlag & 0x81) == 0x81)
{
UEP4_T_LEN = GAMEPAD_XMIT_DATA_LEN;
UEP4_CTRL = UEP4_CTRL & ~ MASK_UEP_T_RES | UEP_T_RES_ACK;
SentFlag ^= 1;
}
}
#endif
void HIDValueHandle(int gamepadID)
{
// Copy the freshest data to the endpoints and alert the Host.
switch (gamepadID)
{
case 0:
Enp1IntIn();
break;
#if NUM_GAMEPADS > 1
case 1:
Enp2IntIn();
break;
#endif
#if NUM_GAMEPADS > 2
case 2:
Enp3IntIn();
break;
#endif
#if NUM_GAMEPADS > 3
case 3:
Enp4IntIn();
break;
#endif
}
}
void GamepadGetLatest()
{
int i;
CONTROLLER_UPDATE(); // polls pads
// Each of these converts the controller data into the HID data
// in the format that our report descriptor specifies.
for (i = 0; i < NUM_GAMEPADS; i++)
{
BUILD_CONTROLLER_REPORT(HIDCtrl[i], i+1);
if (Ready)
{
HIDValueHandle(i);
}
}
}
void DeviceInterrupt(void) __interrupt (INT_NO_USB)
{
uint8_t len = 0;
while(UIF_TRANSFER) // USB transfer complete flag־
{
switch (USB_INT_ST & (MASK_UIS_TOKEN | MASK_UIS_ENDP))
{
case UIS_TOKEN_IN | 4: // Endpoint 4 interrupt endpoint upload
#if NUM_GAMEPADS > 3
SentFlag |= 0x81; // Let the code know that no other interrupt endpoints are sending
#endif
UEP4_T_LEN = 0; // Clear the length (don't send data)
UEP4_CTRL = UEP4_CTRL & ~ MASK_UEP_T_RES | UEP_T_RES_NAK; // NACK requests
UEP4_CTRL ^= bUEP_T_TOG; // Toggle DATA0/DATA1 so that the next message we send is known to be fresh
// NOTE: Endpoint 4 on CH55x MUST toggle the DATA0/DATA1 -- it doesn't support auto-toggle.
break;
case UIS_TOKEN_IN | 3: // Endpoint 3 interrupt endpoint upload
#if NUM_GAMEPADS > 2
SentFlag |= 0x41; // Let the code know that no other interrupt endpoints are sending
#endif
UEP3_T_LEN = 0;
UEP3_CTRL = UEP3_CTRL & ~ MASK_UEP_T_RES | UEP_T_RES_NAK;
UEP3_CTRL ^= bUEP_T_TOG; // Don't need to actually do this on Endpoints 1-3,
// but for consistency, I turned off auto-toggle.
break;
case UIS_TOKEN_IN | 2: // Endpoint 2 interrupt endpoint upload
#if NUM_GAMEPADS > 1
SentFlag |= 0x21; // Let the code know that no other interrupt endpoints are sending
#endif
UEP2_T_LEN = 0;
UEP2_CTRL = UEP2_CTRL & ~ MASK_UEP_T_RES | UEP_T_RES_NAK;
UEP2_CTRL ^= bUEP_T_TOG;
break;
case UIS_TOKEN_IN | 1: // Endpoint 1 interrupt endpoint upload
SentFlag |= 0x11; // Let the code know that no other interrupt endpoints are sending
UEP1_T_LEN = 0;
UEP1_CTRL = UEP1_CTRL & ~ MASK_UEP_T_RES | UEP_T_RES_NAK;
UEP1_CTRL ^= bUEP_T_TOG;
break;
case UIS_TOKEN_SETUP | 0: // Setup transaction
len = USB_RX_LEN;
if(len == (sizeof(USB_SETUP_REQ)))
{
SetupLen = UsbSetupBuf->wLengthL;
if(UsbSetupBuf->wLengthH || SetupLen > 0x7F )
{
SetupLen = 0x7F; // Limit the total length to the maximum
}
len = 0; // Default is successful with 0 length
SetupReq = UsbSetupBuf->bRequest;
if ( ( UsbSetupBuf->bRequestType & USB_REQ_TYP_MASK ) != USB_REQ_TYP_STANDARD ) // HID class command
{
switch( SetupReq )
{
case 0x01://GetReport
break;
case 0x02://GetIdle
break;
case 0x03://GetProtocol
break;
case 0x09://SetReport
break;
case 0x0A://SetIdle
break;
case 0x0B://SetProtocol
break;
default:
len = 0xFF; // command not supported
break;
}
}
else
{ // Standard request
switch(SetupReq) // Request code
{
case USB_GET_DESCRIPTOR:
switch(UsbSetupBuf->wValueH)
{
case 1: // Device Descriptor
pDescr = DevDesc; // Premade buffer to be sent
len = sizeof(DevDesc);
break;
case 2: // Configuration Descriptor
pDescr = CfgDesc; // Premade buffer to be sent
len = sizeof(CfgDesc);
break;
case 3: // String Descriptor
len = UsbSetupBuf->wValueL; // Index
if (len < USB_STRINGDESC_COUNT) {
pDescr = (uint8_t*)(StringDescs[len]);
len = pDescr[0];
} else {
len = 0xFF; // Not supported
}
break;
case 0x22: //Report Descriptor
#if USE_SINGLE_EP
pDescr = ControllerRepDesc[0];
len = REP_DESC_LEN*NUM_GAMEPADS;
Ready = 1;
#else
if (UsbSetupBuf->wIndexL < NUM_GAMEPADS)
{
pDescr = ControllerRepDesc[UsbSetupBuf->wIndexL]; // Premade buffer to be sent
len = REP_DESC_LEN;
if (UsbSetupBuf->wIndexL == (NUM_GAMEPADS-1)) {
Ready = 1;
}
}
else {
len = 0xff; // Normally wouldn't execute: Host should only ask for reports for 4 endpoints
}
#endif
break;
default:
len = 0xff; // Unsupported command or error
break;
}
if ( SetupLen > len )
{
SetupLen = len; // Limit the total length
}
len = SetupLen >= 8 ? 8 : SetupLen; // Set current transmission length
memcpy(Ep0Buffer,pDescr,len); // Set the data to upload
SetupLen -= len; // SetupLen = remaining bytes to send
pDescr += len; // Increment to get ready to send the next chunk (if exists)
break;
case USB_SET_ADDRESS:
SetupLen = UsbSetupBuf->wValueL; // Set temporary USB device address
break;
case USB_GET_CONFIGURATION:
Ep0Buffer[0] = UsbConfig;
if ( SetupLen >= 1 )
{
len = 1;
}
break;
case USB_SET_CONFIGURATION:
UsbConfig = UsbSetupBuf->wValueL;
break;
case 0x0A:
break;
case USB_CLEAR_FEATURE:
if ( ( UsbSetupBuf->bRequestType & USB_REQ_RECIP_MASK ) == USB_REQ_RECIP_ENDP ) // Endpoint
{
switch( UsbSetupBuf->wIndexL )
{
case 0x84:
UEP4_CTRL = UEP4_CTRL & ~ ( bUEP_T_TOG | MASK_UEP_T_RES ) | UEP_T_RES_NAK;
//SentFlag |= (0x80);
break;
case 0x83:
UEP3_CTRL = UEP3_CTRL & ~ ( bUEP_T_TOG | MASK_UEP_T_RES ) | UEP_T_RES_NAK;
//SentFlag |= (0x40);
break;
case 0x82:
UEP2_CTRL = UEP2_CTRL & ~ ( bUEP_T_TOG | MASK_UEP_T_RES ) | UEP_T_RES_NAK;
//SentFlag |= (0x20);
break;
case 0x81:
UEP1_CTRL = UEP1_CTRL & ~ ( bUEP_T_TOG | MASK_UEP_T_RES ) | UEP_T_RES_NAK;
//SentFlag |= (0x10);
break;
case 0x01:
UEP1_CTRL = UEP1_CTRL & ~ ( bUEP_R_TOG | MASK_UEP_R_RES ) | UEP_R_RES_ACK;
break;
default:
len = 0xFF; // Unsupported endpoint
break;
}
}
if ( ( UsbSetupBuf->bRequestType & USB_REQ_RECIP_MASK ) == USB_REQ_RECIP_DEVICE )
{
break;
}
else
{
len = 0xFF; // Unsupported
}
break;
case USB_SET_FEATURE:
if( ( UsbSetupBuf->bRequestType & 0x1F ) == 0x00 ) // Setting up the device
{
if( ( ( ( uint16_t )UsbSetupBuf->wValueH << 8 ) | UsbSetupBuf->wValueL ) == 0x01 )
{
if( CfgDesc[ 7 ] & 0x20 )
{
// Setting up wake up enable
}
else
{
len = 0xFF; // Operation failed
}
}
else
{
len = 0xFF; // Operation failed
}
}
else if( ( UsbSetupBuf->bRequestType & 0x1F ) == 0x02 ) // Set endpoint
{
if( ( ( ( uint16_t )UsbSetupBuf->wValueH << 8 ) | UsbSetupBuf->wValueL ) == 0x00 )
{
switch( ( ( uint16_t )UsbSetupBuf->wIndexH << 8 ) | UsbSetupBuf->wIndexL )
{
case 0x84:
UEP4_CTRL = UEP4_CTRL & (~bUEP_T_TOG) | UEP_T_RES_STALL; // EP4 IN STALL
//SentFlag &= ~(0x80);
break;
case 0x83:
UEP3_CTRL = UEP3_CTRL & (~bUEP_T_TOG) | UEP_T_RES_STALL; // EP3 IN STALL
//SentFlag &= ~(0x40);
break;
case 0x82:
UEP2_CTRL = UEP2_CTRL & (~bUEP_T_TOG) | UEP_T_RES_STALL; // EP2 IN STALL
//SentFlag &= ~(0x20);
break;
case 0x81:
UEP1_CTRL = UEP1_CTRL & (~bUEP_T_TOG) | UEP_T_RES_STALL; // EP1 IN STALL
//SentFlag &= ~(0x10);
break;
default:
len = 0xFF; // Operation failed
break;
}
}
else
{
len = 0xFF; // Operation failed
}
}
else
{
len = 0xFF; // Operation failed
}
break;
case USB_GET_STATUS:
Ep0Buffer[0] = 0x00;
Ep0Buffer[1] = 0x00;
if ( SetupLen >= 2 )
{
len = 2;
}
else
{
len = SetupLen;
}
break;
default:
len = 0xff; // Operation failed
break;
}
}
}
else
{
len = 0xff; // Packet length error
}
if(len == 0xff)
{
SetupReq = 0xFF;
UEP0_CTRL = bUEP_R_TOG | bUEP_T_TOG | UEP_R_RES_STALL | UEP_T_RES_STALL;//STALL
}
else if(len) // Uploading data or status phase returns 0 length packets
{
UEP0_T_LEN = len;
UEP0_CTRL = bUEP_R_TOG | bUEP_T_TOG | UEP_R_RES_ACK | UEP_T_RES_ACK; // Default is DATA1 + ACK
}
else
{
// Because it has not yet reached the status phase, it is preset to upload 0-length packets in advance
// to prevent the host from entering the status phase too early.
UEP0_T_LEN = 0;
UEP0_CTRL = bUEP_R_TOG | bUEP_T_TOG | UEP_R_RES_ACK | UEP_T_RES_ACK; // Default is DATA1 + ACK
}
break;
case UIS_TOKEN_IN | 0: // Endpoint0 IN
switch(SetupReq)
{
case USB_GET_DESCRIPTOR:
len = SetupLen >= 8 ? 8 : SetupLen; // Current transmission length
memcpy( Ep0Buffer, pDescr, len ); // Data to send to host
SetupLen -= len; // SetupLen holds the remaining byte count
pDescr += len; // Increment pDescr to get ready for the next transaction
UEP0_T_LEN = len; // Tell the host that "len" bytes are available
UEP0_CTRL ^= bUEP_T_TOG; // Flip the sync flag
break;
case USB_SET_ADDRESS:
USB_DEV_AD = USB_DEV_AD & bUDA_GP_BIT | SetupLen;
UEP0_CTRL = UEP_R_RES_ACK | UEP_T_RES_NAK;
break;
default:
UEP0_T_LEN = 0; // Interruption of status phase completion or forced upload of
// 0-length data packets to end control transmission
UEP0_CTRL = UEP_R_RES_ACK | UEP_T_RES_NAK;
break;
}
break;
case UIS_TOKEN_OUT | 0: // endpoint0 OUT
len = USB_RX_LEN;
UEP0_CTRL ^= bUEP_R_TOG; // Flip the sync flag
break;
default:
break;
}
UIF_TRANSFER = 0; // Write 0 to clear interrupt
}
if(UIF_BUS_RST) // Device mode USB bus reset interrupt
{
UEP0_CTRL = UEP_R_RES_ACK | UEP_T_RES_NAK;
UEP1_CTRL = UEP_T_RES_NAK;
UEP2_CTRL = UEP_T_RES_NAK;
UEP3_CTRL = UEP_T_RES_NAK;
UEP4_CTRL = UEP_T_RES_NAK;
USB_DEV_AD = 0x00;
UIF_SUSPEND = 0;
UIF_TRANSFER = 0;
UIF_BUS_RST = 0; // Write 0 to clear interrupt
}
if (UIF_SUSPEND) // USB bus suspend / wake up completed
{
UIF_SUSPEND = 0;
if ( USB_MIS_ST & bUMS_SUSPEND ) // ?
{
}
}
else { // Unexpected interrupt, should never happen
USB_INT_FG = 0xFF; // Clear all the interrupts and just blow out of the ISR.
}
}
main()
{
int i;
CfgFsys( );
mDelaymS(5);
mInitSTDIO( );
SentFlag = 1;
USBDeviceInit();
// Put the I/O in a known state
enableLiveAutodetect();
CONTROLLER_INIT();
for (i = 0; i < NUM_GAMEPADS; i++)
{
HIDCtrl[i][0] = i+1;
}
TMOD = 0x11;
TH0 = (65536 - 2000)/256;
TL0 = (65536 - 2000)%256;
TR0 = 1; // Start Timer 0
ET0 = 1; // Enable Timer 0 Interrupt
EA = 1; // Global interrupt enable
// Make sure we don't send any weird data on the IN endpoints...
UEP1_T_LEN = 0;
UEP2_T_LEN = 0;
UEP3_T_LEN = 0;
UEP4_T_LEN = 0;
Ready = 0; // This gets set as soon as the last Report Descriptor is read.
while(1)
{
if (millis-last > 40)
{ // Toggle the LED to show that the thing is alive.
LED1 = !LED1;
last = millis;
}
GamepadGetLatest(); // Always get fresh data from the gamepads, because the fresher, the better!
/*
if(Ready)
{
HIDValueHandle(i); // Send it to the Host if the host wants it
}
i++;
i %= NUM_GAMEPADS;
*/
}
}