Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

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