Teensy 4.1 core updated for C++20
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

745 lines
22KB

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