-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathUSBFramework.c
executable file
·872 lines (769 loc) · 32.5 KB
/
USBFramework.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
865
866
867
868
869
870
871
872
//
// Author: Bradley A. Minch
// Organization: Franklin W. Olin College of Engineering
// Revision History:
// 01/19/2006 - Added wait for initial SE0 condition to clear at the end
// of InitUSB().
// 01/13/2006 - Fixed problem with DATA OUT transfers (only worked properly
// with class requests) in ProcessSetupToken. Added code to
// disable all EPs except EP0 on a valid SET_CONFIGURATION
// request. Changed code to use BSTALL instead of EPSTALL for
// Request Error on EP0. Changed CLEAR_FEATURE, SET_FEATURE,
// and GET_STATUS requests to use BSTALL instead of EPSTALL.
// Eliminated initial for loop in main().
// 11/14/2005 - Initial port to C for the Microchip C18 compiler complete
// 06/22/2005 - Added code to disable all endpoints on USRTIF and to mask
// bits 0, 1, and 7 of USTAT on TRNIF in ServiceUSB.
// 04/21/2005 - Initial public release.
//
// ============================================================================
//
// Peripheral Description:
//
// This peripheral enumerates as a vendor-specific device. The main event loop
// blinks an LED connected to RA1 on and off at about 2 Hz and the peripheral
// responds to a pair of vendor-specific requests that turn on or off an LED
// connected to RA0. The firmware is configured to use an external 4-MHz
// crystal, to operate as a low-speed USB device, and to use the internal
// pull-up resistor.
//
// ============================================================================
//
// Software Licence Agreement:
//
// THIS SOFTWARE IS PROVIDED IN AN "AS IS" CONDITION. NO WARRANTIES, WHETHER
// EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY
// TO THIS SOFTWARE. THE AUTHOR SHALL NOT, UNDER ANY CIRCUMSTANCES, BE LIABLE
// FOR SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
//
#include <p18f14k50.h>
#include <delays.h>
#include "USBFramework.h"
// Configuration bits
#pragma config CPUDIV = NOCLKDIV
#pragma config USBDIV = OFF
#pragma config FOSC = HS
#pragma config PLLEN = ON
#pragma config FCMEN = OFF
#pragma config IESO = OFF
#pragma config PWRTEN = OFF
#pragma config BOREN = OFF
#pragma config BORV = 30
#pragma config WDTEN = OFF
#pragma config WDTPS = 32768
#pragma config MCLRE = ON
#pragma config HFOFST = OFF
#pragma config STVREN = ON
#pragma config LVP = OFF
#pragma config XINST = OFF
#pragma config BBSIZ = OFF
#pragma config CP0 = OFF
#pragma config CP1 = OFF
#pragma config CPB = OFF
#pragma config WRT0 = OFF
#pragma config WRT1 = OFF
#pragma config WRTB = OFF
#pragma config WRTC = OFF
#pragma config EBTR0 = OFF
#pragma config EBTR1 = OFF
#pragma config EBTRB = OFF
#pragma config DEBUG = OFF
#define SHOW_ENUM_STATUS
#define ENABLE_LOGGING
#define MINCH_RESET
#ifdef SHOW_ENUM_STATUS
#define UPDATE_ENUM_STATUS(status) LATC=(1<<status)
#else
#define UPDATE_ENUM_STATUS(status);
#endif
#define flag LATC |= 0x01
#define clrflag LATC &= 0xFE
#define whoa flag; while(1)
typedef enum _USBLogCode {
USBLogCodeReset = 1,
USBLogCodeGetDeviceDescriptor = 2,
USBLogCodeGetConfigurationDescriptor = 3,
USBLogCodeSetAddress = 4,
USBLogCodeError = 5,
USBLogCodeMiscError = 6
} USBLogCode;
#pragma udata
USBDeviceState _USBDeviceState;
USBEngineStatus _USBEngineStatus;
USBDeviceStatus _USBDeviceStatus;
unsigned char _USBCurrentConfiguration;
unsigned char _USBPendingDeviceAddress;
rom unsigned char *_USBDescriptorToSend;
unsigned char _USBDescriptorBytesLeft;
USBLogCode _USBLogArray[32];
unsigned char _USBLogArrayCount;
#pragma romdata
rom const unsigned char _USBDeviceDescriptor[] = {
0x12, // bLength
USB_DEVICE_DESCRIPTOR_TYPE, // bDescriptorType
0x00, // bcdUSB (low byte)
0x02, // bcdUSB (high byte)
0x00, // bDeviceClass
0x00, // bDeviceSubClass
0x00, // bDeviceProtocol
EP0_SIZE, // bMaxPacketSize
0xD8, // idVendor (low byte)
0x04, // idVendor (high byte)
0x01, // idProduct (low byte)
0x00, // idProduct (high byte)
0x00, // bcdDevice (low byte)
0x00, // bcdDevice (high byte)
0x01, // iManufacturer
0x02, // iProduct
0x00, // iSerialNumber (none)
NUMBER_OF_USB_CONFIGURATIONS // bNumConfigurations
};
rom const unsigned char _USBConfigurationDescriptor[] = {
9, // bLength
USB_CONFIGURATION_DESCRIPTOR_TYPE, // bDescriptorType
25, // wTotalLength (low byte)
0x00, // wTotalLength (high byte)
NUMBER_OF_USB_INTERFACES, // bNumInterfaces
0x01, // bConfigurationValue
0x00, // iConfiguration (none)
0xA0, // bmAttributes
50, // bMaxPower (100 mA)
0x09, // bLength (Interface1 descriptor starts here)
USB_INTERFACE_DESCRIPTOR_TYPE, // bDescriptorType
0, // bInterfaceNumber
0, // bAlternateSetting
NUMBER_OF_USB_ENDPOINTS, // bNumEndpoints (excluding EP0)
0xFF, // bInterfaceClass (vendor specific class code)
0x00, // bInterfaceSubClass
0xFF, // bInterfaceProtocol (vendor specific protocol used)
0x00, // iInterface (none)
7, // bLength (Endpoint1 descriptor starts here)
USB_ENDPOINT_DESCRIPTOR_TYPE,
0x81, // Endpoint address 1-IN
0x03, // Interrupt endpoint
8, // wMaxPacketSize (low byte)
0x00, // wMaxPacketSize (high byte)
250, // bInterval (250 ms at full speed)
};
rom const rom const unsigned char *_USBConfigurationDescriptors[] = {
_USBConfigurationDescriptor
};
rom const unsigned char _USBLangIDStringDescriptor[] = { // LangID (special string descriptor at index 0)
0x04, // bLength
USB_STRING_DESCRIPTOR_TYPE, // bDescriptorType
0x09, // wLANGID[0] (low byte)
0x04 // wLANGID[0] (high byte)
};
rom const unsigned char _USBManufacturerStringDescriptor[] = {
0x36, // bLength
USB_STRING_DESCRIPTOR_TYPE, // bDescriptorType
'M', 0x00, 'i', 0x00, 'c', 0x00, 'r', 0x00, 'o', 0x00, 'c', 0x00, 'h', 0x00, 'i', 0x00, 'p', 0x00, ' ', 0x00,
'T', 0x00, 'e', 0x00, 'c', 0x00, 'h', 0x00, 'n', 0x00, 'o', 0x00, 'l', 0x00, 'o', 0x00, 'g', 0x00, 'y', 0x00, ',', 0x00, ' ', 0x00,
'I', 0x00, 'n', 0x00, 'c', 0x00, '.', 0x00
};
rom const unsigned char _USBDeviceStringDescriptor[] = {
0x44, // bLength
USB_STRING_DESCRIPTOR_TYPE, // bDescriptorType
'E', 0x00, 'N', 0x00, 'G', 0x00, 'R', 0x00, ' ', 0x00, '2', 0x00, '2', 0x00, '1', 0x00, '0', 0x00, ' ', 0x00,
'P', 0x00, 'I', 0x00, 'C', 0x00, '1', 0x00, '8', 0x00, 'F', 0x00, '2', 0x00, '4', 0x00, '5', 0x00, '5', 0x00, ' ', 0x00,
'U', 0x00, 'S', 0x00, 'B', 0x00, ' ', 0x00,
'F', 0x00, 'i', 0x00, 'r', 0x00, 'm', 0x00, 'w', 0x00, 'a', 0x00, 'r', 0x00, 'e', 0x00
};
rom const rom const unsigned char *_USBStringDescriptors[] = {
_USBLangIDStringDescriptor,
_USBManufacturerStringDescriptor,
_USBDeviceStringDescriptor
};
#pragma code
void HighPriorityISR(void);
void LowPriorityISR(void);
void _USBLog(USBLogCode logCode);
USBTransaction _USBGetCurrentTransaction(void);
void _USBHandleControlError(void);
void _USBResetEP0InBuffer(void);
void _USBWriteValue(unsigned value, unsigned char bytes);
void _USBSendDescriptor(void);
void _USBWriteDescriptor(rom const unsigned char *descriptor, unsigned char bytes);
void _USBWriteSingleDescriptor(rom const unsigned char *descriptor);
void _USBProcessEP0(USBTransaction *transaction);
void _USBProcessEP1(USBTransaction *transaction);
void _USBEngineReset(void);
void _USBConfigureBufferDescriptors(void);
void _USBSetup(void);
// Interrupt service routines
#pragma code __HIGH_PRIORITY_ISR__=0x0008
void _high_priority_isr(void) {_asm goto HighPriorityISR _endasm}
#pragma code __LOW_PRIORITY_ISR__=0x0018
void _low_priority_isr(void) {_asm goto LowPriorityISR _endasm}
#pragma interrupt HighPriorityISR
void HighPriorityISR(void)
{
// Suspend when idle
if (UIRbits.IDLEIF) {
UCONbits.SUSPND = 1;
UIRbits.IDLEIF = 0;
}
// Unsuspend when bus active
if (UIRbits.ACTVIF) {
UCONbits.SUSPND = 0;
while (UIRbits.ACTVIF) {
UIRbits.ACTVIF = 0;
}
}
// Start of Frame - ignored
if (UIRbits.SOFIF) {
// Ignored
UIRbits.SOFIF = 0;
}
// Bus stall - ignored
if (UIRbits.STALLIF) {
// Ignored
UIRbits.STALLIF = 0;
}
// Error handling - ignored
if (UIRbits.UERRIF) {
// Ignored
if (UEIRbits.CRC5EF) _USBLog(USBLogCodeError);
else _USBLog(USBLogCodeMiscError);
UEIR = 0x00; // Clear all USB err flags
UIRbits.UERRIF = 0;
}
// Handle USB reset
if (UIRbits.URSTIF) {
_USBLog(USBLogCodeReset);
_USBEngineReset();
UIRbits.URSTIF = 0;
}
// Handle USB transaction
if (UIRbits.TRNIF) {
USBTransaction transaction = _USBGetCurrentTransaction();
// Route transaction to endpoint
switch (transaction.endpoint) {
case 0:
_USBProcessEP0(&transaction);
break;
case 1:
_USBProcessEP1(&transaction);
default:
break;
}
// Clear flags
UIRbits.TRNIF = 0;
}
PIR2bits.USBIF = 0;
}
#pragma interruptlow LowPriorityISR
void LowPriorityISR(void)
{
// Low interrupt service routine
}
void _USBLog(USBLogCode logCode)
{
#ifdef ENABLE_LOGGING
static unsigned char writeIndex = 0;
_USBLogArrayCount = writeIndex + 1;
if (writeIndex < 32) {
_USBLogArray[writeIndex++] = logCode;
}
#endif
}
void _USBDisplayLog(void)
{
#ifdef ENABLE_LOGGING
unsigned char index;
LATC = 255;
Delay10KTCYx(255);
Delay10KTCYx(255);
Delay10KTCYx(255);
Delay10KTCYx(255);
for(index = 0; index < _USBLogArrayCount; index++) {
LATC = _USBLogArray[index];
Delay10KTCYx(255);
Delay10KTCYx(255);
Delay10KTCYx(255);
Delay10KTCYx(255);
LATC = 0x0E;
Delay10KTCYx(255);
Delay10KTCYx(255);
Delay10KTCYx(255);
Delay10KTCYx(255);
}
#endif
}
USBTransaction _USBGetCurrentTransaction(void)
{
USBTransaction transaction;
USBBufferDescriptor *bd;
bd = (USBBufferDescriptor *)(0x200+USTAT);
transaction.endpoint = (USTAT&0x38)>>3; // Transaction endpoint is recorded in USTAT<5:3>
transaction.token = (bd->status & 0x3C) >> 2; // Transaction PID is recorded in BDnSTAT<5:2>
transaction.bd = bd; // Point to the USB SIE buffer descriptor
return transaction;
}
void _USBHandleControlError(void)
{
// Reset EP0 OUT buffer capacity
_USBBD0O.count = EP0_SIZE;
// Stall endpoint 0
_USBBD0O.status = (UOWN|BSTALL);
_USBBD0I.status = (UOWN|BSTALL);
}
void _USBResetEP0InBuffer(void)
{
_USBBD0I.count = 0; // Not sending a byte
_USBBD0I.status = (UOWN|DTS|DTSEN); // Set UOWN, send byte as DATA1
}
void _USBWriteValue(unsigned value, unsigned char bytes)
{
unsigned char index;
// Copy value to EP0 IN buffer 8bits at a time
for (index = 0; index < bytes; index++) {
_USBBD0I.address[index] = (value>>(index*8))&0x00FF;
}
// Return data to SIE through buffer descriptor
_USBBD0I.count = bytes;
_USBBD0I.status = (UOWN|DTS|DTSEN); // Set UOWN, send as DATA1
}
void _USBSendDescriptor(void)
{
unsigned char index;
if (_USBDescriptorBytesLeft <= EP0_SIZE) {
// Copy remaining bytes to EP0 IN buffer
for (index = 0; index < _USBDescriptorBytesLeft; index++) {
_USBBD0I.address[index] = *_USBDescriptorToSend++;
}
// Return data to SIE through buffer descriptor
_USBBD0I.count = _USBDescriptorBytesLeft;
_USBBD0I.status = _USBBD0I.status^DTS; // Toggle the DATAx bit
_USBBD0I.status = (_USBBD0I.status&DTS)|(UOWN|DTSEN); // Preserve DATAx, clear rest, set UOWN and DTSEN
// Not sending a descriptor any more
_USBEngineStatus &= ~USBEngineStatusSendingDescriptor;
_USBDescriptorBytesLeft = 0;
} else {
// Copy a full length of bytes to EP0 IN buffer
for (index = 0; index < EP0_SIZE; index++) {
_USBBD0I.address[index] = *_USBDescriptorToSend++;
}
// Return data to SIE through buffer descriptor
_USBBD0I.count = EP0_SIZE;
_USBBD0I.status = _USBBD0I.status^DTS; // Toggle the DATAx bit
_USBBD0I.status = (_USBBD0I.status&DTS)|(UOWN|DTSEN); // Preserve DATAx, clear rest, set UOWN and DTSEN
// Inform IN token processing to keep sending descriptor
_USBEngineStatus |= USBEngineStatusSendingDescriptor;
_USBDescriptorBytesLeft -= EP0_SIZE;
}
}
void _USBWriteDescriptor(rom const unsigned char *descriptor, unsigned char bytes)
{
// Simply setup system for global descriptor-sending function
_USBDescriptorToSend = descriptor;
_USBDescriptorBytesLeft = bytes;
// Call the function that sends the first set of EP0_SIZE bytes
_USBSendDescriptor();
}
void _USBWriteSingleDescriptor(rom const unsigned char *descriptor)
{
_USBWriteDescriptor(descriptor, descriptor[bLength_OFFSET]);
}
void _USBProcessEP0(USBTransaction *transaction)
{
unsigned char *transactionData;
unsigned char bRequest;
unsigned char bmRequestType;
unsigned wValue;
unsigned wIndex;
unsigned wLength;
transactionData = transaction->bd->address;
switch (transaction->token) { // extract PID bits
case USBTokenSETUP:
// SETUP token processing
bRequest = transactionData[bRequest_OFFSET];
bmRequestType = transactionData[bmRequestType_OFFSET];
wValue = transactionData[wValueL_OFFSET] + (transactionData[wValueH_OFFSET])*0x100;
wIndex = transactionData[wIndexL_OFFSET] + (transactionData[wIndexH_OFFSET])*0x100;
wLength = transactionData[wLengthL_OFFSET] + (transactionData[wLengthH_OFFSET])*0x100;
// Reset OUT endpoint, dequeue any IN packets, and re-enable packet transfers
_USBBD0O.status = ((!(bmRequestType&HOST_TO_DEVICE)&&wValue) ? (UOWN|DTS|DTSEN) : (UOWN|DTSEN)); // Choose correct DATA0/DATA1
_USBBD0O.count = EP0_SIZE;
_USBBD0I.status = DTSEN; // steal UOWN to dequeue IN packets
UCONbits.PKTDIS = 0;
_USBEngineStatus = USBEngineStatusReset;
switch (bRequest) {
case GET_STATUS:
switch (bmRequestType) {
case USB_DEVICE_REQUEST:
_USBWriteValue(_USBDeviceStatus, 2);
break;
case USB_INTERFACE_REQUEST:
if ((_USBDeviceState == USBDeviceStateConfigured) && (wIndex<NUMBER_OF_USB_INTERFACES)) {
_USBWriteValue(0, 2); // Always 0
} else {
_USBHandleControlError();
}
break;
case USB_ENDPOINT_REQUEST:
// Caluculate buffer descriptor status register and move BSTALL bit to bit <0>
if (_USBDeviceState == USBDeviceStateAddressed) {
_USBWriteValue((*(unsigned char *)(0x200+((wIndex&0x000F)*0x08)+((wIndex&0x0080)*0x04))&BSTALL)>>2, 2);
} else if (_USBDeviceState == USBDeviceStateConfigured) {
if (*(&UEP0+(wIndex&0x000F)) & (wIndex&0x80?EPINEN:EPOUTEN)) { // If endpoint is enabled in specified directiong
_USBWriteValue((*(unsigned char *)(0x200+((wIndex&0x000F)*0x08)+((wIndex&0x0080)*0x04))&BSTALL)>>2, 2);
} else {
_USBResetEP0InBuffer();
}
} else {
_USBResetEP0InBuffer();
}
break;
default:
_USBHandleControlError();
break;
}
break;
case CLEAR_FEATURE:
switch (bmRequestType) {
case USB_DEVICE_REQUEST:
switch (wValue) {
case DEVICE_REMOTE_WAKEUP:
_USBDeviceStatus &= ~USBDeviceStatusDeviceRemoteWakeup;
_USBResetEP0InBuffer();
break;
case TEST_MODE:
_USBDeviceStatus &= ~USBDeviceStatusTestMode;
_USBResetEP0InBuffer();
break;
default:
_USBHandleControlError();
break;
}
break;
case USB_ENDPOINT_REQUEST:
if (_USBDeviceState == USBDeviceStateAddressed) {
_USBResetEP0InBuffer();
} else if (_USBDeviceState == USBDeviceStateConfigured) {
if (*(&UEP0+(wIndex&0x000F)) & (wIndex&0x80?EPINEN:EPOUTEN)) { // If endpoint is enabled in specified directiong
*(unsigned char *)(0x200+((wIndex&0x000F)*0x08)+((wIndex&0x0080)*0x04)) = 0x00; // Clear endpoint descriptor
} else {
_USBResetEP0InBuffer();
}
} else {
_USBHandleControlError();
}
break;
case USB_INTERFACE_REQUEST: // Should never happen
default:
_USBHandleControlError();
break;
}
break;
case SET_FEATURE:
switch (bmRequestType) {
case USB_DEVICE_REQUEST:
switch (wValue) {
case DEVICE_REMOTE_WAKEUP:
_USBDeviceStatus |= USBDeviceStatusDeviceRemoteWakeup;
_USBResetEP0InBuffer();
break;
case TEST_MODE:
_USBDeviceStatus |= USBDeviceStatusTestMode;
_USBResetEP0InBuffer();
break;
default:
_USBHandleControlError();
break;
}
break;
case USB_ENDPOINT_REQUEST:
if (_USBDeviceState == USBDeviceStateAddressed) {
_USBResetEP0InBuffer();
} else if (_USBDeviceState == USBDeviceStateConfigured) {
if (*(&UEP0+(wIndex&0x000F)) & (wIndex&0x80?EPINEN:EPOUTEN)) { // If endpoint is enabled in specified directiong
*(unsigned char *)(0x200+((wIndex&0x000F)*0x08)+((wIndex&0x0080)*0x04)) = (UOWN|BSTALL); // Stall endpoint
} else {
_USBResetEP0InBuffer();
}
} else {
_USBHandleControlError();
}
break;
case USB_INTERFACE_REQUEST: // Should never happen
default:
_USBHandleControlError();
break;
}
break;
case SET_ADDRESS:
// Device only
_USBLog(USBLogCodeSetAddress);
if (wValue <= 127) {
_USBEngineStatus |= USBEngineStatusAddressing;
_USBPendingDeviceAddress = wValue;
_USBResetEP0InBuffer();
} else {
_USBHandleControlError();
}
break;
case GET_DESCRIPTOR:
// Device only (bmRequestType)
switch ((wValue>>8)&0x00FF) { // High byte contains descriptor type
case USB_DEVICE_DESCRIPTOR_TYPE:
_USBLog(USBLogCodeGetDeviceDescriptor);
_USBWriteSingleDescriptor(_USBDeviceDescriptor);
break;
case USB_CONFIGURATION_DESCRIPTOR_TYPE:
_USBLog(USBLogCodeGetConfigurationDescriptor);
_USBWriteDescriptor(_USBConfigurationDescriptors[wValue&0x00FF], wLength);
break;
case USB_STRING_DESCRIPTOR_TYPE:
_USBWriteSingleDescriptor(_USBStringDescriptors[wValue]);
break;
default:
_USBHandleControlError();
break;
}
break;
case GET_CONFIGURATION:
// Device only
_USBWriteValue(_USBCurrentConfiguration, 1);
break;
case SET_CONFIGURATION:
// Device only
if (wValue <= NUMBER_OF_USB_CONFIGURATIONS) {
_USBCurrentConfiguration = wValue;
// Disable all endpoints except 0
#warning must update so that other endpoints are to work later
//UEP1 = 0x00;
UEP2 = 0x00;
UEP3 = 0x00;
UEP4 = 0x00;
UEP5 = 0x00;
UEP6 = 0x00;
UEP7 = 0x00;
if (_USBCurrentConfiguration == 0) {
_USBDeviceState |= USBDeviceStateAddressed;
UPDATE_ENUM_STATUS(_USBDeviceState);
} else {
_USBDeviceState = USBDeviceStateConfigured;
UPDATE_ENUM_STATUS(_USBDeviceState);
}
_USBResetEP0InBuffer();
} else {
_USBHandleControlError();
}
break;
case GET_INTERFACE:
// Interface only
if ((_USBDeviceState == USBDeviceStateConfigured) && (wIndex < NUMBER_OF_USB_INTERFACES)) {
if (wValue == 0) { // Only support bAlternateSetting of 0 for EP0
_USBWriteValue(0, 1);
} else {
_USBHandleControlError();
}
} else {
_USBHandleControlError();
}
break;
case SET_INTERFACE:
// Interface only
if ((_USBDeviceState == USBDeviceStateConfigured) && (wIndex < NUMBER_OF_USB_INTERFACES)) {
if (wValue == 0) { // Only support bAlternateSetting of 0 for EP0
_USBResetEP0InBuffer();
} else {
_USBHandleControlError();
}
} else {
_USBHandleControlError();
}
break;
case SET_DESCRIPTOR: // Useless
case SYNCH_FRAME: // Unused
default:
_USBHandleControlError();
break;
}
break;
case USBTokenIN:
// A pending USB address is committed on an IN token
if (_USBEngineStatus & USBEngineStatusAddressing) {
UADDR = _USBPendingDeviceAddress;
switch (UADDR) {
case 0:
_USBDeviceState |= USBDeviceStateInitialized;
UPDATE_ENUM_STATUS(_USBDeviceState);
break;
default:
_USBDeviceState = USBDeviceStateAddressed;
UPDATE_ENUM_STATUS(_USBDeviceState);
break;
}
_USBEngineStatus &= ~USBEngineStatusAddressing;
}
// Continue sending any unfinished descriptor transmission
else if (_USBEngineStatus & USBEngineStatusSendingDescriptor) {
_USBSendDescriptor();
}
break;
case USBTokenOUT:
// Any OUT token means no more IN tokens are necessary
_USBResetEP0InBuffer();
// Also reset the OUT Buffer; no OUT tokens required for EP0
_USBBD0O.status = (UOWN|DTSEN); // SIE owns BD, data toggle enabled
_USBBD0O.count = EP0_SIZE; // Allow to receive full packet
break;
default:
_USBHandleControlError();
break;
}
}
void _USBProcessEP1(USBTransaction *transaction)
{
unsigned char index;
switch (transaction->token) {
case USBTokenIN:
_USBBD1I.status = _USBBD1I.status^DTS;
_USBBD1I.status = (_USBBD1I.status&DTS)|(UOWN|DTSEN);
_USBBD1I.count = 8;
for (index = 0; index < 8; index++) {
_USBEP1InBuffer[index] = PORTC+index;
}
break;
default:
_USBBD1I.status = (_USBBD1I.status&DTS)|(UOWN|DTSEN);
_USBBD1I.count = 0;
break;
}
}
void _USBEngineReset(void)
{
#ifdef MINCH_RESET
_USBCurrentConfiguration = 0;
UIRbits.TRNIF = 0; // clear TRNIF four times to clear out the USTAT FIFO
UIRbits.TRNIF = 0;
UIRbits.TRNIF = 0;
UIRbits.TRNIF = 0;
UEP0 = 0x00; // clear all EP control registers to disable all endpoints
UEP1 = 0x00;
UEP2 = 0x00;
UEP3 = 0x00;
UEP4 = 0x00;
UEP5 = 0x00;
UEP6 = 0x00;
UEP7 = 0x00;
_USBBD0O.count = EP0_SIZE;
_USBBD0O.address = _USBEP0OutBuffer; // EP0 OUT gets a buffer
_USBBD0O.status = 0x88; // set UOWN bit (USB can write)
_USBBD0I.address = _USBEP0InBuffer; // EP0 IN gets a buffer
_USBBD0I.status = 0x08; // clear UOWN bit (MCU can write)
UADDR = 0x00; // set USB Address to 0
UIR = 0x00; // clear all the USB interrupt flags
UEP0 = 0x16; // EP0 is a control pipe and requires an ACK
UEP1 = 0x1A; // EP1 is an IN pipe and requires ACK
UEIE = 0xFF; // enable all error interrupts
_USBDeviceState = USBDeviceStateReset;
_USBDeviceStatus = 0x01; // self powered, remote wakeup disabled
UPDATE_ENUM_STATUS(_USBDeviceState);
#else
unsigned char *index;
// Clear USTAT FIFO
UIRbits.TRNIF = 0;
UIRbits.TRNIF = 0;
UIRbits.TRNIF = 0;
UIRbits.TRNIF = 0;
// Disable all endpoints
for (index = (unsigned char *)&UEP0;
index <= (unsigned char *)&UEP7;
index++) {
*index = 0x00;
}
// Reset endpoint buffer descriptors
_USBConfigureBufferDescriptors();
// Reset general USB
_USBDeviceState = USBDeviceStateReset;
_USBEngineStatus = USBEngineStatusReset;
_USBDeviceStatus = 0x01;
_USBCurrentConfiguration = 0;
UADDR = 0;
// Clear all USB interrupts, enable all USB error interrupts
UEIE = 0x00;
UEIR = 0x00;
UIR = 0x00;
UEIE = 0xFF;
// Enable EP0
UEP0 = 0x16; // SETUP endpoint, not stalled
UEP1 = 0x1A; // IN endpoint, not stalled
#endif
}
void _USBConfigureBufferDescriptors(void)
{
_USBBD0I.status = (DTSEN);
// IN buffer count is left alone
_USBBD0I.address = _USBEP0InBuffer;
_USBBD0O.status = (UOWN|DTSEN); // SIE owns BD, data toggle enabled
_USBBD0O.count = EP0_SIZE; // Allow to receive full packet
_USBBD0I.address = _USBEP0OutBuffer;
}
void _USBSetup()
{
// Setup
LATC = 0x00;
TRISC = 0xF0;
// Set device variables
_USBDeviceState = USBDeviceStateReset; // Reset USB state
_USBEngineStatus = USBEngineStatusReset;
_USBDeviceStatus = 0x01;
_USBCurrentConfiguration = 0;
UPDATE_ENUM_STATUS(_USBDeviceState);
// General USB setup
UEIE = 0x00; // Disable and clear all USB interrupts
UEIR = 0x00;
UIE = 0x00;
UIR = 0x00;
UCON = 0x00; // Resume signaling off, suspend mode off, USB off
UCFG = 0x14; // Eye off, pull ups enabled, full speed, ping pong all
UADDR = 0x00; // Reset USB address
// Configure endpoint buffer descriptors
_USBConfigureBufferDescriptors();
// Configure endpoints
UEP0 = 0x16; // SETUP endpoint, not stalled
UEP1 = 0x1A;
// Enable USB
Delay1KTCYx(24); // Wait 2ms for clock to stabilize
UCONbits.USBEN = 1; // then enable USB
while (UCONbits.SE0); // Wait for first SE0 to end (marks bus power)
// Configure USB interrupts
UIEbits.ACTVIE = 1;
UIEbits.IDLEIE = 1;
UIEbits.SOFIE = 1;
UIEbits.STALLIE = 1;
UIEbits.URSTIE = 1;
UIEbits.TRNIE = 1; // Enable transaction complete interrupts
PIE2bits.USBIE = 1; // Enable USB interrupts
// Configure USB error interrupts
UEIEbits.BTOEE = 1;
UEIEbits.BTSEE = 1;
UEIEbits.CRC16EE = 1;
UEIEbits.CRC5EE = 1;
UEIEbits.DFN8EE = 1;
UEIEbits.PIDEE = 1;
UIEbits.UERRIE = 1; // Enable USB error interrupts
// Enable global interrupts
INTCONbits.PEIE = 1;
INTCONbits.GIE = 1;
_USBDeviceState = USBDeviceStateInitialized; // Set USB state to initialized
UPDATE_ENUM_STATUS(_USBDeviceState);
}
void Setup()
{
_USBSetup();
}
void main(void) {
unsigned char index;
Setup();
while (1) {
for (index = 0; index < 25; index++) {
Delay10KTCYx(255);
}
_USBDisplayLog();
}
}