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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802
  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. NVIC_SetPriority(USB_IRQn, NVIC_EncodePriority(0, 5, 0));
  125. USB_Reset();
  126. USB_SetAddress(0);
  127. }
  128. /*
  129. * USB Connect Function
  130. * Called by the User to Connect/Disconnect USB
  131. * Parameters: con: Connect/Disconnect
  132. * Return Value: None
  133. */
  134. void USB_Connect (uint32_t con) {
  135. WrCmdDat(CMD_SET_DEV_STAT, DAT_WR_BYTE(con ? DEV_CON : 0));
  136. }
  137. /*
  138. * USB Reset Function
  139. * Called automatically on USB Reset
  140. * Return Value: None
  141. */
  142. void USB_Reset (void) {
  143. #if USB_DMA
  144. uint32_t n;
  145. #endif
  146. LPC_USB->USBEpInd = 0;
  147. LPC_USB->USBMaxPSize = USB_MAX_PACKET0;
  148. LPC_USB->USBEpInd = 1;
  149. LPC_USB->USBMaxPSize = USB_MAX_PACKET0;
  150. while ((LPC_USB->USBDevIntSt & EP_RLZED_INT) == 0);
  151. LPC_USB->USBEpIntClr = 0xFFFFFFFF;
  152. LPC_USB->USBEpIntEn = 0xFFFFFFFF ^ USB_DMA_EP;
  153. LPC_USB->USBDevIntClr = 0xFFFFFFFF;
  154. LPC_USB->USBDevIntEn = DEV_STAT_INT | EP_SLOW_INT |
  155. (USB_SOF_EVENT ? FRAME_INT : 0) |
  156. (USB_ERROR_EVENT ? ERR_INT : 0);
  157. WrCmdDat(CMD_SET_MODE, DAT_WR_BYTE(INAK_BI));
  158. #if USB_DMA
  159. LPC_USB->USBUDCAH = USB_RAM_ADR;
  160. LPC_USB->USBDMARClr = 0xFFFFFFFF;
  161. LPC_USB->USBEpDMADis = 0xFFFFFFFF;
  162. LPC_USB->USBEpDMAEn = USB_DMA_EP;
  163. LPC_USB->USBEoTIntClr = 0xFFFFFFFF;
  164. LPC_USB->USBNDDRIntClr = 0xFFFFFFFF;
  165. LPC_USB->USBSysErrIntClr = 0xFFFFFFFF;
  166. LPC_USB->USBDMAIntEn = 0x00000007;
  167. DDMemMap[0] = 0x00000000;
  168. DDMemMap[1] = 0x00000000;
  169. for (n = 0; n < USB_EP_NUM; n++) {
  170. udca[n] = 0;
  171. UDCA[n] = 0;
  172. }
  173. #endif
  174. }
  175. /*
  176. * USB Suspend Function
  177. * Called automatically on USB Suspend
  178. * Return Value: None
  179. */
  180. void USB_Suspend (void) {
  181. /* Performed by Hardware */
  182. }
  183. /*
  184. * USB Resume Function
  185. * Called automatically on USB Resume
  186. * Return Value: None
  187. */
  188. void USB_Resume (void) {
  189. /* Performed by Hardware */
  190. }
  191. /*
  192. * USB Remote Wakeup Function
  193. * Called automatically on USB Remote Wakeup
  194. * Return Value: None
  195. */
  196. void USB_WakeUp (void) {
  197. if (USB_DeviceStatus & USB_GETSTATUS_REMOTE_WAKEUP) {
  198. WrCmdDat(CMD_SET_DEV_STAT, DAT_WR_BYTE(DEV_CON));
  199. }
  200. }
  201. /*
  202. * USB Remote Wakeup Configuration Function
  203. * Parameters: cfg: Enable/Disable
  204. * Return Value: None
  205. */
  206. void USB_WakeUpCfg (uint32_t cfg) {
  207. /* Not needed */
  208. }
  209. /*
  210. * USB Set Address Function
  211. * Parameters: adr: USB Address
  212. * Return Value: None
  213. */
  214. void USB_SetAddress (uint32_t adr) {
  215. WrCmdDat(CMD_SET_ADDR, DAT_WR_BYTE(DEV_EN | adr)); /* Don't wait for next */
  216. WrCmdDat(CMD_SET_ADDR, DAT_WR_BYTE(DEV_EN | adr)); /* Setup Status Phase */
  217. }
  218. /*
  219. * USB Configure Function
  220. * Parameters: cfg: Configure/Deconfigure
  221. * Return Value: None
  222. */
  223. void USB_Configure (uint32_t cfg) {
  224. WrCmdDat(CMD_CFG_DEV, DAT_WR_BYTE(cfg ? CONF_DVICE : 0));
  225. LPC_USB->USBReEp = 0x00000003;
  226. while ((LPC_USB->USBDevIntSt & EP_RLZED_INT) == 0);
  227. LPC_USB->USBDevIntClr = EP_RLZED_INT;
  228. }
  229. /*
  230. * Configure USB Endpoint according to Descriptor
  231. * Parameters: pEPD: Pointer to Endpoint Descriptor
  232. * Return Value: None
  233. */
  234. void USB_ConfigEP (USB_ENDPOINT_DESCRIPTOR *pEPD) {
  235. uint32_t num;
  236. num = EPAdr(pEPD->bEndpointAddress);
  237. LPC_USB->USBReEp |= (1 << num);
  238. LPC_USB->USBEpInd = num;
  239. LPC_USB->USBMaxPSize = pEPD->wMaxPacketSize;
  240. while ((LPC_USB->USBDevIntSt & EP_RLZED_INT) == 0);
  241. LPC_USB->USBDevIntClr = EP_RLZED_INT;
  242. }
  243. /*
  244. * Set Direction for USB Control Endpoint
  245. * Parameters: dir: Out (dir == 0), In (dir <> 0)
  246. * Return Value: None
  247. */
  248. void USB_DirCtrlEP (uint32_t dir) {
  249. /* Not needed */
  250. }
  251. /*
  252. * Enable USB Endpoint
  253. * Parameters: EPNum: Endpoint Number
  254. * EPNum.0..3: Address
  255. * EPNum.7: Dir
  256. * Return Value: None
  257. */
  258. void USB_EnableEP (uint32_t EPNum) {
  259. WrCmdDat(CMD_SET_EP_STAT(EPAdr(EPNum)), DAT_WR_BYTE(0));
  260. }
  261. /*
  262. * Disable USB Endpoint
  263. * Parameters: EPNum: Endpoint Number
  264. * EPNum.0..3: Address
  265. * EPNum.7: Dir
  266. * Return Value: None
  267. */
  268. void USB_DisableEP (uint32_t EPNum) {
  269. WrCmdDat(CMD_SET_EP_STAT(EPAdr(EPNum)), DAT_WR_BYTE(EP_STAT_DA));
  270. }
  271. /*
  272. * Reset USB Endpoint
  273. * Parameters: EPNum: Endpoint Number
  274. * EPNum.0..3: Address
  275. * EPNum.7: Dir
  276. * Return Value: None
  277. */
  278. void USB_ResetEP (uint32_t EPNum) {
  279. WrCmdDat(CMD_SET_EP_STAT(EPAdr(EPNum)), DAT_WR_BYTE(0));
  280. }
  281. /*
  282. * Set Stall for USB Endpoint
  283. * Parameters: EPNum: Endpoint Number
  284. * EPNum.0..3: Address
  285. * EPNum.7: Dir
  286. * Return Value: None
  287. */
  288. void USB_SetStallEP (uint32_t EPNum) {
  289. WrCmdDat(CMD_SET_EP_STAT(EPAdr(EPNum)), DAT_WR_BYTE(EP_STAT_ST));
  290. }
  291. /*
  292. * Clear Stall for USB Endpoint
  293. * Parameters: EPNum: Endpoint Number
  294. * EPNum.0..3: Address
  295. * EPNum.7: Dir
  296. * Return Value: None
  297. */
  298. void USB_ClrStallEP (uint32_t EPNum) {
  299. WrCmdDat(CMD_SET_EP_STAT(EPAdr(EPNum)), DAT_WR_BYTE(0));
  300. }
  301. /*
  302. * Clear USB Endpoint Buffer
  303. * Parameters: EPNum: Endpoint Number
  304. * EPNum.0..3: Address
  305. * EPNum.7: Dir
  306. * Return Value: None
  307. */
  308. void USB_ClearEPBuf (uint32_t EPNum) {
  309. WrCmdEP(EPNum, CMD_CLR_BUF);
  310. }
  311. /*
  312. * Read USB Endpoint Data
  313. * Parameters: EPNum: Endpoint Number
  314. * EPNum.0..3: Address
  315. * EPNum.7: Dir
  316. * pData: Pointer to Data Buffer
  317. * Return Value: Number of bytes read
  318. */
  319. uint32_t USB_ReadEP (uint32_t EPNum, uint8_t *pData) {
  320. uint32_t cnt, n;
  321. LPC_USB->USBCtrl = ((EPNum & 0x0F) << 2) | CTRL_RD_EN;
  322. do {
  323. cnt = LPC_USB->USBRxPLen;
  324. } while ((cnt & PKT_RDY) == 0);
  325. cnt &= PKT_LNGTH_MASK;
  326. for (n = 0; n < (cnt + 3) / 4; n++) {
  327. *((__packed uint32_t *)pData) = LPC_USB->USBRxData;
  328. pData += 4;
  329. }
  330. LPC_USB->USBCtrl = 0;
  331. if (((EP_MSK_ISO >> EPNum) & 1) == 0) { /* Non-Isochronous Endpoint */
  332. WrCmdEP(EPNum, CMD_CLR_BUF);
  333. }
  334. return (cnt);
  335. }
  336. /*
  337. * Write USB Endpoint Data
  338. * Parameters: EPNum: Endpoint Number
  339. * EPNum.0..3: Address
  340. * EPNum.7: Dir
  341. * pData: Pointer to Data Buffer
  342. * cnt: Number of bytes to write
  343. * Return Value: Number of bytes written
  344. */
  345. uint32_t USB_WriteEP (uint32_t EPNum, uint8_t *pData, uint32_t cnt) {
  346. uint32_t n;
  347. LPC_USB->USBCtrl = ((EPNum & 0x0F) << 2) | CTRL_WR_EN;
  348. LPC_USB->USBTxPLen = cnt;
  349. for (n = 0; n < (cnt + 3) / 4; n++) {
  350. LPC_USB->USBTxData = *((__packed uint32_t *)pData);
  351. pData += 4;
  352. }
  353. LPC_USB->USBCtrl = 0;
  354. WrCmdEP(EPNum, CMD_VALID_BUF);
  355. return (cnt);
  356. }
  357. #if USB_DMA
  358. /* DMA Descriptor Memory Layout */
  359. const uint32_t DDAdr[2] = { DD_NISO_ADR, DD_ISO_ADR };
  360. const uint32_t DDSz [2] = { 16, 20 };
  361. /*
  362. * Setup USB DMA Transfer for selected Endpoint
  363. * Parameters: EPNum: Endpoint Number
  364. * pDD: Pointer to DMA Descriptor
  365. * Return Value: TRUE - Success, FALSE - Error
  366. */
  367. uint32_t USB_DMA_Setup(uint32_t EPNum, USB_DMA_DESCRIPTOR *pDD) {
  368. uint32_t num, ptr, nxt, iso, n;
  369. iso = pDD->Cfg.Type.IsoEP; /* Iso or Non-Iso Descriptor */
  370. num = EPAdr(EPNum); /* Endpoint's Physical Address */
  371. ptr = 0; /* Current Descriptor */
  372. nxt = udca[num]; /* Initial Descriptor */
  373. while (nxt) { /* Go through Descriptor List */
  374. ptr = nxt; /* Current Descriptor */
  375. if (!pDD->Cfg.Type.Link) { /* Check for Linked Descriptors */
  376. n = (ptr - DDAdr[iso]) / DDSz[iso]; /* Descriptor Index */
  377. DDMemMap[iso] &= ~(1 << n); /* Unmark Memory Usage */
  378. }
  379. nxt = *((uint32_t *)ptr); /* Next Descriptor */
  380. }
  381. for (n = 0; n < 32; n++) { /* Search for available Memory */
  382. if ((DDMemMap[iso] & (1 << n)) == 0) {
  383. break; /* Memory found */
  384. }
  385. }
  386. if (n == 32) return (FALSE); /* Memory not available */
  387. DDMemMap[iso] |= 1 << n; /* Mark Memory Usage */
  388. nxt = DDAdr[iso] + n * DDSz[iso]; /* Next Descriptor */
  389. if (ptr && pDD->Cfg.Type.Link) {
  390. *((uint32_t *)(ptr + 0)) = nxt; /* Link in new Descriptor */
  391. *((uint32_t *)(ptr + 4)) |= 0x00000004; /* Next DD is Valid */
  392. } else {
  393. udca[num] = nxt; /* Save new Descriptor */
  394. UDCA[num] = nxt; /* Update UDCA in USB */
  395. }
  396. uint32_t * nxt_ptr = (uint32_t *)nxt;
  397. /* Fill in DMA Descriptor */
  398. *nxt_ptr++ = 0; /* Next DD Pointer */
  399. *nxt_ptr++ = (pDD->Cfg.Type.ATLE) |
  400. (pDD->Cfg.Type.IsoEP << 4) |
  401. (pDD->MaxSize << 5) |
  402. (pDD->BufLen << 16);
  403. *nxt_ptr++ = pDD->BufAdr;
  404. *nxt_ptr++ = pDD->Cfg.Type.LenPos << 8;
  405. if (iso) {
  406. *nxt_ptr = pDD->InfoAdr;
  407. }
  408. return (TRUE); /* Success */
  409. }
  410. /*
  411. * Enable USB DMA Endpoint
  412. * Parameters: EPNum: Endpoint Number
  413. * EPNum.0..3: Address
  414. * EPNum.7: Dir
  415. * Return Value: None
  416. */
  417. void USB_DMA_Enable (uint32_t EPNum) {
  418. LPC_USB->USBEpDMAEn = 1 << EPAdr(EPNum);
  419. }
  420. /*
  421. * Disable USB DMA Endpoint
  422. * Parameters: EPNum: Endpoint Number
  423. * EPNum.0..3: Address
  424. * EPNum.7: Dir
  425. * Return Value: None
  426. */
  427. void USB_DMA_Disable (uint32_t EPNum) {
  428. LPC_USB->USBEpDMADis = 1 << EPAdr(EPNum);
  429. }
  430. /*
  431. * Get USB DMA Endpoint Status
  432. * Parameters: EPNum: Endpoint Number
  433. * EPNum.0..3: Address
  434. * EPNum.7: Dir
  435. * Return Value: DMA Status
  436. */
  437. uint32_t USB_DMA_Status (uint32_t EPNum) {
  438. uint32_t ptr, val;
  439. ptr = UDCA[EPAdr(EPNum)]; /* Current Descriptor */
  440. if (ptr == 0) 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) return ((uint32_t)(-1)); /* DMA Invalid */
  469. val = *((uint32_t *)(ptr + 2*4)); /* Buffer Address */
  470. return (val); /* Current Address */
  471. }
  472. /*
  473. * Get USB DMA Endpoint Current Buffer Count
  474. * Number of transfered Bytes or Iso Packets
  475. * Parameters: EPNum: Endpoint Number
  476. * EPNum.0..3: Address
  477. * EPNum.7: Dir
  478. * Return Value: DMA Count (or -1 when DMA is Invalid)
  479. */
  480. uint32_t USB_DMA_BufCnt (uint32_t EPNum) {
  481. uint32_t ptr, val;
  482. ptr = UDCA[EPAdr(EPNum)]; /* Current Descriptor */
  483. if (ptr == 0) return ((uint32_t)(-1)); /* DMA Invalid */
  484. val = *((uint32_t *)(ptr + 3*4)); /* Status Information */
  485. return (val >> 16); /* Current Count */
  486. }
  487. #endif /* USB_DMA */
  488. /*
  489. * Get USB Last Frame Number
  490. * Parameters: None
  491. * Return Value: Frame Number
  492. */
  493. uint32_t USB_GetFrame (void) {
  494. uint32_t val;
  495. WrCmd(CMD_RD_FRAME);
  496. val = RdCmdDat(DAT_RD_FRAME);
  497. val = val | (RdCmdDat(DAT_RD_FRAME) << 8);
  498. return (val);
  499. }
  500. /*
  501. * USB Interrupt Service Routine
  502. */
  503. void USB_IRQHandler (void) {
  504. uint32_t disr, val, n, m;
  505. uint32_t episr, episrCur;
  506. disr = LPC_USB->USBDevIntSt; /* Device Interrupt Status */
  507. /* Device Status Interrupt (Reset, Connect change, Suspend/Resume) */
  508. if (disr & DEV_STAT_INT) {
  509. LPC_USB->USBDevIntClr = DEV_STAT_INT;
  510. WrCmd(CMD_GET_DEV_STAT);
  511. val = RdCmdDat(DAT_GET_DEV_STAT); /* Device Status */
  512. if (val & DEV_RST) { /* Reset */
  513. USB_Reset();
  514. #if USB_RESET_EVENT
  515. USB_Reset_Event();
  516. #endif
  517. }
  518. if (val & DEV_CON_CH) { /* Connect change */
  519. #if USB_POWER_EVENT
  520. USB_Power_Event(val & DEV_CON);
  521. #endif
  522. }
  523. if (val & DEV_SUS_CH) { /* Suspend/Resume */
  524. if (val & DEV_SUS) { /* Suspend */
  525. USB_Suspend();
  526. #if USB_SUSPEND_EVENT
  527. USB_Suspend_Event();
  528. #endif
  529. } else { /* Resume */
  530. USB_Resume();
  531. #if USB_RESUME_EVENT
  532. USB_Resume_Event();
  533. #endif
  534. }
  535. }
  536. goto isr_end;
  537. }
  538. #if USB_SOF_EVENT
  539. /* Start of Frame Interrupt */
  540. if (disr & FRAME_INT) {
  541. LPC_USB->USBDevIntClr = FRAME_INT;
  542. USB_SOF_Event();
  543. }
  544. #endif
  545. #if USB_ERROR_EVENT
  546. /* Error Interrupt */
  547. if (disr & ERR_INT) {
  548. LPC_USB->USBDevIntClr = ERR_INT;
  549. WrCmd(CMD_RD_ERR_STAT);
  550. val = RdCmdDat(DAT_RD_ERR_STAT);
  551. USB_Error_Event(val);
  552. }
  553. #endif
  554. /* Endpoint's Slow Interrupt */
  555. if (disr & EP_SLOW_INT) {
  556. episrCur = 0;
  557. episr = LPC_USB->USBEpIntSt;
  558. for (n = 0; n < USB_EP_NUM; n++) { /* Check All Endpoints */
  559. if (episr == episrCur) break; /* break if all EP interrupts handled */
  560. if (episr & (1 << n)) {
  561. episrCur |= (1 << n);
  562. m = n >> 1;
  563. LPC_USB->USBEpIntClr = (1 << n);
  564. while ((LPC_USB->USBDevIntSt & CDFULL_INT) == 0);
  565. val = LPC_USB->USBCmdData;
  566. if ((n & 1) == 0) { /* OUT Endpoint */
  567. if (n == 0) { /* Control OUT Endpoint */
  568. if (val & EP_SEL_STP) { /* Setup Packet */
  569. if (USB_P_EP[0]) {
  570. USB_P_EP[0](USB_EVT_SETUP);
  571. continue;
  572. }
  573. }
  574. }
  575. if (USB_P_EP[m]) {
  576. USB_P_EP[m](USB_EVT_OUT);
  577. }
  578. } else { /* IN Endpoint */
  579. if (USB_P_EP[m]) {
  580. USB_P_EP[m](USB_EVT_IN);
  581. }
  582. }
  583. }
  584. }
  585. LPC_USB->USBDevIntClr = EP_SLOW_INT;
  586. }
  587. #if USB_DMA
  588. if (LPC_USB->USBDMAIntSt & 0x00000001) { /* End of Transfer Interrupt */
  589. val = LPC_USB->USBEoTIntSt;
  590. for (n = 2; n < USB_EP_NUM; n++) { /* Check All Endpoints */
  591. if (val & (1 << n)) {
  592. m = n >> 1;
  593. if ((n & 1) == 0) { /* OUT Endpoint */
  594. if (USB_P_EP[m]) {
  595. USB_P_EP[m](USB_EVT_OUT_DMA_EOT);
  596. }
  597. } else { /* IN Endpoint */
  598. if (USB_P_EP[m]) {
  599. USB_P_EP[m](USB_EVT_IN_DMA_EOT);
  600. }
  601. }
  602. }
  603. }
  604. LPC_USB->USBEoTIntClr = val;
  605. }
  606. if (LPC_USB->USBDMAIntSt & 0x00000002) { /* New DD Request Interrupt */
  607. val = LPC_USB->USBNDDRIntSt;
  608. for (n = 2; n < USB_EP_NUM; n++) { /* Check All Endpoints */
  609. if (val & (1 << n)) {
  610. m = n >> 1;
  611. if ((n & 1) == 0) { /* OUT Endpoint */
  612. if (USB_P_EP[m]) {
  613. USB_P_EP[m](USB_EVT_OUT_DMA_NDR);
  614. }
  615. } else { /* IN Endpoint */
  616. if (USB_P_EP[m]) {
  617. USB_P_EP[m](USB_EVT_IN_DMA_NDR);
  618. }
  619. }
  620. }
  621. }
  622. LPC_USB->USBNDDRIntClr = val;
  623. }
  624. if (LPC_USB->USBDMAIntSt & 0x00000004) { /* System Error Interrupt */
  625. val = LPC_USB->USBSysErrIntSt;
  626. for (n = 2; n < USB_EP_NUM; n++) { /* Check All Endpoints */
  627. if (val & (1 << n)) {
  628. m = n >> 1;
  629. if ((n & 1) == 0) { /* OUT Endpoint */
  630. if (USB_P_EP[m]) {
  631. USB_P_EP[m](USB_EVT_OUT_DMA_ERR);
  632. }
  633. } else { /* IN Endpoint */
  634. if (USB_P_EP[m]) {
  635. USB_P_EP[m](USB_EVT_IN_DMA_ERR);
  636. }
  637. }
  638. }
  639. }
  640. LPC_USB->USBSysErrIntClr = val;
  641. }
  642. #endif /* USB_DMA */
  643. isr_end:
  644. return;
  645. }