You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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