Kiibohd Controller
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
Tento repozitář je archivovaný. Můžete prohlížet soubory, klonovat, ale nemůžete nahrávat a vytvářet nové úkoly a požadavky na natažení.

usb_keyboard_debug.c 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726
  1. /* USB Keyboard Plus Debug Channel Example for Teensy USB Development Board
  2. * http://www.pjrc.com/teensy/usb_keyboard.html
  3. * Copyright (c) 2009 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. // Version 1.0: Initial Release
  24. // Version 1.1: Add support for Teensy 2.0
  25. #define USB_SERIAL_PRIVATE_INCLUDE
  26. #include "usb_keyboard_debug.h"
  27. /**************************************************************************
  28. *
  29. * Configurable Options
  30. *
  31. **************************************************************************/
  32. // USB devices are supposed to implment a halt feature, which is
  33. // rarely (if ever) used. If you comment this line out, the halt
  34. // code will be removed, saving 102 bytes of space (gcc 4.3.0).
  35. // This is not strictly USB compliant, but works with all major
  36. // operating systems.
  37. #define SUPPORT_ENDPOINT_HALT
  38. /**************************************************************************
  39. *
  40. * Endpoint Buffer Configuration
  41. *
  42. **************************************************************************/
  43. #define ENDPOINT0_SIZE 32
  44. #define KEYBOARD_INTERFACE 0
  45. #define KEYBOARD_ENDPOINT 3
  46. #define KEYBOARD_SIZE 8
  47. #define KEYBOARD_BUFFER EP_DOUBLE_BUFFER
  48. #define DEBUG_INTERFACE 1
  49. #define DEBUG_TX_ENDPOINT 4
  50. #define DEBUG_TX_SIZE 32
  51. #define DEBUG_TX_BUFFER EP_DOUBLE_BUFFER
  52. static const uint8_t PROGMEM endpoint_config_table[] = {
  53. 0,
  54. 0,
  55. 1, EP_TYPE_INTERRUPT_IN, EP_SIZE(KEYBOARD_SIZE) | KEYBOARD_BUFFER,
  56. 1, EP_TYPE_INTERRUPT_IN, EP_SIZE(DEBUG_TX_SIZE) | DEBUG_TX_BUFFER
  57. };
  58. /**************************************************************************
  59. *
  60. * Descriptor Data
  61. *
  62. **************************************************************************/
  63. // Descriptors are the data that your computer reads when it auto-detects
  64. // this USB device (called "enumeration" in USB lingo). The most commonly
  65. // changed items are editable at the top of this file. Changing things
  66. // in here should only be done by those who've read chapter 9 of the USB
  67. // spec and relevant portions of any USB class specifications!
  68. static const uint8_t PROGMEM device_descriptor[] = {
  69. 18, // bLength
  70. 1, // bDescriptorType
  71. 0x00, 0x02, // bcdUSB
  72. 0, // bDeviceClass
  73. 0, // bDeviceSubClass
  74. 0, // bDeviceProtocol
  75. ENDPOINT0_SIZE, // bMaxPacketSize0
  76. LSB(VENDOR_ID), MSB(VENDOR_ID), // idVendor
  77. LSB(PRODUCT_ID), MSB(PRODUCT_ID), // idProduct
  78. 0x00, 0x01, // bcdDevice
  79. 1, // iManufacturer
  80. 2, // iProduct
  81. 0, // iSerialNumber
  82. 1 // bNumConfigurations
  83. };
  84. // Keyboard Protocol 1, HID 1.11 spec, Appendix B, page 59-60
  85. static const uint8_t PROGMEM keyboard_hid_report_desc[] = {
  86. 0x05, 0x01, // Usage Page (Generic Desktop),
  87. 0x09, 0x06, // Usage (Keyboard),
  88. 0xA1, 0x01, // Collection (Application),
  89. 0x75, 0x01, // Report Size (1),
  90. 0x95, 0x08, // Report Count (8),
  91. 0x05, 0x07, // Usage Page (Key Codes),
  92. 0x19, 0xE0, // Usage Minimum (224),
  93. 0x29, 0xE7, // Usage Maximum (231),
  94. 0x15, 0x00, // Logical Minimum (0),
  95. 0x25, 0x01, // Logical Maximum (1),
  96. 0x81, 0x02, // Input (Data, Variable, Absolute), ;Modifier byte
  97. 0x95, 0x01, // Report Count (1),
  98. 0x75, 0x08, // Report Size (8),
  99. 0x81, 0x03, // Input (Constant), ;Reserved byte
  100. 0x95, 0x05, // Report Count (5),
  101. 0x75, 0x01, // Report Size (1),
  102. 0x05, 0x08, // Usage Page (LEDs),
  103. 0x19, 0x01, // Usage Minimum (1),
  104. 0x29, 0x05, // Usage Maximum (5),
  105. 0x91, 0x02, // Output (Data, Variable, Absolute), ;LED report
  106. 0x95, 0x01, // Report Count (1),
  107. 0x75, 0x03, // Report Size (3),
  108. 0x91, 0x03, // Output (Constant), ;LED report padding
  109. 0x95, 0x06, // Report Count (6),
  110. 0x75, 0x08, // Report Size (8),
  111. 0x15, 0x00, // Logical Minimum (0),
  112. 0x25, 0x68, // Logical Maximum(104),
  113. 0x05, 0x07, // Usage Page (Key Codes),
  114. 0x19, 0x00, // Usage Minimum (0),
  115. 0x29, 0x68, // Usage Maximum (104),
  116. 0x81, 0x00, // Input (Data, Array),
  117. 0xc0 // End Collection
  118. };
  119. static const uint8_t PROGMEM debug_hid_report_desc[] = {
  120. 0x06, 0x31, 0xFF, // Usage Page 0xFF31 (vendor defined)
  121. 0x09, 0x74, // Usage 0x74
  122. 0xA1, 0x53, // Collection 0x53
  123. 0x75, 0x08, // report size = 8 bits
  124. 0x15, 0x00, // logical minimum = 0
  125. 0x26, 0xFF, 0x00, // logical maximum = 255
  126. 0x95, DEBUG_TX_SIZE, // report count
  127. 0x09, 0x75, // usage
  128. 0x81, 0x02, // Input (array)
  129. 0xC0 // end collection
  130. };
  131. #define CONFIG1_DESC_SIZE (9+9+9+7+9+9+7)
  132. #define KEYBOARD_HID_DESC_OFFSET (9+9)
  133. #define DEBUG_HID_DESC_OFFSET (9+9+9+7+9)
  134. static const uint8_t PROGMEM config1_descriptor[CONFIG1_DESC_SIZE] = {
  135. // configuration descriptor, USB spec 9.6.3, page 264-266, Table 9-10
  136. 9, // bLength;
  137. 2, // bDescriptorType;
  138. LSB(CONFIG1_DESC_SIZE), // wTotalLength
  139. MSB(CONFIG1_DESC_SIZE),
  140. 2, // bNumInterfaces
  141. 1, // bConfigurationValue
  142. 0, // iConfiguration
  143. 0xC0, // bmAttributes
  144. 50, // bMaxPower
  145. // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
  146. 9, // bLength
  147. 4, // bDescriptorType
  148. KEYBOARD_INTERFACE, // bInterfaceNumber
  149. 0, // bAlternateSetting
  150. 1, // bNumEndpoints
  151. 0x03, // bInterfaceClass (0x03 = HID)
  152. 0x01, // bInterfaceSubClass (0x01 = Boot)
  153. 0x01, // bInterfaceProtocol (0x01 = Keyboard)
  154. 0, // iInterface
  155. // HID interface descriptor, HID 1.11 spec, section 6.2.1
  156. 9, // bLength
  157. 0x21, // bDescriptorType
  158. 0x11, 0x01, // bcdHID
  159. 0, // bCountryCode
  160. 1, // bNumDescriptors
  161. 0x22, // bDescriptorType
  162. sizeof(keyboard_hid_report_desc), // wDescriptorLength
  163. 0,
  164. // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
  165. 7, // bLength
  166. 5, // bDescriptorType
  167. KEYBOARD_ENDPOINT | 0x80, // bEndpointAddress
  168. 0x03, // bmAttributes (0x03=intr)
  169. KEYBOARD_SIZE, 0, // wMaxPacketSize
  170. 1, // bInterval
  171. // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
  172. 9, // bLength
  173. 4, // bDescriptorType
  174. DEBUG_INTERFACE, // bInterfaceNumber
  175. 0, // bAlternateSetting
  176. 1, // bNumEndpoints
  177. 0x03, // bInterfaceClass (0x03 = HID)
  178. 0x00, // bInterfaceSubClass
  179. 0x00, // bInterfaceProtocol
  180. 0, // iInterface
  181. // HID interface descriptor, HID 1.11 spec, section 6.2.1
  182. 9, // bLength
  183. 0x21, // bDescriptorType
  184. 0x11, 0x01, // bcdHID
  185. 0, // bCountryCode
  186. 1, // bNumDescriptors
  187. 0x22, // bDescriptorType
  188. sizeof(debug_hid_report_desc), // wDescriptorLength
  189. 0,
  190. // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
  191. 7, // bLength
  192. 5, // bDescriptorType
  193. DEBUG_TX_ENDPOINT | 0x80, // bEndpointAddress
  194. 0x03, // bmAttributes (0x03=intr)
  195. DEBUG_TX_SIZE, 0, // wMaxPacketSize
  196. 1 // bInterval
  197. };
  198. // If you're desperate for a little extra code memory, these strings
  199. // can be completely removed if iManufacturer, iProduct, iSerialNumber
  200. // in the device desciptor are changed to zeros.
  201. struct usb_string_descriptor_struct {
  202. uint8_t bLength;
  203. uint8_t bDescriptorType;
  204. int16_t wString[];
  205. };
  206. static const struct usb_string_descriptor_struct PROGMEM string0 = {
  207. 4,
  208. 3,
  209. {0x0409}
  210. };
  211. static const struct usb_string_descriptor_struct PROGMEM string1 = {
  212. sizeof(STR_MANUFACTURER),
  213. 3,
  214. STR_MANUFACTURER
  215. };
  216. static const struct usb_string_descriptor_struct PROGMEM string2 = {
  217. sizeof(STR_PRODUCT),
  218. 3,
  219. STR_PRODUCT
  220. };
  221. // This table defines which descriptor data is sent for each specific
  222. // request from the host (in wValue and wIndex).
  223. static const struct descriptor_list_struct {
  224. uint16_t wValue;
  225. uint16_t wIndex;
  226. const uint8_t *addr;
  227. uint8_t length;
  228. } PROGMEM descriptor_list[] = {
  229. {0x0100, 0x0000, device_descriptor, sizeof(device_descriptor)},
  230. {0x0200, 0x0000, config1_descriptor, sizeof(config1_descriptor)},
  231. {0x2200, KEYBOARD_INTERFACE, keyboard_hid_report_desc, sizeof(keyboard_hid_report_desc)},
  232. {0x2100, KEYBOARD_INTERFACE, config1_descriptor+KEYBOARD_HID_DESC_OFFSET, 9},
  233. {0x2200, DEBUG_INTERFACE, debug_hid_report_desc, sizeof(debug_hid_report_desc)},
  234. {0x2100, DEBUG_INTERFACE, config1_descriptor+DEBUG_HID_DESC_OFFSET, 9},
  235. {0x0300, 0x0000, (const uint8_t *)&string0, 4},
  236. {0x0301, 0x0409, (const uint8_t *)&string1, sizeof(STR_MANUFACTURER)},
  237. {0x0302, 0x0409, (const uint8_t *)&string2, sizeof(STR_PRODUCT)}
  238. };
  239. #define NUM_DESC_LIST (sizeof(descriptor_list)/sizeof(struct descriptor_list_struct))
  240. /**************************************************************************
  241. *
  242. * Variables - these are the only non-stack RAM usage
  243. *
  244. **************************************************************************/
  245. // zero when we are not configured, non-zero when enumerated
  246. static volatile uint8_t usb_configuration=0;
  247. // the time remaining before we transmit any partially full
  248. // packet, or send a zero length packet.
  249. static volatile uint8_t debug_flush_timer=0;
  250. // protocol setting from the host. We use exactly the same report
  251. // either way, so this variable only stores the setting since we
  252. // are required to be able to report which setting is in use.
  253. static uint8_t keyboard_protocol=1;
  254. // the idle configuration, how often we send the report to the
  255. // host (ms * 4) even when it hasn't changed
  256. static uint8_t keyboard_idle_config=125;
  257. // count until idle timeout
  258. static uint8_t keyboard_idle_count=0;
  259. /**************************************************************************
  260. *
  261. * Public Functions - these are the API intended for the user
  262. *
  263. **************************************************************************/
  264. // initialize USB
  265. void usb_init(void)
  266. {
  267. HW_CONFIG();
  268. USB_FREEZE(); // enable USB
  269. PLL_CONFIG(); // config PLL
  270. while (!(PLLCSR & (1<<PLOCK))) ; // wait for PLL lock
  271. USB_CONFIG(); // start USB clock
  272. UDCON = 0; // enable attach resistor
  273. usb_configuration = 0;
  274. UDIEN = (1<<EORSTE)|(1<<SOFE);
  275. sei();
  276. }
  277. // return 0 if the USB is not configured, or the configuration
  278. // number selected by the HOST
  279. uint8_t usb_configured(void)
  280. {
  281. return usb_configuration;
  282. }
  283. // perform a single keystroke
  284. int8_t usb_keyboard_press(uint8_t key, uint8_t modifier)
  285. {
  286. int8_t r;
  287. USBKeys_Modifiers = modifier;
  288. USBKeys_Array[0] = key;
  289. r = usb_keyboard_send();
  290. if (r) return r;
  291. USBKeys_Modifiers = 0;
  292. USBKeys_Array[0] = 0;
  293. return usb_keyboard_send();
  294. }
  295. // send the contents of USBKeys_Array and USBKeys_Modifiers
  296. int8_t usb_keyboard_send(void)
  297. {
  298. uint8_t i, intr_state, timeout;
  299. if (!usb_configuration) return -1;
  300. intr_state = SREG;
  301. cli();
  302. UENUM = KEYBOARD_ENDPOINT;
  303. timeout = UDFNUML + 50;
  304. while (1) {
  305. // are we ready to transmit?
  306. if (UEINTX & (1<<RWAL)) break;
  307. SREG = intr_state;
  308. // has the USB gone offline?
  309. if (!usb_configuration) return -1;
  310. // have we waited too long?
  311. if (UDFNUML == timeout) return -1;
  312. // get ready to try checking again
  313. intr_state = SREG;
  314. cli();
  315. UENUM = KEYBOARD_ENDPOINT;
  316. }
  317. UEDATX = USBKeys_Modifiers;
  318. UEDATX = 0;
  319. for (i=0; i<6; i++) {
  320. UEDATX = USBKeys_Array[i];
  321. }
  322. UEINTX = 0x3A;
  323. keyboard_idle_count = 0;
  324. SREG = intr_state;
  325. return 0;
  326. }
  327. // transmit a character. 0 returned on success, -1 on error
  328. int8_t usb_debug_putchar(uint8_t c)
  329. {
  330. static uint8_t previous_timeout=0;
  331. uint8_t timeout, intr_state;
  332. // if we're not online (enumerated and configured), error
  333. if (!usb_configuration) return -1;
  334. // interrupts are disabled so these functions can be
  335. // used from the main program or interrupt context,
  336. // even both in the same program!
  337. intr_state = SREG;
  338. cli();
  339. UENUM = DEBUG_TX_ENDPOINT;
  340. // if we gave up due to timeout before, don't wait again
  341. if (previous_timeout) {
  342. if (!(UEINTX & (1<<RWAL))) {
  343. SREG = intr_state;
  344. return -1;
  345. }
  346. previous_timeout = 0;
  347. }
  348. // wait for the FIFO to be ready to accept data
  349. timeout = UDFNUML + 4;
  350. while (1) {
  351. // are we ready to transmit?
  352. if (UEINTX & (1<<RWAL)) break;
  353. SREG = intr_state;
  354. // have we waited too long?
  355. if (UDFNUML == timeout) {
  356. previous_timeout = 1;
  357. return -1;
  358. }
  359. // has the USB gone offline?
  360. if (!usb_configuration) return -1;
  361. // get ready to try checking again
  362. intr_state = SREG;
  363. cli();
  364. UENUM = DEBUG_TX_ENDPOINT;
  365. }
  366. // actually write the byte into the FIFO
  367. UEDATX = c;
  368. // if this completed a packet, transmit it now!
  369. if (!(UEINTX & (1<<RWAL))) {
  370. UEINTX = 0x3A;
  371. debug_flush_timer = 0;
  372. } else {
  373. debug_flush_timer = 2;
  374. }
  375. SREG = intr_state;
  376. return 0;
  377. }
  378. // immediately transmit any buffered output.
  379. void usb_debug_flush_output(void)
  380. {
  381. uint8_t intr_state;
  382. intr_state = SREG;
  383. cli();
  384. if (debug_flush_timer) {
  385. UENUM = DEBUG_TX_ENDPOINT;
  386. while ((UEINTX & (1<<RWAL))) {
  387. UEDATX = 0;
  388. }
  389. UEINTX = 0x3A;
  390. debug_flush_timer = 0;
  391. }
  392. SREG = intr_state;
  393. }
  394. /**************************************************************************
  395. *
  396. * Private Functions - not intended for general user consumption....
  397. *
  398. **************************************************************************/
  399. // USB Device Interrupt - handle all device-level events
  400. // the transmit buffer flushing is triggered by the start of frame
  401. //
  402. ISR(USB_GEN_vect)
  403. {
  404. uint8_t intbits, t, i;
  405. static uint8_t div4=0;
  406. intbits = UDINT;
  407. UDINT = 0;
  408. if (intbits & (1<<EORSTI)) {
  409. UENUM = 0;
  410. UECONX = 1;
  411. UECFG0X = EP_TYPE_CONTROL;
  412. UECFG1X = EP_SIZE(ENDPOINT0_SIZE) | EP_SINGLE_BUFFER;
  413. UEIENX = (1<<RXSTPE);
  414. usb_configuration = 0;
  415. }
  416. if ((intbits & (1<<SOFI)) && usb_configuration) {
  417. t = debug_flush_timer;
  418. if (t) {
  419. debug_flush_timer = -- t;
  420. if (!t) {
  421. UENUM = DEBUG_TX_ENDPOINT;
  422. while ((UEINTX & (1<<RWAL))) {
  423. UEDATX = 0;
  424. }
  425. UEINTX = 0x3A;
  426. }
  427. }
  428. if (keyboard_idle_config && (++div4 & 3) == 0) {
  429. UENUM = KEYBOARD_ENDPOINT;
  430. if (UEINTX & (1<<RWAL)) {
  431. keyboard_idle_count++;
  432. if (keyboard_idle_count == keyboard_idle_config) {
  433. keyboard_idle_count = 0;
  434. UEDATX = USBKeys_Modifiers;
  435. UEDATX = 0;
  436. for (i=0; i<6; i++) {
  437. UEDATX = USBKeys_Array[i];
  438. }
  439. UEINTX = 0x3A;
  440. }
  441. }
  442. }
  443. }
  444. }
  445. // Misc functions to wait for ready and send/receive packets
  446. static inline void usb_wait_in_ready(void)
  447. {
  448. while (!(UEINTX & (1<<TXINI))) ;
  449. }
  450. static inline void usb_send_in(void)
  451. {
  452. UEINTX = ~(1<<TXINI);
  453. }
  454. static inline void usb_wait_receive_out(void)
  455. {
  456. while (!(UEINTX & (1<<RXOUTI))) ;
  457. }
  458. static inline void usb_ack_out(void)
  459. {
  460. UEINTX = ~(1<<RXOUTI);
  461. }
  462. // USB Endpoint Interrupt - endpoint 0 is handled here. The
  463. // other endpoints are manipulated by the user-callable
  464. // functions, and the start-of-frame interrupt.
  465. //
  466. ISR(USB_COM_vect)
  467. {
  468. uint8_t intbits;
  469. const uint8_t *list;
  470. const uint8_t *cfg;
  471. uint8_t i, n, len, en;
  472. uint8_t bmRequestType;
  473. uint8_t bRequest;
  474. uint16_t wValue;
  475. uint16_t wIndex;
  476. uint16_t wLength;
  477. uint16_t desc_val;
  478. const uint8_t *desc_addr;
  479. uint8_t desc_length;
  480. UENUM = 0;
  481. intbits = UEINTX;
  482. if (intbits & (1<<RXSTPI)) {
  483. bmRequestType = UEDATX;
  484. bRequest = UEDATX;
  485. wValue = UEDATX;
  486. wValue |= (UEDATX << 8);
  487. wIndex = UEDATX;
  488. wIndex |= (UEDATX << 8);
  489. wLength = UEDATX;
  490. wLength |= (UEDATX << 8);
  491. UEINTX = ~((1<<RXSTPI) | (1<<RXOUTI) | (1<<TXINI));
  492. if (bRequest == GET_DESCRIPTOR) {
  493. list = (const uint8_t *)descriptor_list;
  494. for (i=0; ; i++) {
  495. if (i >= NUM_DESC_LIST) {
  496. UECONX = (1<<STALLRQ)|(1<<EPEN); //stall
  497. return;
  498. }
  499. desc_val = pgm_read_word(list);
  500. if (desc_val != wValue) {
  501. list += sizeof(struct descriptor_list_struct);
  502. continue;
  503. }
  504. list += 2;
  505. desc_val = pgm_read_word(list);
  506. if (desc_val != wIndex) {
  507. list += sizeof(struct descriptor_list_struct)-2;
  508. continue;
  509. }
  510. list += 2;
  511. desc_addr = (const uint8_t *)pgm_read_word(list);
  512. list += 2;
  513. desc_length = pgm_read_byte(list);
  514. break;
  515. }
  516. len = (wLength < 256) ? wLength : 255;
  517. if (len > desc_length) len = desc_length;
  518. do {
  519. // wait for host ready for IN packet
  520. do {
  521. i = UEINTX;
  522. } while (!(i & ((1<<TXINI)|(1<<RXOUTI))));
  523. if (i & (1<<RXOUTI)) return; // abort
  524. // send IN packet
  525. n = len < ENDPOINT0_SIZE ? len : ENDPOINT0_SIZE;
  526. for (i = n; i; i--) {
  527. UEDATX = pgm_read_byte(desc_addr++);
  528. }
  529. len -= n;
  530. usb_send_in();
  531. } while (len || n == ENDPOINT0_SIZE);
  532. return;
  533. }
  534. if (bRequest == SET_ADDRESS) {
  535. usb_send_in();
  536. usb_wait_in_ready();
  537. UDADDR = wValue | (1<<ADDEN);
  538. return;
  539. }
  540. if (bRequest == SET_CONFIGURATION && bmRequestType == 0) {
  541. usb_configuration = wValue;
  542. usb_send_in();
  543. cfg = endpoint_config_table;
  544. for (i=1; i<5; i++) {
  545. UENUM = i;
  546. en = pgm_read_byte(cfg++);
  547. UECONX = en;
  548. if (en) {
  549. UECFG0X = pgm_read_byte(cfg++);
  550. UECFG1X = pgm_read_byte(cfg++);
  551. }
  552. }
  553. UERST = 0x1E;
  554. UERST = 0;
  555. return;
  556. }
  557. if (bRequest == GET_CONFIGURATION && bmRequestType == 0x80) {
  558. usb_wait_in_ready();
  559. UEDATX = usb_configuration;
  560. usb_send_in();
  561. return;
  562. }
  563. if (bRequest == GET_STATUS) {
  564. usb_wait_in_ready();
  565. i = 0;
  566. #ifdef SUPPORT_ENDPOINT_HALT
  567. if (bmRequestType == 0x82) {
  568. UENUM = wIndex;
  569. if (UECONX & (1<<STALLRQ)) i = 1;
  570. UENUM = 0;
  571. }
  572. #endif
  573. UEDATX = i;
  574. UEDATX = 0;
  575. usb_send_in();
  576. return;
  577. }
  578. #ifdef SUPPORT_ENDPOINT_HALT
  579. if ((bRequest == CLEAR_FEATURE || bRequest == SET_FEATURE)
  580. && bmRequestType == 0x02 && wValue == 0) {
  581. i = wIndex & 0x7F;
  582. if (i >= 1 && i <= MAX_ENDPOINT) {
  583. usb_send_in();
  584. UENUM = i;
  585. if (bRequest == SET_FEATURE) {
  586. UECONX = (1<<STALLRQ)|(1<<EPEN);
  587. } else {
  588. UECONX = (1<<STALLRQC)|(1<<RSTDT)|(1<<EPEN);
  589. UERST = (1 << i);
  590. UERST = 0;
  591. }
  592. return;
  593. }
  594. }
  595. #endif
  596. if (wIndex == KEYBOARD_INTERFACE) {
  597. if (bmRequestType == 0xA1) {
  598. if (bRequest == HID_GET_REPORT) {
  599. usb_wait_in_ready();
  600. UEDATX = USBKeys_Modifiers;
  601. UEDATX = 0;
  602. for (i=0; i<6; i++) {
  603. UEDATX = USBKeys_Array[i];
  604. }
  605. usb_send_in();
  606. return;
  607. }
  608. if (bRequest == HID_GET_IDLE) {
  609. usb_wait_in_ready();
  610. UEDATX = keyboard_idle_config;
  611. usb_send_in();
  612. return;
  613. }
  614. if (bRequest == HID_GET_PROTOCOL) {
  615. usb_wait_in_ready();
  616. UEDATX = keyboard_protocol;
  617. usb_send_in();
  618. return;
  619. }
  620. }
  621. if (bmRequestType == 0x21) {
  622. if (bRequest == HID_SET_REPORT) {
  623. usb_wait_receive_out();
  624. USBKeys_LEDs = UEDATX;
  625. usb_ack_out();
  626. usb_send_in();
  627. return;
  628. }
  629. if (bRequest == HID_SET_IDLE) {
  630. keyboard_idle_config = (wValue >> 8);
  631. keyboard_idle_count = 0;
  632. //usb_wait_in_ready();
  633. usb_send_in();
  634. return;
  635. }
  636. if (bRequest == HID_SET_PROTOCOL) {
  637. keyboard_protocol = wValue;
  638. //usb_wait_in_ready();
  639. usb_send_in();
  640. return;
  641. }
  642. }
  643. }
  644. if (wIndex == DEBUG_INTERFACE) {
  645. if (bRequest == HID_GET_REPORT && bmRequestType == 0xA1) {
  646. len = wLength;
  647. do {
  648. // wait for host ready for IN packet
  649. do {
  650. i = UEINTX;
  651. } while (!(i & ((1<<TXINI)|(1<<RXOUTI))));
  652. if (i & (1<<RXOUTI)) return; // abort
  653. // send IN packet
  654. n = len < ENDPOINT0_SIZE ? len : ENDPOINT0_SIZE;
  655. for (i = n; i; i--) {
  656. UEDATX = 0;
  657. }
  658. len -= n;
  659. usb_send_in();
  660. } while (len || n == ENDPOINT0_SIZE);
  661. return;
  662. }
  663. }
  664. }
  665. UECONX = (1<<STALLRQ) | (1<<EPEN); // stall
  666. }