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.

804 lines
24KB

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