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.

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