You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

пре 11 година
пре 11 година
пре 10 година
пре 11 година
пре 10 година
пре 11 година
пре 11 година
пре 11 година
пре 11 година
пре 11 година
пре 11 година
пре 9 година
пре 11 година
пре 9 година
пре 11 година
пре 10 година
пре 11 година
пре 11 година
пре 11 година
пре 9 година
пре 11 година
пре 11 година
пре 11 година
пре 11 година
пре 11 година
пре 10 година
пре 11 година
пре 11 година
пре 11 година
пре 11 година
пре 9 година
пре 11 година
пре 11 година
пре 9 година
пре 11 година
пре 11 година
пре 11 година
пре 11 година
пре 11 година
пре 10 година
пре 11 година
пре 9 година
пре 11 година
пре 9 година
пре 11 година
пре 9 година
пре 11 година
пре 11 година
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158
  1. /* Teensyduino Core Library
  2. * http://www.pjrc.com/teensy/
  3. * Copyright (c) 2016 PJRC.COM, LLC.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining
  6. * a copy of this software and associated documentation files (the
  7. * "Software"), to deal in the Software without restriction, including
  8. * without limitation the rights to use, copy, modify, merge, publish,
  9. * distribute, sublicense, and/or sell copies of the Software, and to
  10. * permit persons to whom the Software is furnished to do so, subject to
  11. * the following conditions:
  12. *
  13. * 1. The above copyright notice and this permission notice shall be
  14. * included in all copies or substantial portions of the Software.
  15. *
  16. * 2. If the Software is incorporated into a build system that allows
  17. * selection among a list of target devices, then similar target
  18. * devices manufactured by PJRC.COM must be included in the list of
  19. * target devices and selectable in the same manner.
  20. *
  21. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  25. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  26. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  27. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  28. * SOFTWARE.
  29. *
  30. * Trying to understand this rather complex code?
  31. *
  32. * Kevin Cuzner wrote a simpler version, and a great blog article:
  33. * http://kevincuzner.com/2014/12/12/teensy-3-1-bare-metal-writing-a-usb-driver/
  34. * https://github.com/kcuzner/teensy-oscilloscope/blob/master/scope-teensy/src/usb.c
  35. *
  36. * Andy Payne wrote another relatively simple USB example for Freescale Kinetis
  37. * https://github.com/payne92/bare-metal-arm
  38. */
  39. #include "usb_dev.h"
  40. #if F_CPU >= 20000000 && defined(NUM_ENDPOINTS)
  41. #include "kinetis.h"
  42. //#include "HardwareSerial.h"
  43. #include "usb_mem.h"
  44. #include <string.h> // for memset
  45. // buffer descriptor table
  46. typedef struct {
  47. uint32_t desc;
  48. void * addr;
  49. } bdt_t;
  50. __attribute__ ((section(".usbdescriptortable"), used))
  51. static bdt_t table[(NUM_ENDPOINTS+1)*4];
  52. static usb_packet_t *rx_first[NUM_ENDPOINTS];
  53. static usb_packet_t *rx_last[NUM_ENDPOINTS];
  54. static usb_packet_t *tx_first[NUM_ENDPOINTS];
  55. static usb_packet_t *tx_last[NUM_ENDPOINTS];
  56. uint16_t usb_rx_byte_count_data[NUM_ENDPOINTS];
  57. static uint8_t tx_state[NUM_ENDPOINTS];
  58. #define TX_STATE_BOTH_FREE_EVEN_FIRST 0
  59. #define TX_STATE_BOTH_FREE_ODD_FIRST 1
  60. #define TX_STATE_EVEN_FREE 2
  61. #define TX_STATE_ODD_FREE 3
  62. #define TX_STATE_NONE_FREE_EVEN_FIRST 4
  63. #define TX_STATE_NONE_FREE_ODD_FIRST 5
  64. #define BDT_OWN 0x80
  65. #define BDT_DATA1 0x40
  66. #define BDT_DATA0 0x00
  67. #define BDT_DTS 0x08
  68. #define BDT_STALL 0x04
  69. #define BDT_PID(n) (((n) >> 2) & 15)
  70. #define BDT_DESC(count, data) (BDT_OWN | BDT_DTS \
  71. | ((data) ? BDT_DATA1 : BDT_DATA0) \
  72. | ((count) << 16))
  73. #define TX 1
  74. #define RX 0
  75. #define ODD 1
  76. #define EVEN 0
  77. #define DATA0 0
  78. #define DATA1 1
  79. #define index(endpoint, tx, odd) (((endpoint) << 2) | ((tx) << 1) | (odd))
  80. #define stat2bufferdescriptor(stat) (table + ((stat) >> 2))
  81. static union {
  82. struct {
  83. union {
  84. struct {
  85. uint8_t bmRequestType;
  86. uint8_t bRequest;
  87. };
  88. uint16_t wRequestAndType;
  89. };
  90. uint16_t wValue;
  91. uint16_t wIndex;
  92. uint16_t wLength;
  93. };
  94. struct {
  95. uint32_t word1;
  96. uint32_t word2;
  97. };
  98. } setup;
  99. #define GET_STATUS 0
  100. #define CLEAR_FEATURE 1
  101. #define SET_FEATURE 3
  102. #define SET_ADDRESS 5
  103. #define GET_DESCRIPTOR 6
  104. #define SET_DESCRIPTOR 7
  105. #define GET_CONFIGURATION 8
  106. #define SET_CONFIGURATION 9
  107. #define GET_INTERFACE 10
  108. #define SET_INTERFACE 11
  109. #define SYNCH_FRAME 12
  110. // SETUP always uses a DATA0 PID for the data field of the SETUP transaction.
  111. // transactions in the data phase start with DATA1 and toggle (figure 8-12, USB1.1)
  112. // Status stage uses a DATA1 PID.
  113. static uint8_t ep0_rx0_buf[EP0_SIZE] __attribute__ ((aligned (4)));
  114. static uint8_t ep0_rx1_buf[EP0_SIZE] __attribute__ ((aligned (4)));
  115. static const uint8_t *ep0_tx_ptr = NULL;
  116. static uint16_t ep0_tx_len;
  117. static uint8_t ep0_tx_bdt_bank = 0;
  118. static uint8_t ep0_tx_data_toggle = 0;
  119. uint8_t usb_rx_memory_needed = 0;
  120. volatile uint8_t usb_configuration = 0;
  121. volatile uint8_t usb_reboot_timer = 0;
  122. static void endpoint0_stall(void)
  123. {
  124. USB0_ENDPT0 = USB_ENDPT_EPSTALL | USB_ENDPT_EPRXEN | USB_ENDPT_EPTXEN | USB_ENDPT_EPHSHK;
  125. }
  126. static void endpoint0_transmit(const void *data, uint32_t len)
  127. {
  128. #if 0
  129. serial_print("tx0:");
  130. serial_phex32((uint32_t)data);
  131. serial_print(",");
  132. serial_phex16(len);
  133. serial_print(ep0_tx_bdt_bank ? ", odd" : ", even");
  134. serial_print(ep0_tx_data_toggle ? ", d1\n" : ", d0\n");
  135. #endif
  136. table[index(0, TX, ep0_tx_bdt_bank)].addr = (void *)data;
  137. table[index(0, TX, ep0_tx_bdt_bank)].desc = BDT_DESC(len, ep0_tx_data_toggle);
  138. ep0_tx_data_toggle ^= 1;
  139. ep0_tx_bdt_bank ^= 1;
  140. }
  141. static uint8_t reply_buffer[8];
  142. static void usb_setup(void)
  143. {
  144. const uint8_t *data = NULL;
  145. uint32_t datalen = 0;
  146. const usb_descriptor_list_t *list;
  147. uint32_t size;
  148. volatile uint8_t *reg;
  149. uint8_t epconf;
  150. const uint8_t *cfg;
  151. int i;
  152. switch (setup.wRequestAndType) {
  153. case 0x0500: // SET_ADDRESS
  154. break;
  155. case 0x0900: // SET_CONFIGURATION
  156. //serial_print("configure\n");
  157. usb_configuration = setup.wValue;
  158. reg = &USB0_ENDPT1;
  159. cfg = usb_endpoint_config_table;
  160. // clear all BDT entries, free any allocated memory...
  161. for (i=4; i < (NUM_ENDPOINTS+1)*4; i++) {
  162. if (table[i].desc & BDT_OWN) {
  163. usb_free((usb_packet_t *)((uint8_t *)(table[i].addr) - 8));
  164. }
  165. }
  166. // free all queued packets
  167. for (i=0; i < NUM_ENDPOINTS; i++) {
  168. usb_packet_t *p, *n;
  169. p = rx_first[i];
  170. while (p) {
  171. n = p->next;
  172. usb_free(p);
  173. p = n;
  174. }
  175. rx_first[i] = NULL;
  176. rx_last[i] = NULL;
  177. p = tx_first[i];
  178. while (p) {
  179. n = p->next;
  180. usb_free(p);
  181. p = n;
  182. }
  183. tx_first[i] = NULL;
  184. tx_last[i] = NULL;
  185. usb_rx_byte_count_data[i] = 0;
  186. switch (tx_state[i]) {
  187. case TX_STATE_EVEN_FREE:
  188. case TX_STATE_NONE_FREE_EVEN_FIRST:
  189. tx_state[i] = TX_STATE_BOTH_FREE_EVEN_FIRST;
  190. break;
  191. case TX_STATE_ODD_FREE:
  192. case TX_STATE_NONE_FREE_ODD_FIRST:
  193. tx_state[i] = TX_STATE_BOTH_FREE_ODD_FIRST;
  194. break;
  195. default:
  196. break;
  197. }
  198. }
  199. usb_rx_memory_needed = 0;
  200. for (i=1; i <= NUM_ENDPOINTS; i++) {
  201. epconf = *cfg++;
  202. *reg = epconf;
  203. reg += 4;
  204. #ifdef AUDIO_INTERFACE
  205. if (i == AUDIO_RX_ENDPOINT) {
  206. table[index(i, RX, EVEN)].addr = usb_audio_receive_buffer;
  207. table[index(i, RX, EVEN)].desc = (AUDIO_RX_SIZE<<16) | BDT_OWN;
  208. table[index(i, RX, ODD)].addr = usb_audio_receive_buffer;
  209. table[index(i, RX, ODD)].desc = (AUDIO_RX_SIZE<<16) | BDT_OWN;
  210. } else
  211. #endif
  212. if (epconf & USB_ENDPT_EPRXEN) {
  213. usb_packet_t *p;
  214. p = usb_malloc();
  215. if (p) {
  216. table[index(i, RX, EVEN)].addr = p->buf;
  217. table[index(i, RX, EVEN)].desc = BDT_DESC(64, 0);
  218. } else {
  219. table[index(i, RX, EVEN)].desc = 0;
  220. usb_rx_memory_needed++;
  221. }
  222. p = usb_malloc();
  223. if (p) {
  224. table[index(i, RX, ODD)].addr = p->buf;
  225. table[index(i, RX, ODD)].desc = BDT_DESC(64, 1);
  226. } else {
  227. table[index(i, RX, ODD)].desc = 0;
  228. usb_rx_memory_needed++;
  229. }
  230. }
  231. table[index(i, TX, EVEN)].desc = 0;
  232. table[index(i, TX, ODD)].desc = 0;
  233. #ifdef AUDIO_INTERFACE
  234. if (i == AUDIO_SYNC_ENDPOINT) {
  235. table[index(i, TX, EVEN)].addr = &usb_audio_sync_feedback;
  236. table[index(i, TX, EVEN)].desc = (3<<16) | BDT_OWN;
  237. }
  238. #endif
  239. }
  240. break;
  241. case 0x0880: // GET_CONFIGURATION
  242. reply_buffer[0] = usb_configuration;
  243. datalen = 1;
  244. data = reply_buffer;
  245. break;
  246. case 0x0080: // GET_STATUS (device)
  247. reply_buffer[0] = 0;
  248. reply_buffer[1] = 0;
  249. datalen = 2;
  250. data = reply_buffer;
  251. break;
  252. case 0x0082: // GET_STATUS (endpoint)
  253. if (setup.wIndex > NUM_ENDPOINTS) {
  254. // TODO: do we need to handle IN vs OUT here?
  255. endpoint0_stall();
  256. return;
  257. }
  258. reply_buffer[0] = 0;
  259. reply_buffer[1] = 0;
  260. if (*(uint8_t *)(&USB0_ENDPT0 + setup.wIndex * 4) & 0x02) reply_buffer[0] = 1;
  261. data = reply_buffer;
  262. datalen = 2;
  263. break;
  264. case 0x0102: // CLEAR_FEATURE (endpoint)
  265. i = setup.wIndex & 0x7F;
  266. if (i > NUM_ENDPOINTS || setup.wValue != 0) {
  267. // TODO: do we need to handle IN vs OUT here?
  268. endpoint0_stall();
  269. return;
  270. }
  271. (*(uint8_t *)(&USB0_ENDPT0 + i * 4)) &= ~0x02;
  272. // TODO: do we need to clear the data toggle here?
  273. break;
  274. case 0x0302: // SET_FEATURE (endpoint)
  275. i = setup.wIndex & 0x7F;
  276. if (i > NUM_ENDPOINTS || setup.wValue != 0) {
  277. // TODO: do we need to handle IN vs OUT here?
  278. endpoint0_stall();
  279. return;
  280. }
  281. (*(uint8_t *)(&USB0_ENDPT0 + i * 4)) |= 0x02;
  282. // TODO: do we need to clear the data toggle here?
  283. break;
  284. case 0x0680: // GET_DESCRIPTOR
  285. case 0x0681:
  286. //serial_print("desc:");
  287. //serial_phex16(setup.wValue);
  288. //serial_print("\n");
  289. for (list = usb_descriptor_list; 1; list++) {
  290. if (list->addr == NULL) break;
  291. //if (setup.wValue == list->wValue &&
  292. //(setup.wIndex == list->wIndex) || ((setup.wValue >> 8) == 3)) {
  293. if (setup.wValue == list->wValue && setup.wIndex == list->wIndex) {
  294. data = list->addr;
  295. if ((setup.wValue >> 8) == 3) {
  296. // for string descriptors, use the descriptor's
  297. // length field, allowing runtime configured
  298. // length.
  299. datalen = *(list->addr);
  300. } else {
  301. datalen = list->length;
  302. }
  303. #if 0
  304. serial_print("Desc found, ");
  305. serial_phex32((uint32_t)data);
  306. serial_print(",");
  307. serial_phex16(datalen);
  308. serial_print(",");
  309. serial_phex(data[0]);
  310. serial_phex(data[1]);
  311. serial_phex(data[2]);
  312. serial_phex(data[3]);
  313. serial_phex(data[4]);
  314. serial_phex(data[5]);
  315. serial_print("\n");
  316. #endif
  317. goto send;
  318. }
  319. }
  320. //serial_print("desc: not found\n");
  321. endpoint0_stall();
  322. return;
  323. #if defined(CDC_STATUS_INTERFACE)
  324. case 0x2221: // CDC_SET_CONTROL_LINE_STATE
  325. usb_cdc_line_rtsdtr_millis = systick_millis_count;
  326. usb_cdc_line_rtsdtr = setup.wValue;
  327. //serial_print("set control line state\n");
  328. break;
  329. case 0x2321: // CDC_SEND_BREAK
  330. break;
  331. case 0x2021: // CDC_SET_LINE_CODING
  332. //serial_print("set coding, waiting...\n");
  333. return;
  334. #endif
  335. #if defined(MTP_INTERFACE)
  336. case 0x2164: // Cancel Request (PTP spec, 5.2.1, page 8)
  337. // TODO: required by PTP spec
  338. endpoint0_stall();
  339. return;
  340. case 0x2166: // Device Reset (PTP spec, 5.2.3, page 10)
  341. // TODO: required by PTP spec
  342. endpoint0_stall();
  343. return;
  344. case 0x2167: // Get Device Statis (PTP spec, 5.2.4, page 10)
  345. // TODO: required by PTP spec
  346. endpoint0_stall();
  347. return;
  348. #endif
  349. // TODO: this does not work... why?
  350. #if defined(SEREMU_INTERFACE) || defined(KEYBOARD_INTERFACE)
  351. case 0x0921: // HID SET_REPORT
  352. //serial_print(":)\n");
  353. return;
  354. case 0x0A21: // HID SET_IDLE
  355. break;
  356. // case 0xC940:
  357. #endif
  358. #if defined(AUDIO_INTERFACE)
  359. case 0x0B01: // SET_INTERFACE (alternate setting)
  360. if (setup.wIndex == AUDIO_INTERFACE+1) {
  361. usb_audio_transmit_setting = setup.wValue;
  362. if (usb_audio_transmit_setting > 0) {
  363. bdt_t *b = &table[index(AUDIO_TX_ENDPOINT, TX, EVEN)];
  364. uint8_t state = tx_state[AUDIO_TX_ENDPOINT-1];
  365. if (state) b++;
  366. if (!(b->desc & BDT_OWN)) {
  367. memset(usb_audio_transmit_buffer, 0, 176);
  368. b->addr = usb_audio_transmit_buffer;
  369. b->desc = (176 << 16) | BDT_OWN;
  370. tx_state[AUDIO_TX_ENDPOINT-1] = state ^ 1;
  371. }
  372. }
  373. } else if (setup.wIndex == AUDIO_INTERFACE+2) {
  374. usb_audio_receive_setting = setup.wValue;
  375. } else {
  376. endpoint0_stall();
  377. return;
  378. }
  379. break;
  380. case 0x0A81: // GET_INTERFACE (alternate setting)
  381. datalen = 1;
  382. data = reply_buffer;
  383. if (setup.wIndex == AUDIO_INTERFACE+1) {
  384. reply_buffer[0] = usb_audio_transmit_setting;
  385. } else if (setup.wIndex == AUDIO_INTERFACE+2) {
  386. reply_buffer[0] = usb_audio_receive_setting;
  387. } else {
  388. endpoint0_stall();
  389. return;
  390. }
  391. break;
  392. case 0x0121: // SET FEATURE
  393. case 0x0221:
  394. case 0x0321:
  395. case 0x0421:
  396. // handle these on the next packet. See usb_audio_set_feature()
  397. return;
  398. case 0x81A1: // GET FEATURE
  399. case 0x82A1:
  400. case 0x83A1:
  401. case 0x84A1:
  402. if (usb_audio_get_feature(&setup, reply_buffer, &datalen)) {
  403. data = reply_buffer;
  404. }
  405. else {
  406. endpoint0_stall();
  407. return;
  408. }
  409. break;
  410. case 0x81A2: // GET_CUR (wValue=0, wIndex=interface, wLength=len)
  411. if (setup.wLength >= 3) {
  412. reply_buffer[0] = 44100 & 255;
  413. reply_buffer[1] = 44100 >> 8;
  414. reply_buffer[2] = 0;
  415. datalen = 3;
  416. data = reply_buffer;
  417. } else {
  418. endpoint0_stall();
  419. return;
  420. }
  421. break;
  422. #endif
  423. #if defined(MULTITOUCH_INTERFACE)
  424. case 0x01A1:
  425. if (setup.wValue == 0x0300 && setup.wIndex == MULTITOUCH_INTERFACE) {
  426. reply_buffer[0] = MULTITOUCH_FINGERS;
  427. data = reply_buffer;
  428. datalen = 1;
  429. } else {
  430. endpoint0_stall();
  431. return;
  432. }
  433. break;
  434. #endif
  435. default:
  436. endpoint0_stall();
  437. return;
  438. }
  439. send:
  440. //serial_print("setup send ");
  441. //serial_phex32(data);
  442. //serial_print(",");
  443. //serial_phex16(datalen);
  444. //serial_print("\n");
  445. if (datalen > setup.wLength) datalen = setup.wLength;
  446. size = datalen;
  447. if (size > EP0_SIZE) size = EP0_SIZE;
  448. endpoint0_transmit(data, size);
  449. data += size;
  450. datalen -= size;
  451. if (datalen == 0 && size < EP0_SIZE) return;
  452. size = datalen;
  453. if (size > EP0_SIZE) size = EP0_SIZE;
  454. endpoint0_transmit(data, size);
  455. data += size;
  456. datalen -= size;
  457. if (datalen == 0 && size < EP0_SIZE) return;
  458. ep0_tx_ptr = data;
  459. ep0_tx_len = datalen;
  460. }
  461. //A bulk endpoint's toggle sequence is initialized to DATA0 when the endpoint
  462. //experiences any configuration event (configuration events are explained in
  463. //Sections 9.1.1.5 and 9.4.5).
  464. //Configuring a device or changing an alternate setting causes all of the status
  465. //and configuration values associated with endpoints in the affected interfaces
  466. //to be set to their default values. This includes setting the data toggle of
  467. //any endpoint using data toggles to the value DATA0.
  468. //For endpoints using data toggle, regardless of whether an endpoint has the
  469. //Halt feature set, a ClearFeature(ENDPOINT_HALT) request always results in the
  470. //data toggle being reinitialized to DATA0.
  471. // #define stat2bufferdescriptor(stat) (table + ((stat) >> 2))
  472. static void usb_control(uint32_t stat)
  473. {
  474. bdt_t *b;
  475. uint32_t pid, size;
  476. uint8_t *buf;
  477. const uint8_t *data;
  478. b = stat2bufferdescriptor(stat);
  479. pid = BDT_PID(b->desc);
  480. //count = b->desc >> 16;
  481. buf = b->addr;
  482. //serial_print("pid:");
  483. //serial_phex(pid);
  484. //serial_print(", count:");
  485. //serial_phex(count);
  486. //serial_print("\n");
  487. switch (pid) {
  488. case 0x0D: // Setup received from host
  489. //serial_print("PID=Setup\n");
  490. //if (count != 8) ; // panic?
  491. // grab the 8 byte setup info
  492. setup.word1 = *(uint32_t *)(buf);
  493. setup.word2 = *(uint32_t *)(buf + 4);
  494. // give the buffer back
  495. b->desc = BDT_DESC(EP0_SIZE, DATA1);
  496. //table[index(0, RX, EVEN)].desc = BDT_DESC(EP0_SIZE, 1);
  497. //table[index(0, RX, ODD)].desc = BDT_DESC(EP0_SIZE, 1);
  498. // clear any leftover pending IN transactions
  499. ep0_tx_ptr = NULL;
  500. if (ep0_tx_data_toggle) {
  501. }
  502. //if (table[index(0, TX, EVEN)].desc & 0x80) {
  503. //serial_print("leftover tx even\n");
  504. //}
  505. //if (table[index(0, TX, ODD)].desc & 0x80) {
  506. //serial_print("leftover tx odd\n");
  507. //}
  508. table[index(0, TX, EVEN)].desc = 0;
  509. table[index(0, TX, ODD)].desc = 0;
  510. // first IN after Setup is always DATA1
  511. ep0_tx_data_toggle = 1;
  512. #if 0
  513. serial_print("bmRequestType:");
  514. serial_phex(setup.bmRequestType);
  515. serial_print(", bRequest:");
  516. serial_phex(setup.bRequest);
  517. serial_print(", wValue:");
  518. serial_phex16(setup.wValue);
  519. serial_print(", wIndex:");
  520. serial_phex16(setup.wIndex);
  521. serial_print(", len:");
  522. serial_phex16(setup.wLength);
  523. serial_print("\n");
  524. #endif
  525. // actually "do" the setup request
  526. usb_setup();
  527. // unfreeze the USB, now that we're ready
  528. USB0_CTL = USB_CTL_USBENSOFEN; // clear TXSUSPENDTOKENBUSY bit
  529. break;
  530. case 0x01: // OUT transaction received from host
  531. case 0x02:
  532. //serial_print("PID=OUT\n");
  533. #ifdef CDC_STATUS_INTERFACE
  534. if (setup.wRequestAndType == 0x2021 /*CDC_SET_LINE_CODING*/) {
  535. int i;
  536. uint8_t *dst = (uint8_t *)usb_cdc_line_coding;
  537. //serial_print("set line coding ");
  538. for (i=0; i<7; i++) {
  539. //serial_phex(*buf);
  540. *dst++ = *buf++;
  541. }
  542. //serial_phex32(usb_cdc_line_coding[0]);
  543. //serial_print("\n");
  544. if (usb_cdc_line_coding[0] == 134) usb_reboot_timer = 15;
  545. endpoint0_transmit(NULL, 0);
  546. }
  547. #endif
  548. #ifdef KEYBOARD_INTERFACE
  549. if (setup.word1 == 0x02000921 && setup.word2 == ((1<<16)|KEYBOARD_INTERFACE)) {
  550. keyboard_leds = buf[0];
  551. endpoint0_transmit(NULL, 0);
  552. }
  553. #endif
  554. #ifdef SEREMU_INTERFACE
  555. if (setup.word1 == 0x03000921 && setup.word2 == ((4<<16)|SEREMU_INTERFACE)
  556. && buf[0] == 0xA9 && buf[1] == 0x45 && buf[2] == 0xC2 && buf[3] == 0x6B) {
  557. usb_reboot_timer = 5;
  558. endpoint0_transmit(NULL, 0);
  559. }
  560. #endif
  561. #ifdef AUDIO_INTERFACE
  562. if (usb_audio_set_feature(&setup, buf)) {
  563. endpoint0_transmit(NULL, 0);
  564. }
  565. #endif
  566. // give the buffer back
  567. b->desc = BDT_DESC(EP0_SIZE, DATA1);
  568. break;
  569. case 0x09: // IN transaction completed to host
  570. //serial_print("PID=IN:");
  571. //serial_phex(stat);
  572. //serial_print("\n");
  573. // send remaining data, if any...
  574. data = ep0_tx_ptr;
  575. if (data) {
  576. size = ep0_tx_len;
  577. if (size > EP0_SIZE) size = EP0_SIZE;
  578. endpoint0_transmit(data, size);
  579. data += size;
  580. ep0_tx_len -= size;
  581. ep0_tx_ptr = (ep0_tx_len > 0 || size == EP0_SIZE) ? data : NULL;
  582. }
  583. if (setup.bRequest == 5 && setup.bmRequestType == 0) {
  584. setup.bRequest = 0;
  585. //serial_print("set address: ");
  586. //serial_phex16(setup.wValue);
  587. //serial_print("\n");
  588. USB0_ADDR = setup.wValue;
  589. }
  590. break;
  591. //default:
  592. //serial_print("PID=unknown:");
  593. //serial_phex(pid);
  594. //serial_print("\n");
  595. }
  596. USB0_CTL = USB_CTL_USBENSOFEN; // clear TXSUSPENDTOKENBUSY bit
  597. }
  598. usb_packet_t *usb_rx(uint32_t endpoint)
  599. {
  600. usb_packet_t *ret;
  601. endpoint--;
  602. if (endpoint >= NUM_ENDPOINTS) return NULL;
  603. __disable_irq();
  604. ret = rx_first[endpoint];
  605. if (ret) {
  606. rx_first[endpoint] = ret->next;
  607. usb_rx_byte_count_data[endpoint] -= ret->len;
  608. }
  609. __enable_irq();
  610. //serial_print("rx, epidx=");
  611. //serial_phex(endpoint);
  612. //serial_print(", packet=");
  613. //serial_phex32(ret);
  614. //serial_print("\n");
  615. return ret;
  616. }
  617. static uint32_t usb_queue_byte_count(const usb_packet_t *p)
  618. {
  619. uint32_t count=0;
  620. __disable_irq();
  621. for ( ; p; p = p->next) {
  622. count += p->len;
  623. }
  624. __enable_irq();
  625. return count;
  626. }
  627. // TODO: make this an inline function...
  628. /*
  629. uint32_t usb_rx_byte_count(uint32_t endpoint)
  630. {
  631. endpoint--;
  632. if (endpoint >= NUM_ENDPOINTS) return 0;
  633. return usb_rx_byte_count_data[endpoint];
  634. //return usb_queue_byte_count(rx_first[endpoint]);
  635. }
  636. */
  637. uint32_t usb_tx_byte_count(uint32_t endpoint)
  638. {
  639. endpoint--;
  640. if (endpoint >= NUM_ENDPOINTS) return 0;
  641. return usb_queue_byte_count(tx_first[endpoint]);
  642. }
  643. uint32_t usb_tx_packet_count(uint32_t endpoint)
  644. {
  645. const usb_packet_t *p;
  646. uint32_t count=0;
  647. endpoint--;
  648. if (endpoint >= NUM_ENDPOINTS) return 0;
  649. __disable_irq();
  650. for (p = tx_first[endpoint]; p; p = p->next) count++;
  651. __enable_irq();
  652. return count;
  653. }
  654. // Called from usb_free, but only when usb_rx_memory_needed > 0, indicating
  655. // receive endpoints are starving for memory. The intention is to give
  656. // endpoints needing receive memory priority over the user's code, which is
  657. // likely calling usb_malloc to obtain memory for transmitting. When the
  658. // user is creating data very quickly, their consumption could starve reception
  659. // without this prioritization. The packet buffer (input) is assigned to the
  660. // first endpoint needing memory.
  661. //
  662. void usb_rx_memory(usb_packet_t *packet)
  663. {
  664. unsigned int i;
  665. const uint8_t *cfg;
  666. cfg = usb_endpoint_config_table;
  667. //serial_print("rx_mem:");
  668. __disable_irq();
  669. for (i=1; i <= NUM_ENDPOINTS; i++) {
  670. #ifdef AUDIO_INTERFACE
  671. if (i == AUDIO_RX_ENDPOINT) continue;
  672. #endif
  673. if (*cfg++ & USB_ENDPT_EPRXEN) {
  674. if (table[index(i, RX, EVEN)].desc == 0) {
  675. table[index(i, RX, EVEN)].addr = packet->buf;
  676. table[index(i, RX, EVEN)].desc = BDT_DESC(64, 0);
  677. usb_rx_memory_needed--;
  678. __enable_irq();
  679. //serial_phex(i);
  680. //serial_print(",even\n");
  681. return;
  682. }
  683. if (table[index(i, RX, ODD)].desc == 0) {
  684. table[index(i, RX, ODD)].addr = packet->buf;
  685. table[index(i, RX, ODD)].desc = BDT_DESC(64, 1);
  686. usb_rx_memory_needed--;
  687. __enable_irq();
  688. //serial_phex(i);
  689. //serial_print(",odd\n");
  690. return;
  691. }
  692. }
  693. }
  694. __enable_irq();
  695. // we should never reach this point. If we get here, it means
  696. // usb_rx_memory_needed was set greater than zero, but no memory
  697. // was actually needed.
  698. usb_rx_memory_needed = 0;
  699. usb_free(packet);
  700. return;
  701. }
  702. //#define index(endpoint, tx, odd) (((endpoint) << 2) | ((tx) << 1) | (odd))
  703. //#define stat2bufferdescriptor(stat) (table + ((stat) >> 2))
  704. void usb_tx(uint32_t endpoint, usb_packet_t *packet)
  705. {
  706. bdt_t *b = &table[index(endpoint, TX, EVEN)];
  707. uint8_t next;
  708. endpoint--;
  709. if (endpoint >= NUM_ENDPOINTS) return;
  710. __disable_irq();
  711. //serial_print("txstate=");
  712. //serial_phex(tx_state[endpoint]);
  713. //serial_print("\n");
  714. switch (tx_state[endpoint]) {
  715. case TX_STATE_BOTH_FREE_EVEN_FIRST:
  716. next = TX_STATE_ODD_FREE;
  717. break;
  718. case TX_STATE_BOTH_FREE_ODD_FIRST:
  719. b++;
  720. next = TX_STATE_EVEN_FREE;
  721. break;
  722. case TX_STATE_EVEN_FREE:
  723. next = TX_STATE_NONE_FREE_ODD_FIRST;
  724. break;
  725. case TX_STATE_ODD_FREE:
  726. b++;
  727. next = TX_STATE_NONE_FREE_EVEN_FIRST;
  728. break;
  729. default:
  730. if (tx_first[endpoint] == NULL) {
  731. tx_first[endpoint] = packet;
  732. } else {
  733. tx_last[endpoint]->next = packet;
  734. }
  735. tx_last[endpoint] = packet;
  736. __enable_irq();
  737. return;
  738. }
  739. tx_state[endpoint] = next;
  740. b->addr = packet->buf;
  741. b->desc = BDT_DESC(packet->len, ((uint32_t)b & 8) ? DATA1 : DATA0);
  742. __enable_irq();
  743. }
  744. void usb_tx_isochronous(uint32_t endpoint, void *data, uint32_t len)
  745. {
  746. bdt_t *b = &table[index(endpoint, TX, EVEN)];
  747. uint8_t next, state;
  748. endpoint--;
  749. if (endpoint >= NUM_ENDPOINTS) return;
  750. __disable_irq();
  751. state = tx_state[endpoint];
  752. if (state == 0) {
  753. next = 1;
  754. } else {
  755. b++;
  756. next = 0;
  757. }
  758. tx_state[endpoint] = next;
  759. b->addr = data;
  760. b->desc = (len << 16) | BDT_OWN;
  761. __enable_irq();
  762. }
  763. void _reboot_Teensyduino_(void)
  764. {
  765. // TODO: initialize R0 with a code....
  766. __asm__ volatile("bkpt");
  767. }
  768. void usb_isr(void)
  769. {
  770. uint8_t status, stat, t;
  771. //serial_print("isr");
  772. //status = USB0_ISTAT;
  773. //serial_phex(status);
  774. //serial_print("\n");
  775. restart:
  776. status = USB0_ISTAT;
  777. if ((status & USB_ISTAT_SOFTOK /* 04 */ )) {
  778. if (usb_configuration) {
  779. t = usb_reboot_timer;
  780. if (t) {
  781. usb_reboot_timer = --t;
  782. if (!t) _reboot_Teensyduino_();
  783. }
  784. #ifdef CDC_DATA_INTERFACE
  785. t = usb_cdc_transmit_flush_timer;
  786. if (t) {
  787. usb_cdc_transmit_flush_timer = --t;
  788. if (t == 0) usb_serial_flush_callback();
  789. }
  790. #endif
  791. #ifdef SEREMU_INTERFACE
  792. t = usb_seremu_transmit_flush_timer;
  793. if (t) {
  794. usb_seremu_transmit_flush_timer = --t;
  795. if (t == 0) usb_seremu_flush_callback();
  796. }
  797. #endif
  798. #ifdef MIDI_INTERFACE
  799. usb_midi_flush_output();
  800. #endif
  801. #ifdef FLIGHTSIM_INTERFACE
  802. usb_flightsim_flush_callback();
  803. #endif
  804. #ifdef MULTITOUCH_INTERFACE
  805. usb_touchscreen_update_callback();
  806. #endif
  807. }
  808. USB0_ISTAT = USB_ISTAT_SOFTOK;
  809. }
  810. if ((status & USB_ISTAT_TOKDNE /* 08 */ )) {
  811. uint8_t endpoint;
  812. stat = USB0_STAT;
  813. //serial_print("token: ep=");
  814. //serial_phex(stat >> 4);
  815. //serial_print(stat & 0x08 ? ",tx" : ",rx");
  816. //serial_print(stat & 0x04 ? ",odd\n" : ",even\n");
  817. endpoint = stat >> 4;
  818. if (endpoint == 0) {
  819. usb_control(stat);
  820. } else {
  821. bdt_t *b = stat2bufferdescriptor(stat);
  822. usb_packet_t *packet = (usb_packet_t *)((uint8_t *)(b->addr) - 8);
  823. #if 0
  824. serial_print("ep:");
  825. serial_phex(endpoint);
  826. serial_print(", pid:");
  827. serial_phex(BDT_PID(b->desc));
  828. serial_print(((uint32_t)b & 8) ? ", odd" : ", even");
  829. serial_print(", count:");
  830. serial_phex(b->desc >> 16);
  831. serial_print("\n");
  832. #endif
  833. endpoint--; // endpoint is index to zero-based arrays
  834. #ifdef AUDIO_INTERFACE
  835. if ((endpoint == AUDIO_TX_ENDPOINT-1) && (stat & 0x08)) {
  836. unsigned int len;
  837. len = usb_audio_transmit_callback();
  838. if (len > 0) {
  839. b = (bdt_t *)((uint32_t)b ^ 8);
  840. b->addr = usb_audio_transmit_buffer;
  841. b->desc = (len << 16) | BDT_OWN;
  842. tx_state[endpoint] ^= 1;
  843. }
  844. } else if ((endpoint == AUDIO_RX_ENDPOINT-1) && !(stat & 0x08)) {
  845. usb_audio_receive_callback(b->desc >> 16);
  846. b->addr = usb_audio_receive_buffer;
  847. b->desc = (AUDIO_RX_SIZE << 16) | BDT_OWN;
  848. } else if ((endpoint == AUDIO_SYNC_ENDPOINT-1) && (stat & 0x08)) {
  849. b = (bdt_t *)((uint32_t)b ^ 8);
  850. b->addr = &usb_audio_sync_feedback;
  851. b->desc = (3 << 16) | BDT_OWN;
  852. tx_state[endpoint] ^= 1;
  853. } else
  854. #endif
  855. if (stat & 0x08) { // transmit
  856. usb_free(packet);
  857. packet = tx_first[endpoint];
  858. if (packet) {
  859. //serial_print("tx packet\n");
  860. tx_first[endpoint] = packet->next;
  861. b->addr = packet->buf;
  862. switch (tx_state[endpoint]) {
  863. case TX_STATE_BOTH_FREE_EVEN_FIRST:
  864. tx_state[endpoint] = TX_STATE_ODD_FREE;
  865. break;
  866. case TX_STATE_BOTH_FREE_ODD_FIRST:
  867. tx_state[endpoint] = TX_STATE_EVEN_FREE;
  868. break;
  869. case TX_STATE_EVEN_FREE:
  870. tx_state[endpoint] = TX_STATE_NONE_FREE_ODD_FIRST;
  871. break;
  872. case TX_STATE_ODD_FREE:
  873. tx_state[endpoint] = TX_STATE_NONE_FREE_EVEN_FIRST;
  874. break;
  875. default:
  876. break;
  877. }
  878. b->desc = BDT_DESC(packet->len,
  879. ((uint32_t)b & 8) ? DATA1 : DATA0);
  880. } else {
  881. //serial_print("tx no packet\n");
  882. switch (tx_state[endpoint]) {
  883. case TX_STATE_BOTH_FREE_EVEN_FIRST:
  884. case TX_STATE_BOTH_FREE_ODD_FIRST:
  885. break;
  886. case TX_STATE_EVEN_FREE:
  887. tx_state[endpoint] = TX_STATE_BOTH_FREE_EVEN_FIRST;
  888. break;
  889. case TX_STATE_ODD_FREE:
  890. tx_state[endpoint] = TX_STATE_BOTH_FREE_ODD_FIRST;
  891. break;
  892. default:
  893. tx_state[endpoint] = ((uint32_t)b & 8) ?
  894. TX_STATE_ODD_FREE : TX_STATE_EVEN_FREE;
  895. break;
  896. }
  897. }
  898. } else { // receive
  899. packet->len = b->desc >> 16;
  900. if (packet->len > 0) {
  901. packet->index = 0;
  902. packet->next = NULL;
  903. if (rx_first[endpoint] == NULL) {
  904. //serial_print("rx 1st, epidx=");
  905. //serial_phex(endpoint);
  906. //serial_print(", packet=");
  907. //serial_phex32((uint32_t)packet);
  908. //serial_print("\n");
  909. rx_first[endpoint] = packet;
  910. } else {
  911. //serial_print("rx Nth, epidx=");
  912. //serial_phex(endpoint);
  913. //serial_print(", packet=");
  914. //serial_phex32((uint32_t)packet);
  915. //serial_print("\n");
  916. rx_last[endpoint]->next = packet;
  917. }
  918. rx_last[endpoint] = packet;
  919. usb_rx_byte_count_data[endpoint] += packet->len;
  920. // TODO: implement a per-endpoint maximum # of allocated
  921. // packets, so a flood of incoming data on 1 endpoint
  922. // doesn't starve the others if the user isn't reading
  923. // it regularly
  924. packet = usb_malloc();
  925. if (packet) {
  926. b->addr = packet->buf;
  927. b->desc = BDT_DESC(64,
  928. ((uint32_t)b & 8) ? DATA1 : DATA0);
  929. } else {
  930. //serial_print("starving ");
  931. //serial_phex(endpoint + 1);
  932. b->desc = 0;
  933. usb_rx_memory_needed++;
  934. }
  935. } else {
  936. b->desc = BDT_DESC(64, ((uint32_t)b & 8) ? DATA1 : DATA0);
  937. }
  938. }
  939. }
  940. USB0_ISTAT = USB_ISTAT_TOKDNE;
  941. goto restart;
  942. }
  943. if (status & USB_ISTAT_USBRST /* 01 */ ) {
  944. //serial_print("reset\n");
  945. // initialize BDT toggle bits
  946. USB0_CTL = USB_CTL_ODDRST;
  947. ep0_tx_bdt_bank = 0;
  948. // set up buffers to receive Setup and OUT packets
  949. table[index(0, RX, EVEN)].desc = BDT_DESC(EP0_SIZE, 0);
  950. table[index(0, RX, EVEN)].addr = ep0_rx0_buf;
  951. table[index(0, RX, ODD)].desc = BDT_DESC(EP0_SIZE, 0);
  952. table[index(0, RX, ODD)].addr = ep0_rx1_buf;
  953. table[index(0, TX, EVEN)].desc = 0;
  954. table[index(0, TX, ODD)].desc = 0;
  955. // activate endpoint 0
  956. USB0_ENDPT0 = USB_ENDPT_EPRXEN | USB_ENDPT_EPTXEN | USB_ENDPT_EPHSHK;
  957. // clear all ending interrupts
  958. USB0_ERRSTAT = 0xFF;
  959. USB0_ISTAT = 0xFF;
  960. // set the address to zero during enumeration
  961. USB0_ADDR = 0;
  962. // enable other interrupts
  963. USB0_ERREN = 0xFF;
  964. USB0_INTEN = USB_INTEN_TOKDNEEN |
  965. USB_INTEN_SOFTOKEN |
  966. USB_INTEN_STALLEN |
  967. USB_INTEN_ERROREN |
  968. USB_INTEN_USBRSTEN |
  969. USB_INTEN_SLEEPEN;
  970. // is this necessary?
  971. USB0_CTL = USB_CTL_USBENSOFEN;
  972. return;
  973. }
  974. if ((status & USB_ISTAT_STALL /* 80 */ )) {
  975. //serial_print("stall:\n");
  976. USB0_ENDPT0 = USB_ENDPT_EPRXEN | USB_ENDPT_EPTXEN | USB_ENDPT_EPHSHK;
  977. USB0_ISTAT = USB_ISTAT_STALL;
  978. }
  979. if ((status & USB_ISTAT_ERROR /* 02 */ )) {
  980. uint8_t err = USB0_ERRSTAT;
  981. USB0_ERRSTAT = err;
  982. //serial_print("err:");
  983. //serial_phex(err);
  984. //serial_print("\n");
  985. USB0_ISTAT = USB_ISTAT_ERROR;
  986. }
  987. if ((status & USB_ISTAT_SLEEP /* 10 */ )) {
  988. //serial_print("sleep\n");
  989. USB0_ISTAT = USB_ISTAT_SLEEP;
  990. }
  991. }
  992. void usb_init(void)
  993. {
  994. int i;
  995. //serial_begin(BAUD2DIV(115200));
  996. //serial_print("usb_init\n");
  997. usb_init_serialnumber();
  998. for (i=0; i <= NUM_ENDPOINTS*4; i++) {
  999. table[i].desc = 0;
  1000. table[i].addr = 0;
  1001. }
  1002. // this basically follows the flowchart in the Kinetis
  1003. // Quick Reference User Guide, Rev. 1, 03/2012, page 141
  1004. // assume 48 MHz clock already running
  1005. // SIM - enable clock
  1006. SIM_SCGC4 |= SIM_SCGC4_USBOTG;
  1007. #ifdef HAS_KINETIS_MPU
  1008. MPU_RGDAAC0 |= 0x03000000;
  1009. #endif
  1010. #if F_CPU == 180000000 || F_CPU == 216000000
  1011. // if using IRC48M, turn on the USB clock recovery hardware
  1012. USB0_CLK_RECOVER_IRC_EN = USB_CLK_RECOVER_IRC_EN_IRC_EN | USB_CLK_RECOVER_IRC_EN_REG_EN;
  1013. USB0_CLK_RECOVER_CTRL = USB_CLK_RECOVER_CTRL_CLOCK_RECOVER_EN |
  1014. USB_CLK_RECOVER_CTRL_RESTART_IFRTRIM_EN;
  1015. #endif
  1016. // reset USB module
  1017. //USB0_USBTRC0 = USB_USBTRC_USBRESET;
  1018. //while ((USB0_USBTRC0 & USB_USBTRC_USBRESET) != 0) ; // wait for reset to end
  1019. // set desc table base addr
  1020. USB0_BDTPAGE1 = ((uint32_t)table) >> 8;
  1021. USB0_BDTPAGE2 = ((uint32_t)table) >> 16;
  1022. USB0_BDTPAGE3 = ((uint32_t)table) >> 24;
  1023. // clear all ISR flags
  1024. USB0_ISTAT = 0xFF;
  1025. USB0_ERRSTAT = 0xFF;
  1026. USB0_OTGISTAT = 0xFF;
  1027. //USB0_USBTRC0 |= 0x40; // undocumented bit
  1028. // enable USB
  1029. USB0_CTL = USB_CTL_USBENSOFEN;
  1030. USB0_USBCTRL = 0;
  1031. // enable reset interrupt
  1032. USB0_INTEN = USB_INTEN_USBRSTEN;
  1033. // enable interrupt in NVIC...
  1034. NVIC_SET_PRIORITY(IRQ_USBOTG, 112);
  1035. NVIC_ENABLE_IRQ(IRQ_USBOTG);
  1036. // enable d+ pullup
  1037. USB0_CONTROL = USB_CONTROL_DPPULLUPNONOTG;
  1038. }
  1039. #else // F_CPU < 20 MHz && defined(NUM_ENDPOINTS)
  1040. void usb_init(void)
  1041. {
  1042. }
  1043. #endif // F_CPU >= 20 MHz && defined(NUM_ENDPOINTS)