Teensy 4.1 core updated for C++20
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

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