My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

usbhw.cpp 21KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811
  1. /*----------------------------------------------------------------------------
  2. * U S B - K e r n e l
  3. *----------------------------------------------------------------------------
  4. * Name: usbhw.c
  5. * Purpose: USB Hardware Layer Module for NXP's LPC17xx MCU
  6. * Version: V1.20
  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.
  15. *
  16. * Copyright (c) 2009 Keil - An ARM Company. All rights reserved.
  17. *----------------------------------------------------------------------------
  18. * History:
  19. * V1.20 Added USB_ClearEPBuf
  20. * V1.00 Initial Version
  21. *----------------------------------------------------------------------------*/
  22. extern "C" {
  23. #include "LPC17xx.h" /* LPC17xx definitions */
  24. }
  25. #include "usb.h"
  26. #include "usbcfg.h"
  27. #include "usbreg.h"
  28. #include "usbhw.h"
  29. #include "usbcore.h"
  30. #include "usbuser.h"
  31. #define EP_MSK_CTRL 0x0001 /* Control Endpoint Logical Address Mask */
  32. #define EP_MSK_BULK 0xC924 /* Bulk Endpoint Logical Address Mask */
  33. #define EP_MSK_INT 0x4492 /* Interrupt Endpoint Logical Address Mask */
  34. #define EP_MSK_ISO 0x1248 /* Isochronous Endpoint Logical Address Mask */
  35. #if USB_DMA
  36. uint32_t UDCA[USB_EP_NUM] __attribute__((section("USB_RAM"))); /* UDCA in USB RAM */
  37. uint32_t DD_NISO_Mem[4*DD_NISO_CNT] __attribute__((section("USB_RAM"))); /* Non-Iso DMA Descriptor Memory */
  38. uint32_t DD_ISO_Mem [5*DD_ISO_CNT] __attribute__((section("USB_RAM"))); /* Iso DMA Descriptor Memory */
  39. uint32_t udca[USB_EP_NUM]; /* UDCA saved values */
  40. uint32_t DDMemMap[2];
  41. #endif
  42. /*
  43. * Get Endpoint Physical Address
  44. * Parameters: EPNum: Endpoint Number
  45. * EPNum.0..3: Address
  46. * EPNum.7: Dir
  47. * Return Value: Endpoint Physical Address
  48. */
  49. uint32_t EPAdr (uint32_t EPNum) {
  50. uint32_t val;
  51. val = (EPNum & 0x0F) << 1;
  52. if (EPNum & 0x80) {
  53. val += 1;
  54. }
  55. return (val);
  56. }
  57. /*
  58. * Write Command
  59. * Parameters: cmd: Command
  60. * Return Value: None
  61. */
  62. void WrCmd (uint32_t cmd) {
  63. LPC_USB->USBDevIntClr = CCEMTY_INT;
  64. LPC_USB->USBCmdCode = cmd;
  65. while ((LPC_USB->USBDevIntSt & CCEMTY_INT) == 0);
  66. }
  67. /*
  68. * Write Command Data
  69. * Parameters: cmd: Command
  70. * val: Data
  71. * Return Value: None
  72. */
  73. void WrCmdDat (uint32_t cmd, uint32_t val) {
  74. LPC_USB->USBDevIntClr = CCEMTY_INT;
  75. LPC_USB->USBCmdCode = cmd;
  76. while ((LPC_USB->USBDevIntSt & CCEMTY_INT) == 0);
  77. LPC_USB->USBDevIntClr = CCEMTY_INT;
  78. LPC_USB->USBCmdCode = val;
  79. while ((LPC_USB->USBDevIntSt & CCEMTY_INT) == 0);
  80. }
  81. /*
  82. * Write Command to Endpoint
  83. * Parameters: cmd: Command
  84. * val: Data
  85. * Return Value: None
  86. */
  87. void WrCmdEP (uint32_t EPNum, uint32_t cmd){
  88. LPC_USB->USBDevIntClr = CCEMTY_INT;
  89. LPC_USB->USBCmdCode = CMD_SEL_EP(EPAdr(EPNum));
  90. while ((LPC_USB->USBDevIntSt & CCEMTY_INT) == 0);
  91. LPC_USB->USBDevIntClr = CCEMTY_INT;
  92. LPC_USB->USBCmdCode = cmd;
  93. while ((LPC_USB->USBDevIntSt & CCEMTY_INT) == 0);
  94. }
  95. /*
  96. * Read Command Data
  97. * Parameters: cmd: Command
  98. * Return Value: Data Value
  99. */
  100. uint32_t RdCmdDat (uint32_t cmd) {
  101. LPC_USB->USBDevIntClr = CCEMTY_INT | CDFULL_INT;
  102. LPC_USB->USBCmdCode = cmd;
  103. while ((LPC_USB->USBDevIntSt & CDFULL_INT) == 0);
  104. return (LPC_USB->USBCmdData);
  105. }
  106. /*
  107. * USB Initialize Function
  108. * Called by the User to initialize USB
  109. * Return Value: None
  110. */
  111. void USB_Init (void) {
  112. LPC_PINCON->PINSEL1 &= ~((3<<26)|(3<<28)); /* P0.29 D+, P0.30 D- */
  113. LPC_PINCON->PINSEL1 |= ((1<<26)|(1<<28)); /* PINSEL1 26.27, 28.29 = 01 */
  114. //todo: VBUS not used by smoothieboard (though spec requires it for self powered devices), pin used for beeper
  115. //todo: Goodlink used for servo4?
  116. //LPC_PINCON->PINSEL3 &= ~((3<< 4)|(3<<28)); /* P1.18 GoodLink, P1.30 VBUS */
  117. //LPC_PINCON->PINSEL3 |= ((1<< 4)|(2<<28)); /* PINSEL3 4.5 = 01, 28.29 = 10 */
  118. LPC_PINCON->PINSEL4 &= ~((3<<18) ); /* P2.9 SoftConnect */
  119. LPC_PINCON->PINSEL4 |= ((1<<18) ); /* PINSEL4 18.19 = 01 */
  120. LPC_SC->PCONP |= (1UL<<31); /* USB PCLK -> enable USB Per. */
  121. LPC_USB->USBClkCtrl = 0x1A; /* Dev, PortSel, AHB clock enable */
  122. while ((LPC_USB->USBClkSt & 0x1A) != 0x1A);
  123. NVIC_EnableIRQ(USB_IRQn); /* enable USB interrupt */
  124. USB_Reset();
  125. USB_SetAddress(0);
  126. }
  127. /*
  128. * USB Connect Function
  129. * Called by the User to Connect/Disconnect USB
  130. * Parameters: con: Connect/Disconnect
  131. * Return Value: None
  132. */
  133. void USB_Connect (uint32_t con) {
  134. WrCmdDat(CMD_SET_DEV_STAT, DAT_WR_BYTE(con ? DEV_CON : 0));
  135. }
  136. /*
  137. * USB Reset Function
  138. * Called automatically on USB Reset
  139. * Return Value: None
  140. */
  141. void USB_Reset (void) {
  142. #if USB_DMA
  143. uint32_t n;
  144. #endif
  145. LPC_USB->USBEpInd = 0;
  146. LPC_USB->USBMaxPSize = USB_MAX_PACKET0;
  147. LPC_USB->USBEpInd = 1;
  148. LPC_USB->USBMaxPSize = USB_MAX_PACKET0;
  149. while ((LPC_USB->USBDevIntSt & EP_RLZED_INT) == 0);
  150. LPC_USB->USBEpIntClr = 0xFFFFFFFF;
  151. LPC_USB->USBEpIntEn = 0xFFFFFFFF ^ USB_DMA_EP;
  152. LPC_USB->USBDevIntClr = 0xFFFFFFFF;
  153. LPC_USB->USBDevIntEn = DEV_STAT_INT | EP_SLOW_INT |
  154. (USB_SOF_EVENT ? FRAME_INT : 0) |
  155. (USB_ERROR_EVENT ? ERR_INT : 0);
  156. WrCmdDat(CMD_SET_MODE, DAT_WR_BYTE(INAK_BI));
  157. #if USB_DMA
  158. LPC_USB->USBUDCAH = USB_RAM_ADR;
  159. LPC_USB->USBDMARClr = 0xFFFFFFFF;
  160. LPC_USB->USBEpDMADis = 0xFFFFFFFF;
  161. LPC_USB->USBEpDMAEn = USB_DMA_EP;
  162. LPC_USB->USBEoTIntClr = 0xFFFFFFFF;
  163. LPC_USB->USBNDDRIntClr = 0xFFFFFFFF;
  164. LPC_USB->USBSysErrIntClr = 0xFFFFFFFF;
  165. LPC_USB->USBDMAIntEn = 0x00000007;
  166. DDMemMap[0] = 0x00000000;
  167. DDMemMap[1] = 0x00000000;
  168. for (n = 0; n < USB_EP_NUM; n++) {
  169. udca[n] = 0;
  170. UDCA[n] = 0;
  171. }
  172. #endif
  173. }
  174. /*
  175. * USB Suspend Function
  176. * Called automatically on USB Suspend
  177. * Return Value: None
  178. */
  179. void USB_Suspend (void) {
  180. /* Performed by Hardware */
  181. }
  182. /*
  183. * USB Resume Function
  184. * Called automatically on USB Resume
  185. * Return Value: None
  186. */
  187. void USB_Resume (void) {
  188. /* Performed by Hardware */
  189. }
  190. /*
  191. * USB Remote Wakeup Function
  192. * Called automatically on USB Remote Wakeup
  193. * Return Value: None
  194. */
  195. void USB_WakeUp (void) {
  196. if (USB_DeviceStatus & USB_GETSTATUS_REMOTE_WAKEUP) {
  197. WrCmdDat(CMD_SET_DEV_STAT, DAT_WR_BYTE(DEV_CON));
  198. }
  199. }
  200. /*
  201. * USB Remote Wakeup Configuration Function
  202. * Parameters: cfg: Enable/Disable
  203. * Return Value: None
  204. */
  205. void USB_WakeUpCfg (uint32_t cfg) {
  206. /* Not needed */
  207. }
  208. /*
  209. * USB Set Address Function
  210. * Parameters: adr: USB Address
  211. * Return Value: None
  212. */
  213. void USB_SetAddress (uint32_t adr) {
  214. WrCmdDat(CMD_SET_ADDR, DAT_WR_BYTE(DEV_EN | adr)); /* Don't wait for next */
  215. WrCmdDat(CMD_SET_ADDR, DAT_WR_BYTE(DEV_EN | adr)); /* Setup Status Phase */
  216. }
  217. /*
  218. * USB Configure Function
  219. * Parameters: cfg: Configure/Deconfigure
  220. * Return Value: None
  221. */
  222. void USB_Configure (uint32_t cfg) {
  223. WrCmdDat(CMD_CFG_DEV, DAT_WR_BYTE(cfg ? CONF_DVICE : 0));
  224. LPC_USB->USBReEp = 0x00000003;
  225. while ((LPC_USB->USBDevIntSt & EP_RLZED_INT) == 0);
  226. LPC_USB->USBDevIntClr = EP_RLZED_INT;
  227. }
  228. /*
  229. * Configure USB Endpoint according to Descriptor
  230. * Parameters: pEPD: Pointer to Endpoint Descriptor
  231. * Return Value: None
  232. */
  233. void USB_ConfigEP (USB_ENDPOINT_DESCRIPTOR *pEPD) {
  234. uint32_t num;
  235. num = EPAdr(pEPD->bEndpointAddress);
  236. LPC_USB->USBReEp |= (1 << num);
  237. LPC_USB->USBEpInd = num;
  238. LPC_USB->USBMaxPSize = pEPD->wMaxPacketSize;
  239. while ((LPC_USB->USBDevIntSt & EP_RLZED_INT) == 0);
  240. LPC_USB->USBDevIntClr = EP_RLZED_INT;
  241. }
  242. /*
  243. * Set Direction for USB Control Endpoint
  244. * Parameters: dir: Out (dir == 0), In (dir <> 0)
  245. * Return Value: None
  246. */
  247. void USB_DirCtrlEP (uint32_t dir) {
  248. /* Not needed */
  249. }
  250. /*
  251. * Enable USB Endpoint
  252. * Parameters: EPNum: Endpoint Number
  253. * EPNum.0..3: Address
  254. * EPNum.7: Dir
  255. * Return Value: None
  256. */
  257. void USB_EnableEP (uint32_t EPNum) {
  258. WrCmdDat(CMD_SET_EP_STAT(EPAdr(EPNum)), DAT_WR_BYTE(0));
  259. }
  260. /*
  261. * Disable USB Endpoint
  262. * Parameters: EPNum: Endpoint Number
  263. * EPNum.0..3: Address
  264. * EPNum.7: Dir
  265. * Return Value: None
  266. */
  267. void USB_DisableEP (uint32_t EPNum) {
  268. WrCmdDat(CMD_SET_EP_STAT(EPAdr(EPNum)), DAT_WR_BYTE(EP_STAT_DA));
  269. }
  270. /*
  271. * Reset USB Endpoint
  272. * Parameters: EPNum: Endpoint Number
  273. * EPNum.0..3: Address
  274. * EPNum.7: Dir
  275. * Return Value: None
  276. */
  277. void USB_ResetEP (uint32_t EPNum) {
  278. WrCmdDat(CMD_SET_EP_STAT(EPAdr(EPNum)), DAT_WR_BYTE(0));
  279. }
  280. /*
  281. * Set Stall for USB Endpoint
  282. * Parameters: EPNum: Endpoint Number
  283. * EPNum.0..3: Address
  284. * EPNum.7: Dir
  285. * Return Value: None
  286. */
  287. void USB_SetStallEP (uint32_t EPNum) {
  288. WrCmdDat(CMD_SET_EP_STAT(EPAdr(EPNum)), DAT_WR_BYTE(EP_STAT_ST));
  289. }
  290. /*
  291. * Clear Stall for USB Endpoint
  292. * Parameters: EPNum: Endpoint Number
  293. * EPNum.0..3: Address
  294. * EPNum.7: Dir
  295. * Return Value: None
  296. */
  297. void USB_ClrStallEP (uint32_t EPNum) {
  298. WrCmdDat(CMD_SET_EP_STAT(EPAdr(EPNum)), DAT_WR_BYTE(0));
  299. }
  300. /*
  301. * Clear USB Endpoint Buffer
  302. * Parameters: EPNum: Endpoint Number
  303. * EPNum.0..3: Address
  304. * EPNum.7: Dir
  305. * Return Value: None
  306. */
  307. void USB_ClearEPBuf (uint32_t EPNum) {
  308. WrCmdEP(EPNum, CMD_CLR_BUF);
  309. }
  310. /*
  311. * Read USB Endpoint Data
  312. * Parameters: EPNum: Endpoint Number
  313. * EPNum.0..3: Address
  314. * EPNum.7: Dir
  315. * pData: Pointer to Data Buffer
  316. * Return Value: Number of bytes read
  317. */
  318. uint32_t USB_ReadEP (uint32_t EPNum, uint8_t *pData) {
  319. uint32_t cnt, n;
  320. LPC_USB->USBCtrl = ((EPNum & 0x0F) << 2) | CTRL_RD_EN;
  321. do {
  322. cnt = LPC_USB->USBRxPLen;
  323. } while ((cnt & PKT_RDY) == 0);
  324. cnt &= PKT_LNGTH_MASK;
  325. for (n = 0; n < (cnt + 3) / 4; n++) {
  326. *((__packed uint32_t *)pData) = LPC_USB->USBRxData;
  327. pData += 4;
  328. }
  329. LPC_USB->USBCtrl = 0;
  330. if (((EP_MSK_ISO >> EPNum) & 1) == 0) { /* Non-Isochronous Endpoint */
  331. WrCmdEP(EPNum, CMD_CLR_BUF);
  332. }
  333. return (cnt);
  334. }
  335. /*
  336. * Write USB Endpoint Data
  337. * Parameters: EPNum: Endpoint Number
  338. * EPNum.0..3: Address
  339. * EPNum.7: Dir
  340. * pData: Pointer to Data Buffer
  341. * cnt: Number of bytes to write
  342. * Return Value: Number of bytes written
  343. */
  344. uint32_t USB_WriteEP (uint32_t EPNum, uint8_t *pData, uint32_t cnt) {
  345. uint32_t n;
  346. LPC_USB->USBCtrl = ((EPNum & 0x0F) << 2) | CTRL_WR_EN;
  347. LPC_USB->USBTxPLen = cnt;
  348. for (n = 0; n < (cnt + 3) / 4; n++) {
  349. LPC_USB->USBTxData = *((__packed uint32_t *)pData);
  350. pData += 4;
  351. }
  352. LPC_USB->USBCtrl = 0;
  353. WrCmdEP(EPNum, CMD_VALID_BUF);
  354. return (cnt);
  355. }
  356. #if USB_DMA
  357. /* DMA Descriptor Memory Layout */
  358. const uint32_t DDAdr[2] = { DD_NISO_ADR, DD_ISO_ADR };
  359. const uint32_t DDSz [2] = { 16, 20 };
  360. /*
  361. * Setup USB DMA Transfer for selected Endpoint
  362. * Parameters: EPNum: Endpoint Number
  363. * pDD: Pointer to DMA Descriptor
  364. * Return Value: TRUE - Success, FALSE - Error
  365. */
  366. uint32_t USB_DMA_Setup(uint32_t EPNum, USB_DMA_DESCRIPTOR *pDD) {
  367. uint32_t num, ptr, nxt, iso, n;
  368. iso = pDD->Cfg.Type.IsoEP; /* Iso or Non-Iso Descriptor */
  369. num = EPAdr(EPNum); /* Endpoint's Physical Address */
  370. ptr = 0; /* Current Descriptor */
  371. nxt = udca[num]; /* Initial Descriptor */
  372. while (nxt) { /* Go through Descriptor List */
  373. ptr = nxt; /* Current Descriptor */
  374. if (!pDD->Cfg.Type.Link) { /* Check for Linked Descriptors */
  375. n = (ptr - DDAdr[iso]) / DDSz[iso]; /* Descriptor Index */
  376. DDMemMap[iso] &= ~(1 << n); /* Unmark Memory Usage */
  377. }
  378. nxt = *((uint32_t *)ptr); /* Next Descriptor */
  379. }
  380. for (n = 0; n < 32; n++) { /* Search for available Memory */
  381. if ((DDMemMap[iso] & (1 << n)) == 0) {
  382. break; /* Memory found */
  383. }
  384. }
  385. if (n == 32) return (FALSE); /* Memory not available */
  386. DDMemMap[iso] |= 1 << n; /* Mark Memory Usage */
  387. nxt = DDAdr[iso] + n * DDSz[iso]; /* Next Descriptor */
  388. if (ptr && pDD->Cfg.Type.Link) {
  389. *((uint32_t *)(ptr + 0)) = nxt; /* Link in new Descriptor */
  390. *((uint32_t *)(ptr + 4)) |= 0x00000004; /* Next DD is Valid */
  391. } else {
  392. udca[num] = nxt; /* Save new Descriptor */
  393. UDCA[num] = nxt; /* Update UDCA in USB */
  394. }
  395. uint32_t * nxt_ptr = (uint32_t *)nxt;
  396. /* Fill in DMA Descriptor */
  397. *nxt_ptr++ = 0; /* Next DD Pointer */
  398. *nxt_ptr++ = (pDD->Cfg.Type.ATLE) |
  399. (pDD->Cfg.Type.IsoEP << 4) |
  400. (pDD->MaxSize << 5) |
  401. (pDD->BufLen << 16);
  402. *nxt_ptr++ = pDD->BufAdr;
  403. *nxt_ptr++ = pDD->Cfg.Type.LenPos << 8;
  404. if (iso) {
  405. *nxt_ptr = pDD->InfoAdr;
  406. }
  407. return (TRUE); /* Success */
  408. }
  409. /*
  410. * Enable USB DMA Endpoint
  411. * Parameters: EPNum: Endpoint Number
  412. * EPNum.0..3: Address
  413. * EPNum.7: Dir
  414. * Return Value: None
  415. */
  416. void USB_DMA_Enable (uint32_t EPNum) {
  417. LPC_USB->USBEpDMAEn = 1 << EPAdr(EPNum);
  418. }
  419. /*
  420. * Disable USB DMA Endpoint
  421. * Parameters: EPNum: Endpoint Number
  422. * EPNum.0..3: Address
  423. * EPNum.7: Dir
  424. * Return Value: None
  425. */
  426. void USB_DMA_Disable (uint32_t EPNum) {
  427. LPC_USB->USBEpDMADis = 1 << EPAdr(EPNum);
  428. }
  429. /*
  430. * Get USB DMA Endpoint Status
  431. * Parameters: EPNum: Endpoint Number
  432. * EPNum.0..3: Address
  433. * EPNum.7: Dir
  434. * Return Value: DMA Status
  435. */
  436. uint32_t USB_DMA_Status (uint32_t EPNum) {
  437. uint32_t ptr, val;
  438. ptr = UDCA[EPAdr(EPNum)]; /* Current Descriptor */
  439. if (ptr == 0)
  440. return (USB_DMA_INVALID);
  441. val = *((uint32_t *)(ptr + 3*4)); /* Status Information */
  442. switch ((val >> 1) & 0x0F) {
  443. case 0x00: /* Not serviced */
  444. return (USB_DMA_IDLE);
  445. case 0x01: /* Being serviced */
  446. return (USB_DMA_BUSY);
  447. case 0x02: /* Normal Completition */
  448. return (USB_DMA_DONE);
  449. case 0x03: /* Data Under Run */
  450. return (USB_DMA_UNDER_RUN);
  451. case 0x08: /* Data Over Run */
  452. return (USB_DMA_OVER_RUN);
  453. case 0x09: /* System Error */
  454. return (USB_DMA_ERROR);
  455. }
  456. return (USB_DMA_UNKNOWN);
  457. }
  458. /*
  459. * Get USB DMA Endpoint Current Buffer Address
  460. * Parameters: EPNum: Endpoint Number
  461. * EPNum.0..3: Address
  462. * EPNum.7: Dir
  463. * Return Value: DMA Address (or -1 when DMA is Invalid)
  464. */
  465. uint32_t USB_DMA_BufAdr (uint32_t EPNum) {
  466. uint32_t ptr, val;
  467. ptr = UDCA[EPAdr(EPNum)]; /* Current Descriptor */
  468. if (ptr == 0)
  469. {
  470. return ((uint32_t)(-1)); /* DMA Invalid */
  471. }
  472. val = *((uint32_t *)(ptr + 2*4)); /* Buffer Address */
  473. return (val); /* Current Address */
  474. }
  475. /*
  476. * Get USB DMA Endpoint Current Buffer Count
  477. * Number of transfered Bytes or Iso Packets
  478. * Parameters: EPNum: Endpoint Number
  479. * EPNum.0..3: Address
  480. * EPNum.7: Dir
  481. * Return Value: DMA Count (or -1 when DMA is Invalid)
  482. */
  483. uint32_t USB_DMA_BufCnt (uint32_t EPNum) {
  484. uint32_t ptr, val;
  485. ptr = UDCA[EPAdr(EPNum)]; /* Current Descriptor */
  486. if (ptr == 0)
  487. {
  488. return ((uint32_t)(-1)); /* DMA Invalid */
  489. }
  490. val = *((uint32_t *)(ptr + 3*4)); /* Status Information */
  491. return (val >> 16); /* Current Count */
  492. }
  493. #endif /* USB_DMA */
  494. /*
  495. * Get USB Last Frame Number
  496. * Parameters: None
  497. * Return Value: Frame Number
  498. */
  499. uint32_t USB_GetFrame (void) {
  500. uint32_t val;
  501. WrCmd(CMD_RD_FRAME);
  502. val = RdCmdDat(DAT_RD_FRAME);
  503. val = val | (RdCmdDat(DAT_RD_FRAME) << 8);
  504. return (val);
  505. }
  506. /*
  507. * USB Interrupt Service Routine
  508. */
  509. void USB_IRQHandler (void) {
  510. uint32_t disr, val, n, m;
  511. uint32_t episr, episrCur;
  512. disr = LPC_USB->USBDevIntSt; /* Device Interrupt Status */
  513. /* Device Status Interrupt (Reset, Connect change, Suspend/Resume) */
  514. if (disr & DEV_STAT_INT) {
  515. LPC_USB->USBDevIntClr = DEV_STAT_INT;
  516. WrCmd(CMD_GET_DEV_STAT);
  517. val = RdCmdDat(DAT_GET_DEV_STAT); /* Device Status */
  518. if (val & DEV_RST) { /* Reset */
  519. USB_Reset();
  520. #if USB_RESET_EVENT
  521. USB_Reset_Event();
  522. #endif
  523. }
  524. if (val & DEV_CON_CH) { /* Connect change */
  525. #if USB_POWER_EVENT
  526. USB_Power_Event(val & DEV_CON);
  527. #endif
  528. }
  529. if (val & DEV_SUS_CH) { /* Suspend/Resume */
  530. if (val & DEV_SUS) { /* Suspend */
  531. USB_Suspend();
  532. #if USB_SUSPEND_EVENT
  533. USB_Suspend_Event();
  534. #endif
  535. } else { /* Resume */
  536. USB_Resume();
  537. #if USB_RESUME_EVENT
  538. USB_Resume_Event();
  539. #endif
  540. }
  541. }
  542. goto isr_end;
  543. }
  544. #if USB_SOF_EVENT
  545. /* Start of Frame Interrupt */
  546. if (disr & FRAME_INT) {
  547. LPC_USB->USBDevIntClr = FRAME_INT;
  548. USB_SOF_Event();
  549. }
  550. #endif
  551. #if USB_ERROR_EVENT
  552. /* Error Interrupt */
  553. if (disr & ERR_INT) {
  554. LPC_USB->USBDevIntClr = ERR_INT;
  555. WrCmd(CMD_RD_ERR_STAT);
  556. val = RdCmdDat(DAT_RD_ERR_STAT);
  557. USB_Error_Event(val);
  558. }
  559. #endif
  560. /* Endpoint's Slow Interrupt */
  561. if (disr & EP_SLOW_INT) {
  562. episrCur = 0;
  563. episr = LPC_USB->USBEpIntSt;
  564. for (n = 0; n < USB_EP_NUM; n++) { /* Check All Endpoints */
  565. if (episr == episrCur) break; /* break if all EP interrupts handled */
  566. if (episr & (1 << n)) {
  567. episrCur |= (1 << n);
  568. m = n >> 1;
  569. LPC_USB->USBEpIntClr = (1 << n);
  570. while ((LPC_USB->USBDevIntSt & CDFULL_INT) == 0);
  571. val = LPC_USB->USBCmdData;
  572. if ((n & 1) == 0) { /* OUT Endpoint */
  573. if (n == 0) { /* Control OUT Endpoint */
  574. if (val & EP_SEL_STP) { /* Setup Packet */
  575. if (USB_P_EP[0]) {
  576. USB_P_EP[0](USB_EVT_SETUP);
  577. continue;
  578. }
  579. }
  580. }
  581. if (USB_P_EP[m]) {
  582. USB_P_EP[m](USB_EVT_OUT);
  583. }
  584. } else { /* IN Endpoint */
  585. if (USB_P_EP[m]) {
  586. USB_P_EP[m](USB_EVT_IN);
  587. }
  588. }
  589. }
  590. }
  591. LPC_USB->USBDevIntClr = EP_SLOW_INT;
  592. }
  593. #if USB_DMA
  594. if (LPC_USB->USBDMAIntSt & 0x00000001) { /* End of Transfer Interrupt */
  595. val = LPC_USB->USBEoTIntSt;
  596. for (n = 2; n < USB_EP_NUM; n++) { /* Check All Endpoints */
  597. if (val & (1 << n)) {
  598. m = n >> 1;
  599. if ((n & 1) == 0) { /* OUT Endpoint */
  600. if (USB_P_EP[m]) {
  601. USB_P_EP[m](USB_EVT_OUT_DMA_EOT);
  602. }
  603. } else { /* IN Endpoint */
  604. if (USB_P_EP[m]) {
  605. USB_P_EP[m](USB_EVT_IN_DMA_EOT);
  606. }
  607. }
  608. }
  609. }
  610. LPC_USB->USBEoTIntClr = val;
  611. }
  612. if (LPC_USB->USBDMAIntSt & 0x00000002) { /* New DD Request Interrupt */
  613. val = LPC_USB->USBNDDRIntSt;
  614. for (n = 2; n < USB_EP_NUM; n++) { /* Check All Endpoints */
  615. if (val & (1 << n)) {
  616. m = n >> 1;
  617. if ((n & 1) == 0) { /* OUT Endpoint */
  618. if (USB_P_EP[m]) {
  619. USB_P_EP[m](USB_EVT_OUT_DMA_NDR);
  620. }
  621. } else { /* IN Endpoint */
  622. if (USB_P_EP[m]) {
  623. USB_P_EP[m](USB_EVT_IN_DMA_NDR);
  624. }
  625. }
  626. }
  627. }
  628. LPC_USB->USBNDDRIntClr = val;
  629. }
  630. if (LPC_USB->USBDMAIntSt & 0x00000004) { /* System Error Interrupt */
  631. val = LPC_USB->USBSysErrIntSt;
  632. for (n = 2; n < USB_EP_NUM; n++) { /* Check All Endpoints */
  633. if (val & (1 << n)) {
  634. m = n >> 1;
  635. if ((n & 1) == 0) { /* OUT Endpoint */
  636. if (USB_P_EP[m]) {
  637. USB_P_EP[m](USB_EVT_OUT_DMA_ERR);
  638. }
  639. } else { /* IN Endpoint */
  640. if (USB_P_EP[m]) {
  641. USB_P_EP[m](USB_EVT_IN_DMA_ERR);
  642. }
  643. }
  644. }
  645. }
  646. LPC_USB->USBSysErrIntClr = val;
  647. }
  648. #endif /* USB_DMA */
  649. isr_end:
  650. return;
  651. }