No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

1015 líneas
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. // fall through to next case, to always send ZLP ACK
  514. case 0x2321: // CDC_SEND_BREAK
  515. endpoint0_receive(NULL, 0, 0);
  516. return;
  517. case 0x2021: // CDC_SET_LINE_CODING
  518. if (setup.wLength != 7) break;
  519. endpoint0_setupdata.bothwords = setupdata;
  520. endpoint0_receive(endpoint0_buffer, 7, 1);
  521. return;
  522. #endif
  523. #if defined(SEREMU_INTERFACE) || defined(KEYBOARD_INTERFACE)
  524. case 0x0921: // HID SET_REPORT
  525. if (setup.wLength <= sizeof(endpoint0_buffer)) {
  526. //printf("hid set report %x %x\n", setup.word1, setup.word2);
  527. endpoint0_setupdata.bothwords = setup.bothwords;
  528. endpoint0_buffer[0] = 0xE9;
  529. endpoint0_receive(endpoint0_buffer, setup.wLength, 1);
  530. return;
  531. }
  532. break;
  533. #endif
  534. #if defined(AUDIO_INTERFACE)
  535. case 0x0B01: // SET_INTERFACE (alternate setting)
  536. if (setup.wIndex == AUDIO_INTERFACE+1) {
  537. usb_audio_transmit_setting = setup.wValue;
  538. if (usb_audio_transmit_setting > 0) {
  539. // TODO: set up AUDIO_TX_ENDPOINT to transmit
  540. }
  541. endpoint0_receive(NULL, 0, 0);
  542. return;
  543. } else if (setup.wIndex == AUDIO_INTERFACE+2) {
  544. usb_audio_receive_setting = setup.wValue;
  545. endpoint0_receive(NULL, 0, 0);
  546. return;
  547. }
  548. break;
  549. case 0x0A81: // GET_INTERFACE (alternate setting)
  550. if (setup.wIndex == AUDIO_INTERFACE+1) {
  551. endpoint0_buffer[0] = usb_audio_transmit_setting;
  552. endpoint0_transmit(endpoint0_buffer, 1, 0);
  553. return;
  554. } else if (setup.wIndex == AUDIO_INTERFACE+2) {
  555. endpoint0_buffer[0] = usb_audio_receive_setting;
  556. endpoint0_transmit(endpoint0_buffer, 1, 0);
  557. return;
  558. }
  559. break;
  560. case 0x0121: // SET FEATURE
  561. case 0x0221:
  562. case 0x0321:
  563. case 0x0421:
  564. //printf("set_feature, word1=%x, len=%d\n", setup.word1, setup.wLength);
  565. if (setup.wLength <= sizeof(endpoint0_buffer)) {
  566. endpoint0_setupdata.bothwords = setupdata;
  567. endpoint0_receive(endpoint0_buffer, setup.wLength, 1);
  568. return; // handle these after ACK
  569. }
  570. break;
  571. case 0x81A1: // GET FEATURE
  572. case 0x82A1:
  573. case 0x83A1:
  574. case 0x84A1:
  575. if (setup.wLength <= sizeof(endpoint0_buffer)) {
  576. uint32_t len;
  577. if (usb_audio_get_feature(&setup, endpoint0_buffer, &len)) {
  578. //printf("GET feature, len=%d\n", len);
  579. endpoint0_transmit(endpoint0_buffer, len, 0);
  580. return;
  581. }
  582. }
  583. break;
  584. case 0x81A2: // GET_CUR (wValue=0, wIndex=interface, wLength=len)
  585. if (setup.wLength >= 3) {
  586. endpoint0_buffer[0] = 44100 & 255;
  587. endpoint0_buffer[1] = 44100 >> 8;
  588. endpoint0_buffer[2] = 0;
  589. endpoint0_transmit(endpoint0_buffer, 3, 0);
  590. return;
  591. }
  592. break;
  593. #endif
  594. }
  595. USB1_ENDPTCTRL0 = 0x000010001; // stall
  596. }
  597. static void endpoint0_transmit(const void *data, uint32_t len, int notify)
  598. {
  599. //printf("tx %lu\n", len);
  600. if (len > 0) {
  601. // Executing A Transfer Descriptor, page 3182
  602. endpoint0_transfer_data.next = 1;
  603. endpoint0_transfer_data.status = (len << 16) | (1<<7);
  604. uint32_t addr = (uint32_t)data;
  605. endpoint0_transfer_data.pointer0 = addr; // format: table 55-60, pg 3159
  606. endpoint0_transfer_data.pointer1 = addr + 4096;
  607. endpoint0_transfer_data.pointer2 = addr + 8192;
  608. endpoint0_transfer_data.pointer3 = addr + 12288;
  609. endpoint0_transfer_data.pointer4 = addr + 16384;
  610. // Case 1: Link list is empty, page 3182
  611. endpoint_queue_head[1].next = (uint32_t)&endpoint0_transfer_data;
  612. endpoint_queue_head[1].status = 0;
  613. USB1_ENDPTPRIME |= (1<<16);
  614. while (USB1_ENDPTPRIME) ;
  615. }
  616. endpoint0_transfer_ack.next = 1;
  617. endpoint0_transfer_ack.status = (1<<7) | (notify ? (1 << 15) : 0);
  618. endpoint0_transfer_ack.pointer0 = 0;
  619. endpoint_queue_head[0].next = (uint32_t)&endpoint0_transfer_ack;
  620. endpoint_queue_head[0].status = 0;
  621. USB1_ENDPTCOMPLETE = (1<<0) | (1<<16);
  622. USB1_ENDPTPRIME |= (1<<0);
  623. endpoint0_notify_mask = (notify ? (1 << 0) : 0);
  624. while (USB1_ENDPTPRIME) ;
  625. }
  626. static void endpoint0_receive(void *data, uint32_t len, int notify)
  627. {
  628. //printf("rx %lu\n", len);
  629. if (len > 0) {
  630. // Executing A Transfer Descriptor, page 3182
  631. endpoint0_transfer_data.next = 1;
  632. endpoint0_transfer_data.status = (len << 16) | (1<<7);
  633. uint32_t addr = (uint32_t)data;
  634. endpoint0_transfer_data.pointer0 = addr; // format: table 55-60, pg 3159
  635. endpoint0_transfer_data.pointer1 = addr + 4096;
  636. endpoint0_transfer_data.pointer2 = addr + 8192;
  637. endpoint0_transfer_data.pointer3 = addr + 12288;
  638. endpoint0_transfer_data.pointer4 = addr + 16384;
  639. // Case 1: Link list is empty, page 3182
  640. endpoint_queue_head[0].next = (uint32_t)&endpoint0_transfer_data;
  641. endpoint_queue_head[0].status = 0;
  642. USB1_ENDPTPRIME |= (1<<0);
  643. while (USB1_ENDPTPRIME) ;
  644. }
  645. endpoint0_transfer_ack.next = 1;
  646. endpoint0_transfer_ack.status = (1<<7) | (notify ? (1 << 15) : 0);
  647. endpoint0_transfer_ack.pointer0 = 0;
  648. endpoint_queue_head[1].next = (uint32_t)&endpoint0_transfer_ack;
  649. endpoint_queue_head[1].status = 0;
  650. USB1_ENDPTCOMPLETE = (1<<0) | (1<<16);
  651. USB1_ENDPTPRIME |= (1<<16);
  652. endpoint0_notify_mask = (notify ? (1 << 16) : 0);
  653. while (USB1_ENDPTPRIME) ;
  654. }
  655. /*typedef union {
  656. struct {
  657. union {
  658. struct {
  659. uint8_t bmRequestType;
  660. uint8_t bRequest;
  661. };
  662. uint16_t wRequestAndType;
  663. };
  664. uint16_t wValue;
  665. uint16_t wIndex;
  666. uint16_t wLength;
  667. };
  668. struct {
  669. uint32_t word1;
  670. uint32_t word2;
  671. };
  672. uint64_t bothwords;
  673. } setup_t; */
  674. static void endpoint0_complete(void)
  675. {
  676. setup_t setup;
  677. setup.bothwords = endpoint0_setupdata.bothwords;
  678. //printf("complete %x %x %x\n", setup.word1, setup.word2, endpoint0_buffer[0]);
  679. #ifdef CDC_STATUS_INTERFACE
  680. // 0x2021 is CDC_SET_LINE_CODING
  681. if (setup.wRequestAndType == 0x2021 && setup.wIndex == CDC_STATUS_INTERFACE) {
  682. memcpy(usb_cdc_line_coding, endpoint0_buffer, 7);
  683. printf("usb_cdc_line_coding, baud=%u\n", usb_cdc_line_coding[0]);
  684. if (usb_cdc_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 CDC2_STATUS_INTERFACE
  691. if (setup.wRequestAndType == 0x2021 && setup.wIndex == CDC2_STATUS_INTERFACE) {
  692. memcpy(usb_cdc2_line_coding, endpoint0_buffer, 7);
  693. printf("usb_cdc2_line_coding, baud=%u\n", usb_cdc2_line_coding[0]);
  694. if (usb_cdc2_line_coding[0] == 134) {
  695. usb_start_sof_interrupts(NUM_INTERFACE);
  696. usb_reboot_timer = 80; // TODO: 10 if only 12 Mbit/sec
  697. }
  698. }
  699. #endif
  700. #ifdef CDC3_STATUS_INTERFACE
  701. if (setup.wRequestAndType == 0x2021 && setup.wIndex == CDC3_STATUS_INTERFACE) {
  702. memcpy(usb_cdc3_line_coding, endpoint0_buffer, 7);
  703. printf("usb_cdc3_line_coding, baud=%u\n", usb_cdc3_line_coding[0]);
  704. if (usb_cdc3_line_coding[0] == 134) {
  705. usb_start_sof_interrupts(NUM_INTERFACE);
  706. usb_reboot_timer = 80; // TODO: 10 if only 12 Mbit/sec
  707. }
  708. }
  709. #endif
  710. #ifdef KEYBOARD_INTERFACE
  711. if (setup.word1 == 0x02000921 && setup.word2 == ((1 << 16) | KEYBOARD_INTERFACE)) {
  712. keyboard_leds = endpoint0_buffer[0];
  713. endpoint0_transmit(NULL, 0, 0);
  714. }
  715. #endif
  716. #ifdef SEREMU_INTERFACE
  717. if (setup.word1 == 0x03000921 && setup.word2 == ((4<<16)|SEREMU_INTERFACE)
  718. && endpoint0_buffer[0] == 0xA9 && endpoint0_buffer[1] == 0x45
  719. && endpoint0_buffer[2] == 0xC2 && endpoint0_buffer[3] == 0x6B) {
  720. printf("seremu reboot request\n");
  721. usb_start_sof_interrupts(NUM_INTERFACE);
  722. usb_reboot_timer = 80; // TODO: 10 if only 12 Mbit/sec
  723. }
  724. #endif
  725. #ifdef AUDIO_INTERFACE
  726. if (setup.word1 == 0x02010121 /* TODO: check setup.word2 */) {
  727. usb_audio_set_feature(&endpoint0_setupdata, endpoint0_buffer);
  728. }
  729. #endif
  730. }
  731. static void usb_endpoint_config(endpoint_t *qh, uint32_t config, void (*callback)(transfer_t *))
  732. {
  733. memset(qh, 0, sizeof(endpoint_t));
  734. qh->config = config;
  735. qh->next = 1; // Terminate bit = 1
  736. qh->callback_function = callback;
  737. }
  738. void usb_config_rx(uint32_t ep, uint32_t packet_size, int do_zlp, void (*cb)(transfer_t *))
  739. {
  740. uint32_t config = (packet_size << 16) | (do_zlp ? 0 : (1 << 29));
  741. if (ep < 2 || ep > NUM_ENDPOINTS) return;
  742. usb_endpoint_config(endpoint_queue_head + ep * 2, config, cb);
  743. if (cb) endpointN_notify_mask |= (1 << ep);
  744. }
  745. void usb_config_tx(uint32_t ep, uint32_t packet_size, int do_zlp, void (*cb)(transfer_t *))
  746. {
  747. uint32_t config = (packet_size << 16) | (do_zlp ? 0 : (1 << 29));
  748. if (ep < 2 || ep > NUM_ENDPOINTS) return;
  749. usb_endpoint_config(endpoint_queue_head + ep * 2 + 1, config, cb);
  750. if (cb) endpointN_notify_mask |= (1 << (ep + 16));
  751. }
  752. void usb_config_rx_iso(uint32_t ep, uint32_t packet_size, int mult, void (*cb)(transfer_t *))
  753. {
  754. if (mult < 1 || mult > 3) return;
  755. uint32_t config = (packet_size << 16) | (mult << 30);
  756. if (ep < 2 || ep > NUM_ENDPOINTS) return;
  757. usb_endpoint_config(endpoint_queue_head + ep * 2, config, cb);
  758. if (cb) endpointN_notify_mask |= (1 << ep);
  759. }
  760. void usb_config_tx_iso(uint32_t ep, uint32_t packet_size, int mult, void (*cb)(transfer_t *))
  761. {
  762. if (mult < 1 || mult > 3) return;
  763. uint32_t config = (packet_size << 16) | (mult << 30);
  764. if (ep < 2 || ep > NUM_ENDPOINTS) return;
  765. usb_endpoint_config(endpoint_queue_head + ep * 2 + 1, config, cb);
  766. if (cb) endpointN_notify_mask |= (1 << (ep + 16));
  767. }
  768. void usb_prepare_transfer(transfer_t *transfer, const void *data, uint32_t len, uint32_t param)
  769. {
  770. transfer->next = 1;
  771. transfer->status = (len << 16) | (1<<7);
  772. uint32_t addr = (uint32_t)data;
  773. transfer->pointer0 = addr;
  774. transfer->pointer1 = addr + 4096;
  775. transfer->pointer2 = addr + 8192;
  776. transfer->pointer3 = addr + 12288;
  777. transfer->pointer4 = addr + 16384;
  778. transfer->callback_param = param;
  779. }
  780. #if 0
  781. void usb_print_transfer_log(void)
  782. {
  783. uint32_t i, count;
  784. printf("log %d transfers\n", transfer_log_count);
  785. count = transfer_log_count;
  786. if (count > LOG_SIZE) count = LOG_SIZE;
  787. for (i=0; i < count; i++) {
  788. if (transfer_log_head == 0) transfer_log_head = LOG_SIZE;
  789. transfer_log_head--;
  790. uint32_t log = transfer_log[transfer_log_head];
  791. printf(" %c %X\n", log >> 8, (int)(log & 255));
  792. }
  793. }
  794. #endif
  795. static void schedule_transfer(endpoint_t *endpoint, uint32_t epmask, transfer_t *transfer)
  796. {
  797. // when we stop at 6, why is the last transfer missing from the USB output?
  798. //if (transfer_log_count >= 6) return;
  799. //uint32_t ret = (*(const uint8_t *)transfer->pointer0) << 8;
  800. if (endpoint->callback_function) {
  801. transfer->status |= (1<<15);
  802. }
  803. __disable_irq();
  804. //digitalWriteFast(1, HIGH);
  805. // Executing A Transfer Descriptor, page 2468 (RT1060 manual, Rev 1, 12/2018)
  806. transfer_t *last = endpoint->last_transfer;
  807. if (last) {
  808. last->next = (uint32_t)transfer;
  809. if (USB1_ENDPTPRIME & epmask) goto end;
  810. //digitalWriteFast(2, HIGH);
  811. //ret |= 0x01;
  812. uint32_t status, cyccnt=ARM_DWT_CYCCNT;
  813. do {
  814. USB1_USBCMD |= USB_USBCMD_ATDTW;
  815. status = USB1_ENDPTSTATUS;
  816. } while (!(USB1_USBCMD & USB_USBCMD_ATDTW) && (ARM_DWT_CYCCNT - cyccnt < 2400));
  817. //USB1_USBCMD &= ~USB_USBCMD_ATDTW;
  818. if (status & epmask) goto end;
  819. //ret |= 0x02;
  820. }
  821. //digitalWriteFast(4, HIGH);
  822. endpoint->next = (uint32_t)transfer;
  823. endpoint->status = 0;
  824. USB1_ENDPTPRIME |= epmask;
  825. endpoint->first_transfer = transfer;
  826. end:
  827. endpoint->last_transfer = transfer;
  828. __enable_irq();
  829. //digitalWriteFast(4, LOW);
  830. //digitalWriteFast(3, LOW);
  831. //digitalWriteFast(2, LOW);
  832. //digitalWriteFast(1, LOW);
  833. //if (transfer_log_head > LOG_SIZE) transfer_log_head = 0;
  834. //transfer_log[transfer_log_head++] = ret;
  835. //transfer_log_count++;
  836. }
  837. // ENDPTPRIME - Software should write a one to the corresponding bit when
  838. // posting a new transfer descriptor to an endpoint queue head.
  839. // Hardware automatically uses this bit to begin parsing for a
  840. // new transfer descriptor from the queue head and prepare a
  841. // transmit buffer. Hardware clears this bit when the associated
  842. // endpoint(s) is (are) successfully primed.
  843. // Momentarily set by hardware during hardware re-priming
  844. // operations when a dTD is retired, and the dQH is updated.
  845. // ENDPTSTATUS - Transmit Buffer Ready - set to one by the hardware as a
  846. // response to receiving a command from a corresponding bit
  847. // in the ENDPTPRIME register. . Buffer ready is cleared by
  848. // USB reset, by the USB DMA system, or through the ENDPTFLUSH
  849. // register. (so 0=buffer ready, 1=buffer primed for transmit)
  850. // USBCMD.ATDTW - This bit is used as a semaphore to ensure proper addition
  851. // of a new dTD to an active (primed) endpoint's linked list.
  852. // This bit is set and cleared by software.
  853. // This bit would also be cleared by hardware when state machine
  854. // is hazard region for which adding a dTD to a primed endpoint
  855. // may go unrecognized.
  856. /*struct endpoint_struct {
  857. uint32_t config;
  858. uint32_t current;
  859. uint32_t next;
  860. uint32_t status;
  861. uint32_t pointer0;
  862. uint32_t pointer1;
  863. uint32_t pointer2;
  864. uint32_t pointer3;
  865. uint32_t pointer4;
  866. uint32_t reserved;
  867. uint32_t setup0;
  868. uint32_t setup1;
  869. transfer_t *first_transfer;
  870. transfer_t *last_transfer;
  871. void (*callback_function)(transfer_t *completed_transfer);
  872. uint32_t unused1;
  873. };*/
  874. static void run_callbacks(endpoint_t *ep)
  875. {
  876. //printf("run_callbacks\n");
  877. transfer_t *first = ep->first_transfer;
  878. if (first == NULL) return;
  879. // count how many transfers are completed, then remove them from the endpoint's list
  880. uint32_t count = 0;
  881. transfer_t *t = first;
  882. while (1) {
  883. if (t->status & (1<<7)) {
  884. // found a still-active transfer, new list begins here
  885. //printf(" still active\n");
  886. ep->first_transfer = t;
  887. break;
  888. }
  889. count++;
  890. t = (transfer_t *)t->next;
  891. if ((uint32_t)t == 1) {
  892. // reached end of list, all need callbacks, new list is empty
  893. //printf(" end of list\n");
  894. ep->first_transfer = NULL;
  895. ep->last_transfer = NULL;
  896. break;
  897. }
  898. }
  899. // do all the callbacks
  900. while (count) {
  901. transfer_t *next = (transfer_t *)first->next;
  902. ep->callback_function(first);
  903. first = next;
  904. count--;
  905. }
  906. }
  907. void usb_transmit(int endpoint_number, transfer_t *transfer)
  908. {
  909. if (endpoint_number < 2 || endpoint_number > NUM_ENDPOINTS) return;
  910. endpoint_t *endpoint = endpoint_queue_head + endpoint_number * 2 + 1;
  911. uint32_t mask = 1 << (endpoint_number + 16);
  912. schedule_transfer(endpoint, mask, transfer);
  913. }
  914. void usb_receive(int endpoint_number, transfer_t *transfer)
  915. {
  916. if (endpoint_number < 2 || endpoint_number > NUM_ENDPOINTS) return;
  917. endpoint_t *endpoint = endpoint_queue_head + endpoint_number * 2;
  918. uint32_t mask = 1 << endpoint_number;
  919. schedule_transfer(endpoint, mask, transfer);
  920. }
  921. uint32_t usb_transfer_status(const transfer_t *transfer)
  922. {
  923. #if 0
  924. uint32_t status, cmd;
  925. //int count=0;
  926. cmd = USB1_USBCMD;
  927. while (1) {
  928. __disable_irq();
  929. USB1_USBCMD = cmd | USB_USBCMD_ATDTW;
  930. status = transfer->status;
  931. cmd = USB1_USBCMD;
  932. __enable_irq();
  933. if (cmd & USB_USBCMD_ATDTW) return status;
  934. //if (!(cmd & USB_USBCMD_ATDTW)) continue;
  935. //if (status & 0x80) break; // for still active, only 1 reading needed
  936. //if (++count > 1) break; // for completed, check 10 times
  937. }
  938. #else
  939. return transfer->status;
  940. #endif
  941. }