PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
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.

1231 lines
32KB

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