Kiibohd Controller
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
Tento repozitář je archivovaný. Můžete prohlížet soubory, klonovat, ale nemůžete nahrávat a vytvářet nové úkoly a požadavky na natažení.

usb_keyboard_serial.c 24KB

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