Teensy 4.1 core updated for C++20
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

usb.c 21KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  1. /* USB Serial Example for Teensy USB Development Board
  2. * http://www.pjrc.com/teensy/usb_serial.html
  3. * Copyright (c) 2008 PJRC.COM, LLC
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a copy
  6. * of this software and associated documentation files (the "Software"), to deal
  7. * in the Software without restriction, including without limitation the rights
  8. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. * copies of the Software, and to permit persons to whom the Software is
  10. * furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in
  13. * all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. * THE SOFTWARE.
  22. */
  23. #include "usb_common.h"
  24. #include "usb_private.h"
  25. /**************************************************************************
  26. *
  27. * Endpoint Buffer Configuration
  28. *
  29. **************************************************************************/
  30. static const uint8_t PROGMEM endpoint_config_table[] = {
  31. 1, EP_TYPE_INTERRUPT_IN, EP_SIZE(DEBUG_TX_SIZE) | DEBUG_TX_BUFFER,
  32. 1, EP_TYPE_INTERRUPT_OUT, EP_SIZE(DEBUG_RX_SIZE) | DEBUG_RX_BUFFER,
  33. 1, EP_TYPE_BULK_IN, EP_SIZE(MIDI_TX_SIZE) | MIDI_TX_BUFFER,
  34. 1, EP_TYPE_BULK_OUT, EP_SIZE(MIDI_RX_SIZE) | MIDI_RX_BUFFER
  35. };
  36. /**************************************************************************
  37. *
  38. * Descriptor Data
  39. *
  40. **************************************************************************/
  41. // Descriptors are the data that your computer reads when it auto-detects
  42. // this USB device (called "enumeration" in USB lingo). The most commonly
  43. // changed items are editable at the top of this file. Changing things
  44. // in here should only be done by those who've read chapter 9 of the USB
  45. // spec and relevant portions of any USB class specifications!
  46. static const uint8_t PROGMEM device_descriptor[] = {
  47. 18, // bLength
  48. 1, // bDescriptorType
  49. 0x00, 0x02, // bcdUSB
  50. 0, // bDeviceClass
  51. 0, // bDeviceSubClass
  52. 0, // bDeviceProtocol
  53. ENDPOINT0_SIZE, // bMaxPacketSize0
  54. LSB(VENDOR_ID), MSB(VENDOR_ID), // idVendor
  55. LSB(PRODUCT_ID), MSB(PRODUCT_ID), // idProduct
  56. #if defined(__AVR_ATmega32U4__)
  57. 0x71, 0x02,
  58. #elif defined(__AVR_AT90USB1286__)
  59. 0x72, 0x02,
  60. #else
  61. 0x00, 0x01, // bcdDevice
  62. #endif
  63. 0, // iManufacturer
  64. 1, // iProduct
  65. 0, // iSerialNumber
  66. 1 // bNumConfigurations
  67. };
  68. static const uint8_t PROGMEM debug_hid_report_desc[] = {
  69. 0x06, 0xC9, 0xFF, // Usage Page 0xFFC9 (vendor defined)
  70. 0x09, 0x04, // Usage 0x04
  71. 0xA1, 0x5C, // Collection 0x5C
  72. 0x75, 0x08, // report size = 8 bits (global)
  73. 0x15, 0x00, // logical minimum = 0 (global)
  74. 0x26, 0xFF, 0x00, // logical maximum = 255 (global)
  75. 0x95, DEBUG_TX_SIZE, // report count (global)
  76. 0x09, 0x75, // usage (local)
  77. 0x81, 0x02, // Input
  78. 0x95, DEBUG_RX_SIZE, // report count (global)
  79. 0x09, 0x76, // usage (local)
  80. 0x91, 0x02, // Output
  81. 0x95, 0x04, // report count (global)
  82. 0x09, 0x76, // usage (local)
  83. 0xB1, 0x02, // Feature
  84. 0xC0 // end collection
  85. };
  86. #define CONFIG1_DESC_SIZE ( 9 + 74 + 32 )
  87. #define DEBUG_HID_DESC_OFFSET ( 9 + 74 + 9 )
  88. //#define CONFIG1_DESC_SIZE ( 9 + 92 + 32 )
  89. //#define DEBUG_HID_DESC_OFFSET ( 9 + 92 + 9 )
  90. static const uint8_t PROGMEM config1_descriptor[CONFIG1_DESC_SIZE] = {
  91. // configuration descriptor, USB spec 9.6.3, page 264-266, Table 9-10
  92. 9, // bLength;
  93. 2, // bDescriptorType;
  94. LSB(CONFIG1_DESC_SIZE), // wTotalLength
  95. MSB(CONFIG1_DESC_SIZE),
  96. NUM_INTERFACE, // bNumInterfaces
  97. 1, // bConfigurationValue
  98. 0, // iConfiguration
  99. 0xC0, // bmAttributes
  100. 50, // bMaxPower
  101. // This MIDI stuff is a copy of the example from the Audio Class
  102. // MIDI spec 1.0 (Nov 1, 1999), Appendix B, pages 37 to 43.
  103. #if 0
  104. http://www.usb.org/developers/devclass_docs/midi10.pdf
  105. Section B.3 seems to say these extra descriptors are required,
  106. but when I add them, MIDI breaks on Linux (haven't tried Mac and
  107. Windows yet). TODO: investigate these....
  108. reported by "John K." on May 7, 2012, subject "USB MIDI descriptors"
  109. // Standard AC Interface Descriptor
  110. 9, // bLength
  111. 4, // bDescriptorType = INTERFACE
  112. 0, // bInterfaceNumber
  113. 0, // bAlternateSetting
  114. 0, // bNumEndpoints
  115. 1, // bInterfaceClass = AUDIO
  116. 1, // bInterfaceSubclass = AUDIO_CONTROL
  117. 0, // bInterfaceProtocol
  118. 0, // iInterface
  119. // Class-specific AC Interface Descriptor
  120. 9, // bLength
  121. 0x24, // bDescriptorType = CS_INTERFACE
  122. 1, // bDescriptorSubtype = HEADER
  123. 0x00, 0x01, // bcdADC
  124. 0x09, 0x00, // wTotalLength
  125. 1, // bInCollection
  126. 1, // baInterfaceNr(1)
  127. #endif
  128. // Standard MS Interface Descriptor,
  129. 9, // bLength
  130. 4, // bDescriptorType
  131. MIDI_INTERFACE, // bInterfaceNumber
  132. 0, // bAlternateSetting
  133. 2, // bNumEndpoints
  134. 0x01, // bInterfaceClass (0x01 = Audio)
  135. 0x03, // bInterfaceSubClass (0x03 = MIDI)
  136. 0x00, // bInterfaceProtocol (unused for MIDI)
  137. 0, // iInterface
  138. // MIDI MS Interface Header, USB MIDI 6.1.2.1, page 21, Table 6-2
  139. 7, // bLength
  140. 0x24, // bDescriptorType = CS_INTERFACE
  141. 0x01, // bDescriptorSubtype = MS_HEADER
  142. 0x00, 0x01, // bcdMSC = revision 01.00
  143. 0x41, 0x00, // wTotalLength
  144. // MIDI IN Jack Descriptor, B.4.3, Table B-7 (embedded), page 40
  145. 6, // bLength
  146. 0x24, // bDescriptorType = CS_INTERFACE
  147. 0x02, // bDescriptorSubtype = MIDI_IN_JACK
  148. 0x01, // bJackType = EMBEDDED
  149. 1, // bJackID, ID = 1
  150. 0, // iJack
  151. // MIDI IN Jack Descriptor, B.4.3, Table B-8 (external), page 40
  152. 6, // bLength
  153. 0x24, // bDescriptorType = CS_INTERFACE
  154. 0x02, // bDescriptorSubtype = MIDI_IN_JACK
  155. 0x02, // bJackType = EXTERNAL
  156. 2, // bJackID, ID = 2
  157. 0, // iJack
  158. // MIDI OUT Jack Descriptor, B.4.4, Table B-9, page 41
  159. 9,
  160. 0x24, // bDescriptorType = CS_INTERFACE
  161. 0x03, // bDescriptorSubtype = MIDI_OUT_JACK
  162. 0x01, // bJackType = EMBEDDED
  163. 3, // bJackID, ID = 3
  164. 1, // bNrInputPins = 1 pin
  165. 2, // BaSourceID(1) = 2
  166. 1, // BaSourcePin(1) = first pin
  167. 0, // iJack
  168. // MIDI OUT Jack Descriptor, B.4.4, Table B-10, page 41
  169. 9,
  170. 0x24, // bDescriptorType = CS_INTERFACE
  171. 0x03, // bDescriptorSubtype = MIDI_OUT_JACK
  172. 0x02, // bJackType = EXTERNAL
  173. 4, // bJackID, ID = 4
  174. 1, // bNrInputPins = 1 pin
  175. 1, // BaSourceID(1) = 1
  176. 1, // BaSourcePin(1) = first pin
  177. 0, // iJack
  178. // Standard Bulk OUT Endpoint Descriptor, B.5.1, Table B-11, pae 42
  179. 9, // bLength
  180. 5, // bDescriptorType = ENDPOINT
  181. MIDI_RX_ENDPOINT, // bEndpointAddress
  182. 0x02, // bmAttributes (0x02=bulk)
  183. MIDI_RX_SIZE, 0, // wMaxPacketSize
  184. 0, // bInterval
  185. 0, // bRefresh
  186. 0, // bSynchAddress
  187. // Class-specific MS Bulk OUT Endpoint Descriptor, B.5.2, Table B-12, page 42
  188. 5, // bLength
  189. 0x25, // bDescriptorSubtype = CS_ENDPOINT
  190. 0x01, // bJackType = MS_GENERAL
  191. 1, // bNumEmbMIDIJack = 1 jack
  192. 1, // BaAssocJackID(1) = jack ID #1
  193. // Standard Bulk IN Endpoint Descriptor, B.5.1, Table B-11, pae 42
  194. 9, // bLength
  195. 5, // bDescriptorType = ENDPOINT
  196. MIDI_TX_ENDPOINT | 0x80, // bEndpointAddress
  197. 0x02, // bmAttributes (0x02=bulk)
  198. MIDI_TX_SIZE, 0, // wMaxPacketSize
  199. 0, // bInterval
  200. 0, // bRefresh
  201. 0, // bSynchAddress
  202. // Class-specific MS Bulk IN Endpoint Descriptor, B.5.2, Table B-12, page 42
  203. 5, // bLength
  204. 0x25, // bDescriptorSubtype = CS_ENDPOINT
  205. 0x01, // bJackType = MS_GENERAL
  206. 1, // bNumEmbMIDIJack = 1 jack
  207. 3, // BaAssocJackID(1) = jack ID #3
  208. // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
  209. 9, // bLength
  210. 4, // bDescriptorType
  211. DEBUG_INTERFACE, // bInterfaceNumber
  212. 0, // bAlternateSetting
  213. 2, // bNumEndpoints
  214. 0x03, // bInterfaceClass (0x03 = HID)
  215. 0x00, // bInterfaceSubClass
  216. 0x00, // bInterfaceProtocol
  217. 0, // iInterface
  218. // HID interface descriptor, HID 1.11 spec, section 6.2.1
  219. 9, // bLength
  220. 0x21, // bDescriptorType
  221. 0x11, 0x01, // bcdHID
  222. 0, // bCountryCode
  223. 1, // bNumDescriptors
  224. 0x22, // bDescriptorType
  225. sizeof(debug_hid_report_desc), // wDescriptorLength
  226. 0,
  227. // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
  228. 7, // bLength
  229. 5, // bDescriptorType
  230. DEBUG_TX_ENDPOINT | 0x80, // bEndpointAddress
  231. 0x03, // bmAttributes (0x03=intr)
  232. DEBUG_TX_SIZE, 0, // wMaxPacketSize
  233. DEBUG_TX_INTERVAL, // bInterval
  234. // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
  235. 7, // bLength
  236. 5, // bDescriptorType
  237. DEBUG_RX_ENDPOINT, // bEndpointAddress
  238. 0x03, // bmAttributes (0x03=intr)
  239. DEBUG_RX_SIZE, 0, // wMaxPacketSize
  240. DEBUG_RX_INTERVAL, // bInterval
  241. };
  242. // If you're desperate for a little extra code memory, these strings
  243. // can be completely removed if iManufacturer, iProduct, iSerialNumber
  244. // in the device desciptor are changed to zeros.
  245. struct usb_string_descriptor_struct {
  246. uint8_t bLength;
  247. uint8_t bDescriptorType;
  248. int16_t wString[];
  249. };
  250. static const struct usb_string_descriptor_struct PROGMEM string0 = {
  251. 4,
  252. 3,
  253. {0x0409}
  254. };
  255. static const struct usb_string_descriptor_struct PROGMEM string1 = {
  256. sizeof(STR_PRODUCT),
  257. 3,
  258. STR_PRODUCT
  259. };
  260. // This table defines which descriptor data is sent for each specific
  261. // request from the host (in wValue and wIndex).
  262. static const struct descriptor_list_struct {
  263. uint16_t wValue;
  264. uint16_t wIndex;
  265. const uint8_t *addr;
  266. uint8_t length;
  267. } PROGMEM descriptor_list[] = {
  268. {0x0100, 0x0000, device_descriptor, sizeof(device_descriptor)},
  269. {0x0200, 0x0000, config1_descriptor, sizeof(config1_descriptor)},
  270. {0x2200, DEBUG_INTERFACE, debug_hid_report_desc, sizeof(debug_hid_report_desc)},
  271. {0x2100, DEBUG_INTERFACE, config1_descriptor+DEBUG_HID_DESC_OFFSET, 9},
  272. {0x0300, 0x0000, (const uint8_t *)&string0, 4},
  273. {0x0301, 0x0409, (const uint8_t *)&string1, sizeof(STR_PRODUCT)},
  274. };
  275. #define NUM_DESC_LIST (sizeof(descriptor_list)/sizeof(struct descriptor_list_struct))
  276. /**************************************************************************
  277. *
  278. * Variables - these are the only non-stack RAM usage
  279. *
  280. **************************************************************************/
  281. // zero when we are not configured, non-zero when enumerated
  282. volatile uint8_t usb_configuration USBSTATE;
  283. volatile uint8_t usb_suspended USBSTATE;
  284. // the time remaining before we transmit any partially full
  285. // packet, or send a zero length packet.
  286. volatile uint8_t debug_flush_timer USBSTATE;
  287. /**************************************************************************
  288. *
  289. * Public Functions - these are the API intended for the user
  290. *
  291. **************************************************************************/
  292. // initialize USB serial
  293. void usb_init(void)
  294. {
  295. uint8_t u;
  296. u = USBCON;
  297. if ((u & (1<<USBE)) && !(u & (1<<FRZCLK))) return;
  298. HW_CONFIG();
  299. USB_FREEZE(); // enable USB
  300. PLL_CONFIG(); // config PLL
  301. while (!(PLLCSR & (1<<PLOCK))) ; // wait for PLL lock
  302. USB_CONFIG(); // start USB clock
  303. UDCON = 0; // enable attach resistor
  304. usb_configuration = 0;
  305. usb_suspended = 0;
  306. debug_flush_timer = 0;
  307. UDINT = 0;
  308. UDIEN = (1<<EORSTE)|(1<<SOFE);
  309. //sei(); // init() in wiring.c does this
  310. }
  311. void usb_shutdown(void)
  312. {
  313. UDIEN = 0; // disable interrupts
  314. UDCON = 1; // disconnect attach resistor
  315. USBCON = 0; // shut off USB periperal
  316. PLLCSR = 0; // shut off PLL
  317. usb_configuration = 0;
  318. usb_suspended = 1;
  319. }
  320. // Public API functions moved to usb_api.cpp
  321. /**************************************************************************
  322. *
  323. * Private Functions - not intended for general user consumption....
  324. *
  325. **************************************************************************/
  326. // USB Device Interrupt - handle all device-level events
  327. // the transmit buffer flushing is triggered by the start of frame
  328. //
  329. ISR(USB_GEN_vect)
  330. {
  331. uint8_t intbits, t;
  332. intbits = UDINT;
  333. UDINT = 0;
  334. if (intbits & (1<<EORSTI)) {
  335. UENUM = 0;
  336. UECONX = 1;
  337. UECFG0X = EP_TYPE_CONTROL;
  338. UECFG1X = EP_SIZE(ENDPOINT0_SIZE) | EP_SINGLE_BUFFER;
  339. UEIENX = (1<<RXSTPE);
  340. usb_configuration = 0;
  341. }
  342. if ((intbits & (1<<SOFI)) && usb_configuration) {
  343. t = debug_flush_timer;
  344. if (t) {
  345. debug_flush_timer = --t;
  346. if (!t) {
  347. UENUM = DEBUG_TX_ENDPOINT;
  348. while ((UEINTX & (1<<RWAL))) {
  349. UEDATX = 0;
  350. }
  351. UEINTX = 0x3A;
  352. }
  353. }
  354. UENUM = MIDI_TX_ENDPOINT;
  355. if (UEBCLX) UEINTX = 0x3A;
  356. }
  357. if (intbits & (1<<SUSPI)) {
  358. // USB Suspend (inactivity for 3ms)
  359. UDIEN = (1<<WAKEUPE);
  360. usb_configuration = 0;
  361. usb_suspended = 1;
  362. #if (F_CPU >= 8000000L)
  363. // WAKEUPI does not work with USB clock freeze
  364. // when CPU is running less than 8 MHz.
  365. // Is this a hardware bug?
  366. USB_FREEZE(); // shut off USB
  367. PLLCSR = 0; // shut off PLL
  368. #endif
  369. // to properly meet the USB spec, current must
  370. // reduce to less than 2.5 mA, which means using
  371. // powerdown mode, but that breaks the Arduino
  372. // user's paradigm....
  373. }
  374. if (usb_suspended && (intbits & (1<<WAKEUPI))) {
  375. // USB Resume (pretty much any activity)
  376. #if (F_CPU >= 8000000L)
  377. PLL_CONFIG();
  378. while (!(PLLCSR & (1<<PLOCK))) ;
  379. USB_CONFIG();
  380. #endif
  381. UDIEN = (1<<EORSTE)|(1<<SOFE)|(1<<SUSPE);
  382. usb_suspended = 0;
  383. return;
  384. }
  385. }
  386. // Misc functions to wait for ready and send/receive packets
  387. static inline void usb_wait_in_ready(void)
  388. {
  389. while (!(UEINTX & (1<<TXINI))) ;
  390. }
  391. static inline void usb_send_in(void)
  392. {
  393. UEINTX = ~(1<<TXINI);
  394. }
  395. static inline void usb_wait_receive_out(void)
  396. {
  397. while (!(UEINTX & (1<<RXOUTI))) ;
  398. }
  399. static inline void usb_ack_out(void)
  400. {
  401. UEINTX = ~(1<<RXOUTI);
  402. }
  403. // USB Endpoint Interrupt - endpoint 0 is handled here. The
  404. // other endpoints are manipulated by the user-callable
  405. // functions, and the start-of-frame interrupt.
  406. //
  407. ISR(USB_COM_vect)
  408. {
  409. uint8_t intbits;
  410. const uint8_t *list;
  411. const uint8_t *cfg;
  412. uint8_t i, n, len, en;
  413. uint8_t bmRequestType;
  414. uint8_t bRequest;
  415. uint16_t wValue;
  416. uint16_t wIndex;
  417. uint16_t wLength;
  418. uint16_t desc_val;
  419. const uint8_t *desc_addr;
  420. uint8_t desc_length;
  421. UENUM = 0;
  422. intbits = UEINTX;
  423. if (intbits & (1<<RXSTPI)) {
  424. bmRequestType = UEDATX;
  425. bRequest = UEDATX;
  426. read_word_lsbfirst(wValue, UEDATX);
  427. read_word_lsbfirst(wIndex, UEDATX);
  428. read_word_lsbfirst(wLength, UEDATX);
  429. UEINTX = ~((1<<RXSTPI) | (1<<RXOUTI) | (1<<TXINI));
  430. if (bRequest == GET_DESCRIPTOR) {
  431. list = (const uint8_t *)descriptor_list;
  432. for (i=0; ; i++) {
  433. if (i >= NUM_DESC_LIST) {
  434. UECONX = (1<<STALLRQ)|(1<<EPEN); //stall
  435. return;
  436. }
  437. pgm_read_word_postinc(desc_val, list);
  438. if (desc_val != wValue) {
  439. list += sizeof(struct descriptor_list_struct)-2;
  440. continue;
  441. }
  442. pgm_read_word_postinc(desc_val, list);
  443. if (desc_val != wIndex) {
  444. list += sizeof(struct descriptor_list_struct)-4;
  445. continue;
  446. }
  447. pgm_read_word_postinc(desc_addr, list);
  448. desc_length = pgm_read_byte(list);
  449. break;
  450. }
  451. len = (wLength < 256) ? wLength : 255;
  452. if (len > desc_length) len = desc_length;
  453. list = desc_addr;
  454. do {
  455. // wait for host ready for IN packet
  456. do {
  457. i = UEINTX;
  458. } while (!(i & ((1<<TXINI)|(1<<RXOUTI))));
  459. if (i & (1<<RXOUTI)) return; // abort
  460. // send IN packet
  461. n = len < ENDPOINT0_SIZE ? len : ENDPOINT0_SIZE;
  462. for (i = n; i; i--) {
  463. pgm_read_byte_postinc(UEDATX, list);
  464. }
  465. len -= n;
  466. usb_send_in();
  467. } while (len || n == ENDPOINT0_SIZE);
  468. return;
  469. }
  470. if (bRequest == SET_ADDRESS) {
  471. usb_send_in();
  472. usb_wait_in_ready();
  473. UDADDR = wValue | (1<<ADDEN);
  474. return;
  475. }
  476. if (bRequest == SET_CONFIGURATION && bmRequestType == 0) {
  477. usb_configuration = wValue;
  478. debug_flush_timer = 0;
  479. usb_send_in();
  480. cfg = endpoint_config_table;
  481. for (i=1; i<NUM_ENDPOINTS; i++) {
  482. UENUM = i;
  483. pgm_read_byte_postinc(en, cfg);
  484. UECONX = en;
  485. if (en) {
  486. pgm_read_byte_postinc(UECFG0X, cfg);
  487. pgm_read_byte_postinc(UECFG1X, cfg);
  488. }
  489. }
  490. UERST = 0x1E;
  491. UERST = 0;
  492. return;
  493. }
  494. if (bRequest == GET_CONFIGURATION && bmRequestType == 0x80) {
  495. usb_wait_in_ready();
  496. UEDATX = usb_configuration;
  497. usb_send_in();
  498. return;
  499. }
  500. if (bRequest == GET_STATUS) {
  501. usb_wait_in_ready();
  502. i = 0;
  503. if (bmRequestType == 0x82) {
  504. UENUM = wIndex;
  505. if (UECONX & (1<<STALLRQ)) i = 1;
  506. UENUM = 0;
  507. }
  508. UEDATX = i;
  509. UEDATX = 0;
  510. usb_send_in();
  511. return;
  512. }
  513. if ((bRequest == CLEAR_FEATURE || bRequest == SET_FEATURE)
  514. && bmRequestType == 0x02 && wValue == 0) {
  515. i = wIndex & 0x7F;
  516. if (i >= 1 && i <= MAX_ENDPOINT) {
  517. usb_send_in();
  518. UENUM = i;
  519. if (bRequest == SET_FEATURE) {
  520. UECONX = (1<<STALLRQ)|(1<<EPEN);
  521. } else {
  522. UECONX = (1<<STALLRQC)|(1<<RSTDT)|(1<<EPEN);
  523. UERST = (1 << i);
  524. UERST = 0;
  525. }
  526. return;
  527. }
  528. }
  529. if (wIndex == DEBUG_INTERFACE) {
  530. if (bRequest == HID_GET_REPORT && bmRequestType == 0xA1) {
  531. len = wLength;
  532. do {
  533. // wait for host ready for IN packet
  534. do {
  535. i = UEINTX;
  536. } while (!(i & ((1<<TXINI)|(1<<RXOUTI))));
  537. if (i & (1<<RXOUTI)) return; // abort
  538. // send IN packet
  539. n = len < ENDPOINT0_SIZE ? len : ENDPOINT0_SIZE;
  540. for (i = n; i; i--) {
  541. UEDATX = 0;
  542. }
  543. len -= n;
  544. usb_send_in();
  545. } while (len || n == ENDPOINT0_SIZE);
  546. return;
  547. }
  548. if (bRequest == HID_SET_REPORT && bmRequestType == 0x21) {
  549. if (wValue == 0x0300 && wLength == 0x0004) {
  550. uint8_t b1, b2, b3, b4;
  551. usb_wait_receive_out();
  552. b1 = UEDATX;
  553. b2 = UEDATX;
  554. b3 = UEDATX;
  555. b4 = UEDATX;
  556. usb_ack_out();
  557. usb_send_in();
  558. if (b1 == 0xA9 && b2 == 0x45 && b3 == 0xC2 && b4 == 0x6B)
  559. _reboot_Teensyduino_();
  560. if (b1 == 0x8B && b2 == 0xC5 && b3 == 0x1D && b4 == 0x70)
  561. _restart_Teensyduino_();
  562. }
  563. }
  564. }
  565. if (bRequest == 0xC9 && bmRequestType == 0x40) {
  566. usb_send_in();
  567. usb_wait_in_ready();
  568. _restart_Teensyduino_();
  569. }
  570. }
  571. UECONX = (1<<STALLRQ) | (1<<EPEN); // stall
  572. }