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