Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

702 lines
21KB

  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. //#define LOG_SIZE 20
  9. //uint32_t transfer_log_head=0;
  10. //uint32_t transfer_log_count=0;
  11. //uint32_t transfer_log[LOG_SIZE];
  12. // device mode, page 3155
  13. typedef struct endpoint_struct endpoint_t;
  14. struct endpoint_struct {
  15. uint32_t config;
  16. uint32_t current;
  17. uint32_t next;
  18. uint32_t status;
  19. uint32_t pointer0;
  20. uint32_t pointer1;
  21. uint32_t pointer2;
  22. uint32_t pointer3;
  23. uint32_t pointer4;
  24. uint32_t reserved;
  25. uint32_t setup0;
  26. uint32_t setup1;
  27. transfer_t *first_transfer;
  28. transfer_t *last_transfer;
  29. void (*callback_function)(transfer_t *completed_transfer);
  30. uint32_t unused1;
  31. };
  32. /*struct transfer_struct {
  33. uint32_t next;
  34. uint32_t status;
  35. uint32_t pointer0;
  36. uint32_t pointer1;
  37. uint32_t pointer2;
  38. uint32_t pointer3;
  39. uint32_t pointer4;
  40. uint32_t callback_param;
  41. };*/
  42. endpoint_t endpoint_queue_head[(NUM_ENDPOINTS+1)*2] __attribute__ ((used, aligned(4096)));
  43. transfer_t endpoint0_transfer_data __attribute__ ((used, aligned(32)));
  44. transfer_t endpoint0_transfer_ack __attribute__ ((used, aligned(32)));
  45. typedef union {
  46. struct {
  47. union {
  48. struct {
  49. uint8_t bmRequestType;
  50. uint8_t bRequest;
  51. };
  52. uint16_t wRequestAndType;
  53. };
  54. uint16_t wValue;
  55. uint16_t wIndex;
  56. uint16_t wLength;
  57. };
  58. struct {
  59. uint32_t word1;
  60. uint32_t word2;
  61. };
  62. uint64_t bothwords;
  63. } setup_t;
  64. static setup_t endpoint0_setupdata;
  65. static uint32_t endpoint0_notify_mask=0;
  66. static uint32_t endpointN_notify_mask=0;
  67. //static int reset_count=0;
  68. volatile uint8_t usb_configuration = 0;
  69. static uint8_t endpoint0_buffer[8];
  70. static uint8_t usb_reboot_timer = 0;
  71. void (*usb_timer0_callback)(void) = NULL;
  72. void (*usb_timer1_callback)(void) = NULL;
  73. static void isr(void);
  74. static void endpoint0_setup(uint64_t setupdata);
  75. static void endpoint0_transmit(const void *data, uint32_t len, int notify);
  76. static void endpoint0_receive(void *data, uint32_t len, int notify);
  77. static void endpoint0_complete(void);
  78. static void run_callbacks(endpoint_t *ep);
  79. __attribute__((section(".progmem")))
  80. void usb_init(void)
  81. {
  82. // TODO: only enable when VBUS detected
  83. // TODO: return to low power mode when VBUS removed
  84. // TODO: protect PMU access with MPU
  85. PMU_REG_3P0 = PMU_REG_3P0_OUTPUT_TRG(0x0F) | PMU_REG_3P0_BO_OFFSET(6)
  86. | PMU_REG_3P0_ENABLE_LINREG;
  87. usb_init_serialnumber();
  88. // assume PLL3 is already running - already done by usb_pll_start() in main.c
  89. CCM_CCGR6 |= CCM_CCGR6_USBOH3(CCM_CCGR_ON); // turn on clocks to USB peripheral
  90. printf("BURSTSIZE=%08lX\n", USB1_BURSTSIZE);
  91. //USB1_BURSTSIZE = USB_BURSTSIZE_TXPBURST(4) | USB_BURSTSIZE_RXPBURST(4);
  92. USB1_BURSTSIZE = 0x0404;
  93. printf("BURSTSIZE=%08lX\n", USB1_BURSTSIZE);
  94. printf("USB1_TXFILLTUNING=%08lX\n", USB1_TXFILLTUNING);
  95. // Before programming this register, the PHY clocks must be enabled in registers
  96. // USBPHYx_CTRLn and CCM_ANALOG_USBPHYx_PLL_480_CTRLn.
  97. //printf("USBPHY1_PWD=%08lX\n", USBPHY1_PWD);
  98. //printf("USBPHY1_TX=%08lX\n", USBPHY1_TX);
  99. //printf("USBPHY1_RX=%08lX\n", USBPHY1_RX);
  100. //printf("USBPHY1_CTRL=%08lX\n", USBPHY1_CTRL);
  101. //printf("USB1_USBMODE=%08lX\n", USB1_USBMODE);
  102. // turn on PLL3, wait for 480 MHz lock?
  103. // turn on CCM clock gates? CCGR6[CG0]
  104. #if 1
  105. if ((USBPHY1_PWD & (USBPHY_PWD_RXPWDRX | USBPHY_PWD_RXPWDDIFF | USBPHY_PWD_RXPWD1PT1
  106. | USBPHY_PWD_RXPWDENV | USBPHY_PWD_TXPWDV2I | USBPHY_PWD_TXPWDIBIAS
  107. | USBPHY_PWD_TXPWDFS)) || (USB1_USBMODE & USB_USBMODE_CM_MASK)) {
  108. // USB controller is turned on from previous use
  109. // reset needed to turn it off & start from clean slate
  110. USBPHY1_CTRL_SET = USBPHY_CTRL_SFTRST; // USBPHY1_CTRL page 3292
  111. USB1_USBCMD |= USB_USBCMD_RST; // reset controller
  112. int count=0;
  113. while (USB1_USBCMD & USB_USBCMD_RST) count++;
  114. NVIC_CLEAR_PENDING(IRQ_USB1);
  115. USBPHY1_CTRL_CLR = USBPHY_CTRL_SFTRST; // reset PHY
  116. //USB1_USBSTS = USB1_USBSTS; // TODO: is this needed?
  117. printf("USB reset took %d loops\n", count);
  118. //delay(10);
  119. //printf("\n");
  120. //printf("USBPHY1_PWD=%08lX\n", USBPHY1_PWD);
  121. //printf("USBPHY1_TX=%08lX\n", USBPHY1_TX);
  122. //printf("USBPHY1_RX=%08lX\n", USBPHY1_RX);
  123. //printf("USBPHY1_CTRL=%08lX\n", USBPHY1_CTRL);
  124. //printf("USB1_USBMODE=%08lX\n", USB1_USBMODE);
  125. delay(25);
  126. }
  127. #endif
  128. // Device Controller Initialization, page 3161
  129. // USBCMD pg 3216
  130. // USBSTS pg 3220
  131. // USBINTR pg 3224
  132. // DEVICEADDR pg 3227
  133. // ENDPTLISTADDR 3229
  134. // USBMODE pg 3244
  135. // ENDPTSETUPSTAT 3245
  136. // ENDPTPRIME pg 3246
  137. // ENDPTFLUSH pg 3247
  138. // ENDPTSTAT pg 3247
  139. // ENDPTCOMPLETE 3248
  140. // ENDPTCTRL0 pg 3249
  141. USBPHY1_CTRL_CLR = USBPHY_CTRL_CLKGATE;
  142. USBPHY1_PWD = 0;
  143. //printf("USBPHY1_PWD=%08lX\n", USBPHY1_PWD);
  144. //printf("USBPHY1_CTRL=%08lX\n", USBPHY1_CTRL);
  145. USB1_USBMODE = USB_USBMODE_CM(2) | USB_USBMODE_SLOM;
  146. memset(endpoint_queue_head, 0, sizeof(endpoint_queue_head));
  147. endpoint_queue_head[0].config = (64 << 16) | (1 << 15);
  148. endpoint_queue_head[1].config = (64 << 16);
  149. USB1_ENDPOINTLISTADDR = (uint32_t)&endpoint_queue_head;
  150. // Recommended: enable all device interrupts including: USBINT, USBERRINT,
  151. // Port Change Detect, USB Reset Received, DCSuspend.
  152. USB1_USBINTR = USB_USBINTR_UE | USB_USBINTR_UEE | /* USB_USBINTR_PCE | */
  153. USB_USBINTR_URE | USB_USBINTR_SLE;
  154. //_VectorsRam[IRQ_USB1+16] = &isr;
  155. attachInterruptVector(IRQ_USB1, &isr);
  156. NVIC_ENABLE_IRQ(IRQ_USB1);
  157. //printf("USB1_ENDPTCTRL0=%08lX\n", USB1_ENDPTCTRL0);
  158. //printf("USB1_ENDPTCTRL1=%08lX\n", USB1_ENDPTCTRL1);
  159. //printf("USB1_ENDPTCTRL2=%08lX\n", USB1_ENDPTCTRL2);
  160. //printf("USB1_ENDPTCTRL3=%08lX\n", USB1_ENDPTCTRL3);
  161. USB1_USBCMD = USB_USBCMD_RS;
  162. //transfer_log_head = 0;
  163. //transfer_log_count = 0;
  164. }
  165. static void isr(void)
  166. {
  167. //printf("*");
  168. // Port control in device mode is only used for
  169. // status port reset, suspend, and current connect status.
  170. uint32_t status = USB1_USBSTS;
  171. USB1_USBSTS = status;
  172. // USB_USBSTS_SLI - set to 1 when enters a suspend state from an active state
  173. // USB_USBSTS_SRI - set at start of frame
  174. // USB_USBSTS_SRI - set when USB reset detected
  175. if (status & USB_USBSTS_UI) {
  176. //printf("data\n");
  177. uint32_t setupstatus = USB1_ENDPTSETUPSTAT;
  178. //printf("USB1_ENDPTSETUPSTAT=%X\n", setupstatus);
  179. while (setupstatus) {
  180. USB1_ENDPTSETUPSTAT = setupstatus;
  181. setup_t s;
  182. do {
  183. USB1_USBCMD |= USB_USBCMD_SUTW;
  184. s.word1 = endpoint_queue_head[0].setup0;
  185. s.word2 = endpoint_queue_head[0].setup1;
  186. } while (!(USB1_USBCMD & USB_USBCMD_SUTW));
  187. USB1_USBCMD &= ~USB_USBCMD_SUTW;
  188. //printf("setup %08lX %08lX\n", s.word1, s.word2);
  189. USB1_ENDPTFLUSH = (1<<16) | (1<<0); // page 3174
  190. while (USB1_ENDPTFLUSH & ((1<<16) | (1<<0))) ;
  191. endpoint0_notify_mask = 0;
  192. endpoint0_setup(s.bothwords);
  193. setupstatus = USB1_ENDPTSETUPSTAT; // page 3175
  194. }
  195. uint32_t completestatus = USB1_ENDPTCOMPLETE;
  196. if (completestatus) {
  197. USB1_ENDPTCOMPLETE = completestatus;
  198. //printf("USB1_ENDPTCOMPLETE=%lX\n", completestatus);
  199. if (completestatus & endpoint0_notify_mask) {
  200. endpoint0_notify_mask = 0;
  201. endpoint0_complete();
  202. }
  203. completestatus &= endpointN_notify_mask;
  204. if (completestatus) {
  205. int i; // TODO: optimize with __builtin_ctz()
  206. for (i=2; i < NUM_ENDPOINTS; i++) {
  207. if (completestatus & (1 << i)) { // receive
  208. run_callbacks(endpoint_queue_head + i * 2);
  209. }
  210. if (completestatus & (1 << (i + 16))) { // transmit
  211. run_callbacks(endpoint_queue_head + i * 2 + 1);
  212. }
  213. }
  214. }
  215. }
  216. }
  217. if (status & USB_USBSTS_URI) { // page 3164
  218. USB1_ENDPTSETUPSTAT = USB1_ENDPTSETUPSTAT; // Clear all setup token semaphores
  219. USB1_ENDPTCOMPLETE = USB1_ENDPTCOMPLETE; // Clear all the endpoint complete status
  220. while (USB1_ENDPTPRIME != 0) ; // Wait for any endpoint priming
  221. USB1_ENDPTFLUSH = 0xFFFFFFFF; // Cancel all endpoint primed status
  222. if ((USB1_PORTSC1 & USB_PORTSC1_PR)) {
  223. //printf("reset\n");
  224. } else {
  225. // we took too long to respond :(
  226. // TODO; is this ever really a problem?
  227. //printf("reset too slow\n");
  228. }
  229. #if defined(CDC_STATUS_INTERFACE) && defined(CDC_DATA_INTERFACE)
  230. usb_serial_reset();
  231. #endif
  232. endpointN_notify_mask = 0;
  233. // TODO: Free all allocated dTDs
  234. //if (++reset_count >= 3) {
  235. // shut off USB - easier to see results in protocol analyzer
  236. //USB1_USBCMD &= ~USB_USBCMD_RS;
  237. //printf("shut off USB\n");
  238. //}
  239. }
  240. if (status & USB_USBSTS_TI0) {
  241. if (usb_timer0_callback != NULL) usb_timer0_callback();
  242. }
  243. if (status & USB_USBSTS_TI1) {
  244. if (usb_timer1_callback != NULL) usb_timer1_callback();
  245. }
  246. if (status & USB_USBSTS_PCI) {
  247. if (USB1_PORTSC1 & USB_PORTSC1_HSP) {
  248. //printf("port at 480 Mbit\n");
  249. } else {
  250. //printf("port at 12 Mbit\n");
  251. }
  252. }
  253. if (status & USB_USBSTS_SLI) { // page 3165
  254. //printf("suspend\n");
  255. }
  256. if (status & USB_USBSTS_UEI) {
  257. //printf("error\n");
  258. }
  259. if ((USB1_USBINTR & USB_USBINTR_SRE) && (status & USB_USBSTS_SRI)) {
  260. printf("sof %d\n", usb_reboot_timer);
  261. if (usb_reboot_timer) {
  262. if (--usb_reboot_timer == 0) {
  263. asm("bkpt #251"); // run bootloader
  264. }
  265. } else {
  266. // turn off the SOF interrupt if nothing using it
  267. USB1_USBINTR &= ~USB_USBINTR_SRE;
  268. }
  269. }
  270. }
  271. /*
  272. struct transfer_struct { // table 55-60, pg 3159
  273. uint32_t next;
  274. uint32_t status;
  275. uint32_t pointer0;
  276. uint32_t pointer1;
  277. uint32_t pointer2;
  278. uint32_t pointer3;
  279. uint32_t pointer4;
  280. uint32_t unused1;
  281. };
  282. transfer_t endpoint0_transfer_data __attribute__ ((aligned(32)));;
  283. transfer_t endpoint0_transfer_ack __attribute__ ((aligned(32)));;
  284. */
  285. static void endpoint0_setup(uint64_t setupdata)
  286. {
  287. setup_t setup;
  288. uint32_t datalen = 0;
  289. const usb_descriptor_list_t *list;
  290. setup.bothwords = setupdata;
  291. switch (setup.wRequestAndType) {
  292. case 0x0500: // SET_ADDRESS
  293. endpoint0_receive(NULL, 0, 0);
  294. USB1_DEVICEADDR = USB_DEVICEADDR_USBADR(setup.wValue) | USB_DEVICEADDR_USBADRA;
  295. return;
  296. case 0x0900: // SET_CONFIGURATION
  297. usb_configuration = setup.wValue;
  298. // configure all other endpoints
  299. #if 0
  300. volatile uint32_t *reg = &USB1_ENDPTCTRL1;
  301. const uint32_t *cfg = usb_endpoint_config_table;
  302. int i;
  303. for (i=0; i < NUM_ENDPOINTS; i++) {
  304. uint32_t n = *cfg++;
  305. *reg = n;
  306. // TODO: do the TRX & RXR bits self clear??
  307. uint32_t m = n & ~(USB_ENDPTCTRL_TXR | USB_ENDPTCTRL_RXR);
  308. *reg = m;
  309. //uint32_t p = *reg;
  310. //printf(" ep=%d: cfg=%08lX - %08lX - %08lX\n", i + 1, n, m, p);
  311. reg++;
  312. }
  313. #else
  314. #if defined(ENDPOINT2_CONFIG)
  315. USB1_ENDPTCTRL2 = ENDPOINT2_CONFIG;
  316. #endif
  317. #if defined(ENDPOINT3_CONFIG)
  318. USB1_ENDPTCTRL3 = ENDPOINT3_CONFIG;
  319. #endif
  320. #if defined(ENDPOINT4_CONFIG)
  321. USB1_ENDPTCTRL4 = ENDPOINT4_CONFIG;
  322. #endif
  323. #if defined(ENDPOINT5_CONFIG)
  324. USB1_ENDPTCTRL5 = ENDPOINT5_CONFIG;
  325. #endif
  326. #if defined(ENDPOINT6_CONFIG)
  327. USB1_ENDPTCTRL6 = ENDPOINT6_CONFIG;
  328. #endif
  329. #if defined(ENDPOINT7_CONFIG)
  330. USB1_ENDPTCTRL7 = ENDPOINT7_CONFIG;
  331. #endif
  332. #endif
  333. #if defined(CDC_STATUS_INTERFACE) && defined(CDC_DATA_INTERFACE)
  334. usb_serial_configure();
  335. #endif
  336. endpoint0_receive(NULL, 0, 0);
  337. return;
  338. case 0x0680: // GET_DESCRIPTOR
  339. case 0x0681:
  340. //printf("desc:\n"); // yay - sending device descriptor now works!!!!
  341. for (list = usb_descriptor_list; list->addr != NULL; list++) {
  342. if (setup.wValue == list->wValue && setup.wIndex == list->wIndex) {
  343. if ((setup.wValue >> 8) == 3) {
  344. // for string descriptors, use the descriptor's
  345. // length field, allowing runtime configured length.
  346. datalen = *(list->addr);
  347. } else {
  348. datalen = list->length;
  349. }
  350. if (datalen > setup.wLength) datalen = setup.wLength;
  351. endpoint0_transmit(list->addr, datalen, 0);
  352. return;
  353. }
  354. }
  355. break;
  356. case 0x2221: // CDC_SET_CONTROL_LINE_STATE
  357. usb_cdc_line_rtsdtr_millis = systick_millis_count;
  358. usb_cdc_line_rtsdtr = setup.wValue;
  359. case 0x2321: // CDC_SEND_BREAK
  360. endpoint0_receive(NULL, 0, 0);
  361. return;
  362. case 0x2021: // CDC_SET_LINE_CODING
  363. if (setup.wLength != 7) break;
  364. endpoint0_setupdata.bothwords = setupdata;
  365. endpoint0_receive(endpoint0_buffer, 7, 1);
  366. return;
  367. }
  368. USB1_ENDPTCTRL0 = 0x000010001; // stall
  369. }
  370. static void endpoint0_transmit(const void *data, uint32_t len, int notify)
  371. {
  372. //printf("tx %lu\n", len);
  373. if (len > 0) {
  374. // Executing A Transfer Descriptor, page 3182
  375. endpoint0_transfer_data.next = 1;
  376. endpoint0_transfer_data.status = (len << 16) | (1<<7);
  377. uint32_t addr = (uint32_t)data;
  378. endpoint0_transfer_data.pointer0 = addr; // format: table 55-60, pg 3159
  379. endpoint0_transfer_data.pointer1 = addr + 4096;
  380. endpoint0_transfer_data.pointer2 = addr + 8192;
  381. endpoint0_transfer_data.pointer3 = addr + 12288;
  382. endpoint0_transfer_data.pointer4 = addr + 16384;
  383. // Case 1: Link list is empty, page 3182
  384. endpoint_queue_head[1].next = (uint32_t)&endpoint0_transfer_data;
  385. endpoint_queue_head[1].status = 0;
  386. USB1_ENDPTPRIME |= (1<<16);
  387. while (USB1_ENDPTPRIME) ;
  388. }
  389. endpoint0_transfer_ack.next = 1;
  390. endpoint0_transfer_ack.status = (1<<7) | (notify ? (1 << 15) : 0);
  391. endpoint0_transfer_ack.pointer0 = 0;
  392. endpoint_queue_head[0].next = (uint32_t)&endpoint0_transfer_ack;
  393. endpoint_queue_head[0].status = 0;
  394. USB1_ENDPTPRIME |= (1<<0);
  395. endpoint0_notify_mask = (notify ? (1 << 0) : 0);
  396. while (USB1_ENDPTPRIME) ;
  397. }
  398. static void endpoint0_receive(void *data, uint32_t len, int notify)
  399. {
  400. //printf("rx %lu\n", len);
  401. if (len > 0) {
  402. // Executing A Transfer Descriptor, page 3182
  403. endpoint0_transfer_data.next = 1;
  404. endpoint0_transfer_data.status = (len << 16) | (1<<7) | (notify ? (1 << 15) : 0);
  405. uint32_t addr = (uint32_t)data;
  406. endpoint0_transfer_data.pointer0 = addr; // format: table 55-60, pg 3159
  407. endpoint0_transfer_data.pointer1 = addr + 4096;
  408. endpoint0_transfer_data.pointer2 = addr + 8192;
  409. endpoint0_transfer_data.pointer3 = addr + 12288;
  410. endpoint0_transfer_data.pointer4 = addr + 16384;
  411. // Case 1: Link list is empty, page 3182
  412. endpoint_queue_head[0].next = (uint32_t)&endpoint0_transfer_data;
  413. endpoint_queue_head[0].status = 0;
  414. USB1_ENDPTPRIME |= (1<<0);
  415. while (USB1_ENDPTPRIME) ;
  416. }
  417. endpoint0_transfer_ack.next = 1;
  418. endpoint0_transfer_ack.status = (1<<7);
  419. endpoint0_transfer_ack.pointer0 = 0;
  420. endpoint_queue_head[1].next = (uint32_t)&endpoint0_transfer_ack;
  421. endpoint_queue_head[1].status = 0;
  422. USB1_ENDPTPRIME |= (1<<16);
  423. endpoint0_notify_mask = (notify ? (1 << 16) : 0);
  424. while (USB1_ENDPTPRIME) ;
  425. }
  426. /*typedef union {
  427. struct {
  428. union {
  429. struct {
  430. uint8_t bmRequestType;
  431. uint8_t bRequest;
  432. };
  433. uint16_t wRequestAndType;
  434. };
  435. uint16_t wValue;
  436. uint16_t wIndex;
  437. uint16_t wLength;
  438. };
  439. struct {
  440. uint32_t word1;
  441. uint32_t word2;
  442. };
  443. uint64_t bothwords;
  444. } setup_t; */
  445. static void endpoint0_complete(void)
  446. {
  447. setup_t setup;
  448. setup.bothwords = endpoint0_setupdata.bothwords;
  449. //printf("complete\n");
  450. #ifdef CDC_STATUS_INTERFACE
  451. if (setup.wRequestAndType == 0x2021 /*CDC_SET_LINE_CODING*/) {
  452. memcpy(usb_cdc_line_coding, endpoint0_buffer, 7);
  453. printf("usb_cdc_line_coding, baud=%u\n", usb_cdc_line_coding[0]);
  454. if (usb_cdc_line_coding[0] == 134) {
  455. USB1_USBINTR |= USB_USBINTR_SRE;
  456. usb_reboot_timer = 80; // TODO: 10 if only 12 Mbit/sec
  457. }
  458. }
  459. #endif
  460. }
  461. static void usb_endpoint_config(endpoint_t *qh, uint32_t config, void (*callback)(transfer_t *))
  462. {
  463. memset(qh, 0, sizeof(endpoint_t));
  464. qh->config = config;
  465. qh->next = 1; // Terminate bit = 1
  466. qh->callback_function = callback;
  467. }
  468. void usb_config_rx(uint32_t ep, uint32_t packet_size, int do_zlp, void (*cb)(transfer_t *))
  469. {
  470. uint32_t config = (packet_size << 16) | (do_zlp ? 0 : (1 << 29));
  471. if (ep < 2 || ep > NUM_ENDPOINTS) return;
  472. usb_endpoint_config(endpoint_queue_head + ep * 2, config, cb);
  473. if (cb) endpointN_notify_mask |= (1 << ep);
  474. }
  475. void usb_config_tx(uint32_t ep, uint32_t packet_size, int do_zlp, void (*cb)(transfer_t *))
  476. {
  477. uint32_t config = (packet_size << 16) | (do_zlp ? 0 : (1 << 29));
  478. if (ep < 2 || ep > NUM_ENDPOINTS) return;
  479. usb_endpoint_config(endpoint_queue_head + ep * 2 + 1, config, cb);
  480. if (cb) endpointN_notify_mask |= (1 << (ep + 16));
  481. }
  482. void usb_prepare_transfer(transfer_t *transfer, const void *data, uint32_t len, uint32_t param)
  483. {
  484. transfer->next = 1;
  485. transfer->status = (len << 16) | (1<<7);
  486. uint32_t addr = (uint32_t)data;
  487. transfer->pointer0 = addr;
  488. transfer->pointer1 = addr + 4096;
  489. transfer->pointer2 = addr + 8192;
  490. transfer->pointer3 = addr + 12288;
  491. transfer->pointer4 = addr + 16384;
  492. transfer->callback_param = param;
  493. }
  494. #if 0
  495. void usb_print_transfer_log(void)
  496. {
  497. uint32_t i, count;
  498. printf("log %d transfers\n", transfer_log_count);
  499. count = transfer_log_count;
  500. if (count > LOG_SIZE) count = LOG_SIZE;
  501. for (i=0; i < count; i++) {
  502. if (transfer_log_head == 0) transfer_log_head = LOG_SIZE;
  503. transfer_log_head--;
  504. uint32_t log = transfer_log[transfer_log_head];
  505. printf(" %c %X\n", log >> 8, (int)(log & 255));
  506. }
  507. }
  508. #endif
  509. static void schedule_transfer(endpoint_t *endpoint, uint32_t epmask, transfer_t *transfer)
  510. {
  511. // when we stop at 6, why is the last transfer missing from the USB output?
  512. //if (transfer_log_count >= 6) return;
  513. //uint32_t ret = (*(const uint8_t *)transfer->pointer0) << 8;
  514. if (endpoint->callback_function) {
  515. transfer->status |= (1<<15);
  516. }
  517. __disable_irq();
  518. //digitalWriteFast(1, HIGH);
  519. // Executing A Transfer Descriptor, page 2468 (RT1060 manual, Rev 1, 12/2018)
  520. transfer_t *last = endpoint->last_transfer;
  521. if (last) {
  522. last->next = (uint32_t)transfer;
  523. if (USB1_ENDPTPRIME & epmask) goto end;
  524. //digitalWriteFast(2, HIGH);
  525. //ret |= 0x01;
  526. uint32_t status;
  527. do {
  528. USB1_USBCMD |= USB_USBCMD_ATDTW;
  529. status = USB1_ENDPTSTATUS;
  530. } while (!(USB1_USBCMD & USB_USBCMD_ATDTW));
  531. //USB1_USBCMD &= ~USB_USBCMD_ATDTW;
  532. if (status & epmask) goto end;
  533. //ret |= 0x02;
  534. }
  535. //digitalWriteFast(4, HIGH);
  536. endpoint->next = (uint32_t)transfer;
  537. endpoint->status = 0;
  538. USB1_ENDPTPRIME |= epmask;
  539. endpoint->first_transfer = transfer;
  540. end:
  541. endpoint->last_transfer = transfer;
  542. __enable_irq();
  543. //digitalWriteFast(4, LOW);
  544. //digitalWriteFast(3, LOW);
  545. //digitalWriteFast(2, LOW);
  546. //digitalWriteFast(1, LOW);
  547. //if (transfer_log_head > LOG_SIZE) transfer_log_head = 0;
  548. //transfer_log[transfer_log_head++] = ret;
  549. //transfer_log_count++;
  550. }
  551. // ENDPTPRIME - Software should write a one to the corresponding bit when
  552. // posting a new transfer descriptor to an endpoint queue head.
  553. // Hardware automatically uses this bit to begin parsing for a
  554. // new transfer descriptor from the queue head and prepare a
  555. // transmit buffer. Hardware clears this bit when the associated
  556. // endpoint(s) is (are) successfully primed.
  557. // Momentarily set by hardware during hardware re-priming
  558. // operations when a dTD is retired, and the dQH is updated.
  559. // ENDPTSTATUS - Transmit Buffer Ready - set to one by the hardware as a
  560. // response to receiving a command from a corresponding bit
  561. // in the ENDPTPRIME register. . Buffer ready is cleared by
  562. // USB reset, by the USB DMA system, or through the ENDPTFLUSH
  563. // register. (so 0=buffer ready, 1=buffer primed for transmit)
  564. // USBCMD.ATDTW - This bit is used as a semaphore to ensure proper addition
  565. // of a new dTD to an active (primed) endpoint's linked list.
  566. // This bit is set and cleared by software.
  567. // This bit would also be cleared by hardware when state machine
  568. // is hazard region for which adding a dTD to a primed endpoint
  569. // may go unrecognized.
  570. /*struct endpoint_struct {
  571. uint32_t config;
  572. uint32_t current;
  573. uint32_t next;
  574. uint32_t status;
  575. uint32_t pointer0;
  576. uint32_t pointer1;
  577. uint32_t pointer2;
  578. uint32_t pointer3;
  579. uint32_t pointer4;
  580. uint32_t reserved;
  581. uint32_t setup0;
  582. uint32_t setup1;
  583. transfer_t *first_transfer;
  584. transfer_t *last_transfer;
  585. void (*callback_function)(transfer_t *completed_transfer);
  586. uint32_t unused1;
  587. };*/
  588. static void run_callbacks(endpoint_t *ep)
  589. {
  590. transfer_t *t, *next;
  591. //printf("run_callbacks\n");
  592. t = ep->first_transfer;
  593. while (t && (uint32_t)t != 1) {
  594. if (!(t->status & (1<<7))) {
  595. // transfer not active anymore
  596. next = (transfer_t *)t->next;
  597. ep->callback_function(t);
  598. } else {
  599. // transfer still active
  600. ep->first_transfer = t;
  601. return;
  602. }
  603. if (next == ep->last_transfer) break;
  604. t = next;
  605. }
  606. // all transfers completed
  607. ep->first_transfer = NULL;
  608. ep->last_transfer = NULL;
  609. }
  610. void usb_transmit(int endpoint_number, transfer_t *transfer)
  611. {
  612. if (endpoint_number < 2 || endpoint_number > NUM_ENDPOINTS) return;
  613. endpoint_t *endpoint = endpoint_queue_head + endpoint_number * 2 + 1;
  614. uint32_t mask = 1 << (endpoint_number + 16);
  615. schedule_transfer(endpoint, mask, transfer);
  616. }
  617. void usb_receive(int endpoint_number, transfer_t *transfer)
  618. {
  619. if (endpoint_number < 2 || endpoint_number > NUM_ENDPOINTS) return;
  620. endpoint_t *endpoint = endpoint_queue_head + endpoint_number * 2;
  621. uint32_t mask = 1 << endpoint_number;
  622. schedule_transfer(endpoint, mask, transfer);
  623. }
  624. uint32_t usb_transfer_status(const transfer_t *transfer)
  625. {
  626. uint32_t status, cmd;
  627. //int count=0;
  628. cmd = USB1_USBCMD;
  629. while (1) {
  630. __disable_irq();
  631. USB1_USBCMD = cmd | USB_USBCMD_ATDTW;
  632. status = transfer->status;
  633. cmd = USB1_USBCMD;
  634. __enable_irq();
  635. if (cmd & USB_USBCMD_ATDTW) return status;
  636. //if (!(cmd & USB_USBCMD_ATDTW)) continue;
  637. //if (status & 0x80) break; // for still active, only 1 reading needed
  638. //if (++count > 1) break; // for completed, check 10 times
  639. }
  640. }