Kiibohd Controller
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
Dieses Repo ist archiviert. Du kannst Dateien sehen und es klonen, kannst aber nicht pushen oder Issues/Pull-Requests öffnen.

usb_keyboard_serial.c 22KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888
  1. /* USB Keyboard and CDC Serial Device for Teensy USB Development Board
  2. * Copyright (c) 2009 PJRC.COM, LLC
  3. * Modifications by Jacob Alexander (2011-2014)
  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. // Local Includes
  24. #include "usb_keyboard_serial.h"
  25. // ----- Variables -----
  26. // zero when we are not configured, non-zero when enumerated
  27. static volatile uint8_t usb_configuration = 0;
  28. // the time remaining before we transmit any partially full
  29. // packet, or send a zero length packet.
  30. static volatile uint8_t transmit_flush_timer = 0;
  31. static uint8_t transmit_previous_timeout = 0;
  32. // serial port settings (baud rate, control signals, etc) set
  33. // by the PC. These are ignored, but kept in RAM.
  34. static uint8_t cdc_line_coding[7] = {0x00, 0xE1, 0x00, 0x00, 0x00, 0x00, 0x08};
  35. static uint8_t cdc_line_rtsdtr = 0;
  36. // ----- USB Keyboard Functions -----
  37. // Sends normal keyboard out to host
  38. // NOTE: Make sure to match the descriptor
  39. void usb_keyboard_toHost()
  40. {
  41. uint8_t i;
  42. // Modifiers
  43. UEDATX = USBKeys_Modifiers;
  44. // Normal Keys
  45. for ( i = 0; i < 6; i++)
  46. {
  47. UEDATX = USBKeys_Array[i];
  48. }
  49. UEINTX = 0x3A;
  50. }
  51. // Sends NKRO keyboard out to host
  52. // NOTE: Make sure to match the descriptor
  53. void usb_nkrokeyboard_toHost()
  54. {
  55. uint8_t i;
  56. // Modifiers
  57. /*
  58. UEDATX = 0x02;
  59. UEDATX = USBKeys_Modifiers;
  60. UEINTX = 0x3A;
  61. */
  62. // Media Keys
  63. UEDATX = 0x03;
  64. UEDATX = 0;
  65. UEINTX = 0x3A;
  66. // Normal Keys
  67. UEDATX = 0x04;
  68. for ( i = 0; i < 6; i++)
  69. {
  70. UEDATX = USBKeys_Array[i];
  71. }
  72. UEINTX = 0x3A;
  73. }
  74. // send the contents of USBKeys_Array and USBKeys_Modifiers
  75. int8_t usb_keyboard_send()
  76. {
  77. uint8_t intr_state, timeout;
  78. intr_state = SREG;
  79. timeout = UDFNUML + 50;
  80. // Ready to transmit keypresses?
  81. do
  82. {
  83. SREG = intr_state;
  84. // has the USB gone offline? or exceeded timeout?
  85. if ( !usb_configuration || UDFNUML == timeout )
  86. return -1;
  87. // get ready to try checking again
  88. intr_state = SREG;
  89. cli();
  90. // If not using Boot protocol, send NKRO
  91. UENUM = USBKeys_Protocol ? KEYBOARD_NKRO_ENDPOINT : KEYBOARD_ENDPOINT;
  92. } while ( !( UEINTX & (1 << RWAL) ) );
  93. // Send normal keyboard interrupt packet(s)
  94. switch ( USBKeys_Protocol )
  95. {
  96. }
  97. usb_keyboard_toHost();
  98. USBKeys_Idle_Count = 0;
  99. SREG = intr_state;
  100. return 0;
  101. }
  102. // ----- USB Virtual Serial Port (CDC) Functions -----
  103. // get the next character, or -1 if nothing received
  104. int16_t usb_serial_getchar(void)
  105. {
  106. uint8_t c, intr_state;
  107. // interrupts are disabled so these functions can be
  108. // used from the main program or interrupt context,
  109. // even both in the same program!
  110. intr_state = SREG;
  111. cli();
  112. if (!usb_configuration) {
  113. SREG = intr_state;
  114. return -1;
  115. }
  116. UENUM = CDC_RX_ENDPOINT;
  117. retry:
  118. c = UEINTX;
  119. if (!(c & (1<<RWAL))) {
  120. // no data in buffer
  121. if (c & (1<<RXOUTI)) {
  122. UEINTX = 0x6B;
  123. goto retry;
  124. }
  125. SREG = intr_state;
  126. return -2;
  127. }
  128. // take one byte out of the buffer
  129. c = UEDATX;
  130. // if buffer completely used, release it
  131. if (!(UEINTX & (1<<RWAL))) UEINTX = 0x6B;
  132. SREG = intr_state;
  133. return c;
  134. }
  135. // number of bytes available in the receive buffer
  136. uint8_t usb_serial_available(void)
  137. {
  138. uint8_t n=0, i, intr_state;
  139. intr_state = SREG;
  140. cli();
  141. if (usb_configuration) {
  142. UENUM = CDC_RX_ENDPOINT;
  143. n = UEBCLX;
  144. if (!n) {
  145. i = UEINTX;
  146. if (i & (1<<RXOUTI) && !(i & (1<<RWAL))) UEINTX = 0x6B;
  147. }
  148. }
  149. SREG = intr_state;
  150. return n;
  151. }
  152. // discard any buffered input
  153. void usb_serial_flush_input(void)
  154. {
  155. uint8_t intr_state;
  156. if (usb_configuration) {
  157. intr_state = SREG;
  158. cli();
  159. UENUM = CDC_RX_ENDPOINT;
  160. while ((UEINTX & (1<<RWAL))) {
  161. UEINTX = 0x6B;
  162. }
  163. SREG = intr_state;
  164. }
  165. }
  166. // transmit a character. 0 returned on success, -1 on error
  167. int8_t usb_serial_putchar(uint8_t c)
  168. {
  169. uint8_t timeout, intr_state;
  170. // if we're not online (enumerated and configured), error
  171. if (!usb_configuration) return -1;
  172. // interrupts are disabled so these functions can be
  173. // used from the main program or interrupt context,
  174. // even both in the same program!
  175. intr_state = SREG;
  176. cli();
  177. UENUM = CDC_TX_ENDPOINT;
  178. // if we gave up due to timeout before, don't wait again
  179. if (transmit_previous_timeout) {
  180. if (!(UEINTX & (1<<RWAL))) {
  181. SREG = intr_state;
  182. return -1;
  183. }
  184. transmit_previous_timeout = 0;
  185. }
  186. // wait for the FIFO to be ready to accept data
  187. timeout = UDFNUML + TRANSMIT_TIMEOUT;
  188. while (1) {
  189. // are we ready to transmit?
  190. if (UEINTX & (1<<RWAL)) break;
  191. SREG = intr_state;
  192. // have we waited too long? This happens if the user
  193. // is not running an application that is listening
  194. if (UDFNUML == timeout) {
  195. transmit_previous_timeout = 1;
  196. return -1;
  197. }
  198. // has the USB gone offline?
  199. if (!usb_configuration) return -1;
  200. // get ready to try checking again
  201. intr_state = SREG;
  202. cli();
  203. UENUM = CDC_TX_ENDPOINT;
  204. }
  205. // actually write the byte into the FIFO
  206. UEDATX = c;
  207. // if this completed a packet, transmit it now!
  208. if (!(UEINTX & (1<<RWAL))) UEINTX = 0x3A;
  209. transmit_flush_timer = TRANSMIT_FLUSH_TIMEOUT;
  210. SREG = intr_state;
  211. return 0;
  212. }
  213. // transmit a character, but do not wait if the buffer is full,
  214. // 0 returned on success, -1 on buffer full or error
  215. int8_t usb_serial_putchar_nowait(uint8_t c)
  216. {
  217. uint8_t intr_state;
  218. if (!usb_configuration) return -1;
  219. intr_state = SREG;
  220. cli();
  221. UENUM = CDC_TX_ENDPOINT;
  222. if (!(UEINTX & (1<<RWAL))) {
  223. // buffer is full
  224. SREG = intr_state;
  225. return -2;
  226. }
  227. // actually write the byte into the FIFO
  228. UEDATX = c;
  229. // if this completed a packet, transmit it now!
  230. if (!(UEINTX & (1<<RWAL))) UEINTX = 0x3A;
  231. transmit_flush_timer = TRANSMIT_FLUSH_TIMEOUT;
  232. SREG = intr_state;
  233. return 0;
  234. }
  235. // transmit a buffer.
  236. // 0 returned on success, -1 on error
  237. // This function is optimized for speed! Each call takes approx 6.1 us overhead
  238. // plus 0.25 us per byte. 12 Mbit/sec USB has 8.67 us per-packet overhead and
  239. // takes 0.67 us per byte. If called with 64 byte packet-size blocks, this function
  240. // can transmit at full USB speed using 43% CPU time. The maximum theoretical speed
  241. // is 19 packets per USB frame, or 1216 kbytes/sec. However, bulk endpoints have the
  242. // lowest priority, so any other USB devices will likely reduce the speed. Speed
  243. // can also be limited by how quickly the PC-based software reads data, as the host
  244. // controller in the PC will not allocate bandwitdh without a pending read request.
  245. // (thanks to Victor Suarez for testing and feedback and initial code)
  246. int8_t usb_serial_write(const char *buffer, uint16_t size)
  247. {
  248. uint8_t timeout, intr_state, write_size;
  249. // if we're not online (enumerated and configured), error
  250. if (!usb_configuration) return -1;
  251. // interrupts are disabled so these functions can be
  252. // used from the main program or interrupt context,
  253. // even both in the same program!
  254. intr_state = SREG;
  255. cli();
  256. UENUM = CDC_TX_ENDPOINT;
  257. // if we gave up due to timeout before, don't wait again
  258. /*
  259. if (transmit_previous_timeout) {
  260. if (!(UEINTX & (1<<RWAL))) {
  261. SREG = intr_state;
  262. return -2;
  263. }
  264. transmit_previous_timeout = 0;
  265. }
  266. */
  267. // each iteration of this loop transmits a packet
  268. while (size) {
  269. // wait for the FIFO to be ready to accept data
  270. timeout = UDFNUML + TRANSMIT_TIMEOUT;
  271. while (1) {
  272. // are we ready to transmit?
  273. if (UEINTX & (1<<RWAL)) break;
  274. SREG = intr_state;
  275. // have we waited too long? This happens if the user
  276. // is not running an application that is listening
  277. if (UDFNUML == timeout) {
  278. transmit_previous_timeout = 1;
  279. return -3;
  280. }
  281. // has the USB gone offline?
  282. if (!usb_configuration) return -4;
  283. // get ready to try checking again
  284. intr_state = SREG;
  285. cli();
  286. UENUM = CDC_TX_ENDPOINT;
  287. }
  288. // compute how many bytes will fit into the next packet
  289. write_size = CDC_TX_SIZE - UEBCLX;
  290. if (write_size > size) write_size = size;
  291. size -= write_size;
  292. // write the packet
  293. switch (write_size) {
  294. #if (CDC_TX_SIZE == 64)
  295. case 64: UEDATX = *buffer++;
  296. case 63: UEDATX = *buffer++;
  297. case 62: UEDATX = *buffer++;
  298. case 61: UEDATX = *buffer++;
  299. case 60: UEDATX = *buffer++;
  300. case 59: UEDATX = *buffer++;
  301. case 58: UEDATX = *buffer++;
  302. case 57: UEDATX = *buffer++;
  303. case 56: UEDATX = *buffer++;
  304. case 55: UEDATX = *buffer++;
  305. case 54: UEDATX = *buffer++;
  306. case 53: UEDATX = *buffer++;
  307. case 52: UEDATX = *buffer++;
  308. case 51: UEDATX = *buffer++;
  309. case 50: UEDATX = *buffer++;
  310. case 49: UEDATX = *buffer++;
  311. case 48: UEDATX = *buffer++;
  312. case 47: UEDATX = *buffer++;
  313. case 46: UEDATX = *buffer++;
  314. case 45: UEDATX = *buffer++;
  315. case 44: UEDATX = *buffer++;
  316. case 43: UEDATX = *buffer++;
  317. case 42: UEDATX = *buffer++;
  318. case 41: UEDATX = *buffer++;
  319. case 40: UEDATX = *buffer++;
  320. case 39: UEDATX = *buffer++;
  321. case 38: UEDATX = *buffer++;
  322. case 37: UEDATX = *buffer++;
  323. case 36: UEDATX = *buffer++;
  324. case 35: UEDATX = *buffer++;
  325. case 34: UEDATX = *buffer++;
  326. case 33: UEDATX = *buffer++;
  327. #endif
  328. #if (CDC_TX_SIZE >= 32)
  329. case 32: UEDATX = *buffer++;
  330. case 31: UEDATX = *buffer++;
  331. case 30: UEDATX = *buffer++;
  332. case 29: UEDATX = *buffer++;
  333. case 28: UEDATX = *buffer++;
  334. case 27: UEDATX = *buffer++;
  335. case 26: UEDATX = *buffer++;
  336. case 25: UEDATX = *buffer++;
  337. case 24: UEDATX = *buffer++;
  338. case 23: UEDATX = *buffer++;
  339. case 22: UEDATX = *buffer++;
  340. case 21: UEDATX = *buffer++;
  341. case 20: UEDATX = *buffer++;
  342. case 19: UEDATX = *buffer++;
  343. case 18: UEDATX = *buffer++;
  344. case 17: UEDATX = *buffer++;
  345. #endif
  346. #if (CDC_TX_SIZE >= 16)
  347. case 16: UEDATX = *buffer++;
  348. case 15: UEDATX = *buffer++;
  349. case 14: UEDATX = *buffer++;
  350. case 13: UEDATX = *buffer++;
  351. case 12: UEDATX = *buffer++;
  352. case 11: UEDATX = *buffer++;
  353. case 10: UEDATX = *buffer++;
  354. case 9: UEDATX = *buffer++;
  355. #endif
  356. case 8: UEDATX = *buffer++;
  357. case 7: UEDATX = *buffer++;
  358. case 6: UEDATX = *buffer++;
  359. case 5: UEDATX = *buffer++;
  360. case 4: UEDATX = *buffer++;
  361. case 3: UEDATX = *buffer++;
  362. case 2: UEDATX = *buffer++;
  363. default:
  364. case 1: UEDATX = *buffer++;
  365. case 0: break;
  366. }
  367. // if this completed a packet, transmit it now!
  368. if (!(UEINTX & (1<<RWAL))) UEINTX = 0x3A;
  369. transmit_flush_timer = TRANSMIT_FLUSH_TIMEOUT;
  370. SREG = intr_state;
  371. }
  372. return 0;
  373. }
  374. // immediately transmit any buffered output.
  375. // This doesn't actually transmit the data - that is impossible!
  376. // USB devices only transmit when the host allows, so the best
  377. // we can do is release the FIFO buffer for when the host wants it
  378. void usb_serial_flush_output(void)
  379. {
  380. uint8_t intr_state;
  381. intr_state = SREG;
  382. cli();
  383. if (transmit_flush_timer) {
  384. UENUM = CDC_TX_ENDPOINT;
  385. UEINTX = 0x3A;
  386. transmit_flush_timer = 0;
  387. }
  388. SREG = intr_state;
  389. }
  390. // functions to read the various async serial settings. These
  391. // aren't actually used by USB at all (communication is always
  392. // at full USB speed), but they are set by the host so we can
  393. // set them properly if we're converting the USB to a real serial
  394. // communication
  395. uint32_t usb_serial_get_baud(void)
  396. {
  397. uint32_t *baud = (uint32_t*)cdc_line_coding;
  398. return *baud;
  399. }
  400. uint8_t usb_serial_get_stopbits(void)
  401. {
  402. return cdc_line_coding[4];
  403. }
  404. uint8_t usb_serial_get_paritytype(void)
  405. {
  406. return cdc_line_coding[5];
  407. }
  408. uint8_t usb_serial_get_numbits(void)
  409. {
  410. return cdc_line_coding[6];
  411. }
  412. uint8_t usb_serial_get_control(void)
  413. {
  414. return cdc_line_rtsdtr;
  415. }
  416. // write the control signals, DCD, DSR, RI, etc
  417. // There is no CTS signal. If software on the host has transmitted
  418. // data to you but you haven't been calling the getchar function,
  419. // it remains buffered (either here or on the host) and can not be
  420. // lost because you weren't listening at the right time, like it
  421. // would in real serial communication.
  422. int8_t usb_serial_set_control(uint8_t signals)
  423. {
  424. uint8_t intr_state;
  425. intr_state = SREG;
  426. cli();
  427. if (!usb_configuration) {
  428. // we're not enumerated/configured
  429. SREG = intr_state;
  430. return -1;
  431. }
  432. UENUM = CDC_ACM_ENDPOINT;
  433. if (!(UEINTX & (1<<RWAL))) {
  434. // unable to write
  435. // TODO; should this try to abort the previously
  436. // buffered message??
  437. SREG = intr_state;
  438. return -1;
  439. }
  440. UEDATX = 0xA1;
  441. UEDATX = 0x20;
  442. UEDATX = 0;
  443. UEDATX = 0;
  444. UEDATX = 0; // 0 seems to work nicely. what if this is 1??
  445. UEDATX = 0;
  446. UEDATX = 1;
  447. UEDATX = 0;
  448. UEDATX = signals;
  449. UEINTX = 0x3A;
  450. SREG = intr_state;
  451. return 0;
  452. }
  453. // ----- General USB Functions -----
  454. // Set the avr into firmware reload mode
  455. void usb_device_reload()
  456. {
  457. cli();
  458. // Disable watchdog, if enabled
  459. // Disable all peripherals
  460. UDCON = 1;
  461. USBCON = (1 << FRZCLK); // Disable USB
  462. UCSR1B = 0;
  463. _delay_ms( 5 );
  464. #if defined(__AVR_AT90USB162__) // Teensy 1.0
  465. EIMSK = 0; PCICR = 0; SPCR = 0; ACSR = 0; EECR = 0;
  466. TIMSK0 = 0; TIMSK1 = 0; UCSR1B = 0;
  467. DDRB = 0; DDRC = 0; DDRD = 0;
  468. PORTB = 0; PORTC = 0; PORTD = 0;
  469. asm volatile("jmp 0x3E00");
  470. #elif defined(__AVR_ATmega32U4__) // Teensy 2.0
  471. EIMSK = 0; PCICR = 0; SPCR = 0; ACSR = 0; EECR = 0; ADCSRA = 0;
  472. TIMSK0 = 0; TIMSK1 = 0; TIMSK3 = 0; TIMSK4 = 0; UCSR1B = 0; TWCR = 0;
  473. DDRB = 0; DDRC = 0; DDRD = 0; DDRE = 0; DDRF = 0; TWCR = 0;
  474. PORTB = 0; PORTC = 0; PORTD = 0; PORTE = 0; PORTF = 0;
  475. asm volatile("jmp 0x7E00");
  476. #elif defined(__AVR_AT90USB646__) // Teensy++ 1.0
  477. EIMSK = 0; PCICR = 0; SPCR = 0; ACSR = 0; EECR = 0; ADCSRA = 0;
  478. TIMSK0 = 0; TIMSK1 = 0; TIMSK2 = 0; TIMSK3 = 0; UCSR1B = 0; TWCR = 0;
  479. DDRA = 0; DDRB = 0; DDRC = 0; DDRD = 0; DDRE = 0; DDRF = 0;
  480. PORTA = 0; PORTB = 0; PORTC = 0; PORTD = 0; PORTE = 0; PORTF = 0;
  481. asm volatile("jmp 0xFC00");
  482. #elif defined(__AVR_AT90USB1286__) // Teensy++ 2.0
  483. EIMSK = 0; PCICR = 0; SPCR = 0; ACSR = 0; EECR = 0; ADCSRA = 0;
  484. TIMSK0 = 0; TIMSK1 = 0; TIMSK2 = 0; TIMSK3 = 0; UCSR1B = 0; TWCR = 0;
  485. DDRA = 0; DDRB = 0; DDRC = 0; DDRD = 0; DDRE = 0; DDRF = 0;
  486. PORTA = 0; PORTB = 0; PORTC = 0; PORTD = 0; PORTE = 0; PORTF = 0;
  487. asm volatile("jmp 0x1FC00");
  488. #endif
  489. }
  490. // WDT Setup for software reset the chip
  491. void wdt_init(void)
  492. {
  493. MCUSR = 0;
  494. wdt_disable();
  495. }
  496. // initialize USB
  497. void usb_init(void)
  498. {
  499. HW_CONFIG();
  500. USB_FREEZE(); // enable USB
  501. PLL_CONFIG(); // config PLL
  502. while (!(PLLCSR & (1<<PLOCK))) ; // wait for PLL lock
  503. USB_CONFIG(); // start USB clock
  504. UDCON = 0; // enable attach resistor
  505. usb_configuration = 0;
  506. UDIEN = (1<<EORSTE) | (1<<SOFE);
  507. sei();
  508. // Disable watchdog timer after possible software reset
  509. //wdt_init(); // XXX Not working...seems to be ok without this, not sure though
  510. }
  511. // return 0 if the USB is not configured, or the configuration
  512. // number selected by the HOST
  513. uint8_t usb_configured()
  514. {
  515. return usb_configuration;
  516. }
  517. // USB Device Interrupt - handle all device-level events
  518. // the transmit buffer flushing is triggered by the start of frame
  519. //
  520. ISR( USB_GEN_vect )
  521. {
  522. uint8_t intbits, t_cdc;
  523. intbits = UDINT;
  524. UDINT = 0;
  525. if ( intbits & (1 << EORSTI) )
  526. {
  527. UENUM = 0;
  528. UECONX = 1;
  529. UECFG0X = EP_TYPE_CONTROL;
  530. UECFG1X = EP_SIZE(ENDPOINT0_SIZE) | EP_SINGLE_BUFFER;
  531. UEIENX = (1 << RXSTPE);
  532. usb_configuration = 0;
  533. cdc_line_rtsdtr = 0;
  534. }
  535. if ( (intbits & (1 << SOFI)) && usb_configuration )
  536. {
  537. t_cdc = transmit_flush_timer;
  538. if ( t_cdc )
  539. {
  540. transmit_flush_timer = --t_cdc;
  541. if ( !t_cdc )
  542. {
  543. UENUM = CDC_TX_ENDPOINT;
  544. UEINTX = 0x3A;
  545. }
  546. }
  547. static uint8_t div4 = 0;
  548. if ( USBKeys_Idle_Config && (++div4 & 3) == 0 )
  549. {
  550. USBKeys_Idle_Count++;
  551. if ( USBKeys_Idle_Count == USBKeys_Idle_Config )
  552. {
  553. // XXX TODO Is this even used? If so, when? -Jacob
  554. // From hasu's code, this section looks like it could fix the Mac SET_IDLE problem
  555. // Send normal keyboard interrupt packet(s)
  556. //usb_keyboard_toHost();
  557. }
  558. }
  559. }
  560. }
  561. // Misc functions to wait for ready and send/receive packets
  562. static inline void usb_wait_in_ready(void)
  563. {
  564. while (!(UEINTX & (1<<TXINI))) ;
  565. }
  566. static inline void usb_send_in(void)
  567. {
  568. UEINTX = ~(1<<TXINI);
  569. }
  570. static inline void usb_wait_receive_out(void)
  571. {
  572. while (!(UEINTX & (1<<RXOUTI))) ;
  573. }
  574. static inline void usb_ack_out(void)
  575. {
  576. UEINTX = ~(1<<RXOUTI);
  577. }
  578. // USB Endpoint Interrupt - endpoint 0 is handled here. The
  579. // other endpoints are manipulated by the user-callable
  580. // functions, and the start-of-frame interrupt.
  581. //
  582. ISR(USB_COM_vect)
  583. {
  584. uint8_t intbits;
  585. const uint8_t *list;
  586. const uint8_t *cfg;
  587. uint8_t i, n, len, en;
  588. uint8_t *p;
  589. uint8_t bmRequestType;
  590. uint8_t bRequest;
  591. uint16_t wValue;
  592. uint16_t wIndex;
  593. uint16_t wLength;
  594. uint16_t desc_val;
  595. const uint8_t *desc_addr;
  596. uint8_t desc_length;
  597. UENUM = 0;
  598. intbits = UEINTX;
  599. if (intbits & (1<<RXSTPI)) {
  600. bmRequestType = UEDATX;
  601. bRequest = UEDATX;
  602. wValue = UEDATX;
  603. wValue |= (UEDATX << 8);
  604. wIndex = UEDATX;
  605. wIndex |= (UEDATX << 8);
  606. wLength = UEDATX;
  607. wLength |= (UEDATX << 8);
  608. UEINTX = ~((1<<RXSTPI) | (1<<RXOUTI) | (1<<TXINI));
  609. if ( bRequest == GET_DESCRIPTOR )
  610. {
  611. list = (const uint8_t *)descriptor_list;
  612. for ( i = 0; ; i++ )
  613. {
  614. if ( i >= NUM_DESC_LIST )
  615. {
  616. UECONX = (1 << STALLRQ) | (1 << EPEN); //stall
  617. return;
  618. }
  619. desc_val = pgm_read_word(list);
  620. if ( desc_val != wValue )
  621. {
  622. list += sizeof( struct descriptor_list_struct );
  623. continue;
  624. }
  625. list += 2;
  626. desc_val = pgm_read_word(list);
  627. if ( desc_val != wIndex )
  628. {
  629. list += sizeof(struct descriptor_list_struct) - 2;
  630. continue;
  631. }
  632. list += 2;
  633. desc_addr = (const uint8_t *)pgm_read_word(list);
  634. list += 2;
  635. desc_length = pgm_read_byte(list);
  636. break;
  637. }
  638. len = (wLength < 256) ? wLength : 255;
  639. if (len > desc_length) len = desc_length;
  640. do {
  641. // wait for host ready for IN packet
  642. do {
  643. i = UEINTX;
  644. } while (!(i & ((1<<TXINI)|(1<<RXOUTI))));
  645. if (i & (1<<RXOUTI)) return; // abort
  646. // send IN packet
  647. n = len < ENDPOINT0_SIZE ? len : ENDPOINT0_SIZE;
  648. for (i = n; i; i--) {
  649. UEDATX = pgm_read_byte(desc_addr++);
  650. }
  651. len -= n;
  652. usb_send_in();
  653. } while (len || n == ENDPOINT0_SIZE);
  654. return;
  655. }
  656. if (bRequest == SET_ADDRESS) {
  657. usb_send_in();
  658. usb_wait_in_ready();
  659. UDADDR = wValue | (1<<ADDEN);
  660. return;
  661. }
  662. if ( bRequest == SET_CONFIGURATION && bmRequestType == 0 )
  663. {
  664. usb_configuration = wValue;
  665. cdc_line_rtsdtr = 0;
  666. transmit_flush_timer = 0;
  667. usb_send_in();
  668. cfg = endpoint_config_table;
  669. // Setup each of the 6 additional endpoints (0th already configured)
  670. for ( i = 1; i < 6; i++ )
  671. {
  672. UENUM = i;
  673. en = pgm_read_byte(cfg++);
  674. UECONX = en;
  675. if (en)
  676. {
  677. UECFG0X = pgm_read_byte(cfg++);
  678. UECFG1X = pgm_read_byte(cfg++);
  679. }
  680. }
  681. UERST = 0x7E;
  682. UERST = 0;
  683. return;
  684. }
  685. if (bRequest == GET_CONFIGURATION && bmRequestType == 0x80) {
  686. usb_wait_in_ready();
  687. UEDATX = usb_configuration;
  688. usb_send_in();
  689. return;
  690. }
  691. //if ( wIndex == KEYBOARD_INTERFACE )
  692. if ( wIndex == KEYBOARD_INTERFACE || wIndex == KEYBOARD_NKRO_INTERFACE )
  693. {
  694. if ( bmRequestType == 0xA1)
  695. {
  696. if ( bRequest == HID_GET_REPORT )
  697. {
  698. usb_wait_in_ready();
  699. // XXX TODO Is this even used? If so, when? -Jacob
  700. // Send normal keyboard interrupt packet(s)
  701. //usb_keyboard_toHost();
  702. usb_send_in();
  703. return;
  704. }
  705. if ( bRequest == HID_GET_IDLE )
  706. {
  707. usb_wait_in_ready();
  708. UEDATX = USBKeys_Idle_Config;
  709. usb_send_in();
  710. return;
  711. }
  712. if ( bRequest == HID_GET_PROTOCOL )
  713. {
  714. usb_wait_in_ready();
  715. UEDATX = USBKeys_Protocol;
  716. usb_send_in();
  717. return;
  718. }
  719. USBKeys_Protocol = bRequest;
  720. }
  721. if ( bmRequestType == 0x21 )
  722. {
  723. if ( bRequest == HID_SET_REPORT )
  724. {
  725. usb_wait_receive_out();
  726. USBKeys_LEDs = UEDATX;
  727. usb_ack_out();
  728. usb_send_in();
  729. return;
  730. }
  731. if ( bRequest == HID_SET_IDLE )
  732. {
  733. USBKeys_Idle_Config = (wValue >> 8);
  734. USBKeys_Idle_Count = 0;
  735. //usb_wait_in_ready();
  736. usb_send_in();
  737. return;
  738. }
  739. if ( bRequest == HID_SET_PROTOCOL )
  740. {
  741. USBKeys_Protocol = wValue; // 0 - Boot Mode, 1 - NKRO Mode
  742. //usb_wait_in_ready();
  743. usb_send_in();
  744. return;
  745. }
  746. }
  747. }
  748. if (bRequest == CDC_GET_LINE_CODING && bmRequestType == 0xA1) {
  749. usb_wait_in_ready();
  750. p = cdc_line_coding;
  751. for (i=0; i<7; i++) {
  752. UEDATX = *p++;
  753. }
  754. usb_send_in();
  755. return;
  756. }
  757. if (bRequest == CDC_SET_LINE_CODING && bmRequestType == 0x21) {
  758. usb_wait_receive_out();
  759. p = cdc_line_coding;
  760. for (i=0; i<7; i++) {
  761. *p++ = UEDATX;
  762. }
  763. usb_ack_out();
  764. usb_send_in();
  765. return;
  766. }
  767. if (bRequest == CDC_SET_CONTROL_LINE_STATE && bmRequestType == 0x21) {
  768. cdc_line_rtsdtr = wValue;
  769. usb_wait_in_ready();
  770. usb_send_in();
  771. return;
  772. }
  773. if (bRequest == GET_STATUS) {
  774. usb_wait_in_ready();
  775. i = 0;
  776. if (bmRequestType == 0x82) {
  777. UENUM = wIndex;
  778. if (UECONX & (1<<STALLRQ)) i = 1;
  779. UENUM = 0;
  780. }
  781. UEDATX = i;
  782. UEDATX = 0;
  783. usb_send_in();
  784. return;
  785. }
  786. if ((bRequest == CLEAR_FEATURE || bRequest == SET_FEATURE)
  787. && bmRequestType == 0x02 && wValue == 0) {
  788. i = wIndex & 0x7F;
  789. if (i >= 1 && i <= MAX_ENDPOINT) {
  790. usb_send_in();
  791. UENUM = i;
  792. if (bRequest == SET_FEATURE) {
  793. UECONX = (1<<STALLRQ)|(1<<EPEN);
  794. } else {
  795. UECONX = (1<<STALLRQC)|(1<<RSTDT)|(1<<EPEN);
  796. UERST = (1 << i);
  797. UERST = 0;
  798. }
  799. return;
  800. }
  801. }
  802. }
  803. UECONX = (1 << STALLRQ) | (1 << EPEN); // stall
  804. }