You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

845 lines
25KB

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