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