Teensy 4.1 core updated for C++20
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

1033 lines
31KB

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