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 23KB

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