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.

преди 6 години
преди 6 години
преди 6 години
преди 6 години
преди 6 години
преди 6 години
преди 6 години
преди 6 години
преди 6 години
преди 6 години
преди 6 години
преди 6 години
преди 6 години
преди 6 години
преди 6 години
преди 6 години
преди 6 години
преди 6 години
преди 6 години
преди 6 години
преди 6 години
преди 6 години
преди 6 години
преди 6 години
преди 6 години
преди 6 години
преди 6 години
преди 6 години
преди 6 години
преди 6 години
преди 6 години
преди 6 години
преди 6 години
преди 6 години
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  1. #include "usb_dev.h"
  2. #define USB_DESC_LIST_DEFINE
  3. #include "usb_desc.h"
  4. #include "usb_serial.h"
  5. #include <string.h>
  6. #include "debug/printf.h"
  7. // device mode, page 3155
  8. typedef struct endpoint_struct endpoint_t;
  9. struct endpoint_struct {
  10. uint32_t config;
  11. uint32_t current;
  12. uint32_t next;
  13. uint32_t status;
  14. uint32_t pointer0;
  15. uint32_t pointer1;
  16. uint32_t pointer2;
  17. uint32_t pointer3;
  18. uint32_t pointer4;
  19. uint32_t reserved;
  20. uint32_t setup0;
  21. uint32_t setup1;
  22. transfer_t *first_transfer;
  23. transfer_t *last_transfer;
  24. void (*callback_function)(transfer_t *completed_transfer);
  25. uint32_t unused1;
  26. };
  27. /*struct transfer_struct {
  28. uint32_t next;
  29. uint32_t status;
  30. uint32_t pointer0;
  31. uint32_t pointer1;
  32. uint32_t pointer2;
  33. uint32_t pointer3;
  34. uint32_t pointer4;
  35. uint32_t callback_param;
  36. };*/
  37. endpoint_t endpoint_queue_head[(NUM_ENDPOINTS+1)*2] __attribute__ ((used, aligned(4096)));
  38. transfer_t endpoint0_transfer_data __attribute__ ((used, aligned(32)));
  39. transfer_t endpoint0_transfer_ack __attribute__ ((used, aligned(32)));
  40. typedef union {
  41. struct {
  42. union {
  43. struct {
  44. uint8_t bmRequestType;
  45. uint8_t bRequest;
  46. };
  47. uint16_t wRequestAndType;
  48. };
  49. uint16_t wValue;
  50. uint16_t wIndex;
  51. uint16_t wLength;
  52. };
  53. struct {
  54. uint32_t word1;
  55. uint32_t word2;
  56. };
  57. uint64_t bothwords;
  58. } setup_t;
  59. static setup_t endpoint0_setupdata;
  60. static uint32_t endpoint0_notify_mask=0;
  61. static uint32_t endpointN_notify_mask=0;
  62. //static int reset_count=0;
  63. volatile uint8_t usb_configuration = 0;
  64. static uint8_t endpoint0_buffer[8];
  65. static uint8_t usb_reboot_timer = 0;
  66. static void isr(void);
  67. static void endpoint0_setup(uint64_t setupdata);
  68. static void endpoint0_transmit(const void *data, uint32_t len, int notify);
  69. static void endpoint0_receive(void *data, uint32_t len, int notify);
  70. static void endpoint0_complete(void);
  71. void usb_init(void)
  72. {
  73. // TODO: only enable when VBUS detected
  74. // TODO: return to low power mode when VBUS removed
  75. // TODO: protect PMU access with MPU
  76. PMU_REG_3P0 = PMU_REG_3P0_OUTPUT_TRG(0x0F) | PMU_REG_3P0_BO_OFFSET(6)
  77. | PMU_REG_3P0_ENABLE_LINREG;
  78. // assume PLL3 is already running - already done by usb_pll_start() in main.c
  79. CCM_CCGR6 |= CCM_CCGR6_USBOH3(CCM_CCGR_ON); // turn on clocks to USB peripheral
  80. // Before programming this register, the PHY clocks must be enabled in registers
  81. // USBPHYx_CTRLn and CCM_ANALOG_USBPHYx_PLL_480_CTRLn.
  82. //printf("USBPHY1_PWD=%08lX\n", USBPHY1_PWD);
  83. //printf("USBPHY1_TX=%08lX\n", USBPHY1_TX);
  84. //printf("USBPHY1_RX=%08lX\n", USBPHY1_RX);
  85. //printf("USBPHY1_CTRL=%08lX\n", USBPHY1_CTRL);
  86. //printf("USB1_USBMODE=%08lX\n", USB1_USBMODE);
  87. // turn on PLL3, wait for 480 MHz lock?
  88. // turn on CCM clock gates? CCGR6[CG0]
  89. #if 1
  90. if ((USBPHY1_PWD & (USBPHY_PWD_RXPWDRX | USBPHY_PWD_RXPWDDIFF | USBPHY_PWD_RXPWD1PT1
  91. | USBPHY_PWD_RXPWDENV | USBPHY_PWD_TXPWDV2I | USBPHY_PWD_TXPWDIBIAS
  92. | USBPHY_PWD_TXPWDFS)) || (USB1_USBMODE & USB_USBMODE_CM_MASK)) {
  93. // USB controller is turned on from previous use
  94. // reset needed to turn it off & start from clean slate
  95. USBPHY1_CTRL_SET = USBPHY_CTRL_SFTRST; // USBPHY1_CTRL page 3292
  96. USB1_USBCMD |= USB_USBCMD_RST; // reset controller
  97. int count=0;
  98. while (USB1_USBCMD & USB_USBCMD_RST) count++;
  99. NVIC_CLEAR_PENDING(IRQ_USB1);
  100. USBPHY1_CTRL_CLR = USBPHY_CTRL_SFTRST; // reset PHY
  101. //USB1_USBSTS = USB1_USBSTS; // TODO: is this needed?
  102. printf("USB reset took %d loops\n", count);
  103. //delay(10);
  104. //printf("\n");
  105. //printf("USBPHY1_PWD=%08lX\n", USBPHY1_PWD);
  106. //printf("USBPHY1_TX=%08lX\n", USBPHY1_TX);
  107. //printf("USBPHY1_RX=%08lX\n", USBPHY1_RX);
  108. //printf("USBPHY1_CTRL=%08lX\n", USBPHY1_CTRL);
  109. //printf("USB1_USBMODE=%08lX\n", USB1_USBMODE);
  110. //delay(500);
  111. }
  112. #endif
  113. // Device Controller Initialization, page 3161
  114. // USBCMD pg 3216
  115. // USBSTS pg 3220
  116. // USBINTR pg 3224
  117. // DEVICEADDR pg 3227
  118. // ENDPTLISTADDR 3229
  119. // USBMODE pg 3244
  120. // ENDPTSETUPSTAT 3245
  121. // ENDPTPRIME pg 3246
  122. // ENDPTFLUSH pg 3247
  123. // ENDPTSTAT pg 3247
  124. // ENDPTCOMPLETE 3248
  125. // ENDPTCTRL0 pg 3249
  126. USBPHY1_CTRL_CLR = USBPHY_CTRL_CLKGATE;
  127. USBPHY1_PWD = 0;
  128. //printf("USBPHY1_PWD=%08lX\n", USBPHY1_PWD);
  129. //printf("USBPHY1_CTRL=%08lX\n", USBPHY1_CTRL);
  130. USB1_USBMODE = USB_USBMODE_CM(2) | USB_USBMODE_SLOM;
  131. memset(endpoint_queue_head, 0, sizeof(endpoint_queue_head));
  132. endpoint_queue_head[0].config = (64 << 16) | (1 << 15);
  133. endpoint_queue_head[1].config = (64 << 16);
  134. USB1_ENDPOINTLISTADDR = (uint32_t)&endpoint_queue_head;
  135. // Recommended: enable all device interrupts including: USBINT, USBERRINT,
  136. // Port Change Detect, USB Reset Received, DCSuspend.
  137. USB1_USBINTR = USB_USBINTR_UE | USB_USBINTR_UEE | /* USB_USBINTR_PCE | */
  138. USB_USBINTR_URE | USB_USBINTR_SLE;
  139. //_VectorsRam[IRQ_USB1+16] = &isr;
  140. attachInterruptVector(IRQ_USB1, &isr);
  141. NVIC_ENABLE_IRQ(IRQ_USB1);
  142. //printf("USB1_ENDPTCTRL0=%08lX\n", USB1_ENDPTCTRL0);
  143. //printf("USB1_ENDPTCTRL1=%08lX\n", USB1_ENDPTCTRL1);
  144. //printf("USB1_ENDPTCTRL2=%08lX\n", USB1_ENDPTCTRL2);
  145. //printf("USB1_ENDPTCTRL3=%08lX\n", USB1_ENDPTCTRL3);
  146. USB1_USBCMD |= USB_USBCMD_RS;
  147. }
  148. static void isr(void)
  149. {
  150. //printf("*");
  151. // Port control in device mode is only used for
  152. // status port reset, suspend, and current connect status.
  153. uint32_t status = USB1_USBSTS;
  154. USB1_USBSTS = status;
  155. // USB_USBSTS_SLI - set to 1 when enters a suspend state from an active state
  156. // USB_USBSTS_SRI - set at start of frame
  157. // USB_USBSTS_SRI - set when USB reset detected
  158. if (status & USB_USBSTS_UI) {
  159. //printf("data\n");
  160. uint32_t setupstatus = USB1_ENDPTSETUPSTAT;
  161. //printf("USB1_ENDPTSETUPSTAT=%X\n", setupstatus);
  162. while (setupstatus) {
  163. USB1_ENDPTSETUPSTAT = setupstatus;
  164. setup_t s;
  165. do {
  166. USB1_USBCMD |= USB_USBCMD_SUTW;
  167. s.word1 = endpoint_queue_head[0].setup0;
  168. s.word2 = endpoint_queue_head[0].setup1;
  169. } while (!(USB1_USBCMD & USB_USBCMD_SUTW));
  170. USB1_USBCMD &= ~USB_USBCMD_SUTW;
  171. //printf("setup %08lX %08lX\n", s.word1, s.word2);
  172. USB1_ENDPTFLUSH = (1<<16) | (1<<0); // page 3174
  173. while (USB1_ENDPTFLUSH & ((1<<16) | (1<<0))) ;
  174. endpoint0_notify_mask = 0;
  175. endpoint0_setup(s.bothwords);
  176. setupstatus = USB1_ENDPTSETUPSTAT; // page 3175
  177. }
  178. uint32_t completestatus = USB1_ENDPTCOMPLETE;
  179. if (completestatus) {
  180. USB1_ENDPTCOMPLETE = completestatus;
  181. //printf("USB1_ENDPTCOMPLETE=%lX\n", completestatus);
  182. if (completestatus & endpoint0_notify_mask) {
  183. endpoint0_notify_mask = 0;
  184. endpoint0_complete();
  185. }
  186. if (completestatus & endpointN_notify_mask) {
  187. // TODO: callback functions...
  188. }
  189. }
  190. }
  191. if (status & USB_USBSTS_URI) { // page 3164
  192. USB1_ENDPTSETUPSTAT = USB1_ENDPTSETUPSTAT; // Clear all setup token semaphores
  193. USB1_ENDPTCOMPLETE = USB1_ENDPTCOMPLETE; // Clear all the endpoint complete status
  194. while (USB1_ENDPTPRIME != 0) ; // Wait for any endpoint priming
  195. USB1_ENDPTFLUSH = 0xFFFFFFFF; // Cancel all endpoint primed status
  196. if ((USB1_PORTSC1 & USB_PORTSC1_PR)) {
  197. //printf("reset\n");
  198. } else {
  199. // we took too long to respond :(
  200. // TODO; is this ever really a problem?
  201. //printf("reset too slow\n");
  202. }
  203. // TODO: Free all allocated dTDs
  204. //if (++reset_count >= 3) {
  205. // shut off USB - easier to see results in protocol analyzer
  206. //USB1_USBCMD &= ~USB_USBCMD_RS;
  207. //printf("shut off USB\n");
  208. //}
  209. }
  210. if (status & USB_USBSTS_PCI) {
  211. if (USB1_PORTSC1 & USB_PORTSC1_HSP) {
  212. //printf("port at 480 Mbit\n");
  213. } else {
  214. //printf("port at 12 Mbit\n");
  215. }
  216. }
  217. if (status & USB_USBSTS_SLI) { // page 3165
  218. //printf("suspend\n");
  219. }
  220. if (status & USB_USBSTS_UEI) {
  221. //printf("error\n");
  222. }
  223. if ((USB1_USBINTR & USB_USBINTR_SRE) && (status & USB_USBSTS_SRI)) {
  224. printf("sof %d\n", usb_reboot_timer);
  225. if (usb_reboot_timer) {
  226. if (--usb_reboot_timer == 0) {
  227. asm("bkpt #251"); // run bootloader
  228. }
  229. } else {
  230. // turn off the SOF interrupt if nothing using it
  231. USB1_USBINTR &= ~USB_USBINTR_SRE;
  232. }
  233. }
  234. }
  235. /*
  236. struct transfer_struct { // table 55-60, pg 3159
  237. uint32_t next;
  238. uint32_t status;
  239. uint32_t pointer0;
  240. uint32_t pointer1;
  241. uint32_t pointer2;
  242. uint32_t pointer3;
  243. uint32_t pointer4;
  244. uint32_t unused1;
  245. };
  246. transfer_t endpoint0_transfer_data __attribute__ ((aligned(32)));;
  247. transfer_t endpoint0_transfer_ack __attribute__ ((aligned(32)));;
  248. */
  249. static void endpoint0_setup(uint64_t setupdata)
  250. {
  251. setup_t setup;
  252. uint32_t datalen = 0;
  253. const usb_descriptor_list_t *list;
  254. setup.bothwords = setupdata;
  255. switch (setup.wRequestAndType) {
  256. case 0x0500: // SET_ADDRESS
  257. endpoint0_receive(NULL, 0, 0);
  258. USB1_DEVICEADDR = USB_DEVICEADDR_USBADR(setup.wValue) | USB_DEVICEADDR_USBADRA;
  259. return;
  260. case 0x0900: // SET_CONFIGURATION
  261. usb_configuration = setup.wValue;
  262. // configure all other endpoints
  263. volatile uint32_t *reg = &USB1_ENDPTCTRL1;
  264. const uint32_t *cfg = usb_endpoint_config_table;
  265. int i;
  266. for (i=0; i < NUM_ENDPOINTS; i++) {
  267. uint32_t n = *cfg++;
  268. *reg = n;
  269. // TODO: do the TRX & RXR bits self clear??
  270. uint32_t m = n & ~(USB_ENDPTCTRL_TXR | USB_ENDPTCTRL_RXR);
  271. *reg = m;
  272. //uint32_t p = *reg;
  273. //printf(" ep=%d: cfg=%08lX - %08lX - %08lX\n", i + 1, n, m, p);
  274. reg++;
  275. }
  276. // TODO: configure all queue heads with max packet length, zlt & mult
  277. endpoint_queue_head[CDC_ACM_ENDPOINT*2+1].config = (CDC_ACM_ENDPOINT << 16);
  278. endpoint_queue_head[CDC_RX_ENDPOINT*2+0].config = (CDC_RX_SIZE << 16) | (1 << 29);
  279. endpoint_queue_head[CDC_TX_ENDPOINT*2+1].config = (CDC_TX_SIZE << 16) | (1 << 29);
  280. // TODO: de-allocate any pending transfers?
  281. endpoint0_receive(NULL, 0, 0);
  282. return;
  283. case 0x0680: // GET_DESCRIPTOR
  284. case 0x0681:
  285. //printf("desc:\n"); // yay - sending device descriptor now works!!!!
  286. for (list = usb_descriptor_list; list->addr != NULL; list++) {
  287. if (setup.wValue == list->wValue && setup.wIndex == list->wIndex) {
  288. if ((setup.wValue >> 8) == 3) {
  289. // for string descriptors, use the descriptor's
  290. // length field, allowing runtime configured length.
  291. datalen = *(list->addr);
  292. } else {
  293. datalen = list->length;
  294. }
  295. if (datalen > setup.wLength) datalen = setup.wLength;
  296. endpoint0_transmit(list->addr, datalen, 0);
  297. return;
  298. }
  299. }
  300. break;
  301. case 0x2221: // CDC_SET_CONTROL_LINE_STATE
  302. //usb_cdc_line_rtsdtr_millis = systick_millis_count;
  303. //usb_cdc_line_rtsdtr = setup.wValue;
  304. case 0x2321: // CDC_SEND_BREAK
  305. endpoint0_receive(NULL, 0, 0);
  306. return;
  307. case 0x2021: // CDC_SET_LINE_CODING
  308. if (setup.wLength != 7) break;
  309. endpoint0_setupdata.bothwords = setupdata;
  310. endpoint0_receive(endpoint0_buffer, 7, 1);
  311. return;
  312. }
  313. USB1_ENDPTCTRL0 = 0x000010001; // stall
  314. }
  315. static void endpoint0_transmit(const void *data, uint32_t len, int notify)
  316. {
  317. //printf("tx %lu\n", len);
  318. if (len > 0) {
  319. // Executing A Transfer Descriptor, page 3182
  320. endpoint0_transfer_data.next = 1;
  321. endpoint0_transfer_data.status = (len << 16) | (1<<7);
  322. uint32_t addr = (uint32_t)data;
  323. endpoint0_transfer_data.pointer0 = addr; // format: table 55-60, pg 3159
  324. endpoint0_transfer_data.pointer1 = addr + 4096;
  325. endpoint0_transfer_data.pointer2 = addr + 8192;
  326. endpoint0_transfer_data.pointer3 = addr + 12288;
  327. endpoint0_transfer_data.pointer4 = addr + 16384;
  328. // Case 1: Link list is empty, page 3182
  329. endpoint_queue_head[1].next = (uint32_t)&endpoint0_transfer_data;
  330. endpoint_queue_head[1].status = 0;
  331. USB1_ENDPTPRIME |= (1<<16);
  332. while (USB1_ENDPTPRIME) ;
  333. }
  334. endpoint0_transfer_ack.next = 1;
  335. endpoint0_transfer_ack.status = (1<<7) | (notify ? (1 << 15) : 0);
  336. endpoint0_transfer_ack.pointer0 = 0;
  337. endpoint_queue_head[0].next = (uint32_t)&endpoint0_transfer_ack;
  338. endpoint_queue_head[0].status = 0;
  339. USB1_ENDPTPRIME |= (1<<0);
  340. endpoint0_notify_mask = (notify ? (1 << 0) : 0);
  341. while (USB1_ENDPTPRIME) ;
  342. }
  343. static void endpoint0_receive(void *data, uint32_t len, int notify)
  344. {
  345. //printf("rx %lu\n", len);
  346. if (len > 0) {
  347. // Executing A Transfer Descriptor, page 3182
  348. endpoint0_transfer_data.next = 1;
  349. endpoint0_transfer_data.status = (len << 16) | (1<<7);
  350. uint32_t addr = (uint32_t)data;
  351. endpoint0_transfer_data.pointer0 = addr; // format: table 55-60, pg 3159
  352. endpoint0_transfer_data.pointer1 = addr + 4096;
  353. endpoint0_transfer_data.pointer2 = addr + 8192;
  354. endpoint0_transfer_data.pointer3 = addr + 12288;
  355. endpoint0_transfer_data.pointer4 = addr + 16384;
  356. // Case 1: Link list is empty, page 3182
  357. endpoint_queue_head[0].next = (uint32_t)&endpoint0_transfer_data;
  358. endpoint_queue_head[0].status = 0;
  359. USB1_ENDPTPRIME |= (1<<0);
  360. while (USB1_ENDPTPRIME) ;
  361. }
  362. endpoint0_transfer_ack.next = 1;
  363. endpoint0_transfer_ack.status = (1<<7) | (notify ? (1 << 15) : 0);
  364. endpoint0_transfer_ack.pointer0 = 0;
  365. endpoint_queue_head[1].next = (uint32_t)&endpoint0_transfer_ack;
  366. endpoint_queue_head[1].status = 0;
  367. USB1_ENDPTPRIME |= (1<<16);
  368. endpoint0_notify_mask = (notify ? (1 << 16) : 0);
  369. while (USB1_ENDPTPRIME) ;
  370. }
  371. /*typedef union {
  372. struct {
  373. union {
  374. struct {
  375. uint8_t bmRequestType;
  376. uint8_t bRequest;
  377. };
  378. uint16_t wRequestAndType;
  379. };
  380. uint16_t wValue;
  381. uint16_t wIndex;
  382. uint16_t wLength;
  383. };
  384. struct {
  385. uint32_t word1;
  386. uint32_t word2;
  387. };
  388. uint64_t bothwords;
  389. } setup_t; */
  390. static void endpoint0_complete(void)
  391. {
  392. setup_t setup;
  393. setup.bothwords = endpoint0_setupdata.bothwords;
  394. //printf("complete\n");
  395. #ifdef CDC_STATUS_INTERFACE
  396. if (setup.wRequestAndType == 0x2021 /*CDC_SET_LINE_CODING*/) {
  397. memcpy(usb_cdc_line_coding, endpoint0_buffer, 7);
  398. printf("usb_cdc_line_coding, baud=%u\n", usb_cdc_line_coding[0]);
  399. if (usb_cdc_line_coding[0] == 134) {
  400. USB1_USBINTR |= USB_USBINTR_SRE;
  401. usb_reboot_timer = 80; // TODO: 10 if only 12 Mbit/sec
  402. }
  403. }
  404. #endif
  405. }
  406. void usb_prepare_transfer(transfer_t *transfer, const void *data, uint32_t len, uint32_t param)
  407. {
  408. transfer->next = 1;
  409. transfer->status = (len << 16) | (1<<7);
  410. uint32_t addr = (uint32_t)data;
  411. transfer->pointer0 = addr;
  412. transfer->pointer1 = addr + 4096;
  413. transfer->pointer2 = addr + 8192;
  414. transfer->pointer3 = addr + 12288;
  415. transfer->pointer4 = addr + 16384;
  416. transfer->callback_param = param;
  417. }
  418. void usb_transmit(int endpoint_number, transfer_t *transfer)
  419. {
  420. // endpoint 0 reserved for control
  421. // endpoint 1 reserved for debug
  422. //printf("usb_transmit %d\n", endpoint_number);
  423. if (endpoint_number < 2 || endpoint_number > NUM_ENDPOINTS) return;
  424. endpoint_t *endpoint = &endpoint_queue_head[endpoint_number * 2 + 1];
  425. if (endpoint->callback_function) {
  426. transfer->status |= (1<<15);
  427. } else {
  428. //transfer->status |= (1<<15);
  429. // remove all inactive transfers
  430. }
  431. uint32_t mask = 1 << (endpoint_number + 16);
  432. __disable_irq();
  433. #if 0
  434. if (endpoint->last_transfer) {
  435. if (!(endpoint->last_transfer->status & (1<<7))) {
  436. endpoint->last_transfer->next = (uint32_t)transfer;
  437. } else {
  438. // Case 2: Link list is not empty, page 3182
  439. endpoint->last_transfer->next = (uint32_t)transfer;
  440. if (USB1_ENDPTPRIME & mask) {
  441. endpoint->last_transfer = transfer;
  442. __enable_irq();
  443. printf(" case 2a\n");
  444. return;
  445. }
  446. uint32_t stat;
  447. uint32_t cmd = USB1_USBCMD;
  448. do {
  449. USB1_USBCMD = cmd | USB_USBCMD_ATDTW;
  450. stat = USB1_ENDPTSTATUS;
  451. } while (!(USB1_USBCMD & USB_USBCMD_ATDTW));
  452. USB1_USBCMD = cmd & ~USB_USBCMD_ATDTW;
  453. if (stat & mask) {
  454. endpoint->last_transfer = transfer;
  455. __enable_irq();
  456. printf(" case 2b\n");
  457. return;
  458. }
  459. }
  460. } else {
  461. endpoint->first_transfer = transfer;
  462. }
  463. endpoint->last_transfer = transfer;
  464. #endif
  465. // Case 1: Link list is empty, page 3182
  466. endpoint->next = (uint32_t)transfer;
  467. endpoint->status = 0;
  468. USB1_ENDPTPRIME |= mask;
  469. while (USB1_ENDPTPRIME & mask) ;
  470. __enable_irq();
  471. //printf(" case 1\n");
  472. // ENDPTPRIME - momentarily set by hardware during hardware re-priming
  473. // operations when a dTD is retired, and the dQH is updated.
  474. // ENDPTSTAT - Transmit Buffer Ready - set to one by the hardware as a
  475. // response to receiving a command from a corresponding bit
  476. // in the ENDPTPRIME register. . Buffer ready is cleared by
  477. // USB reset, by the USB DMA system, or through the ENDPTFLUSH
  478. // register. (so 0=buffer ready, 1=buffer primed for transmit)
  479. }
  480. /*struct endpoint_struct {
  481. uint32_t config;
  482. uint32_t current;
  483. uint32_t next;
  484. uint32_t status;
  485. uint32_t pointer0;
  486. uint32_t pointer1;
  487. uint32_t pointer2;
  488. uint32_t pointer3;
  489. uint32_t pointer4;
  490. uint32_t reserved;
  491. uint32_t setup0;
  492. uint32_t setup1;
  493. transfer_t *first_transfer;
  494. transfer_t *last_transfer;
  495. void (*callback_function)(transfer_t *completed_transfer);
  496. uint32_t unused1;
  497. };*/