Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

1169 lines
30KB

  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. // 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 if (setup.wValue == 0x0100 && setup.wIndex == MULTITOUCH_INTERFACE) {
  436. memset(reply_buffer, 0, 8);
  437. data = reply_buffer;
  438. datalen = 8;
  439. } else {
  440. endpoint0_stall();
  441. return;
  442. }
  443. break;
  444. #endif
  445. default:
  446. endpoint0_stall();
  447. return;
  448. }
  449. send:
  450. //serial_print("setup send ");
  451. //serial_phex32(data);
  452. //serial_print(",");
  453. //serial_phex16(datalen);
  454. //serial_print("\n");
  455. if (datalen > setup.wLength) datalen = setup.wLength;
  456. size = datalen;
  457. if (size > EP0_SIZE) size = EP0_SIZE;
  458. endpoint0_transmit(data, size);
  459. data += size;
  460. datalen -= size;
  461. if (datalen == 0 && size < EP0_SIZE) return;
  462. size = datalen;
  463. if (size > EP0_SIZE) size = EP0_SIZE;
  464. endpoint0_transmit(data, size);
  465. data += size;
  466. datalen -= size;
  467. if (datalen == 0 && size < EP0_SIZE) return;
  468. ep0_tx_ptr = data;
  469. ep0_tx_len = datalen;
  470. }
  471. //A bulk endpoint's toggle sequence is initialized to DATA0 when the endpoint
  472. //experiences any configuration event (configuration events are explained in
  473. //Sections 9.1.1.5 and 9.4.5).
  474. //Configuring a device or changing an alternate setting causes all of the status
  475. //and configuration values associated with endpoints in the affected interfaces
  476. //to be set to their default values. This includes setting the data toggle of
  477. //any endpoint using data toggles to the value DATA0.
  478. //For endpoints using data toggle, regardless of whether an endpoint has the
  479. //Halt feature set, a ClearFeature(ENDPOINT_HALT) request always results in the
  480. //data toggle being reinitialized to DATA0.
  481. // #define stat2bufferdescriptor(stat) (table + ((stat) >> 2))
  482. static void usb_control(uint32_t stat)
  483. {
  484. bdt_t *b;
  485. uint32_t pid, size;
  486. uint8_t *buf;
  487. const uint8_t *data;
  488. b = stat2bufferdescriptor(stat);
  489. pid = BDT_PID(b->desc);
  490. //count = b->desc >> 16;
  491. buf = b->addr;
  492. //serial_print("pid:");
  493. //serial_phex(pid);
  494. //serial_print(", count:");
  495. //serial_phex(count);
  496. //serial_print("\n");
  497. switch (pid) {
  498. case 0x0D: // Setup received from host
  499. //serial_print("PID=Setup\n");
  500. //if (count != 8) ; // panic?
  501. // grab the 8 byte setup info
  502. setup.word1 = *(uint32_t *)(buf);
  503. setup.word2 = *(uint32_t *)(buf + 4);
  504. // give the buffer back
  505. b->desc = BDT_DESC(EP0_SIZE, DATA1);
  506. //table[index(0, RX, EVEN)].desc = BDT_DESC(EP0_SIZE, 1);
  507. //table[index(0, RX, ODD)].desc = BDT_DESC(EP0_SIZE, 1);
  508. // clear any leftover pending IN transactions
  509. ep0_tx_ptr = NULL;
  510. if (ep0_tx_data_toggle) {
  511. }
  512. //if (table[index(0, TX, EVEN)].desc & 0x80) {
  513. //serial_print("leftover tx even\n");
  514. //}
  515. //if (table[index(0, TX, ODD)].desc & 0x80) {
  516. //serial_print("leftover tx odd\n");
  517. //}
  518. table[index(0, TX, EVEN)].desc = 0;
  519. table[index(0, TX, ODD)].desc = 0;
  520. // first IN after Setup is always DATA1
  521. ep0_tx_data_toggle = 1;
  522. #if 0
  523. serial_print("bmRequestType:");
  524. serial_phex(setup.bmRequestType);
  525. serial_print(", bRequest:");
  526. serial_phex(setup.bRequest);
  527. serial_print(", wValue:");
  528. serial_phex16(setup.wValue);
  529. serial_print(", wIndex:");
  530. serial_phex16(setup.wIndex);
  531. serial_print(", len:");
  532. serial_phex16(setup.wLength);
  533. serial_print("\n");
  534. #endif
  535. // actually "do" the setup request
  536. usb_setup();
  537. // unfreeze the USB, now that we're ready
  538. USB0_CTL = USB_CTL_USBENSOFEN; // clear TXSUSPENDTOKENBUSY bit
  539. break;
  540. case 0x01: // OUT transaction received from host
  541. case 0x02:
  542. //serial_print("PID=OUT\n");
  543. #ifdef CDC_STATUS_INTERFACE
  544. if (setup.wRequestAndType == 0x2021 /*CDC_SET_LINE_CODING*/) {
  545. int i;
  546. uint8_t *dst = (uint8_t *)usb_cdc_line_coding;
  547. //serial_print("set line coding ");
  548. for (i=0; i<7; i++) {
  549. //serial_phex(*buf);
  550. *dst++ = *buf++;
  551. }
  552. //serial_phex32(usb_cdc_line_coding[0]);
  553. //serial_print("\n");
  554. if (usb_cdc_line_coding[0] == 134) usb_reboot_timer = 15;
  555. endpoint0_transmit(NULL, 0);
  556. }
  557. #endif
  558. #ifdef KEYBOARD_INTERFACE
  559. if (setup.word1 == 0x02000921 && setup.word2 == ((1<<16)|KEYBOARD_INTERFACE)) {
  560. keyboard_leds = buf[0];
  561. endpoint0_transmit(NULL, 0);
  562. }
  563. #endif
  564. #ifdef SEREMU_INTERFACE
  565. if (setup.word1 == 0x03000921 && setup.word2 == ((4<<16)|SEREMU_INTERFACE)
  566. && buf[0] == 0xA9 && buf[1] == 0x45 && buf[2] == 0xC2 && buf[3] == 0x6B) {
  567. usb_reboot_timer = 5;
  568. endpoint0_transmit(NULL, 0);
  569. }
  570. #endif
  571. #ifdef AUDIO_INTERFACE
  572. if (usb_audio_set_feature(&setup, buf)) {
  573. endpoint0_transmit(NULL, 0);
  574. }
  575. #endif
  576. // give the buffer back
  577. b->desc = BDT_DESC(EP0_SIZE, DATA1);
  578. break;
  579. case 0x09: // IN transaction completed to host
  580. //serial_print("PID=IN:");
  581. //serial_phex(stat);
  582. //serial_print("\n");
  583. // send remaining data, if any...
  584. data = ep0_tx_ptr;
  585. if (data) {
  586. size = ep0_tx_len;
  587. if (size > EP0_SIZE) size = EP0_SIZE;
  588. endpoint0_transmit(data, size);
  589. data += size;
  590. ep0_tx_len -= size;
  591. ep0_tx_ptr = (ep0_tx_len > 0 || size == EP0_SIZE) ? data : NULL;
  592. }
  593. if (setup.bRequest == 5 && setup.bmRequestType == 0) {
  594. setup.bRequest = 0;
  595. //serial_print("set address: ");
  596. //serial_phex16(setup.wValue);
  597. //serial_print("\n");
  598. USB0_ADDR = setup.wValue;
  599. }
  600. break;
  601. //default:
  602. //serial_print("PID=unknown:");
  603. //serial_phex(pid);
  604. //serial_print("\n");
  605. }
  606. USB0_CTL = USB_CTL_USBENSOFEN; // clear TXSUSPENDTOKENBUSY bit
  607. }
  608. usb_packet_t *usb_rx(uint32_t endpoint)
  609. {
  610. usb_packet_t *ret;
  611. endpoint--;
  612. if (endpoint >= NUM_ENDPOINTS) return NULL;
  613. __disable_irq();
  614. ret = rx_first[endpoint];
  615. if (ret) {
  616. rx_first[endpoint] = ret->next;
  617. usb_rx_byte_count_data[endpoint] -= ret->len;
  618. }
  619. __enable_irq();
  620. //serial_print("rx, epidx=");
  621. //serial_phex(endpoint);
  622. //serial_print(", packet=");
  623. //serial_phex32(ret);
  624. //serial_print("\n");
  625. return ret;
  626. }
  627. static uint32_t usb_queue_byte_count(const usb_packet_t *p)
  628. {
  629. uint32_t count=0;
  630. __disable_irq();
  631. for ( ; p; p = p->next) {
  632. count += p->len;
  633. }
  634. __enable_irq();
  635. return count;
  636. }
  637. // TODO: make this an inline function...
  638. /*
  639. uint32_t usb_rx_byte_count(uint32_t endpoint)
  640. {
  641. endpoint--;
  642. if (endpoint >= NUM_ENDPOINTS) return 0;
  643. return usb_rx_byte_count_data[endpoint];
  644. //return usb_queue_byte_count(rx_first[endpoint]);
  645. }
  646. */
  647. uint32_t usb_tx_byte_count(uint32_t endpoint)
  648. {
  649. endpoint--;
  650. if (endpoint >= NUM_ENDPOINTS) return 0;
  651. return usb_queue_byte_count(tx_first[endpoint]);
  652. }
  653. uint32_t usb_tx_packet_count(uint32_t endpoint)
  654. {
  655. const usb_packet_t *p;
  656. uint32_t count=0;
  657. endpoint--;
  658. if (endpoint >= NUM_ENDPOINTS) return 0;
  659. __disable_irq();
  660. for (p = tx_first[endpoint]; p; p = p->next) count++;
  661. __enable_irq();
  662. return count;
  663. }
  664. // Called from usb_free, but only when usb_rx_memory_needed > 0, indicating
  665. // receive endpoints are starving for memory. The intention is to give
  666. // endpoints needing receive memory priority over the user's code, which is
  667. // likely calling usb_malloc to obtain memory for transmitting. When the
  668. // user is creating data very quickly, their consumption could starve reception
  669. // without this prioritization. The packet buffer (input) is assigned to the
  670. // first endpoint needing memory.
  671. //
  672. void usb_rx_memory(usb_packet_t *packet)
  673. {
  674. unsigned int i;
  675. const uint8_t *cfg;
  676. cfg = usb_endpoint_config_table;
  677. //serial_print("rx_mem:");
  678. __disable_irq();
  679. for (i=1; i <= NUM_ENDPOINTS; i++) {
  680. #ifdef AUDIO_INTERFACE
  681. if (i == AUDIO_RX_ENDPOINT) continue;
  682. #endif
  683. if (*cfg++ & USB_ENDPT_EPRXEN) {
  684. if (table[index(i, RX, EVEN)].desc == 0) {
  685. table[index(i, RX, EVEN)].addr = packet->buf;
  686. table[index(i, RX, EVEN)].desc = BDT_DESC(64, 0);
  687. usb_rx_memory_needed--;
  688. __enable_irq();
  689. //serial_phex(i);
  690. //serial_print(",even\n");
  691. return;
  692. }
  693. if (table[index(i, RX, ODD)].desc == 0) {
  694. table[index(i, RX, ODD)].addr = packet->buf;
  695. table[index(i, RX, ODD)].desc = BDT_DESC(64, 1);
  696. usb_rx_memory_needed--;
  697. __enable_irq();
  698. //serial_phex(i);
  699. //serial_print(",odd\n");
  700. return;
  701. }
  702. }
  703. }
  704. __enable_irq();
  705. // we should never reach this point. If we get here, it means
  706. // usb_rx_memory_needed was set greater than zero, but no memory
  707. // was actually needed.
  708. usb_rx_memory_needed = 0;
  709. usb_free(packet);
  710. return;
  711. }
  712. //#define index(endpoint, tx, odd) (((endpoint) << 2) | ((tx) << 1) | (odd))
  713. //#define stat2bufferdescriptor(stat) (table + ((stat) >> 2))
  714. void usb_tx(uint32_t endpoint, usb_packet_t *packet)
  715. {
  716. bdt_t *b = &table[index(endpoint, TX, EVEN)];
  717. uint8_t next;
  718. endpoint--;
  719. if (endpoint >= NUM_ENDPOINTS) return;
  720. __disable_irq();
  721. //serial_print("txstate=");
  722. //serial_phex(tx_state[endpoint]);
  723. //serial_print("\n");
  724. switch (tx_state[endpoint]) {
  725. case TX_STATE_BOTH_FREE_EVEN_FIRST:
  726. next = TX_STATE_ODD_FREE;
  727. break;
  728. case TX_STATE_BOTH_FREE_ODD_FIRST:
  729. b++;
  730. next = TX_STATE_EVEN_FREE;
  731. break;
  732. case TX_STATE_EVEN_FREE:
  733. next = TX_STATE_NONE_FREE_ODD_FIRST;
  734. break;
  735. case TX_STATE_ODD_FREE:
  736. b++;
  737. next = TX_STATE_NONE_FREE_EVEN_FIRST;
  738. break;
  739. default:
  740. if (tx_first[endpoint] == NULL) {
  741. tx_first[endpoint] = packet;
  742. } else {
  743. tx_last[endpoint]->next = packet;
  744. }
  745. tx_last[endpoint] = packet;
  746. __enable_irq();
  747. return;
  748. }
  749. tx_state[endpoint] = next;
  750. b->addr = packet->buf;
  751. b->desc = BDT_DESC(packet->len, ((uint32_t)b & 8) ? DATA1 : DATA0);
  752. __enable_irq();
  753. }
  754. void usb_tx_isochronous(uint32_t endpoint, void *data, uint32_t len)
  755. {
  756. bdt_t *b = &table[index(endpoint, TX, EVEN)];
  757. uint8_t next, state;
  758. endpoint--;
  759. if (endpoint >= NUM_ENDPOINTS) return;
  760. __disable_irq();
  761. state = tx_state[endpoint];
  762. if (state == 0) {
  763. next = 1;
  764. } else {
  765. b++;
  766. next = 0;
  767. }
  768. tx_state[endpoint] = next;
  769. b->addr = data;
  770. b->desc = (len << 16) | BDT_OWN;
  771. __enable_irq();
  772. }
  773. void _reboot_Teensyduino_(void)
  774. {
  775. // TODO: initialize R0 with a code....
  776. __asm__ volatile("bkpt");
  777. }
  778. void usb_isr(void)
  779. {
  780. uint8_t status, stat, t;
  781. //serial_print("isr");
  782. //status = USB0_ISTAT;
  783. //serial_phex(status);
  784. //serial_print("\n");
  785. restart:
  786. status = USB0_ISTAT;
  787. if ((status & USB_ISTAT_SOFTOK /* 04 */ )) {
  788. if (usb_configuration) {
  789. t = usb_reboot_timer;
  790. if (t) {
  791. usb_reboot_timer = --t;
  792. if (!t) _reboot_Teensyduino_();
  793. }
  794. #ifdef CDC_DATA_INTERFACE
  795. t = usb_cdc_transmit_flush_timer;
  796. if (t) {
  797. usb_cdc_transmit_flush_timer = --t;
  798. if (t == 0) usb_serial_flush_callback();
  799. }
  800. #endif
  801. #ifdef SEREMU_INTERFACE
  802. t = usb_seremu_transmit_flush_timer;
  803. if (t) {
  804. usb_seremu_transmit_flush_timer = --t;
  805. if (t == 0) usb_seremu_flush_callback();
  806. }
  807. #endif
  808. #ifdef MIDI_INTERFACE
  809. usb_midi_flush_output();
  810. #endif
  811. #ifdef FLIGHTSIM_INTERFACE
  812. usb_flightsim_flush_callback();
  813. #endif
  814. #ifdef MULTITOUCH_INTERFACE
  815. usb_touchscreen_update_callback();
  816. #endif
  817. }
  818. USB0_ISTAT = USB_ISTAT_SOFTOK;
  819. }
  820. if ((status & USB_ISTAT_TOKDNE /* 08 */ )) {
  821. uint8_t endpoint;
  822. stat = USB0_STAT;
  823. //serial_print("token: ep=");
  824. //serial_phex(stat >> 4);
  825. //serial_print(stat & 0x08 ? ",tx" : ",rx");
  826. //serial_print(stat & 0x04 ? ",odd\n" : ",even\n");
  827. endpoint = stat >> 4;
  828. if (endpoint == 0) {
  829. usb_control(stat);
  830. } else {
  831. bdt_t *b = stat2bufferdescriptor(stat);
  832. usb_packet_t *packet = (usb_packet_t *)((uint8_t *)(b->addr) - 8);
  833. #if 0
  834. serial_print("ep:");
  835. serial_phex(endpoint);
  836. serial_print(", pid:");
  837. serial_phex(BDT_PID(b->desc));
  838. serial_print(((uint32_t)b & 8) ? ", odd" : ", even");
  839. serial_print(", count:");
  840. serial_phex(b->desc >> 16);
  841. serial_print("\n");
  842. #endif
  843. endpoint--; // endpoint is index to zero-based arrays
  844. #ifdef AUDIO_INTERFACE
  845. if ((endpoint == AUDIO_TX_ENDPOINT-1) && (stat & 0x08)) {
  846. unsigned int len;
  847. len = usb_audio_transmit_callback();
  848. if (len > 0) {
  849. b = (bdt_t *)((uint32_t)b ^ 8);
  850. b->addr = usb_audio_transmit_buffer;
  851. b->desc = (len << 16) | BDT_OWN;
  852. tx_state[endpoint] ^= 1;
  853. }
  854. } else if ((endpoint == AUDIO_RX_ENDPOINT-1) && !(stat & 0x08)) {
  855. usb_audio_receive_callback(b->desc >> 16);
  856. b->addr = usb_audio_receive_buffer;
  857. b->desc = (AUDIO_RX_SIZE << 16) | BDT_OWN;
  858. } else if ((endpoint == AUDIO_SYNC_ENDPOINT-1) && (stat & 0x08)) {
  859. b = (bdt_t *)((uint32_t)b ^ 8);
  860. b->addr = &usb_audio_sync_feedback;
  861. b->desc = (3 << 16) | BDT_OWN;
  862. tx_state[endpoint] ^= 1;
  863. } else
  864. #endif
  865. if (stat & 0x08) { // transmit
  866. usb_free(packet);
  867. packet = tx_first[endpoint];
  868. if (packet) {
  869. //serial_print("tx packet\n");
  870. tx_first[endpoint] = packet->next;
  871. b->addr = packet->buf;
  872. switch (tx_state[endpoint]) {
  873. case TX_STATE_BOTH_FREE_EVEN_FIRST:
  874. tx_state[endpoint] = TX_STATE_ODD_FREE;
  875. break;
  876. case TX_STATE_BOTH_FREE_ODD_FIRST:
  877. tx_state[endpoint] = TX_STATE_EVEN_FREE;
  878. break;
  879. case TX_STATE_EVEN_FREE:
  880. tx_state[endpoint] = TX_STATE_NONE_FREE_ODD_FIRST;
  881. break;
  882. case TX_STATE_ODD_FREE:
  883. tx_state[endpoint] = TX_STATE_NONE_FREE_EVEN_FIRST;
  884. break;
  885. default:
  886. break;
  887. }
  888. b->desc = BDT_DESC(packet->len,
  889. ((uint32_t)b & 8) ? DATA1 : DATA0);
  890. } else {
  891. //serial_print("tx no packet\n");
  892. switch (tx_state[endpoint]) {
  893. case TX_STATE_BOTH_FREE_EVEN_FIRST:
  894. case TX_STATE_BOTH_FREE_ODD_FIRST:
  895. break;
  896. case TX_STATE_EVEN_FREE:
  897. tx_state[endpoint] = TX_STATE_BOTH_FREE_EVEN_FIRST;
  898. break;
  899. case TX_STATE_ODD_FREE:
  900. tx_state[endpoint] = TX_STATE_BOTH_FREE_ODD_FIRST;
  901. break;
  902. default:
  903. tx_state[endpoint] = ((uint32_t)b & 8) ?
  904. TX_STATE_ODD_FREE : TX_STATE_EVEN_FREE;
  905. break;
  906. }
  907. }
  908. } else { // receive
  909. packet->len = b->desc >> 16;
  910. if (packet->len > 0) {
  911. packet->index = 0;
  912. packet->next = NULL;
  913. if (rx_first[endpoint] == NULL) {
  914. //serial_print("rx 1st, epidx=");
  915. //serial_phex(endpoint);
  916. //serial_print(", packet=");
  917. //serial_phex32((uint32_t)packet);
  918. //serial_print("\n");
  919. rx_first[endpoint] = packet;
  920. } else {
  921. //serial_print("rx Nth, epidx=");
  922. //serial_phex(endpoint);
  923. //serial_print(", packet=");
  924. //serial_phex32((uint32_t)packet);
  925. //serial_print("\n");
  926. rx_last[endpoint]->next = packet;
  927. }
  928. rx_last[endpoint] = packet;
  929. usb_rx_byte_count_data[endpoint] += packet->len;
  930. // TODO: implement a per-endpoint maximum # of allocated
  931. // packets, so a flood of incoming data on 1 endpoint
  932. // doesn't starve the others if the user isn't reading
  933. // it regularly
  934. packet = usb_malloc();
  935. if (packet) {
  936. b->addr = packet->buf;
  937. b->desc = BDT_DESC(64,
  938. ((uint32_t)b & 8) ? DATA1 : DATA0);
  939. } else {
  940. //serial_print("starving ");
  941. //serial_phex(endpoint + 1);
  942. b->desc = 0;
  943. usb_rx_memory_needed++;
  944. }
  945. } else {
  946. b->desc = BDT_DESC(64, ((uint32_t)b & 8) ? DATA1 : DATA0);
  947. }
  948. }
  949. }
  950. USB0_ISTAT = USB_ISTAT_TOKDNE;
  951. goto restart;
  952. }
  953. if (status & USB_ISTAT_USBRST /* 01 */ ) {
  954. //serial_print("reset\n");
  955. // initialize BDT toggle bits
  956. USB0_CTL = USB_CTL_ODDRST;
  957. ep0_tx_bdt_bank = 0;
  958. // set up buffers to receive Setup and OUT packets
  959. table[index(0, RX, EVEN)].desc = BDT_DESC(EP0_SIZE, 0);
  960. table[index(0, RX, EVEN)].addr = ep0_rx0_buf;
  961. table[index(0, RX, ODD)].desc = BDT_DESC(EP0_SIZE, 0);
  962. table[index(0, RX, ODD)].addr = ep0_rx1_buf;
  963. table[index(0, TX, EVEN)].desc = 0;
  964. table[index(0, TX, ODD)].desc = 0;
  965. // activate endpoint 0
  966. USB0_ENDPT0 = USB_ENDPT_EPRXEN | USB_ENDPT_EPTXEN | USB_ENDPT_EPHSHK;
  967. // clear all ending interrupts
  968. USB0_ERRSTAT = 0xFF;
  969. USB0_ISTAT = 0xFF;
  970. // set the address to zero during enumeration
  971. USB0_ADDR = 0;
  972. // enable other interrupts
  973. USB0_ERREN = 0xFF;
  974. USB0_INTEN = USB_INTEN_TOKDNEEN |
  975. USB_INTEN_SOFTOKEN |
  976. USB_INTEN_STALLEN |
  977. USB_INTEN_ERROREN |
  978. USB_INTEN_USBRSTEN |
  979. USB_INTEN_SLEEPEN;
  980. // is this necessary?
  981. USB0_CTL = USB_CTL_USBENSOFEN;
  982. return;
  983. }
  984. if ((status & USB_ISTAT_STALL /* 80 */ )) {
  985. //serial_print("stall:\n");
  986. USB0_ENDPT0 = USB_ENDPT_EPRXEN | USB_ENDPT_EPTXEN | USB_ENDPT_EPHSHK;
  987. USB0_ISTAT = USB_ISTAT_STALL;
  988. }
  989. if ((status & USB_ISTAT_ERROR /* 02 */ )) {
  990. uint8_t err = USB0_ERRSTAT;
  991. USB0_ERRSTAT = err;
  992. //serial_print("err:");
  993. //serial_phex(err);
  994. //serial_print("\n");
  995. USB0_ISTAT = USB_ISTAT_ERROR;
  996. }
  997. if ((status & USB_ISTAT_SLEEP /* 10 */ )) {
  998. //serial_print("sleep\n");
  999. USB0_ISTAT = USB_ISTAT_SLEEP;
  1000. }
  1001. }
  1002. void usb_init(void)
  1003. {
  1004. int i;
  1005. //serial_begin(BAUD2DIV(115200));
  1006. //serial_print("usb_init\n");
  1007. usb_init_serialnumber();
  1008. for (i=0; i <= NUM_ENDPOINTS*4; i++) {
  1009. table[i].desc = 0;
  1010. table[i].addr = 0;
  1011. }
  1012. // this basically follows the flowchart in the Kinetis
  1013. // Quick Reference User Guide, Rev. 1, 03/2012, page 141
  1014. // assume 48 MHz clock already running
  1015. // SIM - enable clock
  1016. SIM_SCGC4 |= SIM_SCGC4_USBOTG;
  1017. #ifdef HAS_KINETIS_MPU
  1018. MPU_RGDAAC0 |= 0x03000000;
  1019. #endif
  1020. #if F_CPU == 180000000 || F_CPU == 216000000
  1021. // if using IRC48M, turn on the USB clock recovery hardware
  1022. USB0_CLK_RECOVER_IRC_EN = USB_CLK_RECOVER_IRC_EN_IRC_EN | USB_CLK_RECOVER_IRC_EN_REG_EN;
  1023. USB0_CLK_RECOVER_CTRL = USB_CLK_RECOVER_CTRL_CLOCK_RECOVER_EN |
  1024. USB_CLK_RECOVER_CTRL_RESTART_IFRTRIM_EN;
  1025. #endif
  1026. // reset USB module
  1027. //USB0_USBTRC0 = USB_USBTRC_USBRESET;
  1028. //while ((USB0_USBTRC0 & USB_USBTRC_USBRESET) != 0) ; // wait for reset to end
  1029. // set desc table base addr
  1030. USB0_BDTPAGE1 = ((uint32_t)table) >> 8;
  1031. USB0_BDTPAGE2 = ((uint32_t)table) >> 16;
  1032. USB0_BDTPAGE3 = ((uint32_t)table) >> 24;
  1033. // clear all ISR flags
  1034. USB0_ISTAT = 0xFF;
  1035. USB0_ERRSTAT = 0xFF;
  1036. USB0_OTGISTAT = 0xFF;
  1037. //USB0_USBTRC0 |= 0x40; // undocumented bit
  1038. // enable USB
  1039. USB0_CTL = USB_CTL_USBENSOFEN;
  1040. USB0_USBCTRL = 0;
  1041. // enable reset interrupt
  1042. USB0_INTEN = USB_INTEN_USBRSTEN;
  1043. // enable interrupt in NVIC...
  1044. NVIC_SET_PRIORITY(IRQ_USBOTG, 112);
  1045. NVIC_ENABLE_IRQ(IRQ_USBOTG);
  1046. // enable d+ pullup
  1047. USB0_CONTROL = USB_CONTROL_DPPULLUPNONOTG;
  1048. }
  1049. #else // F_CPU < 20 MHz && defined(NUM_ENDPOINTS)
  1050. void usb_init(void)
  1051. {
  1052. }
  1053. #endif // F_CPU >= 20 MHz && defined(NUM_ENDPOINTS)