Kiibohd Controller
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
Это архивный репозиторий. Вы можете его клонировать или просматривать файлы, но не вносить изменения или открывать задачи/запросы на слияние.

usb_keyboard_serial.c 24KB

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