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.

848 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. #ifdef MULTITOUCH_INTERFACE
  284. usb_touchscreen_update_callback();
  285. #endif
  286. }
  287. }
  288. void usb_start_sof_interrupts(int interface)
  289. {
  290. __disable_irq();
  291. sof_usage |= (1 << interface);
  292. uint32_t intr = USB1_USBINTR;
  293. if (!(intr & USB_USBINTR_SRE)) {
  294. USB1_USBSTS = USB_USBSTS_SRI; // clear prior SOF before SOF IRQ enable
  295. USB1_USBINTR = intr | USB_USBINTR_SRE;
  296. }
  297. __enable_irq();
  298. }
  299. void usb_stop_sof_interrupts(int interface)
  300. {
  301. sof_usage &= ~(1 << interface);
  302. if (sof_usage == 0) {
  303. USB1_USBINTR &= ~USB_USBINTR_SRE;
  304. }
  305. }
  306. /*
  307. struct transfer_struct { // table 55-60, pg 3159
  308. uint32_t next;
  309. uint32_t status;
  310. uint32_t pointer0;
  311. uint32_t pointer1;
  312. uint32_t pointer2;
  313. uint32_t pointer3;
  314. uint32_t pointer4;
  315. uint32_t unused1;
  316. };
  317. transfer_t endpoint0_transfer_data __attribute__ ((aligned(32)));;
  318. transfer_t endpoint0_transfer_ack __attribute__ ((aligned(32)));;
  319. */
  320. static uint8_t reply_buffer[8];
  321. static void endpoint0_setup(uint64_t setupdata)
  322. {
  323. setup_t setup;
  324. uint32_t endpoint, dir, ctrl;
  325. const usb_descriptor_list_t *list;
  326. setup.bothwords = setupdata;
  327. switch (setup.wRequestAndType) {
  328. case 0x0500: // SET_ADDRESS
  329. endpoint0_receive(NULL, 0, 0);
  330. USB1_DEVICEADDR = USB_DEVICEADDR_USBADR(setup.wValue) | USB_DEVICEADDR_USBADRA;
  331. return;
  332. case 0x0900: // SET_CONFIGURATION
  333. usb_configuration = setup.wValue;
  334. // configure all other endpoints
  335. #if defined(ENDPOINT2_CONFIG)
  336. USB1_ENDPTCTRL2 = ENDPOINT2_CONFIG;
  337. #endif
  338. #if defined(ENDPOINT3_CONFIG)
  339. USB1_ENDPTCTRL3 = ENDPOINT3_CONFIG;
  340. #endif
  341. #if defined(ENDPOINT4_CONFIG)
  342. USB1_ENDPTCTRL4 = ENDPOINT4_CONFIG;
  343. #endif
  344. #if defined(ENDPOINT5_CONFIG)
  345. USB1_ENDPTCTRL5 = ENDPOINT5_CONFIG;
  346. #endif
  347. #if defined(ENDPOINT6_CONFIG)
  348. USB1_ENDPTCTRL6 = ENDPOINT6_CONFIG;
  349. #endif
  350. #if defined(ENDPOINT7_CONFIG)
  351. USB1_ENDPTCTRL7 = ENDPOINT7_CONFIG;
  352. #endif
  353. #if defined(CDC_STATUS_INTERFACE) && defined(CDC_DATA_INTERFACE)
  354. usb_serial_configure();
  355. #elif defined(SEREMU_INTERFACE)
  356. usb_seremu_configure();
  357. #endif
  358. #if defined(RAWHID_INTERFACE)
  359. usb_rawhid_configure();
  360. #endif
  361. #if defined(KEYBOARD_INTERFACE)
  362. usb_keyboard_configure();
  363. #endif
  364. #if defined(MOUSE_INTERFACE)
  365. usb_mouse_configure();
  366. #endif
  367. #if defined(JOYSTICK_INTERFACE)
  368. usb_joystick_configure();
  369. #endif
  370. #if defined(MULTITOUCH_INTERFACE)
  371. usb_touchscreen_configure();
  372. #endif
  373. #if defined(MIDI_INTERFACE)
  374. usb_midi_configure();
  375. #endif
  376. endpoint0_receive(NULL, 0, 0);
  377. return;
  378. case 0x0880: // GET_CONFIGURATION
  379. reply_buffer[0] = usb_configuration;
  380. endpoint0_transmit(reply_buffer, 1, 0);
  381. return;
  382. case 0x0080: // GET_STATUS (device)
  383. reply_buffer[0] = 0;
  384. reply_buffer[1] = 0;
  385. endpoint0_transmit(reply_buffer, 2, 0);
  386. return;
  387. case 0x0082: // GET_STATUS (endpoint)
  388. endpoint = setup.wIndex & 0x7F;
  389. if (endpoint > 7) break;
  390. dir = setup.wIndex & 0x80;
  391. ctrl = *((uint32_t *)&USB1_ENDPTCTRL0 + endpoint);
  392. reply_buffer[0] = 0;
  393. reply_buffer[1] = 0;
  394. if ((dir && (ctrl & USB_ENDPTCTRL_TXS)) || (!dir && (ctrl & USB_ENDPTCTRL_RXS))) {
  395. reply_buffer[0] = 1;
  396. }
  397. endpoint0_transmit(reply_buffer, 2, 0);
  398. return;
  399. case 0x0302: // SET_FEATURE (endpoint)
  400. endpoint = setup.wIndex & 0x7F;
  401. if (endpoint > 7) break;
  402. dir = setup.wIndex & 0x80;
  403. if (dir) {
  404. *((volatile uint32_t *)&USB1_ENDPTCTRL0 + endpoint) |= USB_ENDPTCTRL_TXS;
  405. } else {
  406. *((volatile uint32_t *)&USB1_ENDPTCTRL0 + endpoint) |= USB_ENDPTCTRL_RXS;
  407. }
  408. endpoint0_receive(NULL, 0, 0);
  409. return;
  410. case 0x0102: // CLEAR_FEATURE (endpoint)
  411. endpoint = setup.wIndex & 0x7F;
  412. if (endpoint > 7) break;
  413. dir = setup.wIndex & 0x80;
  414. if (dir) {
  415. *((volatile uint32_t *)&USB1_ENDPTCTRL0 + endpoint) &= ~USB_ENDPTCTRL_TXS;
  416. } else {
  417. *((volatile uint32_t *)&USB1_ENDPTCTRL0 + endpoint) &= ~USB_ENDPTCTRL_RXS;
  418. }
  419. endpoint0_receive(NULL, 0, 0);
  420. return;
  421. case 0x0680: // GET_DESCRIPTOR
  422. case 0x0681:
  423. for (list = usb_descriptor_list; list->addr != NULL; list++) {
  424. if (setup.wValue == list->wValue && setup.wIndex == list->wIndex) {
  425. uint32_t datalen;
  426. if ((setup.wValue >> 8) == 3) {
  427. // for string descriptors, use the descriptor's
  428. // length field, allowing runtime configured length.
  429. datalen = *(list->addr);
  430. } else {
  431. datalen = list->length;
  432. }
  433. if (datalen > setup.wLength) datalen = setup.wLength;
  434. // copy the descriptor, from PROGMEM to DMAMEM
  435. if (setup.wValue == 0x200) {
  436. // config descriptor needs to adapt to speed
  437. const uint8_t *src = usb_config_descriptor_12;
  438. if (usb_high_speed) src = usb_config_descriptor_480;
  439. memcpy(usb_descriptor_buffer, src, datalen);
  440. } else if (setup.wValue == 0x700) {
  441. // other speed config also needs to adapt
  442. const uint8_t *src = usb_config_descriptor_480;
  443. if (usb_high_speed) src = usb_config_descriptor_12;
  444. memcpy(usb_descriptor_buffer, src, datalen);
  445. usb_descriptor_buffer[1] = 7;
  446. } else {
  447. memcpy(usb_descriptor_buffer, list->addr, datalen);
  448. }
  449. // prep transmit
  450. arm_dcache_flush_delete(usb_descriptor_buffer, datalen);
  451. endpoint0_transmit(usb_descriptor_buffer, datalen, 0);
  452. return;
  453. }
  454. }
  455. break;
  456. #if defined(CDC_STATUS_INTERFACE)
  457. case 0x2221: // CDC_SET_CONTROL_LINE_STATE
  458. usb_cdc_line_rtsdtr_millis = systick_millis_count;
  459. usb_cdc_line_rtsdtr = setup.wValue;
  460. case 0x2321: // CDC_SEND_BREAK
  461. endpoint0_receive(NULL, 0, 0);
  462. return;
  463. case 0x2021: // CDC_SET_LINE_CODING
  464. if (setup.wLength != 7) break;
  465. endpoint0_setupdata.bothwords = setupdata;
  466. endpoint0_receive(endpoint0_buffer, 7, 1);
  467. return;
  468. #endif
  469. #if defined(SEREMU_INTERFACE) || defined(KEYBOARD_INTERFACE)
  470. case 0x0921: // HID SET_REPORT
  471. if (setup.wLength <= sizeof(endpoint0_buffer)) {
  472. //printf("hid set report %x %x\n", setup.word1, setup.word2);
  473. endpoint0_setupdata.bothwords = setup.bothwords;
  474. endpoint0_buffer[0] = 0xE9;
  475. endpoint0_receive(endpoint0_buffer, setup.wLength, 1);
  476. return;
  477. }
  478. break;
  479. #endif
  480. }
  481. USB1_ENDPTCTRL0 = 0x000010001; // stall
  482. }
  483. static void endpoint0_transmit(const void *data, uint32_t len, int notify)
  484. {
  485. //printf("tx %lu\n", len);
  486. if (len > 0) {
  487. // Executing A Transfer Descriptor, page 3182
  488. endpoint0_transfer_data.next = 1;
  489. endpoint0_transfer_data.status = (len << 16) | (1<<7);
  490. uint32_t addr = (uint32_t)data;
  491. endpoint0_transfer_data.pointer0 = addr; // format: table 55-60, pg 3159
  492. endpoint0_transfer_data.pointer1 = addr + 4096;
  493. endpoint0_transfer_data.pointer2 = addr + 8192;
  494. endpoint0_transfer_data.pointer3 = addr + 12288;
  495. endpoint0_transfer_data.pointer4 = addr + 16384;
  496. // Case 1: Link list is empty, page 3182
  497. endpoint_queue_head[1].next = (uint32_t)&endpoint0_transfer_data;
  498. endpoint_queue_head[1].status = 0;
  499. USB1_ENDPTPRIME |= (1<<16);
  500. while (USB1_ENDPTPRIME) ;
  501. }
  502. endpoint0_transfer_ack.next = 1;
  503. endpoint0_transfer_ack.status = (1<<7) | (notify ? (1 << 15) : 0);
  504. endpoint0_transfer_ack.pointer0 = 0;
  505. endpoint_queue_head[0].next = (uint32_t)&endpoint0_transfer_ack;
  506. endpoint_queue_head[0].status = 0;
  507. USB1_ENDPTCOMPLETE |= (1<<0) | (1<<16);
  508. USB1_ENDPTPRIME |= (1<<0);
  509. endpoint0_notify_mask = (notify ? (1 << 0) : 0);
  510. while (USB1_ENDPTPRIME) ;
  511. }
  512. static void endpoint0_receive(void *data, uint32_t len, int notify)
  513. {
  514. //printf("rx %lu\n", len);
  515. if (len > 0) {
  516. // Executing A Transfer Descriptor, page 3182
  517. endpoint0_transfer_data.next = 1;
  518. endpoint0_transfer_data.status = (len << 16) | (1<<7);
  519. uint32_t addr = (uint32_t)data;
  520. endpoint0_transfer_data.pointer0 = addr; // format: table 55-60, pg 3159
  521. endpoint0_transfer_data.pointer1 = addr + 4096;
  522. endpoint0_transfer_data.pointer2 = addr + 8192;
  523. endpoint0_transfer_data.pointer3 = addr + 12288;
  524. endpoint0_transfer_data.pointer4 = addr + 16384;
  525. // Case 1: Link list is empty, page 3182
  526. endpoint_queue_head[0].next = (uint32_t)&endpoint0_transfer_data;
  527. endpoint_queue_head[0].status = 0;
  528. USB1_ENDPTPRIME |= (1<<0);
  529. while (USB1_ENDPTPRIME) ;
  530. }
  531. endpoint0_transfer_ack.next = 1;
  532. endpoint0_transfer_ack.status = (1<<7) | (notify ? (1 << 15) : 0);
  533. endpoint0_transfer_ack.pointer0 = 0;
  534. endpoint_queue_head[1].next = (uint32_t)&endpoint0_transfer_ack;
  535. endpoint_queue_head[1].status = 0;
  536. USB1_ENDPTCOMPLETE |= (1<<0) | (1<<16);
  537. USB1_ENDPTPRIME |= (1<<16);
  538. endpoint0_notify_mask = (notify ? (1 << 16) : 0);
  539. while (USB1_ENDPTPRIME) ;
  540. }
  541. /*typedef union {
  542. struct {
  543. union {
  544. struct {
  545. uint8_t bmRequestType;
  546. uint8_t bRequest;
  547. };
  548. uint16_t wRequestAndType;
  549. };
  550. uint16_t wValue;
  551. uint16_t wIndex;
  552. uint16_t wLength;
  553. };
  554. struct {
  555. uint32_t word1;
  556. uint32_t word2;
  557. };
  558. uint64_t bothwords;
  559. } setup_t; */
  560. static void endpoint0_complete(void)
  561. {
  562. setup_t setup;
  563. setup.bothwords = endpoint0_setupdata.bothwords;
  564. //printf("complete %x %x %x\n", setup.word1, setup.word2, endpoint0_buffer[0]);
  565. #ifdef CDC_STATUS_INTERFACE
  566. if (setup.wRequestAndType == 0x2021 /*CDC_SET_LINE_CODING*/) {
  567. memcpy(usb_cdc_line_coding, endpoint0_buffer, 7);
  568. printf("usb_cdc_line_coding, baud=%u\n", usb_cdc_line_coding[0]);
  569. if (usb_cdc_line_coding[0] == 134) {
  570. usb_start_sof_interrupts(NUM_INTERFACE);
  571. usb_reboot_timer = 80; // TODO: 10 if only 12 Mbit/sec
  572. }
  573. }
  574. #endif
  575. #ifdef SEREMU_INTERFACE
  576. if (setup.word1 == 0x03000921 && setup.word2 == ((4<<16)|SEREMU_INTERFACE)
  577. && endpoint0_buffer[0] == 0xA9 && endpoint0_buffer[1] == 0x45
  578. && endpoint0_buffer[2] == 0xC2 && endpoint0_buffer[3] == 0x6B) {
  579. printf("seremu reboot request\n");
  580. usb_start_sof_interrupts(NUM_INTERFACE);
  581. usb_reboot_timer = 80; // TODO: 10 if only 12 Mbit/sec
  582. }
  583. #endif
  584. }
  585. static void usb_endpoint_config(endpoint_t *qh, uint32_t config, void (*callback)(transfer_t *))
  586. {
  587. memset(qh, 0, sizeof(endpoint_t));
  588. qh->config = config;
  589. qh->next = 1; // Terminate bit = 1
  590. qh->callback_function = callback;
  591. }
  592. void usb_config_rx(uint32_t ep, uint32_t packet_size, int do_zlp, void (*cb)(transfer_t *))
  593. {
  594. uint32_t config = (packet_size << 16) | (do_zlp ? 0 : (1 << 29));
  595. if (ep < 2 || ep > NUM_ENDPOINTS) return;
  596. usb_endpoint_config(endpoint_queue_head + ep * 2, config, cb);
  597. if (cb) endpointN_notify_mask |= (1 << ep);
  598. }
  599. void usb_config_tx(uint32_t ep, uint32_t packet_size, int do_zlp, void (*cb)(transfer_t *))
  600. {
  601. uint32_t config = (packet_size << 16) | (do_zlp ? 0 : (1 << 29));
  602. if (ep < 2 || ep > NUM_ENDPOINTS) return;
  603. usb_endpoint_config(endpoint_queue_head + ep * 2 + 1, config, cb);
  604. if (cb) endpointN_notify_mask |= (1 << (ep + 16));
  605. }
  606. void usb_prepare_transfer(transfer_t *transfer, const void *data, uint32_t len, uint32_t param)
  607. {
  608. transfer->next = 1;
  609. transfer->status = (len << 16) | (1<<7);
  610. uint32_t addr = (uint32_t)data;
  611. transfer->pointer0 = addr;
  612. transfer->pointer1 = addr + 4096;
  613. transfer->pointer2 = addr + 8192;
  614. transfer->pointer3 = addr + 12288;
  615. transfer->pointer4 = addr + 16384;
  616. transfer->callback_param = param;
  617. }
  618. #if 0
  619. void usb_print_transfer_log(void)
  620. {
  621. uint32_t i, count;
  622. printf("log %d transfers\n", transfer_log_count);
  623. count = transfer_log_count;
  624. if (count > LOG_SIZE) count = LOG_SIZE;
  625. for (i=0; i < count; i++) {
  626. if (transfer_log_head == 0) transfer_log_head = LOG_SIZE;
  627. transfer_log_head--;
  628. uint32_t log = transfer_log[transfer_log_head];
  629. printf(" %c %X\n", log >> 8, (int)(log & 255));
  630. }
  631. }
  632. #endif
  633. static void schedule_transfer(endpoint_t *endpoint, uint32_t epmask, transfer_t *transfer)
  634. {
  635. // when we stop at 6, why is the last transfer missing from the USB output?
  636. //if (transfer_log_count >= 6) return;
  637. //uint32_t ret = (*(const uint8_t *)transfer->pointer0) << 8;
  638. if (endpoint->callback_function) {
  639. transfer->status |= (1<<15);
  640. }
  641. __disable_irq();
  642. //digitalWriteFast(1, HIGH);
  643. // Executing A Transfer Descriptor, page 2468 (RT1060 manual, Rev 1, 12/2018)
  644. transfer_t *last = endpoint->last_transfer;
  645. if (last) {
  646. last->next = (uint32_t)transfer;
  647. if (USB1_ENDPTPRIME & epmask) goto end;
  648. //digitalWriteFast(2, HIGH);
  649. //ret |= 0x01;
  650. uint32_t status;
  651. do {
  652. USB1_USBCMD |= USB_USBCMD_ATDTW;
  653. status = USB1_ENDPTSTATUS;
  654. } while (!(USB1_USBCMD & USB_USBCMD_ATDTW));
  655. //USB1_USBCMD &= ~USB_USBCMD_ATDTW;
  656. if (status & epmask) goto end;
  657. //ret |= 0x02;
  658. }
  659. //digitalWriteFast(4, HIGH);
  660. endpoint->next = (uint32_t)transfer;
  661. endpoint->status = 0;
  662. USB1_ENDPTPRIME |= epmask;
  663. endpoint->first_transfer = transfer;
  664. end:
  665. endpoint->last_transfer = transfer;
  666. __enable_irq();
  667. //digitalWriteFast(4, LOW);
  668. //digitalWriteFast(3, LOW);
  669. //digitalWriteFast(2, LOW);
  670. //digitalWriteFast(1, LOW);
  671. //if (transfer_log_head > LOG_SIZE) transfer_log_head = 0;
  672. //transfer_log[transfer_log_head++] = ret;
  673. //transfer_log_count++;
  674. }
  675. // ENDPTPRIME - Software should write a one to the corresponding bit when
  676. // posting a new transfer descriptor to an endpoint queue head.
  677. // Hardware automatically uses this bit to begin parsing for a
  678. // new transfer descriptor from the queue head and prepare a
  679. // transmit buffer. Hardware clears this bit when the associated
  680. // endpoint(s) is (are) successfully primed.
  681. // Momentarily set by hardware during hardware re-priming
  682. // operations when a dTD is retired, and the dQH is updated.
  683. // ENDPTSTATUS - Transmit Buffer Ready - set to one by the hardware as a
  684. // response to receiving a command from a corresponding bit
  685. // in the ENDPTPRIME register. . Buffer ready is cleared by
  686. // USB reset, by the USB DMA system, or through the ENDPTFLUSH
  687. // register. (so 0=buffer ready, 1=buffer primed for transmit)
  688. // USBCMD.ATDTW - This bit is used as a semaphore to ensure proper addition
  689. // of a new dTD to an active (primed) endpoint's linked list.
  690. // This bit is set and cleared by software.
  691. // This bit would also be cleared by hardware when state machine
  692. // is hazard region for which adding a dTD to a primed endpoint
  693. // may go unrecognized.
  694. /*struct endpoint_struct {
  695. uint32_t config;
  696. uint32_t current;
  697. uint32_t next;
  698. uint32_t status;
  699. uint32_t pointer0;
  700. uint32_t pointer1;
  701. uint32_t pointer2;
  702. uint32_t pointer3;
  703. uint32_t pointer4;
  704. uint32_t reserved;
  705. uint32_t setup0;
  706. uint32_t setup1;
  707. transfer_t *first_transfer;
  708. transfer_t *last_transfer;
  709. void (*callback_function)(transfer_t *completed_transfer);
  710. uint32_t unused1;
  711. };*/
  712. static void run_callbacks(endpoint_t *ep)
  713. {
  714. //printf("run_callbacks\n");
  715. transfer_t *first = ep->first_transfer;
  716. if (first == NULL) return;
  717. // count how many transfers are completed, then remove them from the endpoint's list
  718. uint32_t count = 0;
  719. transfer_t *t = first;
  720. while (1) {
  721. if (t->status & (1<<7)) {
  722. // found a still-active transfer, new list begins here
  723. //printf(" still active\n");
  724. ep->first_transfer = t;
  725. break;
  726. }
  727. count++;
  728. t = (transfer_t *)t->next;
  729. if ((uint32_t)t == 1) {
  730. // reached end of list, all need callbacks, new list is empty
  731. //printf(" end of list\n");
  732. ep->first_transfer = NULL;
  733. ep->last_transfer = NULL;
  734. break;
  735. }
  736. }
  737. // do all the callbacks
  738. while (count) {
  739. transfer_t *next = (transfer_t *)first->next;
  740. ep->callback_function(first);
  741. first = next;
  742. count--;
  743. }
  744. }
  745. void usb_transmit(int endpoint_number, transfer_t *transfer)
  746. {
  747. if (endpoint_number < 2 || endpoint_number > NUM_ENDPOINTS) return;
  748. endpoint_t *endpoint = endpoint_queue_head + endpoint_number * 2 + 1;
  749. uint32_t mask = 1 << (endpoint_number + 16);
  750. schedule_transfer(endpoint, mask, transfer);
  751. }
  752. void usb_receive(int endpoint_number, transfer_t *transfer)
  753. {
  754. if (endpoint_number < 2 || endpoint_number > NUM_ENDPOINTS) return;
  755. endpoint_t *endpoint = endpoint_queue_head + endpoint_number * 2;
  756. uint32_t mask = 1 << endpoint_number;
  757. schedule_transfer(endpoint, mask, transfer);
  758. }
  759. uint32_t usb_transfer_status(const transfer_t *transfer)
  760. {
  761. #if 0
  762. uint32_t status, cmd;
  763. //int count=0;
  764. cmd = USB1_USBCMD;
  765. while (1) {
  766. __disable_irq();
  767. USB1_USBCMD = cmd | USB_USBCMD_ATDTW;
  768. status = transfer->status;
  769. cmd = USB1_USBCMD;
  770. __enable_irq();
  771. if (cmd & USB_USBCMD_ATDTW) return status;
  772. //if (!(cmd & USB_USBCMD_ATDTW)) continue;
  773. //if (status & 0x80) break; // for still active, only 1 reading needed
  774. //if (++count > 1) break; // for completed, check 10 times
  775. }
  776. #else
  777. return transfer->status;
  778. #endif
  779. }