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.

969 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 "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) rx_first[endpoint] = ret->next;
  482. usb_rx_byte_count_data[endpoint] -= ret->len;
  483. __enable_irq();
  484. //serial_print("rx, epidx=");
  485. //serial_phex(endpoint);
  486. //serial_print(", packet=");
  487. //serial_phex32(ret);
  488. //serial_print("\n");
  489. return ret;
  490. }
  491. static uint32_t usb_queue_byte_count(const usb_packet_t *p)
  492. {
  493. uint32_t count=0;
  494. __disable_irq();
  495. for ( ; p; p = p->next) {
  496. count += p->len;
  497. }
  498. __enable_irq();
  499. return count;
  500. }
  501. // TODO: make this an inline function...
  502. /*
  503. uint32_t usb_rx_byte_count(uint32_t endpoint)
  504. {
  505. endpoint--;
  506. if (endpoint >= NUM_ENDPOINTS) return 0;
  507. return usb_rx_byte_count_data[endpoint];
  508. //return usb_queue_byte_count(rx_first[endpoint]);
  509. }
  510. */
  511. uint32_t usb_tx_byte_count(uint32_t endpoint)
  512. {
  513. endpoint--;
  514. if (endpoint >= NUM_ENDPOINTS) return 0;
  515. return usb_queue_byte_count(tx_first[endpoint]);
  516. }
  517. uint32_t usb_tx_packet_count(uint32_t endpoint)
  518. {
  519. const usb_packet_t *p;
  520. uint32_t count=0;
  521. endpoint--;
  522. if (endpoint >= NUM_ENDPOINTS) return 0;
  523. __disable_irq();
  524. for (p = tx_first[endpoint]; p; p = p->next) count++;
  525. __enable_irq();
  526. return count;
  527. }
  528. // Called from usb_free, but only when usb_rx_memory_needed > 0, indicating
  529. // receive endpoints are starving for memory. The intention is to give
  530. // endpoints needing receive memory priority over the user's code, which is
  531. // likely calling usb_malloc to obtain memory for transmitting. When the
  532. // user is creating data very quickly, their consumption could starve reception
  533. // without this prioritization. The packet buffer (input) is assigned to the
  534. // first endpoint needing memory.
  535. //
  536. void usb_rx_memory(usb_packet_t *packet)
  537. {
  538. unsigned int i;
  539. const uint8_t *cfg;
  540. cfg = usb_endpoint_config_table;
  541. //serial_print("rx_mem:");
  542. __disable_irq();
  543. for (i=1; i <= NUM_ENDPOINTS; i++) {
  544. if (*cfg++ & USB_ENDPT_EPRXEN) {
  545. if (table[index(i, RX, EVEN)].desc == 0) {
  546. table[index(i, RX, EVEN)].addr = packet->buf;
  547. table[index(i, RX, EVEN)].desc = BDT_DESC(64, 0);
  548. usb_rx_memory_needed--;
  549. __enable_irq();
  550. //serial_phex(i);
  551. //serial_print(",even\n");
  552. return;
  553. }
  554. if (table[index(i, RX, ODD)].desc == 0) {
  555. table[index(i, RX, ODD)].addr = packet->buf;
  556. table[index(i, RX, ODD)].desc = BDT_DESC(64, 1);
  557. usb_rx_memory_needed--;
  558. __enable_irq();
  559. //serial_phex(i);
  560. //serial_print(",odd\n");
  561. return;
  562. }
  563. }
  564. }
  565. __enable_irq();
  566. // we should never reach this point. If we get here, it means
  567. // usb_rx_memory_needed was set greater than zero, but no memory
  568. // was actually needed.
  569. usb_rx_memory_needed = 0;
  570. usb_free(packet);
  571. return;
  572. }
  573. //#define index(endpoint, tx, odd) (((endpoint) << 2) | ((tx) << 1) | (odd))
  574. //#define stat2bufferdescriptor(stat) (table + ((stat) >> 2))
  575. void usb_tx(uint32_t endpoint, usb_packet_t *packet)
  576. {
  577. bdt_t *b = &table[index(endpoint, TX, EVEN)];
  578. uint8_t next;
  579. endpoint--;
  580. if (endpoint >= NUM_ENDPOINTS) return;
  581. __disable_irq();
  582. //serial_print("txstate=");
  583. //serial_phex(tx_state[endpoint]);
  584. //serial_print("\n");
  585. switch (tx_state[endpoint]) {
  586. case TX_STATE_BOTH_FREE_EVEN_FIRST:
  587. next = TX_STATE_ODD_FREE;
  588. break;
  589. case TX_STATE_BOTH_FREE_ODD_FIRST:
  590. b++;
  591. next = TX_STATE_EVEN_FREE;
  592. break;
  593. case TX_STATE_EVEN_FREE:
  594. next = TX_STATE_NONE_FREE_ODD_FIRST;
  595. break;
  596. case TX_STATE_ODD_FREE:
  597. b++;
  598. next = TX_STATE_NONE_FREE_EVEN_FIRST;
  599. break;
  600. default:
  601. if (tx_first[endpoint] == NULL) {
  602. tx_first[endpoint] = packet;
  603. } else {
  604. tx_last[endpoint]->next = packet;
  605. }
  606. tx_last[endpoint] = packet;
  607. __enable_irq();
  608. return;
  609. }
  610. tx_state[endpoint] = next;
  611. b->addr = packet->buf;
  612. b->desc = BDT_DESC(packet->len, ((uint32_t)b & 8) ? DATA1 : DATA0);
  613. __enable_irq();
  614. }
  615. void _reboot_Teensyduino_(void)
  616. {
  617. // TODO: initialize R0 with a code....
  618. asm volatile("bkpt");
  619. }
  620. void usb_isr(void)
  621. {
  622. uint8_t status, stat, t;
  623. //serial_print("isr");
  624. //status = USB0_ISTAT;
  625. //serial_phex(status);
  626. //serial_print("\n");
  627. restart:
  628. status = USB0_ISTAT;
  629. if ((status & USB_INTEN_SOFTOKEN /* 04 */ )) {
  630. if (usb_configuration) {
  631. t = usb_reboot_timer;
  632. if (t) {
  633. usb_reboot_timer = --t;
  634. if (!t) _reboot_Teensyduino_();
  635. }
  636. #ifdef CDC_DATA_INTERFACE
  637. t = usb_cdc_transmit_flush_timer;
  638. if (t) {
  639. usb_cdc_transmit_flush_timer = --t;
  640. if (t == 0) usb_serial_flush_callback();
  641. }
  642. #endif
  643. #ifdef SEREMU_INTERFACE
  644. t = usb_seremu_transmit_flush_timer;
  645. if (t) {
  646. usb_seremu_transmit_flush_timer = --t;
  647. if (t == 0) usb_seremu_flush_callback();
  648. }
  649. #endif
  650. #ifdef MIDI_INTERFACE
  651. usb_midi_flush_output();
  652. #endif
  653. #ifdef FLIGHTSIM_INTERFACE
  654. usb_flightsim_flush_callback();
  655. #endif
  656. }
  657. USB0_ISTAT = USB_INTEN_SOFTOKEN;
  658. }
  659. if ((status & USB_ISTAT_TOKDNE /* 08 */ )) {
  660. uint8_t endpoint;
  661. stat = USB0_STAT;
  662. //serial_print("token: ep=");
  663. //serial_phex(stat >> 4);
  664. //serial_print(stat & 0x08 ? ",tx" : ",rx");
  665. //serial_print(stat & 0x04 ? ",odd\n" : ",even\n");
  666. endpoint = stat >> 4;
  667. if (endpoint == 0) {
  668. usb_control(stat);
  669. } else {
  670. bdt_t *b = stat2bufferdescriptor(stat);
  671. usb_packet_t *packet = (usb_packet_t *)((uint8_t *)(b->addr) - 8);
  672. #if 0
  673. serial_print("ep:");
  674. serial_phex(endpoint);
  675. serial_print(", pid:");
  676. serial_phex(BDT_PID(b->desc));
  677. serial_print(((uint32_t)b & 8) ? ", odd" : ", even");
  678. serial_print(", count:");
  679. serial_phex(b->desc >> 16);
  680. serial_print("\n");
  681. #endif
  682. endpoint--; // endpoint is index to zero-based arrays
  683. if (stat & 0x08) { // transmit
  684. usb_free(packet);
  685. packet = tx_first[endpoint];
  686. if (packet) {
  687. //serial_print("tx packet\n");
  688. tx_first[endpoint] = packet->next;
  689. b->addr = packet->buf;
  690. switch (tx_state[endpoint]) {
  691. case TX_STATE_BOTH_FREE_EVEN_FIRST:
  692. tx_state[endpoint] = TX_STATE_ODD_FREE;
  693. break;
  694. case TX_STATE_BOTH_FREE_ODD_FIRST:
  695. tx_state[endpoint] = TX_STATE_EVEN_FREE;
  696. break;
  697. case TX_STATE_EVEN_FREE:
  698. tx_state[endpoint] = TX_STATE_NONE_FREE_ODD_FIRST;
  699. break;
  700. case TX_STATE_ODD_FREE:
  701. tx_state[endpoint] = TX_STATE_NONE_FREE_EVEN_FIRST;
  702. break;
  703. default:
  704. break;
  705. }
  706. b->desc = BDT_DESC(packet->len, ((uint32_t)b & 8) ? DATA1 : DATA0);
  707. } else {
  708. //serial_print("tx no packet\n");
  709. switch (tx_state[endpoint]) {
  710. case TX_STATE_BOTH_FREE_EVEN_FIRST:
  711. case TX_STATE_BOTH_FREE_ODD_FIRST:
  712. break;
  713. case TX_STATE_EVEN_FREE:
  714. tx_state[endpoint] = TX_STATE_BOTH_FREE_EVEN_FIRST;
  715. break;
  716. case TX_STATE_ODD_FREE:
  717. tx_state[endpoint] = TX_STATE_BOTH_FREE_ODD_FIRST;
  718. break;
  719. default:
  720. tx_state[endpoint] = ((uint32_t)b & 8) ?
  721. TX_STATE_ODD_FREE : TX_STATE_EVEN_FREE;
  722. break;
  723. }
  724. }
  725. } else { // receive
  726. packet->len = b->desc >> 16;
  727. if (packet->len > 0) {
  728. packet->index = 0;
  729. packet->next = NULL;
  730. if (rx_first[endpoint] == NULL) {
  731. //serial_print("rx 1st, epidx=");
  732. //serial_phex(endpoint);
  733. //serial_print(", packet=");
  734. //serial_phex32((uint32_t)packet);
  735. //serial_print("\n");
  736. rx_first[endpoint] = packet;
  737. } else {
  738. //serial_print("rx Nth, epidx=");
  739. //serial_phex(endpoint);
  740. //serial_print(", packet=");
  741. //serial_phex32((uint32_t)packet);
  742. //serial_print("\n");
  743. rx_last[endpoint]->next = packet;
  744. }
  745. rx_last[endpoint] = packet;
  746. usb_rx_byte_count_data[endpoint] += packet->len;
  747. // TODO: implement a per-endpoint maximum # of allocated packets
  748. // so a flood of incoming data on 1 endpoint doesn't starve
  749. // the others if the user isn't reading it regularly
  750. packet = usb_malloc();
  751. if (packet) {
  752. b->addr = packet->buf;
  753. b->desc = BDT_DESC(64, ((uint32_t)b & 8) ? DATA1 : DATA0);
  754. } else {
  755. //serial_print("starving ");
  756. //serial_phex(endpoint + 1);
  757. //serial_print(((uint32_t)b & 8) ? ",odd\n" : ",even\n");
  758. b->desc = 0;
  759. usb_rx_memory_needed++;
  760. }
  761. } else {
  762. b->desc = BDT_DESC(64, ((uint32_t)b & 8) ? DATA1 : DATA0);
  763. }
  764. }
  765. }
  766. USB0_ISTAT = USB_ISTAT_TOKDNE;
  767. goto restart;
  768. }
  769. if (status & USB_ISTAT_USBRST /* 01 */ ) {
  770. //serial_print("reset\n");
  771. // initialize BDT toggle bits
  772. USB0_CTL = USB_CTL_ODDRST;
  773. ep0_tx_bdt_bank = 0;
  774. // set up buffers to receive Setup and OUT packets
  775. table[index(0, RX, EVEN)].desc = BDT_DESC(EP0_SIZE, 0);
  776. table[index(0, RX, EVEN)].addr = ep0_rx0_buf;
  777. table[index(0, RX, ODD)].desc = BDT_DESC(EP0_SIZE, 0);
  778. table[index(0, RX, ODD)].addr = ep0_rx1_buf;
  779. table[index(0, TX, EVEN)].desc = 0;
  780. table[index(0, TX, ODD)].desc = 0;
  781. // activate endpoint 0
  782. USB0_ENDPT0 = USB_ENDPT_EPRXEN | USB_ENDPT_EPTXEN | USB_ENDPT_EPHSHK;
  783. // clear all ending interrupts
  784. USB0_ERRSTAT = 0xFF;
  785. USB0_ISTAT = 0xFF;
  786. // set the address to zero during enumeration
  787. USB0_ADDR = 0;
  788. // enable other interrupts
  789. USB0_ERREN = 0xFF;
  790. USB0_INTEN = USB_INTEN_TOKDNEEN |
  791. USB_INTEN_SOFTOKEN |
  792. USB_INTEN_STALLEN |
  793. USB_INTEN_ERROREN |
  794. USB_INTEN_USBRSTEN |
  795. USB_INTEN_SLEEPEN;
  796. // is this necessary?
  797. USB0_CTL = USB_CTL_USBENSOFEN;
  798. return;
  799. }
  800. if ((status & USB_ISTAT_STALL /* 80 */ )) {
  801. //serial_print("stall:\n");
  802. USB0_ENDPT0 = USB_ENDPT_EPRXEN | USB_ENDPT_EPTXEN | USB_ENDPT_EPHSHK;
  803. USB0_ISTAT = USB_ISTAT_STALL;
  804. }
  805. if ((status & USB_ISTAT_ERROR /* 02 */ )) {
  806. uint8_t err = USB0_ERRSTAT;
  807. USB0_ERRSTAT = err;
  808. //serial_print("err:");
  809. //serial_phex(err);
  810. //serial_print("\n");
  811. USB0_ISTAT = USB_ISTAT_ERROR;
  812. }
  813. if ((status & USB_ISTAT_SLEEP /* 10 */ )) {
  814. //serial_print("sleep\n");
  815. USB0_ISTAT = USB_ISTAT_SLEEP;
  816. }
  817. }
  818. void usb_init(void)
  819. {
  820. int i;
  821. //serial_begin(BAUD2DIV(115200));
  822. //serial_print("usb_init\n");
  823. usb_init_serialnumber();
  824. for (i=0; i <= NUM_ENDPOINTS*4; i++) {
  825. table[i].desc = 0;
  826. table[i].addr = 0;
  827. }
  828. // this basically follows the flowchart in the Kinetis
  829. // Quick Reference User Guide, Rev. 1, 03/2012, page 141
  830. // assume 48 MHz clock already running
  831. // SIM - enable clock
  832. SIM_SCGC4 |= SIM_SCGC4_USBOTG;
  833. // reset USB module
  834. USB0_USBTRC0 = USB_USBTRC_USBRESET;
  835. while ((USB0_USBTRC0 & USB_USBTRC_USBRESET) != 0) ; // wait for reset to end
  836. // set desc table base addr
  837. USB0_BDTPAGE1 = ((uint32_t)table) >> 8;
  838. USB0_BDTPAGE2 = ((uint32_t)table) >> 16;
  839. USB0_BDTPAGE3 = ((uint32_t)table) >> 24;
  840. // clear all ISR flags
  841. USB0_ISTAT = 0xFF;
  842. USB0_ERRSTAT = 0xFF;
  843. USB0_OTGISTAT = 0xFF;
  844. USB0_USBTRC0 |= 0x40; // undocumented bit
  845. // enable USB
  846. USB0_CTL = USB_CTL_USBENSOFEN;
  847. USB0_USBCTRL = 0;
  848. // enable reset interrupt
  849. USB0_INTEN = USB_INTEN_USBRSTEN;
  850. // enable interrupt in NVIC...
  851. NVIC_SET_PRIORITY(IRQ_USBOTG, 112);
  852. NVIC_ENABLE_IRQ(IRQ_USBOTG);
  853. // enable d+ pullup
  854. USB0_CONTROL = USB_CONTROL_DPPULLUPNONOTG;
  855. }