Teensy 4.1 core updated for 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.

1110 satır
29KB

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