Kiibohd Controller
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ů.
Tento repozitář je archivovaný. Můžete prohlížet soubory, klonovat, ale nemůžete nahrávat a vytvářet nové úkoly a požadavky na natažení.

usb_dev.c 24KB

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