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.

854 lines
26KB

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