Kiibohd Controller
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_serial.c 22KB

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