您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

1009 行
26KB

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