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.

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