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

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