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.

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