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.

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