-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathusbhw.c
812 lines (647 loc) · 20.8 KB
/
usbhw.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
/*----------------------------------------------------------------------------
* U S B - K e r n e l
*----------------------------------------------------------------------------
* Name: usbhw.c
* Purpose: USB Hardware Layer Module for NXP's LPC17xx MCU
* Version: V1.20
*----------------------------------------------------------------------------
* This software is supplied "AS IS" without any warranties, express,
* implied or statutory, including but not limited to the implied
* warranties of fitness for purpose, satisfactory quality and
* noninfringement. Keil extends you a royalty-free right to reproduce
* and distribute executable files created using this software for use
* on NXP Semiconductors LPC family microcontroller devices only. Nothing
* else gives you the right to use this software.
*
* Copyright (c) 2009 Keil - An ARM Company. All rights reserved.
*----------------------------------------------------------------------------
* History:
* V1.20 Added USB_ClearEPBuf
* V1.00 Initial Version
*----------------------------------------------------------------------------*/
#include "LPC17xx.h" /* LPC17xx definitions */
#include "type.h"
#include "usb.h"
#include "usbcfg.h"
#include "usbreg.h"
#include "usbhw.h"
#include "usbcore.h"
#include "usbuser.h"
#pragma diag_suppress 1441
#define EP_MSK_CTRL 0x0001 /* Control Endpoint Logical Address Mask */
#define EP_MSK_BULK 0xC924 /* Bulk Endpoint Logical Address Mask */
#define EP_MSK_INT 0x4492 /* Interrupt Endpoint Logical Address Mask */
#define EP_MSK_ISO 0x1248 /* Isochronous Endpoint Logical Address Mask */
#if USB_DMA
#pragma arm section zidata = "USB_RAM"
uint32_t UDCA[USB_EP_NUM]; /* UDCA in USB RAM */
uint32_t DD_NISO_Mem[4*DD_NISO_CNT]; /* Non-Iso DMA Descriptor Memory */
uint32_t DD_ISO_Mem [5*DD_ISO_CNT]; /* Iso DMA Descriptor Memory */
#pragma arm section zidata
uint32_t udca[USB_EP_NUM]; /* UDCA saved values */
uint32_t DDMemMap[2]; /* DMA Descriptor Memory Usage */
#endif
/*
* Get Endpoint Physical Address
* Parameters: EPNum: Endpoint Number
* EPNum.0..3: Address
* EPNum.7: Dir
* Return Value: Endpoint Physical Address
*/
uint32_t EPAdr (uint32_t EPNum) {
uint32_t val;
val = (EPNum & 0x0F) << 1;
if (EPNum & 0x80) {
val += 1;
}
return (val);
}
/*
* Write Command
* Parameters: cmd: Command
* Return Value: None
*/
void WrCmd (uint32_t cmd) {
LPC_USB->DevIntClr = CCEMTY_INT;
LPC_USB->CmdCode = cmd;
while ((LPC_USB->DevIntSt & CCEMTY_INT) == 0);
}
/*
* Write Command Data
* Parameters: cmd: Command
* val: Data
* Return Value: None
*/
void WrCmdDat (uint32_t cmd, uint32_t val) {
LPC_USB->DevIntClr = CCEMTY_INT;
LPC_USB->CmdCode = cmd;
while ((LPC_USB->DevIntSt & CCEMTY_INT) == 0);
LPC_USB->DevIntClr = CCEMTY_INT;
LPC_USB->CmdCode = val;
while ((LPC_USB->DevIntSt & CCEMTY_INT) == 0);
}
/*
* Write Command to Endpoint
* Parameters: cmd: Command
* val: Data
* Return Value: None
*/
void WrCmdEP (uint32_t EPNum, uint32_t cmd){
LPC_USB->DevIntClr = CCEMTY_INT;
LPC_USB->CmdCode = CMD_SEL_EP(EPAdr(EPNum));
while ((LPC_USB->DevIntSt & CCEMTY_INT) == 0);
LPC_USB->DevIntClr = CCEMTY_INT;
LPC_USB->CmdCode = cmd;
while ((LPC_USB->DevIntSt & CCEMTY_INT) == 0);
}
/*
* Read Command Data
* Parameters: cmd: Command
* Return Value: Data Value
*/
uint32_t RdCmdDat (uint32_t cmd) {
LPC_USB->DevIntClr = CCEMTY_INT | CDFULL_INT;
LPC_USB->CmdCode = cmd;
while ((LPC_USB->DevIntSt & CDFULL_INT) == 0);
return (LPC_USB->CmdData);
}
/*
* USB Initialize Function
* Called by the User to initialize USB
* Return Value: None
*/
void USB_Init (void) {
LPC_PINCON->PINSEL1 &= ~((3<<26)|(3<<28)); /* P0.29 D+, P0.30 D- */
LPC_PINCON->PINSEL1 |= ((1<<26)|(1<<28)); /* PINSEL1 26.27, 28.29 = 01 */
LPC_PINCON->PINSEL3 &= ~((3<< 4)|(3<<28)); /* P1.18 GoodLink, P1.30 VBUS */
LPC_PINCON->PINSEL3 |= ((1<< 4)|(2<<28)); /* PINSEL3 4.5 = 01, 28.29 = 10 */
LPC_PINCON->PINSEL4 &= ~((3<<18) ); /* P2.9 SoftConnect */
LPC_PINCON->PINSEL4 |= ((1<<18) ); /* PINSEL4 18.19 = 01 */
LPC_SC->PCONP |= (1UL<<31); /* USB PCLK -> enable USB Per. */
LPC_USB->USBClkCtrl = 0x12; /* Dev, AHB clock enable */
while ((LPC_USB->USBClkSt & 0x12) != 0x12);
NVIC_EnableIRQ(USB_IRQn); /* enable USB interrupt */
USB_Reset();
USB_SetAddress(0);
}
/*
* USB Connect Function
* Called by the User to Connect/Disconnect USB
* Parameters: con: Connect/Disconnect
* Return Value: None
*/
void USB_Connect (uint32_t con) {
WrCmdDat(CMD_SET_DEV_STAT, DAT_WR_BYTE(con ? DEV_CON : 0));
}
/*
* USB Reset Function
* Called automatically on USB Reset
* Return Value: None
*/
void USB_Reset (void) {
#if USB_DMA
uint32_t n;
#endif
LPC_USB->EpInd = 0;
LPC_USB->MaxPSize = USB_MAX_PACKET0;
LPC_USB->EpInd = 1;
LPC_USB->MaxPSize = USB_MAX_PACKET0;
while ((LPC_USB->DevIntSt & EP_RLZED_INT) == 0);
LPC_USB->EpIntClr = 0xFFFFFFFF;
LPC_USB->EpIntEn = 0xFFFFFFFF ^ USB_DMA_EP;
LPC_USB->DevIntClr = 0xFFFFFFFF;
LPC_USB->DevIntEn = DEV_STAT_INT | EP_SLOW_INT |
(USB_SOF_EVENT ? FRAME_INT : 0) |
(USB_ERROR_EVENT ? ERR_INT : 0);
#if USB_DMA
LPC_USB->UDCAH = USB_RAM_ADR;
LPC_USB->DMARClr = 0xFFFFFFFF;
LPC_USB->EpDMADis = 0xFFFFFFFF;
LPC_USB->EpDMAEn = USB_DMA_EP;
LPC_USB->EoTIntClr = 0xFFFFFFFF;
LPC_USB->NDDRIntClr = 0xFFFFFFFF;
LPC_USB->SysErrIntClr = 0xFFFFFFFF;
LPC_USB->DMAIntEn = 0x00000007;
DDMemMap[0] = 0x00000000;
DDMemMap[1] = 0x00000000;
for (n = 0; n < USB_EP_NUM; n++) {
udca[n] = 0;
UDCA[n] = 0;
}
#endif
}
/*
* USB Suspend Function
* Called automatically on USB Suspend
* Return Value: None
*/
void USB_Suspend (void) {
/* Performed by Hardware */
}
/*
* USB Resume Function
* Called automatically on USB Resume
* Return Value: None
*/
void USB_Resume (void) {
/* Performed by Hardware */
}
/*
* USB Remote Wakeup Function
* Called automatically on USB Remote Wakeup
* Return Value: None
*/
void USB_WakeUp (void) {
if (USB_DeviceStatus & USB_GETSTATUS_REMOTE_WAKEUP) {
WrCmdDat(CMD_SET_DEV_STAT, DAT_WR_BYTE(DEV_CON));
}
}
/*
* USB Remote Wakeup Configuration Function
* Parameters: cfg: Enable/Disable
* Return Value: None
*/
void USB_WakeUpCfg (uint32_t cfg) {
/* Not needed */
}
/*
* USB Set Address Function
* Parameters: adr: USB Address
* Return Value: None
*/
void USB_SetAddress (uint32_t adr) {
WrCmdDat(CMD_SET_ADDR, DAT_WR_BYTE(DEV_EN | adr)); /* Don't wait for next */
WrCmdDat(CMD_SET_ADDR, DAT_WR_BYTE(DEV_EN | adr)); /* Setup Status Phase */
}
/*
* USB Configure Function
* Parameters: cfg: Configure/Deconfigure
* Return Value: None
*/
void USB_Configure (uint32_t cfg) {
WrCmdDat(CMD_CFG_DEV, DAT_WR_BYTE(cfg ? CONF_DVICE : 0));
LPC_USB->ReEp = 0x00000003;
while ((LPC_USB->DevIntSt & EP_RLZED_INT) == 0);
LPC_USB->DevIntClr = EP_RLZED_INT;
}
/*
* Configure USB Endpoint according to Descriptor
* Parameters: pEPD: Pointer to Endpoint Descriptor
* Return Value: None
*/
void USB_ConfigEP (USB_ENDPOINT_DESCRIPTOR *pEPD) {
uint32_t num;
num = EPAdr(pEPD->bEndpointAddress);
LPC_USB->ReEp |= (1 << num);
LPC_USB->EpInd = num;
LPC_USB->MaxPSize = pEPD->wMaxPacketSize;
while ((LPC_USB->DevIntSt & EP_RLZED_INT) == 0);
LPC_USB->DevIntClr = EP_RLZED_INT;
}
/*
* Set Direction for USB Control Endpoint
* Parameters: dir: Out (dir == 0), In (dir <> 0)
* Return Value: None
*/
void USB_DirCtrlEP (uint32_t dir) {
/* Not needed */
}
/*
* Enable USB Endpoint
* Parameters: EPNum: Endpoint Number
* EPNum.0..3: Address
* EPNum.7: Dir
* Return Value: None
*/
void USB_EnableEP (uint32_t EPNum) {
WrCmdDat(CMD_SET_EP_STAT(EPAdr(EPNum)), DAT_WR_BYTE(0));
}
/*
* Disable USB Endpoint
* Parameters: EPNum: Endpoint Number
* EPNum.0..3: Address
* EPNum.7: Dir
* Return Value: None
*/
void USB_DisableEP (uint32_t EPNum) {
WrCmdDat(CMD_SET_EP_STAT(EPAdr(EPNum)), DAT_WR_BYTE(EP_STAT_DA));
}
/*
* Reset USB Endpoint
* Parameters: EPNum: Endpoint Number
* EPNum.0..3: Address
* EPNum.7: Dir
* Return Value: None
*/
void USB_ResetEP (uint32_t EPNum) {
WrCmdDat(CMD_SET_EP_STAT(EPAdr(EPNum)), DAT_WR_BYTE(0));
}
/*
* Set Stall for USB Endpoint
* Parameters: EPNum: Endpoint Number
* EPNum.0..3: Address
* EPNum.7: Dir
* Return Value: None
*/
void USB_SetStallEP (uint32_t EPNum) {
WrCmdDat(CMD_SET_EP_STAT(EPAdr(EPNum)), DAT_WR_BYTE(EP_STAT_ST));
}
/*
* Clear Stall for USB Endpoint
* Parameters: EPNum: Endpoint Number
* EPNum.0..3: Address
* EPNum.7: Dir
* Return Value: None
*/
void USB_ClrStallEP (uint32_t EPNum) {
WrCmdDat(CMD_SET_EP_STAT(EPAdr(EPNum)), DAT_WR_BYTE(0));
}
/*
* Clear USB Endpoint Buffer
* Parameters: EPNum: Endpoint Number
* EPNum.0..3: Address
* EPNum.7: Dir
* Return Value: None
*/
void USB_ClearEPBuf (uint32_t EPNum) {
WrCmdEP(EPNum, CMD_CLR_BUF);
}
/*
* Read USB Endpoint Data
* Parameters: EPNum: Endpoint Number
* EPNum.0..3: Address
* EPNum.7: Dir
* pData: Pointer to Data Buffer
* Return Value: Number of bytes read
*/
uint32_t USB_ReadEP (uint32_t EPNum, uint8_t *pData) {
uint32_t cnt, n;
LPC_USB->Ctrl = ((EPNum & 0x0F) << 2) | CTRL_RD_EN;
do {
cnt = LPC_USB->RxPLen;
} while ((cnt & PKT_RDY) == 0);
cnt &= PKT_LNGTH_MASK;
for (n = 0; n < (cnt + 3) / 4; n++) {
*((__packed uint32_t *)pData) = LPC_USB->RxData;
pData += 4;
}
LPC_USB->Ctrl = 0;
if (((EP_MSK_ISO >> EPNum) & 1) == 0) { /* Non-Isochronous Endpoint */
WrCmdEP(EPNum, CMD_CLR_BUF);
}
return (cnt);
}
/*
* Write USB Endpoint Data
* Parameters: EPNum: Endpoint Number
* EPNum.0..3: Address
* EPNum.7: Dir
* pData: Pointer to Data Buffer
* cnt: Number of bytes to write
* Return Value: Number of bytes written
*/
uint32_t USB_WriteEP (uint32_t EPNum, uint8_t *pData, uint32_t cnt) {
uint32_t n;
LPC_USB->Ctrl = ((EPNum & 0x0F) << 2) | CTRL_WR_EN;
LPC_USB->TxPLen = cnt;
for (n = 0; n < (cnt + 3) / 4; n++) {
LPC_USB->TxData = *((__packed uint32_t *)pData);
pData += 4;
}
LPC_USB->Ctrl = 0;
WrCmdEP(EPNum, CMD_VALID_BUF);
return (cnt);
}
#if USB_DMA
/* DMA Descriptor Memory Layout */
const uint32_t DDAdr[2] = { DD_NISO_ADR, DD_ISO_ADR };
const uint32_t DDSz [2] = { 16, 20 };
/*
* Setup USB DMA Transfer for selected Endpoint
* Parameters: EPNum: Endpoint Number
* pDD: Pointer to DMA Descriptor
* Return Value: TRUE - Success, FALSE - Error
*/
uint32_t USB_DMA_Setup(uint32_t EPNum, USB_DMA_DESCRIPTOR *pDD) {
uint32_t num, ptr, nxt, iso, n;
iso = pDD->Cfg.Type.IsoEP; /* Iso or Non-Iso Descriptor */
num = EPAdr(EPNum); /* Endpoint's Physical Address */
ptr = 0; /* Current Descriptor */
nxt = udca[num]; /* Initial Descriptor */
while (nxt) { /* Go through Descriptor List */
ptr = nxt; /* Current Descriptor */
if (!pDD->Cfg.Type.Link) { /* Check for Linked Descriptors */
n = (ptr - DDAdr[iso]) / DDSz[iso]; /* Descriptor Index */
DDMemMap[iso] &= ~(1 << n); /* Unmark Memory Usage */
}
nxt = *((uint32_t *)ptr); /* Next Descriptor */
}
for (n = 0; n < 32; n++) { /* Search for available Memory */
if ((DDMemMap[iso] & (1 << n)) == 0) {
break; /* Memory found */
}
}
if (n == 32) return (FALSE); /* Memory not available */
DDMemMap[iso] |= 1 << n; /* Mark Memory Usage */
nxt = DDAdr[iso] + n * DDSz[iso]; /* Next Descriptor */
if (ptr && pDD->Cfg.Type.Link) {
*((uint32_t *)(ptr + 0)) = nxt; /* Link in new Descriptor */
*((uint32_t *)(ptr + 4)) |= 0x00000004; /* Next DD is Valid */
} else {
udca[num] = nxt; /* Save new Descriptor */
UDCA[num] = nxt; /* Update UDCA in USB */
}
/* Fill in DMA Descriptor */
*(((uint32_t *)nxt)++) = 0; /* Next DD Pointer */
*(((uint32_t *)nxt)++) = pDD->Cfg.Type.ATLE |
(pDD->Cfg.Type.IsoEP << 4) |
(pDD->MaxSize << 5) |
(pDD->BufLen << 16);
*(((uint32_t *)nxt)++) = pDD->BufAdr;
*(((uint32_t *)nxt)++) = pDD->Cfg.Type.LenPos << 8;
if (iso) {
*((uint32_t *)nxt) = pDD->InfoAdr;
}
return (TRUE); /* Success */
}
/*
* Enable USB DMA Endpoint
* Parameters: EPNum: Endpoint Number
* EPNum.0..3: Address
* EPNum.7: Dir
* Return Value: None
*/
void USB_DMA_Enable (uint32_t EPNum) {
LPC_USB->EpDMAEn = 1 << EPAdr(EPNum);
}
/*
* Disable USB DMA Endpoint
* Parameters: EPNum: Endpoint Number
* EPNum.0..3: Address
* EPNum.7: Dir
* Return Value: None
*/
void USB_DMA_Disable (uint32_t EPNum) {
LPC_USB->EpDMADis = 1 << EPAdr(EPNum);
}
/*
* Get USB DMA Endpoint Status
* Parameters: EPNum: Endpoint Number
* EPNum.0..3: Address
* EPNum.7: Dir
* Return Value: DMA Status
*/
uint32_t USB_DMA_Status (uint32_t EPNum) {
uint32_t ptr, val;
ptr = UDCA[EPAdr(EPNum)]; /* Current Descriptor */
if (ptr == 0)
return (USB_DMA_INVALID);
val = *((uint32_t *)(ptr + 3*4)); /* Status Information */
switch ((val >> 1) & 0x0F) {
case 0x00: /* Not serviced */
return (USB_DMA_IDLE);
case 0x01: /* Being serviced */
return (USB_DMA_BUSY);
case 0x02: /* Normal Completition */
return (USB_DMA_DONE);
case 0x03: /* Data Under Run */
return (USB_DMA_UNDER_RUN);
case 0x08: /* Data Over Run */
return (USB_DMA_OVER_RUN);
case 0x09: /* System Error */
return (USB_DMA_ERROR);
}
return (USB_DMA_UNKNOWN);
}
/*
* Get USB DMA Endpoint Current Buffer Address
* Parameters: EPNum: Endpoint Number
* EPNum.0..3: Address
* EPNum.7: Dir
* Return Value: DMA Address (or -1 when DMA is Invalid)
*/
uint32_t USB_DMA_BufAdr (uint32_t EPNum) {
uint32_t ptr, val;
ptr = UDCA[EPAdr(EPNum)]; /* Current Descriptor */
if (ptr == 0)
{
return ((uint32_t)(-1)); /* DMA Invalid */
}
val = *((uint32_t *)(ptr + 2*4)); /* Buffer Address */
return (val); /* Current Address */
}
/*
* Get USB DMA Endpoint Current Buffer Count
* Number of transfered Bytes or Iso Packets
* Parameters: EPNum: Endpoint Number
* EPNum.0..3: Address
* EPNum.7: Dir
* Return Value: DMA Count (or -1 when DMA is Invalid)
*/
uint32_t USB_DMA_BufCnt (uint32_t EPNum) {
uint32_t ptr, val;
ptr = UDCA[EPAdr(EPNum)]; /* Current Descriptor */
if (ptr == 0)
{
return ((uint32_t)(-1)); /* DMA Invalid */
}
val = *((uint32_t *)(ptr + 3*4)); /* Status Information */
return (val >> 16); /* Current Count */
}
#endif /* USB_DMA */
/*
* Get USB Last Frame Number
* Parameters: None
* Return Value: Frame Number
*/
uint32_t USB_GetFrame (void) {
uint32_t val;
WrCmd(CMD_RD_FRAME);
val = RdCmdDat(DAT_RD_FRAME);
val = val | (RdCmdDat(DAT_RD_FRAME) << 8);
return (val);
}
/*
* USB Interrupt Service Routine
*/
void USB_IRQHandler (void) {
uint32_t disr, val, n, m;
uint32_t episr, episrCur;
disr = LPC_USB->DevIntSt; /* Device Interrupt Status */
/* Device Status Interrupt (Reset, Connect change, Suspend/Resume) */
if (disr & DEV_STAT_INT) {
LPC_USB->DevIntClr = DEV_STAT_INT;
WrCmd(CMD_GET_DEV_STAT);
val = RdCmdDat(DAT_GET_DEV_STAT); /* Device Status */
if (val & DEV_RST) { /* Reset */
USB_Reset();
#if USB_RESET_EVENT
USB_Reset_Event();
#endif
}
if (val & DEV_CON_CH) { /* Connect change */
#if USB_POWER_EVENT
USB_Power_Event(val & DEV_CON);
#endif
}
if (val & DEV_SUS_CH) { /* Suspend/Resume */
if (val & DEV_SUS) { /* Suspend */
USB_Suspend();
#if USB_SUSPEND_EVENT
USB_Suspend_Event();
#endif
} else { /* Resume */
USB_Resume();
#if USB_RESUME_EVENT
USB_Resume_Event();
#endif
}
}
goto isr_end;
}
#if USB_SOF_EVENT
/* Start of Frame Interrupt */
if (disr & FRAME_INT) {
USB_SOF_Event();
}
#endif
#if USB_ERROR_EVENT
/* Error Interrupt */
if (disr & ERR_INT) {
WrCmd(CMD_RD_ERR_STAT);
val = RdCmdDat(DAT_RD_ERR_STAT);
USB_Error_Event(val);
}
#endif
/* Endpoint's Slow Interrupt */
if (disr & EP_SLOW_INT) {
episrCur = 0;
episr = LPC_USB->EpIntSt;
for (n = 0; n < USB_EP_NUM; n++) { /* Check All Endpoints */
if (episr == episrCur) break; /* break if all EP interrupts handled */
if (episr & (1 << n)) {
episrCur |= (1 << n);
m = n >> 1;
LPC_USB->EpIntClr = (1 << n);
while ((LPC_USB->DevIntSt & CDFULL_INT) == 0);
val = LPC_USB->CmdData;
if ((n & 1) == 0) { /* OUT Endpoint */
if (n == 0) { /* Control OUT Endpoint */
if (val & EP_SEL_STP) { /* Setup Packet */
if (USB_P_EP[0]) {
USB_P_EP[0](USB_EVT_SETUP);
continue;
}
}
}
if (USB_P_EP[m]) {
USB_P_EP[m](USB_EVT_OUT);
}
} else { /* IN Endpoint */
if (USB_P_EP[m]) {
USB_P_EP[m](USB_EVT_IN);
}
}
}
}
LPC_USB->DevIntClr = EP_SLOW_INT;
}
#if USB_DMA
if (LPC_USB->DMAIntSt & 0x00000001) { /* End of Transfer Interrupt */
val = LPC_USB->EoTIntSt;
for (n = 2; n < USB_EP_NUM; n++) { /* Check All Endpoints */
if (val & (1 << n)) {
m = n >> 1;
if ((n & 1) == 0) { /* OUT Endpoint */
if (USB_P_EP[m]) {
USB_P_EP[m](USB_EVT_OUT_DMA_EOT);
}
} else { /* IN Endpoint */
if (USB_P_EP[m]) {
USB_P_EP[m](USB_EVT_IN_DMA_EOT);
}
}
}
}
LPC_USB->EoTIntClr = val;
}
if (LPC_USB->DMAIntSt & 0x00000002) { /* New DD Request Interrupt */
val = LPC_USB->NDDRIntSt;
for (n = 2; n < USB_EP_NUM; n++) { /* Check All Endpoints */
if (val & (1 << n)) {
m = n >> 1;
if ((n & 1) == 0) { /* OUT Endpoint */
if (USB_P_EP[m]) {
USB_P_EP[m](USB_EVT_OUT_DMA_NDR);
}
} else { /* IN Endpoint */
if (USB_P_EP[m]) {
USB_P_EP[m](USB_EVT_IN_DMA_NDR);
}
}
}
}
LPC_USB->NDDRIntClr = val;
}
if (LPC_USB->DMAIntSt & 0x00000004) { /* System Error Interrupt */
val = LPC_USB->SysErrIntSt;
for (n = 2; n < USB_EP_NUM; n++) { /* Check All Endpoints */
if (val & (1 << n)) {
m = n >> 1;
if ((n & 1) == 0) { /* OUT Endpoint */
if (USB_P_EP[m]) {
USB_P_EP[m](USB_EVT_OUT_DMA_ERR);
}
} else { /* IN Endpoint */
if (USB_P_EP[m]) {
USB_P_EP[m](USB_EVT_IN_DMA_ERR);
}
}
}
}
LPC_USB->SysErrIntClr = val;
}
#endif /* USB_DMA */
isr_end:
return;
}