Teensy 4.1 core updated for C++20
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938
  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. endpoint0_receive(NULL, 0, 1); // handle these after ACK
  522. return;
  523. case 0x81A1: // GET FEATURE
  524. case 0x82A1:
  525. case 0x83A1:
  526. case 0x84A1:
  527. if (setup.wLength <= sizeof(endpoint0_buffer)) {
  528. /*if (usb_audio_get_feature(&setup, endpoint0_buffer, setup.wLength)) {
  529. endpoint0_transmit(endpoint0_buffer, setup.wLength, 0);
  530. return;
  531. }*/
  532. }
  533. break;
  534. case 0x81A2: // GET_CUR (wValue=0, wIndex=interface, wLength=len)
  535. if (setup.wLength >= 3) {
  536. endpoint0_buffer[0] = 44100 & 255;
  537. endpoint0_buffer[1] = 44100 >> 8;
  538. endpoint0_buffer[2] = 0;
  539. endpoint0_transmit(endpoint0_buffer, 3, 0);
  540. return;
  541. }
  542. break;
  543. #endif
  544. }
  545. USB1_ENDPTCTRL0 = 0x000010001; // stall
  546. }
  547. static void endpoint0_transmit(const void *data, uint32_t len, int notify)
  548. {
  549. //printf("tx %lu\n", len);
  550. if (len > 0) {
  551. // Executing A Transfer Descriptor, page 3182
  552. endpoint0_transfer_data.next = 1;
  553. endpoint0_transfer_data.status = (len << 16) | (1<<7);
  554. uint32_t addr = (uint32_t)data;
  555. endpoint0_transfer_data.pointer0 = addr; // format: table 55-60, pg 3159
  556. endpoint0_transfer_data.pointer1 = addr + 4096;
  557. endpoint0_transfer_data.pointer2 = addr + 8192;
  558. endpoint0_transfer_data.pointer3 = addr + 12288;
  559. endpoint0_transfer_data.pointer4 = addr + 16384;
  560. // Case 1: Link list is empty, page 3182
  561. endpoint_queue_head[1].next = (uint32_t)&endpoint0_transfer_data;
  562. endpoint_queue_head[1].status = 0;
  563. USB1_ENDPTPRIME |= (1<<16);
  564. while (USB1_ENDPTPRIME) ;
  565. }
  566. endpoint0_transfer_ack.next = 1;
  567. endpoint0_transfer_ack.status = (1<<7) | (notify ? (1 << 15) : 0);
  568. endpoint0_transfer_ack.pointer0 = 0;
  569. endpoint_queue_head[0].next = (uint32_t)&endpoint0_transfer_ack;
  570. endpoint_queue_head[0].status = 0;
  571. USB1_ENDPTCOMPLETE |= (1<<0) | (1<<16);
  572. USB1_ENDPTPRIME |= (1<<0);
  573. endpoint0_notify_mask = (notify ? (1 << 0) : 0);
  574. while (USB1_ENDPTPRIME) ;
  575. }
  576. static void endpoint0_receive(void *data, uint32_t len, int notify)
  577. {
  578. //printf("rx %lu\n", len);
  579. if (len > 0) {
  580. // Executing A Transfer Descriptor, page 3182
  581. endpoint0_transfer_data.next = 1;
  582. endpoint0_transfer_data.status = (len << 16) | (1<<7);
  583. uint32_t addr = (uint32_t)data;
  584. endpoint0_transfer_data.pointer0 = addr; // format: table 55-60, pg 3159
  585. endpoint0_transfer_data.pointer1 = addr + 4096;
  586. endpoint0_transfer_data.pointer2 = addr + 8192;
  587. endpoint0_transfer_data.pointer3 = addr + 12288;
  588. endpoint0_transfer_data.pointer4 = addr + 16384;
  589. // Case 1: Link list is empty, page 3182
  590. endpoint_queue_head[0].next = (uint32_t)&endpoint0_transfer_data;
  591. endpoint_queue_head[0].status = 0;
  592. USB1_ENDPTPRIME |= (1<<0);
  593. while (USB1_ENDPTPRIME) ;
  594. }
  595. endpoint0_transfer_ack.next = 1;
  596. endpoint0_transfer_ack.status = (1<<7) | (notify ? (1 << 15) : 0);
  597. endpoint0_transfer_ack.pointer0 = 0;
  598. endpoint_queue_head[1].next = (uint32_t)&endpoint0_transfer_ack;
  599. endpoint_queue_head[1].status = 0;
  600. USB1_ENDPTCOMPLETE |= (1<<0) | (1<<16);
  601. USB1_ENDPTPRIME |= (1<<16);
  602. endpoint0_notify_mask = (notify ? (1 << 16) : 0);
  603. while (USB1_ENDPTPRIME) ;
  604. }
  605. /*typedef union {
  606. struct {
  607. union {
  608. struct {
  609. uint8_t bmRequestType;
  610. uint8_t bRequest;
  611. };
  612. uint16_t wRequestAndType;
  613. };
  614. uint16_t wValue;
  615. uint16_t wIndex;
  616. uint16_t wLength;
  617. };
  618. struct {
  619. uint32_t word1;
  620. uint32_t word2;
  621. };
  622. uint64_t bothwords;
  623. } setup_t; */
  624. static void endpoint0_complete(void)
  625. {
  626. setup_t setup;
  627. setup.bothwords = endpoint0_setupdata.bothwords;
  628. //printf("complete %x %x %x\n", setup.word1, setup.word2, endpoint0_buffer[0]);
  629. #ifdef CDC_STATUS_INTERFACE
  630. if (setup.wRequestAndType == 0x2021 /*CDC_SET_LINE_CODING*/) {
  631. memcpy(usb_cdc_line_coding, endpoint0_buffer, 7);
  632. printf("usb_cdc_line_coding, baud=%u\n", usb_cdc_line_coding[0]);
  633. if (usb_cdc_line_coding[0] == 134) {
  634. usb_start_sof_interrupts(NUM_INTERFACE);
  635. usb_reboot_timer = 80; // TODO: 10 if only 12 Mbit/sec
  636. }
  637. }
  638. #endif
  639. #ifdef KEYBOARD_INTERFACE
  640. if (setup.word1 == 0x02000921 && setup.word2 == ((1 << 16) | KEYBOARD_INTERFACE)) {
  641. keyboard_leds = endpoint0_buffer[0];
  642. endpoint0_transmit(NULL, 0, 0);
  643. }
  644. #endif
  645. #ifdef SEREMU_INTERFACE
  646. if (setup.word1 == 0x03000921 && setup.word2 == ((4<<16)|SEREMU_INTERFACE)
  647. && endpoint0_buffer[0] == 0xA9 && endpoint0_buffer[1] == 0x45
  648. && endpoint0_buffer[2] == 0xC2 && endpoint0_buffer[3] == 0x6B) {
  649. printf("seremu reboot request\n");
  650. usb_start_sof_interrupts(NUM_INTERFACE);
  651. usb_reboot_timer = 80; // TODO: 10 if only 12 Mbit/sec
  652. }
  653. #endif
  654. #ifdef AUDIO_INTERFACE
  655. // TODO: usb_audio_set_feature()
  656. #endif
  657. }
  658. static void usb_endpoint_config(endpoint_t *qh, uint32_t config, void (*callback)(transfer_t *))
  659. {
  660. memset(qh, 0, sizeof(endpoint_t));
  661. qh->config = config;
  662. qh->next = 1; // Terminate bit = 1
  663. qh->callback_function = callback;
  664. }
  665. void usb_config_rx(uint32_t ep, uint32_t packet_size, int do_zlp, void (*cb)(transfer_t *))
  666. {
  667. uint32_t config = (packet_size << 16) | (do_zlp ? 0 : (1 << 29));
  668. if (ep < 2 || ep > NUM_ENDPOINTS) return;
  669. usb_endpoint_config(endpoint_queue_head + ep * 2, config, cb);
  670. if (cb) endpointN_notify_mask |= (1 << ep);
  671. }
  672. void usb_config_tx(uint32_t ep, uint32_t packet_size, int do_zlp, void (*cb)(transfer_t *))
  673. {
  674. uint32_t config = (packet_size << 16) | (do_zlp ? 0 : (1 << 29));
  675. if (ep < 2 || ep > NUM_ENDPOINTS) return;
  676. usb_endpoint_config(endpoint_queue_head + ep * 2 + 1, config, cb);
  677. if (cb) endpointN_notify_mask |= (1 << (ep + 16));
  678. }
  679. void usb_config_rx_iso(uint32_t ep, uint32_t packet_size, int mult, void (*cb)(transfer_t *))
  680. {
  681. if (mult < 1 || mult > 3) return;
  682. uint32_t config = (packet_size << 16) | (mult << 30);
  683. if (ep < 2 || ep > NUM_ENDPOINTS) return;
  684. usb_endpoint_config(endpoint_queue_head + ep * 2, config, cb);
  685. if (cb) endpointN_notify_mask |= (1 << ep);
  686. }
  687. void usb_config_tx_iso(uint32_t ep, uint32_t packet_size, int mult, void (*cb)(transfer_t *))
  688. {
  689. if (mult < 1 || mult > 3) return;
  690. uint32_t config = (packet_size << 16) | (mult << 30);
  691. if (ep < 2 || ep > NUM_ENDPOINTS) return;
  692. usb_endpoint_config(endpoint_queue_head + ep * 2 + 1, config, cb);
  693. if (cb) endpointN_notify_mask |= (1 << (ep + 16));
  694. }
  695. void usb_prepare_transfer(transfer_t *transfer, const void *data, uint32_t len, uint32_t param)
  696. {
  697. transfer->next = 1;
  698. transfer->status = (len << 16) | (1<<7);
  699. uint32_t addr = (uint32_t)data;
  700. transfer->pointer0 = addr;
  701. transfer->pointer1 = addr + 4096;
  702. transfer->pointer2 = addr + 8192;
  703. transfer->pointer3 = addr + 12288;
  704. transfer->pointer4 = addr + 16384;
  705. transfer->callback_param = param;
  706. }
  707. #if 0
  708. void usb_print_transfer_log(void)
  709. {
  710. uint32_t i, count;
  711. printf("log %d transfers\n", transfer_log_count);
  712. count = transfer_log_count;
  713. if (count > LOG_SIZE) count = LOG_SIZE;
  714. for (i=0; i < count; i++) {
  715. if (transfer_log_head == 0) transfer_log_head = LOG_SIZE;
  716. transfer_log_head--;
  717. uint32_t log = transfer_log[transfer_log_head];
  718. printf(" %c %X\n", log >> 8, (int)(log & 255));
  719. }
  720. }
  721. #endif
  722. static void schedule_transfer(endpoint_t *endpoint, uint32_t epmask, transfer_t *transfer)
  723. {
  724. // when we stop at 6, why is the last transfer missing from the USB output?
  725. //if (transfer_log_count >= 6) return;
  726. //uint32_t ret = (*(const uint8_t *)transfer->pointer0) << 8;
  727. if (endpoint->callback_function) {
  728. transfer->status |= (1<<15);
  729. }
  730. __disable_irq();
  731. //digitalWriteFast(1, HIGH);
  732. // Executing A Transfer Descriptor, page 2468 (RT1060 manual, Rev 1, 12/2018)
  733. transfer_t *last = endpoint->last_transfer;
  734. if (last) {
  735. last->next = (uint32_t)transfer;
  736. if (USB1_ENDPTPRIME & epmask) goto end;
  737. //digitalWriteFast(2, HIGH);
  738. //ret |= 0x01;
  739. uint32_t status;
  740. do {
  741. USB1_USBCMD |= USB_USBCMD_ATDTW;
  742. status = USB1_ENDPTSTATUS;
  743. } while (!(USB1_USBCMD & USB_USBCMD_ATDTW));
  744. //USB1_USBCMD &= ~USB_USBCMD_ATDTW;
  745. if (status & epmask) goto end;
  746. //ret |= 0x02;
  747. }
  748. //digitalWriteFast(4, HIGH);
  749. endpoint->next = (uint32_t)transfer;
  750. endpoint->status = 0;
  751. USB1_ENDPTPRIME |= epmask;
  752. endpoint->first_transfer = transfer;
  753. end:
  754. endpoint->last_transfer = transfer;
  755. __enable_irq();
  756. //digitalWriteFast(4, LOW);
  757. //digitalWriteFast(3, LOW);
  758. //digitalWriteFast(2, LOW);
  759. //digitalWriteFast(1, LOW);
  760. //if (transfer_log_head > LOG_SIZE) transfer_log_head = 0;
  761. //transfer_log[transfer_log_head++] = ret;
  762. //transfer_log_count++;
  763. }
  764. // ENDPTPRIME - Software should write a one to the corresponding bit when
  765. // posting a new transfer descriptor to an endpoint queue head.
  766. // Hardware automatically uses this bit to begin parsing for a
  767. // new transfer descriptor from the queue head and prepare a
  768. // transmit buffer. Hardware clears this bit when the associated
  769. // endpoint(s) is (are) successfully primed.
  770. // Momentarily set by hardware during hardware re-priming
  771. // operations when a dTD is retired, and the dQH is updated.
  772. // ENDPTSTATUS - Transmit Buffer Ready - set to one by the hardware as a
  773. // response to receiving a command from a corresponding bit
  774. // in the ENDPTPRIME register. . Buffer ready is cleared by
  775. // USB reset, by the USB DMA system, or through the ENDPTFLUSH
  776. // register. (so 0=buffer ready, 1=buffer primed for transmit)
  777. // USBCMD.ATDTW - This bit is used as a semaphore to ensure proper addition
  778. // of a new dTD to an active (primed) endpoint's linked list.
  779. // This bit is set and cleared by software.
  780. // This bit would also be cleared by hardware when state machine
  781. // is hazard region for which adding a dTD to a primed endpoint
  782. // may go unrecognized.
  783. /*struct endpoint_struct {
  784. uint32_t config;
  785. uint32_t current;
  786. uint32_t next;
  787. uint32_t status;
  788. uint32_t pointer0;
  789. uint32_t pointer1;
  790. uint32_t pointer2;
  791. uint32_t pointer3;
  792. uint32_t pointer4;
  793. uint32_t reserved;
  794. uint32_t setup0;
  795. uint32_t setup1;
  796. transfer_t *first_transfer;
  797. transfer_t *last_transfer;
  798. void (*callback_function)(transfer_t *completed_transfer);
  799. uint32_t unused1;
  800. };*/
  801. static void run_callbacks(endpoint_t *ep)
  802. {
  803. //printf("run_callbacks\n");
  804. transfer_t *first = ep->first_transfer;
  805. if (first == NULL) return;
  806. // count how many transfers are completed, then remove them from the endpoint's list
  807. uint32_t count = 0;
  808. transfer_t *t = first;
  809. while (1) {
  810. if (t->status & (1<<7)) {
  811. // found a still-active transfer, new list begins here
  812. //printf(" still active\n");
  813. ep->first_transfer = t;
  814. break;
  815. }
  816. count++;
  817. t = (transfer_t *)t->next;
  818. if ((uint32_t)t == 1) {
  819. // reached end of list, all need callbacks, new list is empty
  820. //printf(" end of list\n");
  821. ep->first_transfer = NULL;
  822. ep->last_transfer = NULL;
  823. break;
  824. }
  825. }
  826. // do all the callbacks
  827. while (count) {
  828. transfer_t *next = (transfer_t *)first->next;
  829. ep->callback_function(first);
  830. first = next;
  831. count--;
  832. }
  833. }
  834. void usb_transmit(int endpoint_number, transfer_t *transfer)
  835. {
  836. if (endpoint_number < 2 || endpoint_number > NUM_ENDPOINTS) return;
  837. endpoint_t *endpoint = endpoint_queue_head + endpoint_number * 2 + 1;
  838. uint32_t mask = 1 << (endpoint_number + 16);
  839. schedule_transfer(endpoint, mask, transfer);
  840. }
  841. void usb_receive(int endpoint_number, transfer_t *transfer)
  842. {
  843. if (endpoint_number < 2 || endpoint_number > NUM_ENDPOINTS) return;
  844. endpoint_t *endpoint = endpoint_queue_head + endpoint_number * 2;
  845. uint32_t mask = 1 << endpoint_number;
  846. schedule_transfer(endpoint, mask, transfer);
  847. }
  848. uint32_t usb_transfer_status(const transfer_t *transfer)
  849. {
  850. #if 0
  851. uint32_t status, cmd;
  852. //int count=0;
  853. cmd = USB1_USBCMD;
  854. while (1) {
  855. __disable_irq();
  856. USB1_USBCMD = cmd | USB_USBCMD_ATDTW;
  857. status = transfer->status;
  858. cmd = USB1_USBCMD;
  859. __enable_irq();
  860. if (cmd & USB_USBCMD_ATDTW) return status;
  861. //if (!(cmd & USB_USBCMD_ATDTW)) continue;
  862. //if (status & 0x80) break; // for still active, only 1 reading needed
  863. //if (++count > 1) break; // for completed, check 10 times
  864. }
  865. #else
  866. return transfer->status;
  867. #endif
  868. }