Teensy 4.1 core updated for C++20
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.

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