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.

1013 lines
30KB

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