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

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