Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

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