1 /*----------------------------------------------------------------------------
3 *----------------------------------------------------------------------------
5 * Purpose: USB Hardware Layer Module for NXP's LPC17xx MCU
7 *----------------------------------------------------------------------------
8 * This software is supplied "AS IS" without any warranties, express,
9 * implied or statutory, including but not limited to the implied
10 * warranties of fitness for purpose, satisfactory quality and
11 * noninfringement. Keil extends you a royalty-free right to reproduce
12 * and distribute executable files created using this software for use
13 * on NXP Semiconductors LPC family microcontroller devices only. Nothing
14 * else gives you the right to use this software.
16 * Copyright (c) 2009 Keil - An ARM Company. All rights reserved.
17 *----------------------------------------------------------------------------
19 * V1.20 Added USB_ClearEPBuf
20 * V1.00 Initial Version
21 *----------------------------------------------------------------------------*/
22 #include <board.h> /* LPC17xx definitions */
30 #if defined ( __CC_ARM__ )
31 #pragma diag_suppress 1441
35 #define EP_MSK_CTRL 0x0001 /* Control Endpoint Logical Address Mask */
36 #define EP_MSK_BULK 0xC924 /* Bulk Endpoint Logical Address Mask */
37 #define EP_MSK_INT 0x4492 /* Interrupt Endpoint Logical Address Mask */
38 #define EP_MSK_ISO 0x1248 /* Isochronous Endpoint Logical Address Mask */
43 #pragma arm section zidata = "USB_RAM"
44 uint32_t UDCA[USB_EP_NUM]; /* UDCA in USB RAM */
45 uint32_t DD_NISO_Mem[4*DD_NISO_CNT]; /* Non-Iso DMA Descriptor Memory */
46 uint32_t DD_ISO_Mem [5*DD_ISO_CNT]; /* Iso DMA Descriptor Memory */
47 #pragma arm section zidata
48 uint32_t udca[USB_EP_NUM]; /* UDCA saved values */
50 uint32_t DDMemMap[2]; /* DMA Descriptor Memory Usage */
56 * Get Endpoint Physical Address
57 * Parameters: EPNum: Endpoint Number
60 * Return Value: Endpoint Physical Address
63 uint32_t EPAdr (uint32_t EPNum) {
66 val = (EPNum & 0x0F) << 1;
76 * Parameters: cmd: Command
80 void WrCmd (uint32_t cmd) {
82 USB->USBDevIntClr = CCEMTY_INT;
83 USB->USBCmdCode = cmd;
84 while ((USB->USBDevIntSt & CCEMTY_INT) == 0);
90 * Parameters: cmd: Command
95 void WrCmdDat (uint32_t cmd, uint32_t val) {
97 USB->USBDevIntClr = CCEMTY_INT;
98 USB->USBCmdCode = cmd;
99 while ((USB->USBDevIntSt & CCEMTY_INT) == 0);
100 USB->USBDevIntClr = CCEMTY_INT;
101 USB->USBCmdCode = val;
102 while ((USB->USBDevIntSt & CCEMTY_INT) == 0);
107 * Write Command to Endpoint
108 * Parameters: cmd: Command
113 void WrCmdEP (uint32_t EPNum, uint32_t cmd){
115 USB->USBDevIntClr = CCEMTY_INT;
116 USB->USBCmdCode = CMD_SEL_EP(EPAdr(EPNum));
117 while ((USB->USBDevIntSt & CCEMTY_INT) == 0);
118 USB->USBDevIntClr = CCEMTY_INT;
119 USB->USBCmdCode = cmd;
120 while ((USB->USBDevIntSt & CCEMTY_INT) == 0);
126 * Parameters: cmd: Command
127 * Return Value: Data Value
130 uint32_t RdCmdDat (uint32_t cmd) {
132 USB->USBDevIntClr = CCEMTY_INT | CDFULL_INT;
133 USB->USBCmdCode = cmd;
134 while ((USB->USBDevIntSt & CDFULL_INT) == 0);
135 return (USB->USBCmdData);
140 * USB Initialize Function
141 * Called by the User to initialize USB
145 void USB_Init (void) {
147 PINCON->PINSEL1 &= ~((3<<26)|(3<<28)); /* P0.29 D+, P0.30 D- */
148 PINCON->PINSEL1 |= ((1<<26)|(1<<28)); /* PINSEL1 26.27, 28.29 = 01 */
150 PINCON->PINSEL3 &= ~((3<< 4)|(3<<28)); /* P1.18 GoodLink, P1.30 VBUS */
151 PINCON->PINSEL3 |= ((1<< 4)|(2<<28)); /* PINSEL3 4.5 = 01, 28.29 = 10 */
153 PINCON->PINSEL4 &= ~((3<<18) ); /* P2.9 SoftConnect */
154 PINCON->PINSEL4 |= ((1<<18) ); /* PINSEL4 18.19 = 01 */
156 SC->PCONP |= (1UL<<31); /* USB PCLK -> enable USB Per. */
158 USB->USBClkCtrl = 0x1A; /* Dev, PortSel, AHB clock enable */
159 while ((USB->USBClkSt & 0x1A) != 0x1A);
161 NVIC_EnableIRQ(USB_IRQn); /* enable USB interrupt */
169 * USB Connect Function
170 * Called by the User to Connect/Disconnect USB
171 * Parameters: con: Connect/Disconnect
175 void USB_Connect (uint32_t con) {
176 WrCmdDat(CMD_SET_DEV_STAT, DAT_WR_BYTE(con ? DEV_CON : 0));
182 * Called automatically on USB Reset
186 void USB_Reset (void) {
192 USB->USBMaxPSize = USB_MAX_PACKET0;
194 USB->USBMaxPSize = USB_MAX_PACKET0;
195 while ((USB->USBDevIntSt & EP_RLZED_INT) == 0);
197 USB->USBEpIntClr = 0xFFFFFFFF;
198 USB->USBEpIntEn = 0xFFFFFFFF ^ USB_DMA_EP;
199 USB->USBDevIntClr = 0xFFFFFFFF;
200 USB->USBDevIntEn = DEV_STAT_INT | EP_SLOW_INT |
201 (USB_SOF_EVENT ? FRAME_INT : 0) |
202 (USB_ERROR_EVENT ? ERR_INT : 0);
205 USB->USBUDCAH = USB_RAM_ADR;
206 USB->USBDMARClr = 0xFFFFFFFF;
207 USB->USBEpDMADis = 0xFFFFFFFF;
208 USB->USBEpDMAEn = USB_DMA_EP;
209 USB->USBEoTIntClr = 0xFFFFFFFF;
210 USB->USBNDDRIntClr = 0xFFFFFFFF;
211 USB->USBSysErrIntClr = 0xFFFFFFFF;
212 USB->USBDMAIntEn = 0x00000007;
213 DDMemMap[0] = 0x00000000;
214 DDMemMap[1] = 0x00000000;
215 for (n = 0; n < USB_EP_NUM; n++) {
224 * USB Suspend Function
225 * Called automatically on USB Suspend
229 void USB_Suspend (void) {
230 /* Performed by Hardware */
235 * USB Resume Function
236 * Called automatically on USB Resume
240 void USB_Resume (void) {
241 /* Performed by Hardware */
246 * USB Remote Wakeup Function
247 * Called automatically on USB Remote Wakeup
251 void USB_WakeUp (void) {
253 if (USB_DeviceStatus & USB_GETSTATUS_REMOTE_WAKEUP) {
254 WrCmdDat(CMD_SET_DEV_STAT, DAT_WR_BYTE(DEV_CON));
260 * USB Remote Wakeup Configuration Function
261 * Parameters: cfg: Enable/Disable
265 void USB_WakeUpCfg (uint32_t cfg) {
271 * USB Set Address Function
272 * Parameters: adr: USB Address
276 void USB_SetAddress (uint32_t adr) {
277 WrCmdDat(CMD_SET_ADDR, DAT_WR_BYTE(DEV_EN | adr)); /* Don't wait for next */
278 WrCmdDat(CMD_SET_ADDR, DAT_WR_BYTE(DEV_EN | adr)); /* Setup Status Phase */
283 * USB Configure Function
284 * Parameters: cfg: Configure/Deconfigure
288 void USB_Configure (uint32_t cfg) {
290 WrCmdDat(CMD_CFG_DEV, DAT_WR_BYTE(cfg ? CONF_DVICE : 0));
292 USB->USBReEp = 0x00000003;
293 while ((USB->USBDevIntSt & EP_RLZED_INT) == 0);
294 USB->USBDevIntClr = EP_RLZED_INT;
299 * Configure USB Endpoint according to Descriptor
300 * Parameters: pEPD: Pointer to Endpoint Descriptor
304 void USB_ConfigEP (USB_ENDPOINT_DESCRIPTOR *pEPD) {
307 num = EPAdr(pEPD->bEndpointAddress);
308 USB->USBReEp |= (1 << num);
310 USB->USBMaxPSize = pEPD->wMaxPacketSize;
311 while ((USB->USBDevIntSt & EP_RLZED_INT) == 0);
312 USB->USBDevIntClr = EP_RLZED_INT;
317 * Set Direction for USB Control Endpoint
318 * Parameters: dir: Out (dir == 0), In (dir <> 0)
322 void USB_DirCtrlEP (uint32_t dir) {
328 * Enable USB Endpoint
329 * Parameters: EPNum: Endpoint Number
330 * EPNum.0..3: Address
335 void USB_EnableEP (uint32_t EPNum) {
336 WrCmdDat(CMD_SET_EP_STAT(EPAdr(EPNum)), DAT_WR_BYTE(0));
341 * Disable USB Endpoint
342 * Parameters: EPNum: Endpoint Number
343 * EPNum.0..3: Address
348 void USB_DisableEP (uint32_t EPNum) {
349 WrCmdDat(CMD_SET_EP_STAT(EPAdr(EPNum)), DAT_WR_BYTE(EP_STAT_DA));
355 * Parameters: EPNum: Endpoint Number
356 * EPNum.0..3: Address
361 void USB_ResetEP (uint32_t EPNum) {
362 WrCmdDat(CMD_SET_EP_STAT(EPAdr(EPNum)), DAT_WR_BYTE(0));
367 * Set Stall for USB Endpoint
368 * Parameters: EPNum: Endpoint Number
369 * EPNum.0..3: Address
374 void USB_SetStallEP (uint32_t EPNum) {
375 WrCmdDat(CMD_SET_EP_STAT(EPAdr(EPNum)), DAT_WR_BYTE(EP_STAT_ST));
380 * Clear Stall for USB Endpoint
381 * Parameters: EPNum: Endpoint Number
382 * EPNum.0..3: Address
387 void USB_ClrStallEP (uint32_t EPNum) {
388 WrCmdDat(CMD_SET_EP_STAT(EPAdr(EPNum)), DAT_WR_BYTE(0));
393 * Clear USB Endpoint Buffer
394 * Parameters: EPNum: Endpoint Number
395 * EPNum.0..3: Address
400 void USB_ClearEPBuf (uint32_t EPNum) {
401 WrCmdEP(EPNum, CMD_CLR_BUF);
406 * Read USB Endpoint Data
407 * Parameters: EPNum: Endpoint Number
408 * EPNum.0..3: Address
410 * pData: Pointer to Data Buffer
411 * Return Value: Number of bytes read
414 uint32_t USB_ReadEP (uint32_t EPNum, uint8_t *pData) {
417 USB->USBCtrl = ((EPNum & 0x0F) << 2) | CTRL_RD_EN;
420 cnt = USB->USBRxPLen;
421 } while ((cnt & PKT_RDY) == 0);
422 cnt &= PKT_LNGTH_MASK;
424 for (n = 0; n < (cnt + 3) / 4; n++) {
425 *((__packed uint32_t *)pData) = USB->USBRxData;
430 if (((EP_MSK_ISO >> EPNum) & 1) == 0) { /* Non-Isochronous Endpoint */
431 WrCmdEP(EPNum, CMD_CLR_BUF);
439 * Write USB Endpoint Data
440 * Parameters: EPNum: Endpoint Number
441 * EPNum.0..3: Address
443 * pData: Pointer to Data Buffer
444 * cnt: Number of bytes to write
445 * Return Value: Number of bytes written
448 uint32_t USB_WriteEP (uint32_t EPNum, uint8_t *pData, uint32_t cnt) {
451 USB->USBCtrl = ((EPNum & 0x0F) << 2) | CTRL_WR_EN;
453 USB->USBTxPLen = cnt;
455 for (n = 0; n < (cnt + 3) / 4; n++) {
456 USB->USBTxData = *((__packed uint32_t *)pData);
460 WrCmdEP(EPNum, CMD_VALID_BUF);
466 /* DMA Descriptor Memory Layout */
467 const uint32_t DDAdr[2] = { DD_NISO_ADR, DD_ISO_ADR };
468 const uint32_t DDSz [2] = { 16, 20 };
472 * Setup USB DMA Transfer for selected Endpoint
473 * Parameters: EPNum: Endpoint Number
474 * pDD: Pointer to DMA Descriptor
475 * Return Value: TRUE - Success, FALSE - Error
478 uint32_t USB_DMA_Setup(uint32_t EPNum, USB_DMA_DESCRIPTOR *pDD) {
479 uint32_t num, ptr, nxt, iso, n;
481 iso = pDD->Cfg.Type.IsoEP; /* Iso or Non-Iso Descriptor */
482 num = EPAdr(EPNum); /* Endpoint's Physical Address */
484 ptr = 0; /* Current Descriptor */
485 nxt = udca[num]; /* Initial Descriptor */
486 while (nxt) { /* Go through Descriptor List */
487 ptr = nxt; /* Current Descriptor */
488 if (!pDD->Cfg.Type.Link) { /* Check for Linked Descriptors */
489 n = (ptr - DDAdr[iso]) / DDSz[iso]; /* Descriptor Index */
490 DDMemMap[iso] &= ~(1 << n); /* Unmark Memory Usage */
492 nxt = *((uint32_t *)ptr); /* Next Descriptor */
495 for (n = 0; n < 32; n++) { /* Search for available Memory */
496 if ((DDMemMap[iso] & (1 << n)) == 0) {
497 break; /* Memory found */
500 if (n == 32) return (FALSE); /* Memory not available */
502 DDMemMap[iso] |= 1 << n; /* Mark Memory Usage */
503 nxt = DDAdr[iso] + n * DDSz[iso]; /* Next Descriptor */
505 if (ptr && pDD->Cfg.Type.Link) {
506 *((uint32_t *)(ptr + 0)) = nxt; /* Link in new Descriptor */
507 *((uint32_t *)(ptr + 4)) |= 0x00000004; /* Next DD is Valid */
509 udca[num] = nxt; /* Save new Descriptor */
510 UDCA[num] = nxt; /* Update UDCA in USB */
513 /* Fill in DMA Descriptor */
514 *(((uint32_t *)nxt)++) = 0; /* Next DD Pointer */
515 *(((uint32_t *)nxt)++) = pDD->Cfg.Type.ATLE |
516 (pDD->Cfg.Type.IsoEP << 4) |
517 (pDD->MaxSize << 5) |
519 *(((uint32_t *)nxt)++) = pDD->BufAdr;
520 *(((uint32_t *)nxt)++) = pDD->Cfg.Type.LenPos << 8;
522 *((uint32_t *)nxt) = pDD->InfoAdr;
525 return (TRUE); /* Success */
530 * Enable USB DMA Endpoint
531 * Parameters: EPNum: Endpoint Number
532 * EPNum.0..3: Address
537 void USB_DMA_Enable (uint32_t EPNum) {
538 USB->USBEpDMAEn = 1 << EPAdr(EPNum);
543 * Disable USB DMA Endpoint
544 * Parameters: EPNum: Endpoint Number
545 * EPNum.0..3: Address
550 void USB_DMA_Disable (uint32_t EPNum) {
551 USB->USBEpDMADis = 1 << EPAdr(EPNum);
556 * Get USB DMA Endpoint Status
557 * Parameters: EPNum: Endpoint Number
558 * EPNum.0..3: Address
560 * Return Value: DMA Status
563 uint32_t USB_DMA_Status (uint32_t EPNum) {
566 ptr = UDCA[EPAdr(EPNum)]; /* Current Descriptor */
568 return (USB_DMA_INVALID);
570 val = *((uint32_t *)(ptr + 3*4)); /* Status Information */
571 switch ((val >> 1) & 0x0F) {
572 case 0x00: /* Not serviced */
573 return (USB_DMA_IDLE);
574 case 0x01: /* Being serviced */
575 return (USB_DMA_BUSY);
576 case 0x02: /* Normal Completition */
577 return (USB_DMA_DONE);
578 case 0x03: /* Data Under Run */
579 return (USB_DMA_UNDER_RUN);
580 case 0x08: /* Data Over Run */
581 return (USB_DMA_OVER_RUN);
582 case 0x09: /* System Error */
583 return (USB_DMA_ERROR);
586 return (USB_DMA_UNKNOWN);
591 * Get USB DMA Endpoint Current Buffer Address
592 * Parameters: EPNum: Endpoint Number
593 * EPNum.0..3: Address
595 * Return Value: DMA Address (or -1 when DMA is Invalid)
598 uint32_t USB_DMA_BufAdr (uint32_t EPNum) {
601 ptr = UDCA[EPAdr(EPNum)]; /* Current Descriptor */
604 return ((uint32_t)(-1)); /* DMA Invalid */
607 val = *((uint32_t *)(ptr + 2*4)); /* Buffer Address */
608 return (val); /* Current Address */
613 * Get USB DMA Endpoint Current Buffer Count
614 * Number of transfered Bytes or Iso Packets
615 * Parameters: EPNum: Endpoint Number
616 * EPNum.0..3: Address
618 * Return Value: DMA Count (or -1 when DMA is Invalid)
621 uint32_t USB_DMA_BufCnt (uint32_t EPNum) {
624 ptr = UDCA[EPAdr(EPNum)]; /* Current Descriptor */
627 return ((uint32_t)(-1)); /* DMA Invalid */
629 val = *((uint32_t *)(ptr + 3*4)); /* Status Information */
630 return (val >> 16); /* Current Count */
638 * Get USB Last Frame Number
640 * Return Value: Frame Number
643 uint32_t USB_GetFrame (void) {
647 val = RdCmdDat(DAT_RD_FRAME);
648 val = val | (RdCmdDat(DAT_RD_FRAME) << 8);
655 * USB Interrupt Service Routine
658 void USB_IRQHandler (void) {
659 uint32_t disr, val, n, m;
660 uint32_t episr, episrCur;
662 disr = USB->USBDevIntSt; /* Device Interrupt Status */
664 /* Device Status Interrupt (Reset, Connect change, Suspend/Resume) */
665 if (disr & DEV_STAT_INT) {
666 USB->USBDevIntClr = DEV_STAT_INT;
667 WrCmd(CMD_GET_DEV_STAT);
668 val = RdCmdDat(DAT_GET_DEV_STAT); /* Device Status */
669 if (val & DEV_RST) { /* Reset */
675 if (val & DEV_CON_CH) { /* Connect change */
677 USB_Power_Event(val & DEV_CON);
680 if (val & DEV_SUS_CH) { /* Suspend/Resume */
681 if (val & DEV_SUS) { /* Suspend */
683 #if USB_SUSPEND_EVENT
686 } else { /* Resume */
697 /* Start of Frame Interrupt */
698 if (disr & FRAME_INT) {
704 /* Error Interrupt */
705 if (disr & ERR_INT) {
706 WrCmd(CMD_RD_ERR_STAT);
707 val = RdCmdDat(DAT_RD_ERR_STAT);
708 USB_Error_Event(val);
712 /* Endpoint's Slow Interrupt */
713 if (disr & EP_SLOW_INT) {
715 episr = USB->USBEpIntSt;
716 for (n = 0; n < USB_EP_NUM; n++) { /* Check All Endpoints */
717 if (episr == episrCur) break; /* break if all EP interrupts handled */
718 if (episr & (1 << n)) {
719 episrCur |= (1 << n);
722 USB->USBEpIntClr = (1 << n);
723 while ((USB->USBDevIntSt & CDFULL_INT) == 0);
724 val = USB->USBCmdData;
726 if ((n & 1) == 0) { /* OUT Endpoint */
727 if (n == 0) { /* Control OUT Endpoint */
728 if (val & EP_SEL_STP) { /* Setup Packet */
730 USB_P_EP[0](USB_EVT_SETUP);
736 USB_P_EP[m](USB_EVT_OUT);
738 } else { /* IN Endpoint */
740 USB_P_EP[m](USB_EVT_IN);
745 USB->USBDevIntClr = EP_SLOW_INT;
750 if (USB->USBDMAIntSt & 0x00000001) { /* End of Transfer Interrupt */
751 val = USB->USBEoTIntSt;
752 for (n = 2; n < USB_EP_NUM; n++) { /* Check All Endpoints */
753 if (val & (1 << n)) {
755 if ((n & 1) == 0) { /* OUT Endpoint */
757 USB_P_EP[m](USB_EVT_OUT_DMA_EOT);
759 } else { /* IN Endpoint */
761 USB_P_EP[m](USB_EVT_IN_DMA_EOT);
766 USB->USBEoTIntClr = val;
769 if (USB->USBDMAIntSt & 0x00000002) { /* New DD Request Interrupt */
770 val = USB->USBNDDRIntSt;
771 for (n = 2; n < USB_EP_NUM; n++) { /* Check All Endpoints */
772 if (val & (1 << n)) {
774 if ((n & 1) == 0) { /* OUT Endpoint */
776 USB_P_EP[m](USB_EVT_OUT_DMA_NDR);
778 } else { /* IN Endpoint */
780 USB_P_EP[m](USB_EVT_IN_DMA_NDR);
785 USB->USBNDDRIntClr = val;
788 if (USB->USBDMAIntSt & 0x00000004) { /* System Error Interrupt */
789 val = USB->USBSysErrIntSt;
790 for (n = 2; n < USB_EP_NUM; n++) { /* Check All Endpoints */
791 if (val & (1 << n)) {
793 if ((n & 1) == 0) { /* OUT Endpoint */
795 USB_P_EP[m](USB_EVT_OUT_DMA_ERR);
797 } else { /* IN Endpoint */
799 USB_P_EP[m](USB_EVT_IN_DMA_ERR);
804 USB->USBSysErrIntClr = val;