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.

816 lines
25KB

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