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.

1014 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 2351 (Rev 2, 12/2019)
  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 1
  219. if (completestatus) {
  220. // transmit:
  221. uint32_t tx = completestatus >> 16;
  222. while (tx) {
  223. int p=__builtin_ctz(tx);
  224. run_callbacks(endpoint_queue_head + p * 2 + 1);
  225. tx &= ~(1<<p);
  226. }
  227. // receive:
  228. uint32_t rx = completestatus & 0xffff;
  229. while(rx) {
  230. int p=__builtin_ctz(rx);
  231. run_callbacks(endpoint_queue_head + p * 2);
  232. rx &= ~(1<<p);
  233. };
  234. }
  235. #else
  236. if (completestatus) {
  237. int i; // TODO: optimize with __builtin_ctz()
  238. for (i=2; i <= NUM_ENDPOINTS; i++) {
  239. if (completestatus & (1 << i)) { // receive
  240. run_callbacks(endpoint_queue_head + i * 2);
  241. }
  242. if (completestatus & (1 << (i + 16))) { // transmit
  243. run_callbacks(endpoint_queue_head + i * 2 + 1);
  244. }
  245. }
  246. }
  247. #endif
  248. }
  249. }
  250. if (status & USB_USBSTS_URI) { // page 3164
  251. USB1_ENDPTSETUPSTAT = USB1_ENDPTSETUPSTAT; // Clear all setup token semaphores
  252. USB1_ENDPTCOMPLETE = USB1_ENDPTCOMPLETE; // Clear all the endpoint complete status
  253. while (USB1_ENDPTPRIME != 0) ; // Wait for any endpoint priming
  254. USB1_ENDPTFLUSH = 0xFFFFFFFF; // Cancel all endpoint primed status
  255. if ((USB1_PORTSC1 & USB_PORTSC1_PR)) {
  256. //printf("reset\n");
  257. } else {
  258. // we took too long to respond :(
  259. // TODO; is this ever really a problem?
  260. //printf("reset too slow\n");
  261. }
  262. #if defined(CDC_STATUS_INTERFACE) && defined(CDC_DATA_INTERFACE)
  263. usb_serial_reset();
  264. #endif
  265. endpointN_notify_mask = 0;
  266. // TODO: Free all allocated dTDs
  267. //if (++reset_count >= 3) {
  268. // shut off USB - easier to see results in protocol analyzer
  269. //USB1_USBCMD &= ~USB_USBCMD_RS;
  270. //printf("shut off USB\n");
  271. //}
  272. }
  273. if (status & USB_USBSTS_TI0) {
  274. if (usb_timer0_callback != NULL) usb_timer0_callback();
  275. }
  276. if (status & USB_USBSTS_TI1) {
  277. if (usb_timer1_callback != NULL) usb_timer1_callback();
  278. }
  279. if (status & USB_USBSTS_PCI) {
  280. if (USB1_PORTSC1 & USB_PORTSC1_HSP) {
  281. //printf("port at 480 Mbit\n");
  282. usb_high_speed = 1;
  283. } else {
  284. //printf("port at 12 Mbit\n");
  285. usb_high_speed = 0;
  286. }
  287. }
  288. if (status & USB_USBSTS_SLI) { // page 3165
  289. //printf("suspend\n");
  290. }
  291. if (status & USB_USBSTS_UEI) {
  292. //printf("error\n");
  293. }
  294. if ((USB1_USBINTR & USB_USBINTR_SRE) && (status & USB_USBSTS_SRI)) {
  295. //printf("sof %d\n", usb_reboot_timer);
  296. if (usb_reboot_timer) {
  297. if (--usb_reboot_timer == 0) {
  298. usb_stop_sof_interrupts(NUM_INTERFACE);
  299. asm("bkpt #251"); // run bootloader
  300. }
  301. }
  302. #ifdef MIDI_INTERFACE
  303. usb_midi_flush_output();
  304. #endif
  305. #ifdef MULTITOUCH_INTERFACE
  306. usb_touchscreen_update_callback();
  307. #endif
  308. #ifdef FLIGHTSIM_INTERFACE
  309. usb_flightsim_flush_output();
  310. #endif
  311. }
  312. }
  313. void usb_start_sof_interrupts(int interface)
  314. {
  315. __disable_irq();
  316. sof_usage |= (1 << interface);
  317. uint32_t intr = USB1_USBINTR;
  318. if (!(intr & USB_USBINTR_SRE)) {
  319. USB1_USBSTS = USB_USBSTS_SRI; // clear prior SOF before SOF IRQ enable
  320. USB1_USBINTR = intr | USB_USBINTR_SRE;
  321. }
  322. __enable_irq();
  323. }
  324. void usb_stop_sof_interrupts(int interface)
  325. {
  326. sof_usage &= ~(1 << interface);
  327. if (sof_usage == 0) {
  328. USB1_USBINTR &= ~USB_USBINTR_SRE;
  329. }
  330. }
  331. /*
  332. struct transfer_struct { // table 55-60, pg 3159
  333. uint32_t next;
  334. uint32_t status;
  335. uint32_t pointer0;
  336. uint32_t pointer1;
  337. uint32_t pointer2;
  338. uint32_t pointer3;
  339. uint32_t pointer4;
  340. uint32_t unused1;
  341. };
  342. transfer_t endpoint0_transfer_data __attribute__ ((aligned(32)));;
  343. transfer_t endpoint0_transfer_ack __attribute__ ((aligned(32)));;
  344. */
  345. static uint8_t reply_buffer[8];
  346. static void endpoint0_setup(uint64_t setupdata)
  347. {
  348. setup_t setup;
  349. uint32_t endpoint, dir, ctrl;
  350. const usb_descriptor_list_t *list;
  351. setup.bothwords = setupdata;
  352. switch (setup.wRequestAndType) {
  353. case 0x0500: // SET_ADDRESS
  354. endpoint0_receive(NULL, 0, 0);
  355. USB1_DEVICEADDR = USB_DEVICEADDR_USBADR(setup.wValue) | USB_DEVICEADDR_USBADRA;
  356. return;
  357. case 0x0900: // SET_CONFIGURATION
  358. usb_configuration = setup.wValue;
  359. // configure all other endpoints
  360. #if defined(ENDPOINT2_CONFIG)
  361. USB1_ENDPTCTRL2 = ENDPOINT2_CONFIG;
  362. #endif
  363. #if defined(ENDPOINT3_CONFIG)
  364. USB1_ENDPTCTRL3 = ENDPOINT3_CONFIG;
  365. #endif
  366. #if defined(ENDPOINT4_CONFIG)
  367. USB1_ENDPTCTRL4 = ENDPOINT4_CONFIG;
  368. #endif
  369. #if defined(ENDPOINT5_CONFIG)
  370. USB1_ENDPTCTRL5 = ENDPOINT5_CONFIG;
  371. #endif
  372. #if defined(ENDPOINT6_CONFIG)
  373. USB1_ENDPTCTRL6 = ENDPOINT6_CONFIG;
  374. #endif
  375. #if defined(ENDPOINT7_CONFIG)
  376. USB1_ENDPTCTRL7 = ENDPOINT7_CONFIG;
  377. #endif
  378. #if defined(CDC_STATUS_INTERFACE) && defined(CDC_DATA_INTERFACE)
  379. usb_serial_configure();
  380. #elif defined(SEREMU_INTERFACE)
  381. usb_seremu_configure();
  382. #endif
  383. #if defined(CDC2_STATUS_INTERFACE) && defined(CDC2_DATA_INTERFACE)
  384. usb_serial2_configure();
  385. #endif
  386. #if defined(CDC3_STATUS_INTERFACE) && defined(CDC3_DATA_INTERFACE)
  387. usb_serial3_configure();
  388. #endif
  389. #if defined(RAWHID_INTERFACE)
  390. usb_rawhid_configure();
  391. #endif
  392. #if defined(KEYBOARD_INTERFACE)
  393. usb_keyboard_configure();
  394. #endif
  395. #if defined(MOUSE_INTERFACE)
  396. usb_mouse_configure();
  397. #endif
  398. #if defined(FLIGHTSIM_INTERFACE)
  399. usb_flightsim_configure();
  400. #endif
  401. #if defined(JOYSTICK_INTERFACE)
  402. usb_joystick_configure();
  403. #endif
  404. #if defined(MULTITOUCH_INTERFACE)
  405. usb_touchscreen_configure();
  406. #endif
  407. #if defined(MIDI_INTERFACE)
  408. usb_midi_configure();
  409. #endif
  410. #if defined(AUDIO_INTERFACE)
  411. usb_audio_configure();
  412. #endif
  413. endpoint0_receive(NULL, 0, 0);
  414. return;
  415. case 0x0880: // GET_CONFIGURATION
  416. reply_buffer[0] = usb_configuration;
  417. endpoint0_transmit(reply_buffer, 1, 0);
  418. return;
  419. case 0x0080: // GET_STATUS (device)
  420. reply_buffer[0] = 0;
  421. reply_buffer[1] = 0;
  422. endpoint0_transmit(reply_buffer, 2, 0);
  423. return;
  424. case 0x0082: // GET_STATUS (endpoint)
  425. endpoint = setup.wIndex & 0x7F;
  426. if (endpoint > 7) break;
  427. dir = setup.wIndex & 0x80;
  428. ctrl = *((uint32_t *)&USB1_ENDPTCTRL0 + endpoint);
  429. reply_buffer[0] = 0;
  430. reply_buffer[1] = 0;
  431. if ((dir && (ctrl & USB_ENDPTCTRL_TXS)) || (!dir && (ctrl & USB_ENDPTCTRL_RXS))) {
  432. reply_buffer[0] = 1;
  433. }
  434. endpoint0_transmit(reply_buffer, 2, 0);
  435. return;
  436. case 0x0302: // SET_FEATURE (endpoint)
  437. endpoint = setup.wIndex & 0x7F;
  438. if (endpoint > 7) break;
  439. dir = setup.wIndex & 0x80;
  440. if (dir) {
  441. *((volatile uint32_t *)&USB1_ENDPTCTRL0 + endpoint) |= USB_ENDPTCTRL_TXS;
  442. } else {
  443. *((volatile uint32_t *)&USB1_ENDPTCTRL0 + endpoint) |= USB_ENDPTCTRL_RXS;
  444. }
  445. endpoint0_receive(NULL, 0, 0);
  446. return;
  447. case 0x0102: // CLEAR_FEATURE (endpoint)
  448. endpoint = setup.wIndex & 0x7F;
  449. if (endpoint > 7) break;
  450. dir = setup.wIndex & 0x80;
  451. if (dir) {
  452. *((volatile uint32_t *)&USB1_ENDPTCTRL0 + endpoint) &= ~USB_ENDPTCTRL_TXS;
  453. } else {
  454. *((volatile uint32_t *)&USB1_ENDPTCTRL0 + endpoint) &= ~USB_ENDPTCTRL_RXS;
  455. }
  456. endpoint0_receive(NULL, 0, 0);
  457. return;
  458. case 0x0680: // GET_DESCRIPTOR
  459. case 0x0681:
  460. for (list = usb_descriptor_list; list->addr != NULL; list++) {
  461. if (setup.wValue == list->wValue && setup.wIndex == list->wIndex) {
  462. uint32_t datalen;
  463. if ((setup.wValue >> 8) == 3) {
  464. // for string descriptors, use the descriptor's
  465. // length field, allowing runtime configured length.
  466. datalen = *(list->addr);
  467. } else {
  468. datalen = list->length;
  469. }
  470. if (datalen > setup.wLength) datalen = setup.wLength;
  471. // copy the descriptor, from PROGMEM to DMAMEM
  472. if (setup.wValue == 0x200) {
  473. // config descriptor needs to adapt to speed
  474. const uint8_t *src = usb_config_descriptor_12;
  475. if (usb_high_speed) src = usb_config_descriptor_480;
  476. memcpy(usb_descriptor_buffer, src, datalen);
  477. } else if (setup.wValue == 0x700) {
  478. // other speed config also needs to adapt
  479. const uint8_t *src = usb_config_descriptor_480;
  480. if (usb_high_speed) src = usb_config_descriptor_12;
  481. memcpy(usb_descriptor_buffer, src, datalen);
  482. usb_descriptor_buffer[1] = 7;
  483. } else {
  484. memcpy(usb_descriptor_buffer, list->addr, datalen);
  485. }
  486. // prep transmit
  487. arm_dcache_flush_delete(usb_descriptor_buffer, datalen);
  488. endpoint0_transmit(usb_descriptor_buffer, datalen, 0);
  489. return;
  490. }
  491. }
  492. break;
  493. #if defined(CDC_STATUS_INTERFACE)
  494. case 0x2221: // CDC_SET_CONTROL_LINE_STATE
  495. #ifdef CDC_STATUS_INTERFACE
  496. if (setup.wIndex == CDC_STATUS_INTERFACE) {
  497. usb_cdc_line_rtsdtr_millis = systick_millis_count;
  498. usb_cdc_line_rtsdtr = setup.wValue;
  499. }
  500. #endif
  501. #ifdef CDC2_STATUS_INTERFACE
  502. if (setup.wIndex == CDC2_STATUS_INTERFACE) {
  503. usb_cdc2_line_rtsdtr_millis = systick_millis_count;
  504. usb_cdc2_line_rtsdtr = setup.wValue;
  505. }
  506. #endif
  507. #ifdef CDC3_STATUS_INTERFACE
  508. if (setup.wIndex == CDC3_STATUS_INTERFACE) {
  509. usb_cdc3_line_rtsdtr_millis = systick_millis_count;
  510. usb_cdc3_line_rtsdtr = setup.wValue;
  511. }
  512. #endif
  513. case 0x2321: // CDC_SEND_BREAK
  514. endpoint0_receive(NULL, 0, 0);
  515. return;
  516. case 0x2021: // CDC_SET_LINE_CODING
  517. if (setup.wLength != 7) break;
  518. endpoint0_setupdata.bothwords = setupdata;
  519. endpoint0_receive(endpoint0_buffer, 7, 1);
  520. return;
  521. #endif
  522. #if defined(SEREMU_INTERFACE) || defined(KEYBOARD_INTERFACE)
  523. case 0x0921: // HID SET_REPORT
  524. if (setup.wLength <= sizeof(endpoint0_buffer)) {
  525. //printf("hid set report %x %x\n", setup.word1, setup.word2);
  526. endpoint0_setupdata.bothwords = setup.bothwords;
  527. endpoint0_buffer[0] = 0xE9;
  528. endpoint0_receive(endpoint0_buffer, setup.wLength, 1);
  529. return;
  530. }
  531. break;
  532. #endif
  533. #if defined(AUDIO_INTERFACE)
  534. case 0x0B01: // SET_INTERFACE (alternate setting)
  535. if (setup.wIndex == AUDIO_INTERFACE+1) {
  536. usb_audio_transmit_setting = setup.wValue;
  537. if (usb_audio_transmit_setting > 0) {
  538. // TODO: set up AUDIO_TX_ENDPOINT to transmit
  539. }
  540. endpoint0_receive(NULL, 0, 0);
  541. return;
  542. } else if (setup.wIndex == AUDIO_INTERFACE+2) {
  543. usb_audio_receive_setting = setup.wValue;
  544. endpoint0_receive(NULL, 0, 0);
  545. return;
  546. }
  547. break;
  548. case 0x0A81: // GET_INTERFACE (alternate setting)
  549. if (setup.wIndex == AUDIO_INTERFACE+1) {
  550. endpoint0_buffer[0] = usb_audio_transmit_setting;
  551. endpoint0_transmit(endpoint0_buffer, 1, 0);
  552. return;
  553. } else if (setup.wIndex == AUDIO_INTERFACE+2) {
  554. endpoint0_buffer[0] = usb_audio_receive_setting;
  555. endpoint0_transmit(endpoint0_buffer, 1, 0);
  556. return;
  557. }
  558. break;
  559. case 0x0121: // SET FEATURE
  560. case 0x0221:
  561. case 0x0321:
  562. case 0x0421:
  563. //printf("set_feature, word1=%x, len=%d\n", setup.word1, setup.wLength);
  564. if (setup.wLength <= sizeof(endpoint0_buffer)) {
  565. endpoint0_setupdata.bothwords = setupdata;
  566. endpoint0_receive(endpoint0_buffer, setup.wLength, 1);
  567. return; // handle these after ACK
  568. }
  569. break;
  570. case 0x81A1: // GET FEATURE
  571. case 0x82A1:
  572. case 0x83A1:
  573. case 0x84A1:
  574. if (setup.wLength <= sizeof(endpoint0_buffer)) {
  575. uint32_t len;
  576. if (usb_audio_get_feature(&setup, endpoint0_buffer, &len)) {
  577. //printf("GET feature, len=%d\n", len);
  578. endpoint0_transmit(endpoint0_buffer, len, 0);
  579. return;
  580. }
  581. }
  582. break;
  583. case 0x81A2: // GET_CUR (wValue=0, wIndex=interface, wLength=len)
  584. if (setup.wLength >= 3) {
  585. endpoint0_buffer[0] = 44100 & 255;
  586. endpoint0_buffer[1] = 44100 >> 8;
  587. endpoint0_buffer[2] = 0;
  588. endpoint0_transmit(endpoint0_buffer, 3, 0);
  589. return;
  590. }
  591. break;
  592. #endif
  593. }
  594. USB1_ENDPTCTRL0 = 0x000010001; // stall
  595. }
  596. static void endpoint0_transmit(const void *data, uint32_t len, int notify)
  597. {
  598. //printf("tx %lu\n", len);
  599. if (len > 0) {
  600. // Executing A Transfer Descriptor, page 3182
  601. endpoint0_transfer_data.next = 1;
  602. endpoint0_transfer_data.status = (len << 16) | (1<<7);
  603. uint32_t addr = (uint32_t)data;
  604. endpoint0_transfer_data.pointer0 = addr; // format: table 55-60, pg 3159
  605. endpoint0_transfer_data.pointer1 = addr + 4096;
  606. endpoint0_transfer_data.pointer2 = addr + 8192;
  607. endpoint0_transfer_data.pointer3 = addr + 12288;
  608. endpoint0_transfer_data.pointer4 = addr + 16384;
  609. // Case 1: Link list is empty, page 3182
  610. endpoint_queue_head[1].next = (uint32_t)&endpoint0_transfer_data;
  611. endpoint_queue_head[1].status = 0;
  612. USB1_ENDPTPRIME |= (1<<16);
  613. while (USB1_ENDPTPRIME) ;
  614. }
  615. endpoint0_transfer_ack.next = 1;
  616. endpoint0_transfer_ack.status = (1<<7) | (notify ? (1 << 15) : 0);
  617. endpoint0_transfer_ack.pointer0 = 0;
  618. endpoint_queue_head[0].next = (uint32_t)&endpoint0_transfer_ack;
  619. endpoint_queue_head[0].status = 0;
  620. USB1_ENDPTCOMPLETE = (1<<0) | (1<<16);
  621. USB1_ENDPTPRIME |= (1<<0);
  622. endpoint0_notify_mask = (notify ? (1 << 0) : 0);
  623. while (USB1_ENDPTPRIME) ;
  624. }
  625. static void endpoint0_receive(void *data, uint32_t len, int notify)
  626. {
  627. //printf("rx %lu\n", len);
  628. if (len > 0) {
  629. // Executing A Transfer Descriptor, page 3182
  630. endpoint0_transfer_data.next = 1;
  631. endpoint0_transfer_data.status = (len << 16) | (1<<7);
  632. uint32_t addr = (uint32_t)data;
  633. endpoint0_transfer_data.pointer0 = addr; // format: table 55-60, pg 3159
  634. endpoint0_transfer_data.pointer1 = addr + 4096;
  635. endpoint0_transfer_data.pointer2 = addr + 8192;
  636. endpoint0_transfer_data.pointer3 = addr + 12288;
  637. endpoint0_transfer_data.pointer4 = addr + 16384;
  638. // Case 1: Link list is empty, page 3182
  639. endpoint_queue_head[0].next = (uint32_t)&endpoint0_transfer_data;
  640. endpoint_queue_head[0].status = 0;
  641. USB1_ENDPTPRIME |= (1<<0);
  642. while (USB1_ENDPTPRIME) ;
  643. }
  644. endpoint0_transfer_ack.next = 1;
  645. endpoint0_transfer_ack.status = (1<<7) | (notify ? (1 << 15) : 0);
  646. endpoint0_transfer_ack.pointer0 = 0;
  647. endpoint_queue_head[1].next = (uint32_t)&endpoint0_transfer_ack;
  648. endpoint_queue_head[1].status = 0;
  649. USB1_ENDPTCOMPLETE = (1<<0) | (1<<16);
  650. USB1_ENDPTPRIME |= (1<<16);
  651. endpoint0_notify_mask = (notify ? (1 << 16) : 0);
  652. while (USB1_ENDPTPRIME) ;
  653. }
  654. /*typedef union {
  655. struct {
  656. union {
  657. struct {
  658. uint8_t bmRequestType;
  659. uint8_t bRequest;
  660. };
  661. uint16_t wRequestAndType;
  662. };
  663. uint16_t wValue;
  664. uint16_t wIndex;
  665. uint16_t wLength;
  666. };
  667. struct {
  668. uint32_t word1;
  669. uint32_t word2;
  670. };
  671. uint64_t bothwords;
  672. } setup_t; */
  673. static void endpoint0_complete(void)
  674. {
  675. setup_t setup;
  676. setup.bothwords = endpoint0_setupdata.bothwords;
  677. //printf("complete %x %x %x\n", setup.word1, setup.word2, endpoint0_buffer[0]);
  678. #ifdef CDC_STATUS_INTERFACE
  679. // 0x2021 is CDC_SET_LINE_CODING
  680. if (setup.wRequestAndType == 0x2021 && setup.wIndex == CDC_STATUS_INTERFACE) {
  681. memcpy(usb_cdc_line_coding, endpoint0_buffer, 7);
  682. printf("usb_cdc_line_coding, baud=%u\n", usb_cdc_line_coding[0]);
  683. if (usb_cdc_line_coding[0] == 134) {
  684. usb_start_sof_interrupts(NUM_INTERFACE);
  685. usb_reboot_timer = 80; // TODO: 10 if only 12 Mbit/sec
  686. }
  687. }
  688. #endif
  689. #ifdef CDC2_STATUS_INTERFACE
  690. if (setup.wRequestAndType == 0x2021 && setup.wIndex == CDC2_STATUS_INTERFACE) {
  691. memcpy(usb_cdc2_line_coding, endpoint0_buffer, 7);
  692. printf("usb_cdc2_line_coding, baud=%u\n", usb_cdc2_line_coding[0]);
  693. if (usb_cdc2_line_coding[0] == 134) {
  694. usb_start_sof_interrupts(NUM_INTERFACE);
  695. usb_reboot_timer = 80; // TODO: 10 if only 12 Mbit/sec
  696. }
  697. }
  698. #endif
  699. #ifdef CDC3_STATUS_INTERFACE
  700. if (setup.wRequestAndType == 0x2021 && setup.wIndex == CDC3_STATUS_INTERFACE) {
  701. memcpy(usb_cdc3_line_coding, endpoint0_buffer, 7);
  702. printf("usb_cdc3_line_coding, baud=%u\n", usb_cdc3_line_coding[0]);
  703. if (usb_cdc3_line_coding[0] == 134) {
  704. usb_start_sof_interrupts(NUM_INTERFACE);
  705. usb_reboot_timer = 80; // TODO: 10 if only 12 Mbit/sec
  706. }
  707. }
  708. #endif
  709. #ifdef KEYBOARD_INTERFACE
  710. if (setup.word1 == 0x02000921 && setup.word2 == ((1 << 16) | KEYBOARD_INTERFACE)) {
  711. keyboard_leds = endpoint0_buffer[0];
  712. endpoint0_transmit(NULL, 0, 0);
  713. }
  714. #endif
  715. #ifdef SEREMU_INTERFACE
  716. if (setup.word1 == 0x03000921 && setup.word2 == ((4<<16)|SEREMU_INTERFACE)
  717. && endpoint0_buffer[0] == 0xA9 && endpoint0_buffer[1] == 0x45
  718. && endpoint0_buffer[2] == 0xC2 && endpoint0_buffer[3] == 0x6B) {
  719. printf("seremu reboot request\n");
  720. usb_start_sof_interrupts(NUM_INTERFACE);
  721. usb_reboot_timer = 80; // TODO: 10 if only 12 Mbit/sec
  722. }
  723. #endif
  724. #ifdef AUDIO_INTERFACE
  725. if (setup.word1 == 0x02010121 /* TODO: check setup.word2 */) {
  726. usb_audio_set_feature(&endpoint0_setupdata, endpoint0_buffer);
  727. }
  728. #endif
  729. }
  730. static void usb_endpoint_config(endpoint_t *qh, uint32_t config, void (*callback)(transfer_t *))
  731. {
  732. memset(qh, 0, sizeof(endpoint_t));
  733. qh->config = config;
  734. qh->next = 1; // Terminate bit = 1
  735. qh->callback_function = callback;
  736. }
  737. void usb_config_rx(uint32_t ep, uint32_t packet_size, int do_zlp, void (*cb)(transfer_t *))
  738. {
  739. uint32_t config = (packet_size << 16) | (do_zlp ? 0 : (1 << 29));
  740. if (ep < 2 || ep > NUM_ENDPOINTS) return;
  741. usb_endpoint_config(endpoint_queue_head + ep * 2, config, cb);
  742. if (cb) endpointN_notify_mask |= (1 << ep);
  743. }
  744. void usb_config_tx(uint32_t ep, uint32_t packet_size, int do_zlp, void (*cb)(transfer_t *))
  745. {
  746. uint32_t config = (packet_size << 16) | (do_zlp ? 0 : (1 << 29));
  747. if (ep < 2 || ep > NUM_ENDPOINTS) return;
  748. usb_endpoint_config(endpoint_queue_head + ep * 2 + 1, config, cb);
  749. if (cb) endpointN_notify_mask |= (1 << (ep + 16));
  750. }
  751. void usb_config_rx_iso(uint32_t ep, uint32_t packet_size, int mult, void (*cb)(transfer_t *))
  752. {
  753. if (mult < 1 || mult > 3) return;
  754. uint32_t config = (packet_size << 16) | (mult << 30);
  755. if (ep < 2 || ep > NUM_ENDPOINTS) return;
  756. usb_endpoint_config(endpoint_queue_head + ep * 2, config, cb);
  757. if (cb) endpointN_notify_mask |= (1 << ep);
  758. }
  759. void usb_config_tx_iso(uint32_t ep, uint32_t packet_size, int mult, void (*cb)(transfer_t *))
  760. {
  761. if (mult < 1 || mult > 3) return;
  762. uint32_t config = (packet_size << 16) | (mult << 30);
  763. if (ep < 2 || ep > NUM_ENDPOINTS) return;
  764. usb_endpoint_config(endpoint_queue_head + ep * 2 + 1, config, cb);
  765. if (cb) endpointN_notify_mask |= (1 << (ep + 16));
  766. }
  767. void usb_prepare_transfer(transfer_t *transfer, const void *data, uint32_t len, uint32_t param)
  768. {
  769. transfer->next = 1;
  770. transfer->status = (len << 16) | (1<<7);
  771. uint32_t addr = (uint32_t)data;
  772. transfer->pointer0 = addr;
  773. transfer->pointer1 = addr + 4096;
  774. transfer->pointer2 = addr + 8192;
  775. transfer->pointer3 = addr + 12288;
  776. transfer->pointer4 = addr + 16384;
  777. transfer->callback_param = param;
  778. }
  779. #if 0
  780. void usb_print_transfer_log(void)
  781. {
  782. uint32_t i, count;
  783. printf("log %d transfers\n", transfer_log_count);
  784. count = transfer_log_count;
  785. if (count > LOG_SIZE) count = LOG_SIZE;
  786. for (i=0; i < count; i++) {
  787. if (transfer_log_head == 0) transfer_log_head = LOG_SIZE;
  788. transfer_log_head--;
  789. uint32_t log = transfer_log[transfer_log_head];
  790. printf(" %c %X\n", log >> 8, (int)(log & 255));
  791. }
  792. }
  793. #endif
  794. static void schedule_transfer(endpoint_t *endpoint, uint32_t epmask, transfer_t *transfer)
  795. {
  796. // when we stop at 6, why is the last transfer missing from the USB output?
  797. //if (transfer_log_count >= 6) return;
  798. //uint32_t ret = (*(const uint8_t *)transfer->pointer0) << 8;
  799. if (endpoint->callback_function) {
  800. transfer->status |= (1<<15);
  801. }
  802. __disable_irq();
  803. //digitalWriteFast(1, HIGH);
  804. // Executing A Transfer Descriptor, page 2468 (RT1060 manual, Rev 1, 12/2018)
  805. transfer_t *last = endpoint->last_transfer;
  806. if (last) {
  807. last->next = (uint32_t)transfer;
  808. if (USB1_ENDPTPRIME & epmask) goto end;
  809. //digitalWriteFast(2, HIGH);
  810. //ret |= 0x01;
  811. uint32_t status, cyccnt=ARM_DWT_CYCCNT;
  812. do {
  813. USB1_USBCMD |= USB_USBCMD_ATDTW;
  814. status = USB1_ENDPTSTATUS;
  815. } while (!(USB1_USBCMD & USB_USBCMD_ATDTW) && (ARM_DWT_CYCCNT - cyccnt < 2400));
  816. //USB1_USBCMD &= ~USB_USBCMD_ATDTW;
  817. if (status & epmask) goto end;
  818. //ret |= 0x02;
  819. }
  820. //digitalWriteFast(4, HIGH);
  821. endpoint->next = (uint32_t)transfer;
  822. endpoint->status = 0;
  823. USB1_ENDPTPRIME |= epmask;
  824. endpoint->first_transfer = transfer;
  825. end:
  826. endpoint->last_transfer = transfer;
  827. __enable_irq();
  828. //digitalWriteFast(4, LOW);
  829. //digitalWriteFast(3, LOW);
  830. //digitalWriteFast(2, LOW);
  831. //digitalWriteFast(1, LOW);
  832. //if (transfer_log_head > LOG_SIZE) transfer_log_head = 0;
  833. //transfer_log[transfer_log_head++] = ret;
  834. //transfer_log_count++;
  835. }
  836. // ENDPTPRIME - Software should write a one to the corresponding bit when
  837. // posting a new transfer descriptor to an endpoint queue head.
  838. // Hardware automatically uses this bit to begin parsing for a
  839. // new transfer descriptor from the queue head and prepare a
  840. // transmit buffer. Hardware clears this bit when the associated
  841. // endpoint(s) is (are) successfully primed.
  842. // Momentarily set by hardware during hardware re-priming
  843. // operations when a dTD is retired, and the dQH is updated.
  844. // ENDPTSTATUS - Transmit Buffer Ready - set to one by the hardware as a
  845. // response to receiving a command from a corresponding bit
  846. // in the ENDPTPRIME register. . Buffer ready is cleared by
  847. // USB reset, by the USB DMA system, or through the ENDPTFLUSH
  848. // register. (so 0=buffer ready, 1=buffer primed for transmit)
  849. // USBCMD.ATDTW - This bit is used as a semaphore to ensure proper addition
  850. // of a new dTD to an active (primed) endpoint's linked list.
  851. // This bit is set and cleared by software.
  852. // This bit would also be cleared by hardware when state machine
  853. // is hazard region for which adding a dTD to a primed endpoint
  854. // may go unrecognized.
  855. /*struct endpoint_struct {
  856. uint32_t config;
  857. uint32_t current;
  858. uint32_t next;
  859. uint32_t status;
  860. uint32_t pointer0;
  861. uint32_t pointer1;
  862. uint32_t pointer2;
  863. uint32_t pointer3;
  864. uint32_t pointer4;
  865. uint32_t reserved;
  866. uint32_t setup0;
  867. uint32_t setup1;
  868. transfer_t *first_transfer;
  869. transfer_t *last_transfer;
  870. void (*callback_function)(transfer_t *completed_transfer);
  871. uint32_t unused1;
  872. };*/
  873. static void run_callbacks(endpoint_t *ep)
  874. {
  875. //printf("run_callbacks\n");
  876. transfer_t *first = ep->first_transfer;
  877. if (first == NULL) return;
  878. // count how many transfers are completed, then remove them from the endpoint's list
  879. uint32_t count = 0;
  880. transfer_t *t = first;
  881. while (1) {
  882. if (t->status & (1<<7)) {
  883. // found a still-active transfer, new list begins here
  884. //printf(" still active\n");
  885. ep->first_transfer = t;
  886. break;
  887. }
  888. count++;
  889. t = (transfer_t *)t->next;
  890. if ((uint32_t)t == 1) {
  891. // reached end of list, all need callbacks, new list is empty
  892. //printf(" end of list\n");
  893. ep->first_transfer = NULL;
  894. ep->last_transfer = NULL;
  895. break;
  896. }
  897. }
  898. // do all the callbacks
  899. while (count) {
  900. transfer_t *next = (transfer_t *)first->next;
  901. ep->callback_function(first);
  902. first = next;
  903. count--;
  904. }
  905. }
  906. void usb_transmit(int endpoint_number, transfer_t *transfer)
  907. {
  908. if (endpoint_number < 2 || endpoint_number > NUM_ENDPOINTS) return;
  909. endpoint_t *endpoint = endpoint_queue_head + endpoint_number * 2 + 1;
  910. uint32_t mask = 1 << (endpoint_number + 16);
  911. schedule_transfer(endpoint, mask, transfer);
  912. }
  913. void usb_receive(int endpoint_number, transfer_t *transfer)
  914. {
  915. if (endpoint_number < 2 || endpoint_number > NUM_ENDPOINTS) return;
  916. endpoint_t *endpoint = endpoint_queue_head + endpoint_number * 2;
  917. uint32_t mask = 1 << endpoint_number;
  918. schedule_transfer(endpoint, mask, transfer);
  919. }
  920. uint32_t usb_transfer_status(const transfer_t *transfer)
  921. {
  922. #if 0
  923. uint32_t status, cmd;
  924. //int count=0;
  925. cmd = USB1_USBCMD;
  926. while (1) {
  927. __disable_irq();
  928. USB1_USBCMD = cmd | USB_USBCMD_ATDTW;
  929. status = transfer->status;
  930. cmd = USB1_USBCMD;
  931. __enable_irq();
  932. if (cmd & USB_USBCMD_ATDTW) return status;
  933. //if (!(cmd & USB_USBCMD_ATDTW)) continue;
  934. //if (status & 0x80) break; // for still active, only 1 reading needed
  935. //if (++count > 1) break; // for completed, check 10 times
  936. }
  937. #else
  938. return transfer->status;
  939. #endif
  940. }