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.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

usb_keyboard_debug.c 21KB

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