您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

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