Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990
  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(CDC2_STATUS_INTERFACE) && defined(CDC2_DATA_INTERFACE)
  364. usb_serial2_configure();
  365. #endif
  366. #if defined(CDC3_STATUS_INTERFACE) && defined(CDC3_DATA_INTERFACE)
  367. usb_serial3_configure();
  368. #endif
  369. #if defined(RAWHID_INTERFACE)
  370. usb_rawhid_configure();
  371. #endif
  372. #if defined(KEYBOARD_INTERFACE)
  373. usb_keyboard_configure();
  374. #endif
  375. #if defined(MOUSE_INTERFACE)
  376. usb_mouse_configure();
  377. #endif
  378. #if defined(FLIGHTSIM_INTERFACE)
  379. usb_flightsim_configure();
  380. #endif
  381. #if defined(JOYSTICK_INTERFACE)
  382. usb_joystick_configure();
  383. #endif
  384. #if defined(MULTITOUCH_INTERFACE)
  385. usb_touchscreen_configure();
  386. #endif
  387. #if defined(MIDI_INTERFACE)
  388. usb_midi_configure();
  389. #endif
  390. #if defined(AUDIO_INTERFACE)
  391. usb_audio_configure();
  392. #endif
  393. endpoint0_receive(NULL, 0, 0);
  394. return;
  395. case 0x0880: // GET_CONFIGURATION
  396. reply_buffer[0] = usb_configuration;
  397. endpoint0_transmit(reply_buffer, 1, 0);
  398. return;
  399. case 0x0080: // GET_STATUS (device)
  400. reply_buffer[0] = 0;
  401. reply_buffer[1] = 0;
  402. endpoint0_transmit(reply_buffer, 2, 0);
  403. return;
  404. case 0x0082: // GET_STATUS (endpoint)
  405. endpoint = setup.wIndex & 0x7F;
  406. if (endpoint > 7) break;
  407. dir = setup.wIndex & 0x80;
  408. ctrl = *((uint32_t *)&USB1_ENDPTCTRL0 + endpoint);
  409. reply_buffer[0] = 0;
  410. reply_buffer[1] = 0;
  411. if ((dir && (ctrl & USB_ENDPTCTRL_TXS)) || (!dir && (ctrl & USB_ENDPTCTRL_RXS))) {
  412. reply_buffer[0] = 1;
  413. }
  414. endpoint0_transmit(reply_buffer, 2, 0);
  415. return;
  416. case 0x0302: // SET_FEATURE (endpoint)
  417. endpoint = setup.wIndex & 0x7F;
  418. if (endpoint > 7) break;
  419. dir = setup.wIndex & 0x80;
  420. if (dir) {
  421. *((volatile uint32_t *)&USB1_ENDPTCTRL0 + endpoint) |= USB_ENDPTCTRL_TXS;
  422. } else {
  423. *((volatile uint32_t *)&USB1_ENDPTCTRL0 + endpoint) |= USB_ENDPTCTRL_RXS;
  424. }
  425. endpoint0_receive(NULL, 0, 0);
  426. return;
  427. case 0x0102: // CLEAR_FEATURE (endpoint)
  428. endpoint = setup.wIndex & 0x7F;
  429. if (endpoint > 7) break;
  430. dir = setup.wIndex & 0x80;
  431. if (dir) {
  432. *((volatile uint32_t *)&USB1_ENDPTCTRL0 + endpoint) &= ~USB_ENDPTCTRL_TXS;
  433. } else {
  434. *((volatile uint32_t *)&USB1_ENDPTCTRL0 + endpoint) &= ~USB_ENDPTCTRL_RXS;
  435. }
  436. endpoint0_receive(NULL, 0, 0);
  437. return;
  438. case 0x0680: // GET_DESCRIPTOR
  439. case 0x0681:
  440. for (list = usb_descriptor_list; list->addr != NULL; list++) {
  441. if (setup.wValue == list->wValue && setup.wIndex == list->wIndex) {
  442. uint32_t datalen;
  443. if ((setup.wValue >> 8) == 3) {
  444. // for string descriptors, use the descriptor's
  445. // length field, allowing runtime configured length.
  446. datalen = *(list->addr);
  447. } else {
  448. datalen = list->length;
  449. }
  450. if (datalen > setup.wLength) datalen = setup.wLength;
  451. // copy the descriptor, from PROGMEM to DMAMEM
  452. if (setup.wValue == 0x200) {
  453. // config descriptor needs to adapt to speed
  454. const uint8_t *src = usb_config_descriptor_12;
  455. if (usb_high_speed) src = usb_config_descriptor_480;
  456. memcpy(usb_descriptor_buffer, src, datalen);
  457. } else if (setup.wValue == 0x700) {
  458. // other speed config also needs to adapt
  459. const uint8_t *src = usb_config_descriptor_480;
  460. if (usb_high_speed) src = usb_config_descriptor_12;
  461. memcpy(usb_descriptor_buffer, src, datalen);
  462. usb_descriptor_buffer[1] = 7;
  463. } else {
  464. memcpy(usb_descriptor_buffer, list->addr, datalen);
  465. }
  466. // prep transmit
  467. arm_dcache_flush_delete(usb_descriptor_buffer, datalen);
  468. endpoint0_transmit(usb_descriptor_buffer, datalen, 0);
  469. return;
  470. }
  471. }
  472. break;
  473. #if defined(CDC_STATUS_INTERFACE)
  474. case 0x2221: // CDC_SET_CONTROL_LINE_STATE
  475. #ifdef CDC_STATUS_INTERFACE
  476. if (setup.wIndex == CDC_STATUS_INTERFACE) {
  477. usb_cdc_line_rtsdtr_millis = systick_millis_count;
  478. usb_cdc_line_rtsdtr = setup.wValue;
  479. }
  480. #endif
  481. #ifdef CDC2_STATUS_INTERFACE
  482. if (setup.wIndex == CDC2_STATUS_INTERFACE) {
  483. usb_cdc2_line_rtsdtr_millis = systick_millis_count;
  484. usb_cdc2_line_rtsdtr = setup.wValue;
  485. }
  486. #endif
  487. #ifdef CDC3_STATUS_INTERFACE
  488. if (setup.wIndex == CDC3_STATUS_INTERFACE) {
  489. usb_cdc3_line_rtsdtr_millis = systick_millis_count;
  490. usb_cdc3_line_rtsdtr = setup.wValue;
  491. }
  492. #endif
  493. case 0x2321: // CDC_SEND_BREAK
  494. endpoint0_receive(NULL, 0, 0);
  495. return;
  496. case 0x2021: // CDC_SET_LINE_CODING
  497. if (setup.wLength != 7) break;
  498. endpoint0_setupdata.bothwords = setupdata;
  499. endpoint0_receive(endpoint0_buffer, 7, 1);
  500. return;
  501. #endif
  502. #if defined(SEREMU_INTERFACE) || defined(KEYBOARD_INTERFACE)
  503. case 0x0921: // HID SET_REPORT
  504. if (setup.wLength <= sizeof(endpoint0_buffer)) {
  505. //printf("hid set report %x %x\n", setup.word1, setup.word2);
  506. endpoint0_setupdata.bothwords = setup.bothwords;
  507. endpoint0_buffer[0] = 0xE9;
  508. endpoint0_receive(endpoint0_buffer, setup.wLength, 1);
  509. return;
  510. }
  511. break;
  512. #endif
  513. #if defined(AUDIO_INTERFACE)
  514. case 0x0B01: // SET_INTERFACE (alternate setting)
  515. if (setup.wIndex == AUDIO_INTERFACE+1) {
  516. usb_audio_transmit_setting = setup.wValue;
  517. if (usb_audio_transmit_setting > 0) {
  518. // TODO: set up AUDIO_TX_ENDPOINT to transmit
  519. }
  520. endpoint0_receive(NULL, 0, 0);
  521. return;
  522. } else if (setup.wIndex == AUDIO_INTERFACE+2) {
  523. usb_audio_receive_setting = setup.wValue;
  524. endpoint0_receive(NULL, 0, 0);
  525. return;
  526. }
  527. break;
  528. case 0x0A81: // GET_INTERFACE (alternate setting)
  529. if (setup.wIndex == AUDIO_INTERFACE+1) {
  530. endpoint0_buffer[0] = usb_audio_transmit_setting;
  531. endpoint0_transmit(endpoint0_buffer, 1, 0);
  532. return;
  533. } else if (setup.wIndex == AUDIO_INTERFACE+2) {
  534. endpoint0_buffer[0] = usb_audio_receive_setting;
  535. endpoint0_transmit(endpoint0_buffer, 1, 0);
  536. return;
  537. }
  538. break;
  539. case 0x0121: // SET FEATURE
  540. case 0x0221:
  541. case 0x0321:
  542. case 0x0421:
  543. //printf("set_feature, word1=%x, len=%d\n", setup.word1, setup.wLength);
  544. if (setup.wLength <= sizeof(endpoint0_buffer)) {
  545. endpoint0_setupdata.bothwords = setupdata;
  546. endpoint0_receive(endpoint0_buffer, setup.wLength, 1);
  547. return; // handle these after ACK
  548. }
  549. break;
  550. case 0x81A1: // GET FEATURE
  551. case 0x82A1:
  552. case 0x83A1:
  553. case 0x84A1:
  554. if (setup.wLength <= sizeof(endpoint0_buffer)) {
  555. uint32_t len;
  556. if (usb_audio_get_feature(&setup, endpoint0_buffer, &len)) {
  557. //printf("GET feature, len=%d\n", len);
  558. endpoint0_transmit(endpoint0_buffer, len, 0);
  559. return;
  560. }
  561. }
  562. break;
  563. case 0x81A2: // GET_CUR (wValue=0, wIndex=interface, wLength=len)
  564. if (setup.wLength >= 3) {
  565. endpoint0_buffer[0] = 44100 & 255;
  566. endpoint0_buffer[1] = 44100 >> 8;
  567. endpoint0_buffer[2] = 0;
  568. endpoint0_transmit(endpoint0_buffer, 3, 0);
  569. return;
  570. }
  571. break;
  572. #endif
  573. }
  574. USB1_ENDPTCTRL0 = 0x000010001; // stall
  575. }
  576. static void endpoint0_transmit(const void *data, uint32_t len, int notify)
  577. {
  578. //printf("tx %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[1].next = (uint32_t)&endpoint0_transfer_data;
  591. endpoint_queue_head[1].status = 0;
  592. USB1_ENDPTPRIME |= (1<<16);
  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[0].next = (uint32_t)&endpoint0_transfer_ack;
  599. endpoint_queue_head[0].status = 0;
  600. USB1_ENDPTCOMPLETE = (1<<0) | (1<<16);
  601. USB1_ENDPTPRIME |= (1<<0);
  602. endpoint0_notify_mask = (notify ? (1 << 0) : 0);
  603. while (USB1_ENDPTPRIME) ;
  604. }
  605. static void endpoint0_receive(void *data, uint32_t len, int notify)
  606. {
  607. //printf("rx %lu\n", len);
  608. if (len > 0) {
  609. // Executing A Transfer Descriptor, page 3182
  610. endpoint0_transfer_data.next = 1;
  611. endpoint0_transfer_data.status = (len << 16) | (1<<7);
  612. uint32_t addr = (uint32_t)data;
  613. endpoint0_transfer_data.pointer0 = addr; // format: table 55-60, pg 3159
  614. endpoint0_transfer_data.pointer1 = addr + 4096;
  615. endpoint0_transfer_data.pointer2 = addr + 8192;
  616. endpoint0_transfer_data.pointer3 = addr + 12288;
  617. endpoint0_transfer_data.pointer4 = addr + 16384;
  618. // Case 1: Link list is empty, page 3182
  619. endpoint_queue_head[0].next = (uint32_t)&endpoint0_transfer_data;
  620. endpoint_queue_head[0].status = 0;
  621. USB1_ENDPTPRIME |= (1<<0);
  622. while (USB1_ENDPTPRIME) ;
  623. }
  624. endpoint0_transfer_ack.next = 1;
  625. endpoint0_transfer_ack.status = (1<<7) | (notify ? (1 << 15) : 0);
  626. endpoint0_transfer_ack.pointer0 = 0;
  627. endpoint_queue_head[1].next = (uint32_t)&endpoint0_transfer_ack;
  628. endpoint_queue_head[1].status = 0;
  629. USB1_ENDPTCOMPLETE = (1<<0) | (1<<16);
  630. USB1_ENDPTPRIME |= (1<<16);
  631. endpoint0_notify_mask = (notify ? (1 << 16) : 0);
  632. while (USB1_ENDPTPRIME) ;
  633. }
  634. /*typedef union {
  635. struct {
  636. union {
  637. struct {
  638. uint8_t bmRequestType;
  639. uint8_t bRequest;
  640. };
  641. uint16_t wRequestAndType;
  642. };
  643. uint16_t wValue;
  644. uint16_t wIndex;
  645. uint16_t wLength;
  646. };
  647. struct {
  648. uint32_t word1;
  649. uint32_t word2;
  650. };
  651. uint64_t bothwords;
  652. } setup_t; */
  653. static void endpoint0_complete(void)
  654. {
  655. setup_t setup;
  656. setup.bothwords = endpoint0_setupdata.bothwords;
  657. //printf("complete %x %x %x\n", setup.word1, setup.word2, endpoint0_buffer[0]);
  658. #ifdef CDC_STATUS_INTERFACE
  659. // 0x2021 is CDC_SET_LINE_CODING
  660. if (setup.wRequestAndType == 0x2021 && setup.wIndex == CDC_STATUS_INTERFACE) {
  661. memcpy(usb_cdc_line_coding, endpoint0_buffer, 7);
  662. printf("usb_cdc_line_coding, baud=%u\n", usb_cdc_line_coding[0]);
  663. if (usb_cdc_line_coding[0] == 134) {
  664. usb_start_sof_interrupts(NUM_INTERFACE);
  665. usb_reboot_timer = 80; // TODO: 10 if only 12 Mbit/sec
  666. }
  667. }
  668. #endif
  669. #ifdef CDC2_STATUS_INTERFACE
  670. if (setup.wRequestAndType == 0x2021 && setup.wIndex == CDC2_STATUS_INTERFACE) {
  671. memcpy(usb_cdc2_line_coding, endpoint0_buffer, 7);
  672. printf("usb_cdc2_line_coding, baud=%u\n", usb_cdc2_line_coding[0]);
  673. if (usb_cdc2_line_coding[0] == 134) {
  674. usb_start_sof_interrupts(NUM_INTERFACE);
  675. usb_reboot_timer = 80; // TODO: 10 if only 12 Mbit/sec
  676. }
  677. }
  678. #endif
  679. #ifdef CDC3_STATUS_INTERFACE
  680. if (setup.wRequestAndType == 0x2021 && setup.wIndex == CDC3_STATUS_INTERFACE) {
  681. memcpy(usb_cdc3_line_coding, endpoint0_buffer, 7);
  682. printf("usb_cdc3_line_coding, baud=%u\n", usb_cdc3_line_coding[0]);
  683. if (usb_cdc3_line_coding[0] == 134) {
  684. usb_start_sof_interrupts(NUM_INTERFACE);
  685. usb_reboot_timer = 80; // TODO: 10 if only 12 Mbit/sec
  686. }
  687. }
  688. #endif
  689. #ifdef KEYBOARD_INTERFACE
  690. if (setup.word1 == 0x02000921 && setup.word2 == ((1 << 16) | KEYBOARD_INTERFACE)) {
  691. keyboard_leds = endpoint0_buffer[0];
  692. endpoint0_transmit(NULL, 0, 0);
  693. }
  694. #endif
  695. #ifdef SEREMU_INTERFACE
  696. if (setup.word1 == 0x03000921 && setup.word2 == ((4<<16)|SEREMU_INTERFACE)
  697. && endpoint0_buffer[0] == 0xA9 && endpoint0_buffer[1] == 0x45
  698. && endpoint0_buffer[2] == 0xC2 && endpoint0_buffer[3] == 0x6B) {
  699. printf("seremu reboot request\n");
  700. usb_start_sof_interrupts(NUM_INTERFACE);
  701. usb_reboot_timer = 80; // TODO: 10 if only 12 Mbit/sec
  702. }
  703. #endif
  704. #ifdef AUDIO_INTERFACE
  705. if (setup.word1 == 0x02010121 /* TODO: check setup.word2 */) {
  706. usb_audio_set_feature(&endpoint0_setupdata, endpoint0_buffer);
  707. }
  708. #endif
  709. }
  710. static void usb_endpoint_config(endpoint_t *qh, uint32_t config, void (*callback)(transfer_t *))
  711. {
  712. memset(qh, 0, sizeof(endpoint_t));
  713. qh->config = config;
  714. qh->next = 1; // Terminate bit = 1
  715. qh->callback_function = callback;
  716. }
  717. void usb_config_rx(uint32_t ep, uint32_t packet_size, int do_zlp, void (*cb)(transfer_t *))
  718. {
  719. uint32_t config = (packet_size << 16) | (do_zlp ? 0 : (1 << 29));
  720. if (ep < 2 || ep > NUM_ENDPOINTS) return;
  721. usb_endpoint_config(endpoint_queue_head + ep * 2, config, cb);
  722. if (cb) endpointN_notify_mask |= (1 << ep);
  723. }
  724. void usb_config_tx(uint32_t ep, uint32_t packet_size, int do_zlp, void (*cb)(transfer_t *))
  725. {
  726. uint32_t config = (packet_size << 16) | (do_zlp ? 0 : (1 << 29));
  727. if (ep < 2 || ep > NUM_ENDPOINTS) return;
  728. usb_endpoint_config(endpoint_queue_head + ep * 2 + 1, config, cb);
  729. if (cb) endpointN_notify_mask |= (1 << (ep + 16));
  730. }
  731. void usb_config_rx_iso(uint32_t ep, uint32_t packet_size, int mult, void (*cb)(transfer_t *))
  732. {
  733. if (mult < 1 || mult > 3) return;
  734. uint32_t config = (packet_size << 16) | (mult << 30);
  735. if (ep < 2 || ep > NUM_ENDPOINTS) return;
  736. usb_endpoint_config(endpoint_queue_head + ep * 2, config, cb);
  737. if (cb) endpointN_notify_mask |= (1 << ep);
  738. }
  739. void usb_config_tx_iso(uint32_t ep, uint32_t packet_size, int mult, void (*cb)(transfer_t *))
  740. {
  741. if (mult < 1 || mult > 3) return;
  742. uint32_t config = (packet_size << 16) | (mult << 30);
  743. if (ep < 2 || ep > NUM_ENDPOINTS) return;
  744. usb_endpoint_config(endpoint_queue_head + ep * 2 + 1, config, cb);
  745. if (cb) endpointN_notify_mask |= (1 << (ep + 16));
  746. }
  747. void usb_prepare_transfer(transfer_t *transfer, const void *data, uint32_t len, uint32_t param)
  748. {
  749. transfer->next = 1;
  750. transfer->status = (len << 16) | (1<<7);
  751. uint32_t addr = (uint32_t)data;
  752. transfer->pointer0 = addr;
  753. transfer->pointer1 = addr + 4096;
  754. transfer->pointer2 = addr + 8192;
  755. transfer->pointer3 = addr + 12288;
  756. transfer->pointer4 = addr + 16384;
  757. transfer->callback_param = param;
  758. }
  759. #if 0
  760. void usb_print_transfer_log(void)
  761. {
  762. uint32_t i, count;
  763. printf("log %d transfers\n", transfer_log_count);
  764. count = transfer_log_count;
  765. if (count > LOG_SIZE) count = LOG_SIZE;
  766. for (i=0; i < count; i++) {
  767. if (transfer_log_head == 0) transfer_log_head = LOG_SIZE;
  768. transfer_log_head--;
  769. uint32_t log = transfer_log[transfer_log_head];
  770. printf(" %c %X\n", log >> 8, (int)(log & 255));
  771. }
  772. }
  773. #endif
  774. static void schedule_transfer(endpoint_t *endpoint, uint32_t epmask, transfer_t *transfer)
  775. {
  776. // when we stop at 6, why is the last transfer missing from the USB output?
  777. //if (transfer_log_count >= 6) return;
  778. //uint32_t ret = (*(const uint8_t *)transfer->pointer0) << 8;
  779. if (endpoint->callback_function) {
  780. transfer->status |= (1<<15);
  781. }
  782. __disable_irq();
  783. //digitalWriteFast(1, HIGH);
  784. // Executing A Transfer Descriptor, page 2468 (RT1060 manual, Rev 1, 12/2018)
  785. transfer_t *last = endpoint->last_transfer;
  786. if (last) {
  787. last->next = (uint32_t)transfer;
  788. if (USB1_ENDPTPRIME & epmask) goto end;
  789. //digitalWriteFast(2, HIGH);
  790. //ret |= 0x01;
  791. uint32_t status, cyccnt=ARM_DWT_CYCCNT;
  792. do {
  793. USB1_USBCMD |= USB_USBCMD_ATDTW;
  794. status = USB1_ENDPTSTATUS;
  795. } while (!(USB1_USBCMD & USB_USBCMD_ATDTW) && (ARM_DWT_CYCCNT - cyccnt < 2400));
  796. //USB1_USBCMD &= ~USB_USBCMD_ATDTW;
  797. if (status & epmask) goto end;
  798. //ret |= 0x02;
  799. }
  800. //digitalWriteFast(4, HIGH);
  801. endpoint->next = (uint32_t)transfer;
  802. endpoint->status = 0;
  803. USB1_ENDPTPRIME |= epmask;
  804. endpoint->first_transfer = transfer;
  805. end:
  806. endpoint->last_transfer = transfer;
  807. __enable_irq();
  808. //digitalWriteFast(4, LOW);
  809. //digitalWriteFast(3, LOW);
  810. //digitalWriteFast(2, LOW);
  811. //digitalWriteFast(1, LOW);
  812. //if (transfer_log_head > LOG_SIZE) transfer_log_head = 0;
  813. //transfer_log[transfer_log_head++] = ret;
  814. //transfer_log_count++;
  815. }
  816. // ENDPTPRIME - Software should write a one to the corresponding bit when
  817. // posting a new transfer descriptor to an endpoint queue head.
  818. // Hardware automatically uses this bit to begin parsing for a
  819. // new transfer descriptor from the queue head and prepare a
  820. // transmit buffer. Hardware clears this bit when the associated
  821. // endpoint(s) is (are) successfully primed.
  822. // Momentarily set by hardware during hardware re-priming
  823. // operations when a dTD is retired, and the dQH is updated.
  824. // ENDPTSTATUS - Transmit Buffer Ready - set to one by the hardware as a
  825. // response to receiving a command from a corresponding bit
  826. // in the ENDPTPRIME register. . Buffer ready is cleared by
  827. // USB reset, by the USB DMA system, or through the ENDPTFLUSH
  828. // register. (so 0=buffer ready, 1=buffer primed for transmit)
  829. // USBCMD.ATDTW - This bit is used as a semaphore to ensure proper addition
  830. // of a new dTD to an active (primed) endpoint's linked list.
  831. // This bit is set and cleared by software.
  832. // This bit would also be cleared by hardware when state machine
  833. // is hazard region for which adding a dTD to a primed endpoint
  834. // may go unrecognized.
  835. /*struct endpoint_struct {
  836. uint32_t config;
  837. uint32_t current;
  838. uint32_t next;
  839. uint32_t status;
  840. uint32_t pointer0;
  841. uint32_t pointer1;
  842. uint32_t pointer2;
  843. uint32_t pointer3;
  844. uint32_t pointer4;
  845. uint32_t reserved;
  846. uint32_t setup0;
  847. uint32_t setup1;
  848. transfer_t *first_transfer;
  849. transfer_t *last_transfer;
  850. void (*callback_function)(transfer_t *completed_transfer);
  851. uint32_t unused1;
  852. };*/
  853. static void run_callbacks(endpoint_t *ep)
  854. {
  855. //printf("run_callbacks\n");
  856. transfer_t *first = ep->first_transfer;
  857. if (first == NULL) return;
  858. // count how many transfers are completed, then remove them from the endpoint's list
  859. uint32_t count = 0;
  860. transfer_t *t = first;
  861. while (1) {
  862. if (t->status & (1<<7)) {
  863. // found a still-active transfer, new list begins here
  864. //printf(" still active\n");
  865. ep->first_transfer = t;
  866. break;
  867. }
  868. count++;
  869. t = (transfer_t *)t->next;
  870. if ((uint32_t)t == 1) {
  871. // reached end of list, all need callbacks, new list is empty
  872. //printf(" end of list\n");
  873. ep->first_transfer = NULL;
  874. ep->last_transfer = NULL;
  875. break;
  876. }
  877. }
  878. // do all the callbacks
  879. while (count) {
  880. transfer_t *next = (transfer_t *)first->next;
  881. ep->callback_function(first);
  882. first = next;
  883. count--;
  884. }
  885. }
  886. void usb_transmit(int endpoint_number, transfer_t *transfer)
  887. {
  888. if (endpoint_number < 2 || endpoint_number > NUM_ENDPOINTS) return;
  889. endpoint_t *endpoint = endpoint_queue_head + endpoint_number * 2 + 1;
  890. uint32_t mask = 1 << (endpoint_number + 16);
  891. schedule_transfer(endpoint, mask, transfer);
  892. }
  893. void usb_receive(int endpoint_number, transfer_t *transfer)
  894. {
  895. if (endpoint_number < 2 || endpoint_number > NUM_ENDPOINTS) return;
  896. endpoint_t *endpoint = endpoint_queue_head + endpoint_number * 2;
  897. uint32_t mask = 1 << endpoint_number;
  898. schedule_transfer(endpoint, mask, transfer);
  899. }
  900. uint32_t usb_transfer_status(const transfer_t *transfer)
  901. {
  902. #if 0
  903. uint32_t status, cmd;
  904. //int count=0;
  905. cmd = USB1_USBCMD;
  906. while (1) {
  907. __disable_irq();
  908. USB1_USBCMD = cmd | USB_USBCMD_ATDTW;
  909. status = transfer->status;
  910. cmd = USB1_USBCMD;
  911. __enable_irq();
  912. if (cmd & USB_USBCMD_ATDTW) return status;
  913. //if (!(cmd & USB_USBCMD_ATDTW)) continue;
  914. //if (status & 0x80) break; // for still active, only 1 reading needed
  915. //if (++count > 1) break; // for completed, check 10 times
  916. }
  917. #else
  918. return transfer->status;
  919. #endif
  920. }