Teensy 4.1 core updated for C++20
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.

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