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.

971 satır
25KB

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